Repository: shipwright-io/build
Pull request #612 (2021-02-22 13:28:57)

Replace operator-framework/operator-sdk/pkg/ready
# Changes

As part of the decoupling from the `operator-sdk` code base, there is the need
to introduce a replacement for their convenience `ready` package.

Add new `ready` package within `build` that has a comparable usage style.

Add code to make the `ready` filename configurable.

Change default `ready` filename to remove `operator` reference.

This addresses part of #584 

See https://github.com/operator-framework/operator-sdk/pull/3476 for details regarding the recommendation to create an own `ready` helper package if required.

# Submitter Checklist

- [X] Includes tests if functionality changed/was added
- [ ] Includes docs if changes are user-facing
- [X] Release notes block has been filled in, or marked NONE

See [the contributor guide](https://github.com/shipwright-io/build/blob/master/CONTRIBUTING.md)
for details on coding conventions, github and prow interactions, and the code review process.

# Release Notes

```release-note
action required: The default filename to indicate readiness in the pod was renamed from /tmp/operator-sdk-ready to /tmp/shipwright-build-ready. If you use custom deployment mechanisms, please have a look at the respective readiness probe settings to adjust for the change.
```




openshift-ci-robot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by:
To complete the [pull request process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process), please assign **qu1queee** after the PR has been reviewed.
You can assign the PR to them by writing `/assign @qu1queee` in a comment when ready.

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=shipwright-io%2Fbuild).

<details open>
Needs approval from an approver in each of these files:

- **[OWNERS](https://github.com/shipwright-io/build/blob/master/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["qu1queee"]} -->
openshift-ci-robot: @HeavyWombat: PR needs rebase.

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.
</details>

Patch:
diff --git a/cmd/manager/main.go b/cmd/manager/main.go
index fd48fbfc..3191e93f 100644
--- a/cmd/manager/main.go
+++ b/cmd/manager/main.go
@@ -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"
 
@@ -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"
@@ -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()
diff --git a/deploy/operator.yaml b/deploy/operator.yaml
index 68f0949b..96f175d8 100644
--- a/deploy/operator.yaml
+++ b/deploy/operator.yaml
@@ -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
diff --git a/pkg/controller/ready/ready.go b/pkg/controller/ready/ready.go
new file mode 100644
index 00000000..3c9664d0
--- /dev/null
+++ b/pkg/controller/ready/ready.go
@@ -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)
+}
diff --git a/vendor/github.com/operator-framework/operator-sdk/pkg/ready/ready.go b/vendor/github.com/operator-framework/operator-sdk/pkg/ready/ready.go
deleted file mode 100644
index 8cd04707..00000000
--- a/vendor/github.com/operator-framework/operator-sdk/pkg/ready/ready.go
+++ /dev/null
@@ -1,65 +0,0 @@
-// Copyright 2018 The Operator-SDK Authors
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//     http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package ready
-
-import (
-	"os"
-)
-
-// FileName represent the full path to the file for determining ready status
-const FileName = "/tmp/operator-sdk-ready"
-
-// Ready holds state about whether the operator is ready and communicates that
-// to a Kubernetes readiness probe.
-type Ready interface {
-	// Set ensures that future readiness probes will indicate that the operator
-	// is ready.
-	Set() error
-
-	// Unset ensures that future readiness probes will indicate that the
-	// operator is not ready.
-	Unset() error
-}
-
-// NewFileReady returns a Ready that uses the presence of a file on disk to
-// communicate whether the operator is ready. The operator's Pod definition
-// should include a readinessProbe of "exec" type that calls
-// "stat /tmp/operator-sdk-ready".
-func NewFileReady() Ready {
-	return fileReady{}
-}
-
-type fileReady struct{}
-
-// Set creates a file on disk whose presence can be used by a readiness probe
-// to determine that the operator is ready.
-func (r fileReady) Set() error {
-	f, err := os.Create(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(FileName); os.IsNotExist(err) {
-		return nil
-	}
-	return os.Remove(FileName)
-}
diff --git a/vendor/modules.txt b/vendor/modules.txt
index 58961a05..92c98a5c 100644
--- a/vendor/modules.txt
+++ b/vendor/modules.txt
@@ -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


Test patch:
diff --git a/pkg/controller/ready/ready_suite_test.go b/pkg/controller/ready/ready_suite_test.go
new file mode 100644
index 00000000..0c733c97
--- /dev/null
+++ b/pkg/controller/ready/ready_suite_test.go
@@ -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")
+}
diff --git a/pkg/controller/ready/ready_test.go b/pkg/controller/ready/ready_test.go
new file mode 100644
index 00000000..00be31ba
--- /dev/null
+++ b/pkg/controller/ready/ready_test.go
@@ -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())
+		})
+	})
+})