-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a mutating webhook to modify Capp resources. The webhook ad…
…ds/modifies a last-updated-by annotation on every creation/update of Capp Signed-off-by: Ariel Septon <64063409+arielsepton@users.noreply.github.com>
- Loading branch information
1 parent
53f52aa
commit 67e2817
Showing
10 changed files
with
329 additions
and
35 deletions.
There are no files selected for viewing
This file contains 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 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 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,51 @@ | ||
package webhooks | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
|
||
cappv1alpha1 "github.com/dana-team/container-app-operator/api/v1alpha1" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
) | ||
|
||
type CappMutator struct { | ||
Decoder *admission.Decoder | ||
} | ||
|
||
// +kubebuilder:webhook:path=/mutate-capp,mutating=true,sideEffects=NoneOnDryRun,failurePolicy=fail,groups=rcs.dana.io,resources=capps,verbs=create;update,versions=v1alpha1,name=capp.dana.io,admissionReviewVersions=v1;v1beta1 | ||
|
||
const ( | ||
LastUpdatedByAnnotationKey = "rcs.dana.io/last-updated-by" | ||
MutatorServingPath = "/mutate-capp" | ||
) | ||
|
||
// Handle implements the mutation webhook. | ||
func (c *CappMutator) Handle(ctx context.Context, req admission.Request) admission.Response { | ||
logger := log.FromContext(ctx).WithValues("mutation webhook", "capp mutation Webhook", "Name", req.Name) | ||
capp := cappv1alpha1.Capp{} | ||
if err := c.Decoder.DecodeRaw(req.Object, &capp); err != nil { | ||
logger.Error(err, "could not decode capp object") | ||
return admission.Errored(http.StatusBadRequest, err) | ||
} | ||
|
||
c.handleInner(&capp, req) | ||
|
||
marshaledCapp, err := json.Marshal(capp) | ||
if err != nil { | ||
return admission.Errored(http.StatusInternalServerError, err) | ||
} | ||
|
||
return admission.PatchResponseFromRaw(req.Object.Raw, marshaledCapp) | ||
} | ||
|
||
// handleInner sets an annotation on the Capp object to track the last user who updated it. | ||
func (c *CappMutator) handleInner(capp *cappv1alpha1.Capp, req admission.Request) { | ||
if capp.ObjectMeta.Annotations == nil { | ||
capp.ObjectMeta.Annotations = make(map[string]string) | ||
} | ||
|
||
capp.ObjectMeta.Annotations[LastUpdatedByAnnotationKey] = req.UserInfo.Username | ||
} |
This file contains 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 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 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,49 @@ | ||
package e2e_tests | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/dana-team/rcs-ocm-deployer/internal/webhooks" | ||
mock "github.com/dana-team/rcs-ocm-deployer/test/e2e_tests/mocks" | ||
utilst "github.com/dana-team/rcs-ocm-deployer/test/e2e_tests/utils" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
const ( | ||
adminAnnotationValue = "kubernetes-admin" | ||
) | ||
|
||
var _ = Describe("Validate the mutating webhook", func() { | ||
It("Should add annotation on create", func() { | ||
baseCapp := mock.CreateBaseCapp() | ||
capp := utilst.CreateCapp(k8sClient, baseCapp) | ||
|
||
annotation := capp.ObjectMeta.Annotations[webhooks.LastUpdatedByAnnotationKey] | ||
Expect(annotation).To(Equal(adminAnnotationValue)) | ||
}) | ||
|
||
It("Should add annotation on update", func() { | ||
baseCapp := mock.CreateBaseCapp() | ||
capp := utilst.CreateCapp(k8sClient, baseCapp) | ||
|
||
annotation := capp.ObjectMeta.Annotations[webhooks.LastUpdatedByAnnotationKey] | ||
Expect(annotation).To(Equal(adminAnnotationValue)) | ||
|
||
utilst.SwitchUser(&k8sClient, cfg, mock.NSName, newScheme(), true) | ||
capp = utilst.GetCapp(k8sClient, capp.Name, capp.Namespace) | ||
capp.ObjectMeta.Annotations["test"] = "test" | ||
utilst.UpdateCapp(k8sClient, capp) | ||
|
||
updatedCapp := utilst.GetCapp(k8sClient, capp.Name, capp.Namespace) | ||
|
||
// Check if the annotation has changed | ||
updatedAnnotation := updatedCapp.ObjectMeta.Annotations[webhooks.LastUpdatedByAnnotationKey] | ||
Expect(updatedAnnotation).To(Equal(fmt.Sprintf(utilst.ServiceAccountNameFormat, mock.NSName, utilst.ServiceAccountName))) | ||
}) | ||
|
||
AfterEach(func() { | ||
// Revert k8sClient back to use the original configuration | ||
utilst.SwitchUser(&k8sClient, cfg, mock.NSName, newScheme(), false) | ||
}) | ||
}) |
This file contains 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 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 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.