-
Notifications
You must be signed in to change notification settings - Fork 266
kms: add readiness check hook to revision controller #2316
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,12 +3,13 @@ package revisioncontroller | |||||||||||
| import ( | ||||||||||||
| "context" | ||||||||||||
| "fmt" | ||||||||||||
| clocktesting "k8s.io/utils/clock/testing" | ||||||||||||
| "reflect" | ||||||||||||
| "strings" | ||||||||||||
| "testing" | ||||||||||||
| "time" | ||||||||||||
|
|
||||||||||||
| clocktesting "k8s.io/utils/clock/testing" | ||||||||||||
|
|
||||||||||||
| "github.com/openshift/library-go/pkg/controller/factory" | ||||||||||||
| "github.com/openshift/library-go/pkg/operator/v1helpers" | ||||||||||||
| "github.com/stretchr/testify/require" | ||||||||||||
|
|
@@ -541,6 +542,7 @@ func TestRevisionController(t *testing.T) { | |||||||||||
| kubeClient.CoreV1(), | ||||||||||||
| eventRecorder, | ||||||||||||
| nil, | ||||||||||||
| nil, | ||||||||||||
| ) | ||||||||||||
| syncErr := c.Sync(context.TODO(), factory.NewSyncContext("RevisionController", eventRecorder)) | ||||||||||||
| if tc.validateStatus != nil { | ||||||||||||
|
|
@@ -620,6 +622,7 @@ func TestRevisionControllerRevisionCreatedFailedStatusUpdate(t *testing.T) { | |||||||||||
| kubeClient.CoreV1(), | ||||||||||||
| eventRecorder, | ||||||||||||
| nil, | ||||||||||||
| nil, | ||||||||||||
| ) | ||||||||||||
|
|
||||||||||||
| klog.Infof("Running NewRevisionController.Sync with UpdateLatestRevisionOperatorStatus returning an error") | ||||||||||||
|
|
@@ -816,6 +819,7 @@ func TestSyncWithRevisionPrecondition(t *testing.T) { | |||||||||||
| kubeClient.CoreV1(), | ||||||||||||
| eventRecorder, | ||||||||||||
| tc.revisionPrecondition, | ||||||||||||
| nil, | ||||||||||||
| ) | ||||||||||||
| syncErr := c.Sync(context.TODO(), factory.NewSyncContext("RevisionController", eventRecorder)) | ||||||||||||
| require.Equal(t, syncErr, tc.expSyncErr) | ||||||||||||
|
|
@@ -826,3 +830,119 @@ func TestSyncWithRevisionPrecondition(t *testing.T) { | |||||||||||
| }) | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| func TestSyncWithRevisionReadinessCheck(t *testing.T) { | ||||||||||||
| startingObjects := []runtime.Object{ | ||||||||||||
| &v1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "test-secret", Namespace: targetNamespace}}, | ||||||||||||
| &v1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: "test-config", Namespace: targetNamespace}}, | ||||||||||||
| &v1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: "revision-status", Namespace: targetNamespace}}, | ||||||||||||
| &v1.ConfigMap{ | ||||||||||||
| ObjectMeta: metav1.ObjectMeta{Name: "revision-status-1", Namespace: targetNamespace}, | ||||||||||||
| Data: map[string]string{"revision": "1"}, | ||||||||||||
| }, | ||||||||||||
| } | ||||||||||||
| newStaticPodOperatorClient := func() v1helpers.StaticPodOperatorClient { | ||||||||||||
| return v1helpers.NewFakeStaticPodOperatorClient( | ||||||||||||
| &operatorv1.StaticPodOperatorSpec{ | ||||||||||||
| OperatorSpec: operatorv1.OperatorSpec{ | ||||||||||||
| ManagementState: operatorv1.Managed, | ||||||||||||
| }, | ||||||||||||
| }, | ||||||||||||
| &operatorv1.StaticPodOperatorStatus{ | ||||||||||||
| OperatorStatus: operatorv1.OperatorStatus{ | ||||||||||||
| LatestAvailableRevision: 1, | ||||||||||||
| }, | ||||||||||||
| NodeStatuses: []operatorv1.NodeStatus{ | ||||||||||||
| { | ||||||||||||
| NodeName: "test-node-1", | ||||||||||||
| CurrentRevision: 1, | ||||||||||||
| TargetRevision: 0, | ||||||||||||
| }, | ||||||||||||
| }, | ||||||||||||
| }, | ||||||||||||
| nil, | ||||||||||||
| nil, | ||||||||||||
| ) | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| tests := []struct { | ||||||||||||
| testName string | ||||||||||||
| revisionReadinessCheck ReadinessCheckFunc | ||||||||||||
| expSyncErr error | ||||||||||||
| expUpdatedLatestAvailableRevision int32 | ||||||||||||
| expRevisionReady string | ||||||||||||
| }{ | ||||||||||||
| { | ||||||||||||
| // when readiness check is nil, the default implementation is considered. In this case no error is expected to be | ||||||||||||
| // returned by sync, LatestAvailableRevision should be updated, and the revision should be marked as ready | ||||||||||||
| testName: "readiness check is not supplied", | ||||||||||||
| revisionReadinessCheck: nil, | ||||||||||||
| expSyncErr: nil, | ||||||||||||
| expUpdatedLatestAvailableRevision: 2, | ||||||||||||
| expRevisionReady: "true", | ||||||||||||
| }, | ||||||||||||
| { | ||||||||||||
| // when readiness check passes, no error is expected to be returned by sync, | ||||||||||||
| // LatestAvailableRevision should be updated, and the revision should be marked as ready | ||||||||||||
| testName: "readiness check passes", | ||||||||||||
| revisionReadinessCheck: func(ctx context.Context, revision int32) (bool, error) { | ||||||||||||
| return true, nil | ||||||||||||
| }, | ||||||||||||
| expSyncErr: nil, | ||||||||||||
| expUpdatedLatestAvailableRevision: 2, | ||||||||||||
| expRevisionReady: "true", | ||||||||||||
| }, | ||||||||||||
| { | ||||||||||||
| // when readiness check fails, no error should be returned by sync, LatestAvailableRevision should not be updated, | ||||||||||||
| // and the revision should not be marked as ready even though resources were copied | ||||||||||||
| testName: "readiness check fails", | ||||||||||||
| revisionReadinessCheck: func(ctx context.Context, revision int32) (bool, error) { | ||||||||||||
| return false, nil | ||||||||||||
| }, | ||||||||||||
| expSyncErr: nil, | ||||||||||||
| expUpdatedLatestAvailableRevision: 1, | ||||||||||||
| expRevisionReady: "false", | ||||||||||||
| }, | ||||||||||||
| { | ||||||||||||
| // when readiness check returns error, a wrapped error should be returned by sync, LatestAvailableRevision | ||||||||||||
| // should not be updated, and the revision should not be marked as ready even though resources were copied | ||||||||||||
| testName: "readiness check returns error", | ||||||||||||
| revisionReadinessCheck: func(ctx context.Context, revision int32) (bool, error) { | ||||||||||||
| return true, fmt.Errorf("Error") | ||||||||||||
| }, | ||||||||||||
| expSyncErr: fmt.Errorf("revision readiness check failed for revision 2: %w", fmt.Errorf("Error")), | ||||||||||||
| expUpdatedLatestAvailableRevision: 1, | ||||||||||||
| expRevisionReady: "false", | ||||||||||||
| }, | ||||||||||||
| } | ||||||||||||
| for _, tc := range tests { | ||||||||||||
| t.Run(tc.testName, func(t *testing.T) { | ||||||||||||
| staticPodOperatorClient := newStaticPodOperatorClient() | ||||||||||||
| kubeClient := fake.NewClientset(startingObjects...) | ||||||||||||
| eventRecorder := events.NewRecorder(kubeClient.CoreV1().Events("test"), "test-operator", &v1.ObjectReference{}, clocktesting.NewFakePassiveClock(time.Now())) | ||||||||||||
|
|
||||||||||||
| c := NewRevisionController( | ||||||||||||
| "testing", | ||||||||||||
| targetNamespace, | ||||||||||||
| []RevisionResource{{Name: "test-config"}}, | ||||||||||||
| []RevisionResource{{Name: "test-secret"}}, | ||||||||||||
| informers.NewSharedInformerFactoryWithOptions(kubeClient, 1*time.Minute, informers.WithNamespace(targetNamespace)), | ||||||||||||
| staticPodOperatorClient, | ||||||||||||
| kubeClient.CoreV1(), | ||||||||||||
| kubeClient.CoreV1(), | ||||||||||||
| eventRecorder, | ||||||||||||
| nil, | ||||||||||||
| tc.revisionReadinessCheck, | ||||||||||||
| ) | ||||||||||||
| syncErr := c.Sync(context.TODO(), factory.NewSyncContext("RevisionController", eventRecorder)) | ||||||||||||
| require.Equal(t, tc.expSyncErr, syncErr) | ||||||||||||
|
|
||||||||||||
| _, status, _, _ := staticPodOperatorClient.GetStaticPodOperatorState() | ||||||||||||
| require.Equal(t, tc.expUpdatedLatestAvailableRevision, status.LatestAvailableRevision) | ||||||||||||
|
Comment on lines
+940
to
+941
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle The error return is currently discarded, which can hide state-fetch failures and make the assertion run on invalid data. As per coding guidelines, "Never ignore error returns". ✅ Suggested fix- _, status, _, _ := staticPodOperatorClient.GetStaticPodOperatorState()
+ _, status, _, statusErr := staticPodOperatorClient.GetStaticPodOperatorState()
+ require.NoError(t, statusErr)
require.Equal(t, tc.expUpdatedLatestAvailableRevision, status.LatestAvailableRevision)📝 Committable suggestion
Suggested change
🤖 Prompt for AI AgentsSource: Coding guidelines |
||||||||||||
|
|
||||||||||||
| revisionStatus, err := kubeClient.CoreV1().ConfigMaps(targetNamespace).Get(context.TODO(), "revision-status-2", metav1.GetOptions{}) | ||||||||||||
| require.NoError(t, err, "revision-status-2 configmap should exist") | ||||||||||||
| require.Equal(t, tc.expRevisionReady, revisionStatus.Annotations["operator.openshift.io/revision-ready"]) | ||||||||||||
| }) | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just realized that
Postconditionmight not be the best name because the check is done before the operator conditions are set, right before the revision is marked as ready, not after the condition is set. MaybeRevisionReadinessCheckis a better name?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went ahead and renamed it