-
Notifications
You must be signed in to change notification settings - Fork 236
OCPBUGS-86798: Bug: Poll for CRD Established status after creation to fix TOCTOU race #1396
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
Open
nikhilprajapati-world
wants to merge
2
commits into
openshift:main
Choose a base branch
from
nikhilprajapati-world:fix/crd-established-toctou-race
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| package resourcebuilder | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| ) | ||
|
|
||
| func TestCheckCRDEstablished(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| crd *apiextv1.CustomResourceDefinition | ||
| wantErr bool | ||
| errSubstr string | ||
| }{ | ||
| { | ||
| name: "Established=True returns nil", | ||
| crd: &apiextv1.CustomResourceDefinition{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "test.example.io"}, | ||
| Status: apiextv1.CustomResourceDefinitionStatus{ | ||
| Conditions: []apiextv1.CustomResourceDefinitionCondition{ | ||
| { | ||
| Type: apiextv1.NamesAccepted, | ||
| Status: apiextv1.ConditionTrue, | ||
| }, | ||
| { | ||
| Type: apiextv1.Established, | ||
| Status: apiextv1.ConditionTrue, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "Established=False returns error with reason", | ||
| crd: &apiextv1.CustomResourceDefinition{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "test.example.io"}, | ||
| Status: apiextv1.CustomResourceDefinitionStatus{ | ||
| Conditions: []apiextv1.CustomResourceDefinitionCondition{ | ||
| { | ||
| Type: apiextv1.Established, | ||
| Status: apiextv1.ConditionFalse, | ||
| Reason: "Installing", | ||
| Message: "not yet established", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| wantErr: true, | ||
| errSubstr: "Established=False", | ||
| }, | ||
| { | ||
| name: "empty conditions returns error", | ||
| crd: &apiextv1.CustomResourceDefinition{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "test.example.io"}, | ||
| Status: apiextv1.CustomResourceDefinitionStatus{ | ||
| Conditions: []apiextv1.CustomResourceDefinitionCondition{}, | ||
| }, | ||
| }, | ||
| wantErr: true, | ||
| errSubstr: "does not declare an Established status condition", | ||
| }, | ||
| { | ||
| name: "nil conditions returns error", | ||
| crd: &apiextv1.CustomResourceDefinition{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "test.example.io"}, | ||
| Status: apiextv1.CustomResourceDefinitionStatus{}, | ||
| }, | ||
| wantErr: true, | ||
| errSubstr: "does not declare an Established status condition", | ||
| }, | ||
| { | ||
| name: "NamesAccepted but no Established returns error", | ||
| crd: &apiextv1.CustomResourceDefinition{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "test.example.io"}, | ||
| Status: apiextv1.CustomResourceDefinitionStatus{ | ||
| Conditions: []apiextv1.CustomResourceDefinitionCondition{ | ||
| { | ||
| Type: apiextv1.NamesAccepted, | ||
| Status: apiextv1.ConditionTrue, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| wantErr: true, | ||
| errSubstr: "does not declare an Established status condition", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| err := checkCRDEstablished(tt.crd) | ||
| if tt.wantErr { | ||
| if err == nil { | ||
| t.Error("expected error, got nil") | ||
| } else if tt.errSubstr != "" { | ||
| if !containsSubstring(err.Error(), tt.errSubstr) { | ||
| t.Errorf("error %q does not contain %q", err.Error(), tt.errSubstr) | ||
| } | ||
| } | ||
| } else { | ||
| if err != nil { | ||
| t.Errorf("expected nil error, got: %v", err) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestCheckCustomResourceDefinitionHealthInitializingMode(t *testing.T) { | ||
| b := &builder{mode: InitializingMode} | ||
| crd := &apiextv1.CustomResourceDefinition{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "test.example.io"}, | ||
| Status: apiextv1.CustomResourceDefinitionStatus{}, | ||
| } | ||
| if err := b.checkCustomResourceDefinitionHealth(t.Context(), crd); err != nil { | ||
| t.Errorf("InitializingMode should skip health check, got: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestCheckCustomResourceDefinitionHealthEstablished(t *testing.T) { | ||
| b := &builder{mode: ReconcilingMode} | ||
| crd := &apiextv1.CustomResourceDefinition{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "test.example.io"}, | ||
| Status: apiextv1.CustomResourceDefinitionStatus{ | ||
| Conditions: []apiextv1.CustomResourceDefinitionCondition{ | ||
| { | ||
| Type: apiextv1.Established, | ||
| Status: apiextv1.ConditionTrue, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| if err := b.checkCustomResourceDefinitionHealth(t.Context(), crd); err != nil { | ||
| t.Errorf("Established=True CRD should pass health check, got: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func containsSubstring(s, substr string) bool { | ||
| return len(s) >= len(substr) && (s == substr || len(s) > 0 && containsSubstringImpl(s, substr)) | ||
| } | ||
|
|
||
| func containsSubstringImpl(s, substr string) bool { | ||
| for i := 0; i <= len(s)-len(substr); i++ { | ||
| if s[i:i+len(substr)] == substr { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.