Skip to content

Commit 222f6cb

Browse files
djzagerjmrodri
authored andcommitted
pkg/ready: Make set/unset idempotent (#1761)
* pkg/ready: Make set/unset idempotent Fixes #1760
1 parent 6c7039c commit 222f6cb

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
### Changed
99

1010
- **Breaking change:** CSV config field `role-path` is now `role-paths` and takes a list of strings. Users can now specify multiple `Role` and `ClusterRole` manifests using `role-paths`. ([#1704](https://github.com/operator-framework/operator-sdk/pull/1704))
11+
- Make `ready` package idempotent. Now, a user can call `Set()` or `Unset()` to set the operator's readiness without knowing the current state. ([#1761](https://github.com/operator-framework/operator-sdk/pull/1761))
1112

1213
### Deprecated
1314

pkg/ready/ready.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"os"
1919
)
2020

21+
// FileName represent the full path to the file for determining ready status
2122
const FileName = "/tmp/operator-sdk-ready"
2223

2324
// Ready holds state about whether the operator is ready and communicates that
@@ -47,12 +48,18 @@ type fileReady struct{}
4748
func (r fileReady) Set() error {
4849
f, err := os.Create(FileName)
4950
if err != nil {
51+
if os.IsExist(err) {
52+
return nil
53+
}
5054
return err
5155
}
5256
return f.Close()
5357
}
5458

5559
// Unset removes the file on disk that was created by Set().
5660
func (r fileReady) Unset() error {
61+
if _, err := os.Stat(FileName); os.IsNotExist(err) {
62+
return nil
63+
}
5764
return os.Remove(FileName)
5865
}

pkg/ready/ready_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ func TestFileReady(t *testing.T) {
3131
t.Errorf("Did not find expected file at %s: %v", FileName, err)
3232
}
3333

34+
// Should be safe to set multiple times
35+
err = r.Set()
36+
if err != nil {
37+
t.Errorf("Failed to set ready file when it already exists: %v", err)
38+
}
39+
3440
err = r.Unset()
3541
if err != nil {
3642
t.Errorf("Could not unset ready file: %v", err)
@@ -43,4 +49,9 @@ func TestFileReady(t *testing.T) {
4349
if !os.IsNotExist(err) {
4450
t.Errorf("Error determining if file still exists at %s: %v", FileName, err)
4551
}
52+
53+
err = r.Unset()
54+
if err != nil {
55+
t.Errorf("Could not unset ready file when already removed: %v", err)
56+
}
4657
}

0 commit comments

Comments
 (0)