Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace operator-framework/operator-sdk/pkg/ready #612

Merged
merged 1 commit into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"runtime"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)

"github.com/operator-framework/operator-sdk/pkg/ready"
sdkVersion "github.com/operator-framework/operator-sdk/version"
"github.com/spf13/pflag"

Expand All @@ -24,6 +22,7 @@ import (

buildconfig "github.com/shipwright-io/build/pkg/config"
"github.com/shipwright-io/build/pkg/controller"
"github.com/shipwright-io/build/pkg/controller/ready"
"github.com/shipwright-io/build/pkg/ctxlog"
buildMetrics "github.com/shipwright-io/build/pkg/metrics"
"github.com/shipwright-io/build/version"
Expand Down Expand Up @@ -75,10 +74,10 @@ func main() {
os.Exit(1)
}

r := ready.NewFileReady()
r := ready.NewFileReady("/tmp/shipwright-build-ready")
err = r.Set()
if err != nil {
ctxlog.Error(ctx, err, "Checking for /tmp/operator-sdk-ready failed")
ctxlog.Error(ctx, err, "Checking for /tmp/shipwright-build-ready failed")
os.Exit(1)
}
defer r.Unset()
Expand Down
4 changes: 2 additions & 2 deletions deploy/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ spec:
exec:
command:
- stat
- /tmp/operator-sdk-ready
- /tmp/shipwright-build-ready
initialDelaySeconds: 5
periodSeconds: 10
readinessProbe:
exec:
command:
- stat
- /tmp/operator-sdk-ready
- /tmp/shipwright-build-ready
initialDelaySeconds: 5
periodSeconds: 10
52 changes: 52 additions & 0 deletions pkg/controller/ready/ready.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright The Shipwright Contributors
//
// SPDX-License-Identifier: Apache-2.0

package ready

import "os"

// Ready holds state about whether the controller is ready and communicates
// that to a Kubernetes readiness/liveness probe.
type Ready interface {
// Set ensures that future readiness/liveness probes will indicate that
// the controller is ready.
Set() error

// Unset ensures that future readiness/liveness probes will indicate
// that the controller is not ready.
Unset() error
}

type fileReady struct {
filename string
}

// NewFileReady returns a Ready that uses the presence of a file on disk to
// communicate whether the controller is ready.
func NewFileReady(filename string) Ready {
return fileReady{filename}
}

// Set creates a file on disk whose presence can be used by a
// readiness/liveness probe.
func (r fileReady) Set() error {
f, err := os.Create(r.filename)
if err != nil {
if os.IsExist(err) {
return nil
}
return err
}

return f.Close()
}

// Unset removes the file on disk that was created by Set().
func (r fileReady) Unset() error {
if _, err := os.Stat(r.filename); os.IsNotExist(err) {
return nil
}

return os.Remove(r.filename)
}
17 changes: 17 additions & 0 deletions pkg/controller/ready/ready_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright The Shipwright Contributors
//
// SPDX-License-Identifier: Apache-2.0

package ready_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestReady(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Ready Suite")
}
74 changes: 74 additions & 0 deletions pkg/controller/ready/ready_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright The Shipwright Contributors
//
// SPDX-License-Identifier: Apache-2.0

package ready_test

import (
"os"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

. "github.com/shipwright-io/build/pkg/controller/ready"
)

var _ = Describe("controller ready helper", func() {
var fileExists = func(filename string) bool {
_, err := os.Stat(filename)
return err == nil || os.IsExist(err)
}

Context("setting and unsetting ready file", func() {
const filename = "/tmp/foobar"

BeforeEach(func(){
Expect(fileExists(filename)).To(BeFalse())
})

AfterEach(func(){
os.Remove(filename)
})

It("creates a ready file with the given name when using Set", func() {
r := NewFileReady(filename)
Expect(r.Set()).ToNot(HaveOccurred())
Expect(fileExists(filename)).To(BeTrue())
})

It("should not break if Set is used multiple times", func() {
r := NewFileReady(filename)
Expect(r.Set()).ToNot(HaveOccurred())
Expect(r.Set()).ToNot(HaveOccurred())
Expect(fileExists(filename)).To(BeTrue())
})

It("removes the created file when using Unset", func() {
r := NewFileReady(filename)
Expect(r.Set()).ToNot(HaveOccurred())
Expect(fileExists(filename)).To(BeTrue())

Expect(r.Unset()).ToNot(HaveOccurred())
Expect(fileExists(filename)).To(BeFalse())
})

It("should not break if Unset ist used multiple times", func() {
r := NewFileReady(filename)
Expect(r.Set()).ToNot(HaveOccurred())
Expect(fileExists(filename)).To(BeTrue())

Expect(r.Unset()).ToNot(HaveOccurred())
Expect(r.Unset()).ToNot(HaveOccurred())
Expect(fileExists(filename)).To(BeFalse())
})

It("should not fail if the given file is already present", func() {
_, err :=os.Create(filename)
Expect(err).ToNot(HaveOccurred())

r := NewFileReady(filename)
Expect(r.Set()).ToNot(HaveOccurred())
Expect(fileExists(filename)).To(BeTrue())
})
})
})

This file was deleted.

1 change: 0 additions & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ github.com/operator-framework/operator-sdk/internal/util/k8sutil
github.com/operator-framework/operator-sdk/internal/util/kubebuilder
github.com/operator-framework/operator-sdk/internal/util/projutil
github.com/operator-framework/operator-sdk/pkg/k8sutil
github.com/operator-framework/operator-sdk/pkg/ready
github.com/operator-framework/operator-sdk/pkg/test
github.com/operator-framework/operator-sdk/pkg/test/e2eutil
github.com/operator-framework/operator-sdk/version
Expand Down