Skip to content

Commit 17cb27e

Browse files
committed
status: add method to remove an operand version from the VersionGetter
1 parent 4760434 commit 17cb27e

File tree

3 files changed

+71
-12
lines changed

3 files changed

+71
-12
lines changed

pkg/operator/status/status_controller.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,21 @@ package status
22

33
import (
44
"context"
5-
"k8s.io/utils/clock"
65
"strings"
76
"time"
87

8+
"k8s.io/apimachinery/pkg/api/equality"
9+
apierrors "k8s.io/apimachinery/pkg/api/errors"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
912
"k8s.io/klog/v2"
13+
"k8s.io/utils/clock"
1014

1115
configv1 "github.com/openshift/api/config/v1"
1216
operatorv1 "github.com/openshift/api/operator/v1"
1317
configv1client "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1"
1418
configv1informers "github.com/openshift/client-go/config/informers/externalversions/config/v1"
1519
configv1listers "github.com/openshift/client-go/config/listers/config/v1"
16-
"k8s.io/apimachinery/pkg/api/equality"
17-
apierrors "k8s.io/apimachinery/pkg/api/errors"
18-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
19-
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
20-
2120
configv1helpers "github.com/openshift/library-go/pkg/config/clusteroperator/v1helpers"
2221
"github.com/openshift/library-go/pkg/controller/factory"
2322
"github.com/openshift/library-go/pkg/operator/events"
@@ -29,6 +28,8 @@ import (
2928
type VersionGetter interface {
3029
// SetVersion is a way to set the version for an operand. It must be thread-safe
3130
SetVersion(operandName, version string)
31+
// UnsetVersion removes a version for an operand if it exists; it is a no-op otherwise. It must be thread-safe
32+
UnsetVersion(operandName string)
3233
// GetVersion is way to get the versions for all operands. It must be thread-safe and return an object that doesn't mutate
3334
GetVersions() map[string]string
3435
// VersionChangedChannel is a channel that will get an item whenever SetVersion has been called

pkg/operator/status/version.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,19 @@ func (v *versionGetter) SetVersion(operandName, version string) {
3535
defer v.lock.Unlock()
3636

3737
v.versions[operandName] = version
38+
v.notifyChannelsLocked()
39+
}
3840

39-
for i := range v.notificationChannels {
40-
ch := v.notificationChannels[i]
41-
// don't let a slow consumer block the rest
42-
go func() {
43-
ch <- struct{}{}
44-
}()
41+
func (v *versionGetter) UnsetVersion(operandName string) {
42+
v.lock.Lock()
43+
defer v.lock.Unlock()
44+
45+
if _, exists := v.versions[operandName]; !exists {
46+
return
4547
}
48+
49+
delete(v.versions, operandName)
50+
v.notifyChannelsLocked()
4651
}
4752

4853
func (v *versionGetter) GetVersions() map[string]string {
@@ -65,6 +70,17 @@ func (v *versionGetter) VersionChangedChannel() <-chan struct{} {
6570
return channel
6671
}
6772

73+
// notifyChannelsLocked must be called under a locked v.lock
74+
func (v *versionGetter) notifyChannelsLocked() {
75+
for i := range v.notificationChannels {
76+
ch := v.notificationChannels[i]
77+
// don't let a slow consumer block the rest
78+
go func() {
79+
ch <- struct{}{}
80+
}()
81+
}
82+
}
83+
6884
func ImageForOperandFromEnv() string {
6985
return os.Getenv(operandImageEnvVarName)
7086
}

pkg/operator/status/version_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,45 @@ func TestVersionGetterBasic(t *testing.T) {
3333
}
3434

3535
}
36+
37+
func TestVersionGetterUnset(t *testing.T) {
38+
versionGetter := NewVersionGetter()
39+
40+
versionGetter.UnsetVersion("nonexistent")
41+
expected := map[string]string{}
42+
versions := versionGetter.GetVersions()
43+
if !reflect.DeepEqual(expected, versions) {
44+
t.Fatalf("Expected %v, got %v", expected, versions)
45+
}
46+
47+
versionGetter.SetVersion("foo", "1.0.0")
48+
versionGetter.SetVersion("bar", "2.0.0")
49+
50+
versions = versionGetter.GetVersions()
51+
expected = map[string]string{"foo": "1.0.0", "bar": "2.0.0"}
52+
if !reflect.DeepEqual(expected, versions) {
53+
t.Fatalf("wanted: %v; got: %v", expected, versions)
54+
}
55+
56+
ch := versionGetter.VersionChangedChannel()
57+
versionGetter.UnsetVersion("foo")
58+
59+
select {
60+
case <-ch:
61+
// we got notified
62+
case <-time.After(5 * time.Second):
63+
t.Fatal("expected change notification after UnsetVersion but didn't get any")
64+
}
65+
66+
versions = versionGetter.GetVersions()
67+
expected = map[string]string{"bar": "2.0.0"}
68+
if !reflect.DeepEqual(expected, versions) {
69+
t.Fatalf("Expected %v, got %v", expected, versions)
70+
}
71+
72+
versionGetter.UnsetVersion("nonexistent")
73+
versions = versionGetter.GetVersions()
74+
if !reflect.DeepEqual(expected, versions) {
75+
t.Fatalf("Expected %v, got %v", expected, versions)
76+
}
77+
}

0 commit comments

Comments
 (0)