-
Notifications
You must be signed in to change notification settings - Fork 11
feat(agent): add CRD gate implementation and tests #3839
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
floreks
wants to merge
4
commits into
master
Choose a base branch
from
sebastian/prod-4535-services-with-crds-and-cr-instances-need-multiple-passes-to
base: master
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 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1be9ef7
feat(agent): add CRD gate implementation and tests
floreks bee75d6
fix(crd_gate): correct error formatting in CRD establishment timeout …
floreks 5573b15
fix(store): correct import ordering and improve variadic argument usage
floreks 9622ea2
Merge remote-tracking branch 'origin/master' into sebastian/prod-4535…
floreks 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
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,34 @@ | ||
| package discovery | ||
|
|
||
| import ( | ||
| "sync/atomic" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "k8s.io/apimachinery/pkg/api/meta" | ||
| ) | ||
|
|
||
| type resettableMapper struct { | ||
| meta.RESTMapper | ||
| resetCalls atomic.Int32 | ||
| } | ||
|
|
||
| func (in *resettableMapper) Reset() { | ||
| in.resetCalls.Add(1) | ||
| } | ||
|
|
||
| func TestResetRESTMapper(t *testing.T) { | ||
| t.Run("delegates to resettable mapper", func(t *testing.T) { | ||
| mapper := &resettableMapper{} | ||
| cache := NewCache(nil, mapper) | ||
|
|
||
| cache.ResetRESTMapper() | ||
|
|
||
| assert.Equal(t, int32(1), mapper.resetCalls.Load()) | ||
| }) | ||
|
|
||
| t.Run("ignores missing mapper", func(t *testing.T) { | ||
| cache := NewCache(nil, nil) | ||
| assert.NotPanics(t, cache.ResetRESTMapper) | ||
| }) | ||
| } |
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,54 @@ | ||
| package common | ||
|
|
||
| import ( | ||
| "github.com/samber/lo" | ||
| apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
| "k8s.io/apimachinery/pkg/api/meta" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
| ) | ||
|
|
||
| var crdGroupKind = apiextensionsv1.SchemeGroupVersion.WithKind(CustomResourceDefinitionKind).GroupKind() | ||
|
|
||
| func IsCRD(resource unstructured.Unstructured) bool { | ||
| return resource.GroupVersionKind().GroupKind() == crdGroupKind | ||
| } | ||
|
|
||
| func isStatusConditionTrue(resource unstructured.Unstructured, conditionType string) bool { | ||
| conditions, _, _ := unstructured.NestedSlice(resource.Object, "status", "conditions") | ||
| return meta.IsStatusConditionTrue(lo.FilterMap(conditions, func(condition any, _ int) (metav1.Condition, bool) { | ||
| value, ok := condition.(map[string]any) | ||
| if !ok { | ||
| return metav1.Condition{}, false | ||
| } | ||
|
|
||
| currentType, typeFound, _ := unstructured.NestedString(value, "type") | ||
| conditionStatus, statusFound, _ := unstructured.NestedString(value, "status") | ||
| return metav1.Condition{ | ||
| Type: currentType, | ||
| Status: metav1.ConditionStatus(conditionStatus), | ||
| }, typeFound && statusFound | ||
| }), conditionType) | ||
| } | ||
|
|
||
| func CRDEstablished(resource unstructured.Unstructured) bool { | ||
| return IsCRD(resource) && isStatusConditionTrue(resource, string(apiextensionsv1.Established)) | ||
| } | ||
|
|
||
| func ServedCRDGVKs(resource unstructured.Unstructured) []schema.GroupVersionKind { | ||
| if !IsCRD(resource) { | ||
| return nil | ||
| } | ||
|
|
||
| crd := new(apiextensionsv1.CustomResourceDefinition) | ||
| if err := runtime.DefaultUnstructuredConverter.FromUnstructured(resource.Object, crd); err != nil { | ||
| return nil | ||
| } | ||
|
|
||
| return lo.FilterMap(crd.Spec.Versions, func(version apiextensionsv1.CustomResourceDefinitionVersion, _ int) (schema.GroupVersionKind, bool) { | ||
| gvk := schema.GroupVersion{Group: crd.Spec.Group, Version: version.Name}.WithKind(crd.Spec.Names.Kind) | ||
| return gvk, version.Served && gvk.Group != "" && gvk.Version != "" && gvk.Kind != "" | ||
| }) | ||
| } |
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,40 @@ | ||
| package common | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
| ) | ||
|
|
||
| func TestCRDUtilities(t *testing.T) { | ||
| crd := unstructured.Unstructured{Object: map[string]any{ | ||
| "apiVersion": "apiextensions.k8s.io/v1", | ||
| "kind": "CustomResourceDefinition", | ||
| "spec": map[string]any{ | ||
| "group": "example.com", | ||
| "names": map[string]any{"kind": "Widget", "plural": "widgets"}, | ||
| "versions": []any{ | ||
| map[string]any{"name": "v1", "served": true, "storage": true}, | ||
| map[string]any{"name": "v2", "served": false, "storage": false}, | ||
| }, | ||
| }, | ||
| "status": map[string]any{ | ||
| "conditions": []any{map[string]any{"type": "Established", "status": "True"}}, | ||
| }, | ||
| }} | ||
|
|
||
| assert.True(t, IsCRD(crd)) | ||
| assert.True(t, CRDEstablished(crd)) | ||
| assert.Equal(t, []schema.GroupVersionKind{{Group: "example.com", Version: "v1", Kind: "Widget"}}, ServedCRDGVKs(crd)) | ||
| assert.False(t, CRDEstablished(unstructured.Unstructured{})) | ||
|
|
||
| versions, _, _ := unstructured.NestedSlice(crd.Object, "spec", "versions") | ||
| versions = append(versions, | ||
| map[string]any{"name": "v3", "served": false}, | ||
| map[string]any{"name": "", "served": true}, | ||
| ) | ||
| assert.NoError(t, unstructured.SetNestedSlice(crd.Object, versions, "spec", "versions")) | ||
| assert.Equal(t, []schema.GroupVersionKind{{Group: "example.com", Version: "v1", Kind: "Widget"}}, ServedCRDGVKs(crd)) | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.