-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[feat i2g]add feature flag for ingress plan annotation #4722
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
Closed
Closed
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package ingress | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| networking "k8s.io/api/networking/v1" | ||
| "sigs.k8s.io/aws-load-balancer-controller/pkg/annotations" | ||
| "sigs.k8s.io/aws-load-balancer-controller/pkg/k8s" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| ) | ||
|
|
||
| const ( | ||
| dryRunPlanAnnotation = annotations.AnnotationPrefixIngress + "/" + annotations.IngressSuffixDryRunPlan | ||
| ) | ||
|
|
||
| // patchDryRunPlanAnnotation writes the serialized stack JSON to the dry-run-plan | ||
| // annotation on the given ingress. It skips the patch if the value is unchanged | ||
| // to avoid unnecessary API calls and reconcile loops. | ||
| func patchDryRunPlanAnnotation(ctx context.Context, k8sClient client.Client, ing *networking.Ingress, planJSON string) error { | ||
| if ing.Annotations[dryRunPlanAnnotation] == planJSON { | ||
| return nil | ||
| } | ||
| ingOld := ing.DeepCopy() | ||
| if ing.Annotations == nil { | ||
| ing.Annotations = map[string]string{} | ||
| } | ||
| ing.Annotations[dryRunPlanAnnotation] = planJSON | ||
| if err := k8sClient.Patch(ctx, ing, client.MergeFrom(ingOld)); err != nil { | ||
| return fmt.Errorf("failed to patch dry-run plan annotation on ingress %s: %w", k8s.NamespacedName(ing), err) | ||
| } | ||
| return nil | ||
| } |
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,96 @@ | ||
| package ingress | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| networking "k8s.io/api/networking/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| clientgoscheme "k8s.io/client-go/kubernetes/scheme" | ||
| "sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
| ) | ||
|
|
||
| func Test_patchDryRunPlanAnnotation(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| ingAnnotations map[string]string | ||
| planJSON string | ||
| wantAnnotation string | ||
| wantOtherAnnotations map[string]string | ||
| }{ | ||
| { | ||
| name: "writes annotation when not present", | ||
| ingAnnotations: nil, | ||
| planJSON: `{"id":"test-stack"}`, | ||
| wantAnnotation: `{"id":"test-stack"}`, | ||
| }, | ||
| { | ||
| name: "skips patch when value unchanged", | ||
| ingAnnotations: map[string]string{ | ||
| dryRunPlanAnnotation: `{"id":"test-stack"}`, | ||
| }, | ||
| planJSON: `{"id":"test-stack"}`, | ||
| wantAnnotation: `{"id":"test-stack"}`, | ||
| }, | ||
| { | ||
| name: "updates annotation when value changed", | ||
| ingAnnotations: map[string]string{ | ||
| dryRunPlanAnnotation: `{"id":"old-stack"}`, | ||
| }, | ||
| planJSON: `{"id":"new-stack"}`, | ||
| wantAnnotation: `{"id":"new-stack"}`, | ||
| }, | ||
| { | ||
| name: "preserves existing annotations", | ||
| ingAnnotations: map[string]string{ | ||
| "alb.ingress.kubernetes.io/scheme": "internet-facing", | ||
| }, | ||
| planJSON: `{"id":"test-stack"}`, | ||
| wantAnnotation: `{"id":"test-stack"}`, | ||
| wantOtherAnnotations: map[string]string{ | ||
| "alb.ingress.kubernetes.io/scheme": "internet-facing", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| ing := &networking.Ingress{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-ingress", | ||
| Namespace: "default", | ||
| Annotations: tt.ingAnnotations, | ||
| }, | ||
| } | ||
|
|
||
| scheme := runtime.NewScheme() | ||
| require.NoError(t, clientgoscheme.AddToScheme(scheme)) | ||
|
|
||
| k8sClient := fake.NewClientBuilder(). | ||
| WithScheme(scheme). | ||
| WithObjects(ing). | ||
| Build() | ||
|
|
||
| err := patchDryRunPlanAnnotation(context.Background(), k8sClient, ing, tt.planJSON) | ||
| require.NoError(t, err) | ||
|
|
||
| // Verify the annotation was persisted | ||
| updatedIng := &networking.Ingress{} | ||
| err = k8sClient.Get(context.Background(), types.NamespacedName{ | ||
| Name: "test-ingress", | ||
| Namespace: "default", | ||
| }, updatedIng) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, tt.wantAnnotation, updatedIng.Annotations[dryRunPlanAnnotation]) | ||
|
|
||
| // Verify other annotations were not clobbered | ||
| for k, v := range tt.wantOtherAnnotations { | ||
| assert.Equal(t, v, updatedIng.Annotations[k], "annotation %s should be preserved", k) | ||
| } | ||
| }) | ||
| } | ||
| } |
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
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
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.
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.
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.
Can you write unit tests for this?