Skip to content

Commit

Permalink
Add support for the --log-mutations and --mutation-annotations flags
Browse files Browse the repository at this point in the history
  • Loading branch information
mprahl authored and openshift-merge-bot[bot] committed Dec 6, 2023
1 parent 86c813e commit 660bc20
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 7 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/ci_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,6 @@ jobs:
path: gatekeeper
fetch-depth: 0 # Fetch all history for all tags and branches

# TODO: gatekeeper-operator does not --log-mutations nor --mutation-annotations
- name: Remove unhandled gatekeeper bats tests
run: |
sed -i -e '67,73d;82,85d' /home/runner/work/gatekeeper-operator/gatekeeper-operator/gatekeeper/test/bats/test.bats
- name: Gatekeeper E2E Tests
run: |
make download-binaries
Expand Down
7 changes: 7 additions & 0 deletions api/v1alpha1/gatekeeper_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ type WebhookConfig struct {
Resources *corev1.ResourceRequirements `json:"resources,omitempty"`
// +optional
DisabledBuiltins []string `json:"disabledBuiltins,omitempty"`
// +optional
// Sets the --log-mutations flag which enables logging of mutation events and errors. This defaults to Disabled.
LogMutations *Mode `json:"logMutations,omitempty"`
// +optional
// Sets the --mutation-annotations flag which adds the gatekeeper.sh/mutation-id and gatekeeper.sh/mutations
// annotations on mutated objects. This defaults to Disabled.
MutationAnnotations *Mode `json:"mutationAnnotations,omitempty"`
}

// +kubebuilder:validation:Enum:=DEBUG;INFO;WARNING;ERROR
Expand Down
10 changes: 10 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions bundle/manifests/operator.gatekeeper.sh_gatekeepers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,21 @@ spec:
- WARNING
- ERROR
type: string
logMutations:
description: Sets the --log-mutations flag which enables logging
of mutation events and errors. This defaults to Disabled.
enum:
- Enabled
- Disabled
type: string
mutationAnnotations:
description: Sets the --mutation-annotations flag which adds the
gatekeeper.sh/mutation-id and gatekeeper.sh/mutations annotations
on mutated objects. This defaults to Disabled.
enum:
- Enabled
- Disabled
type: string
namespaceSelector:
description: A label selector is a label query over a set of resources.
The result of matchLabels and matchExpressions are ANDed. An
Expand Down
15 changes: 15 additions & 0 deletions config/crd/bases/operator.gatekeeper.sh_gatekeepers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,21 @@ spec:
- WARNING
- ERROR
type: string
logMutations:
description: Sets the --log-mutations flag which enables logging
of mutation events and errors. This defaults to Disabled.
enum:
- Enabled
- Disabled
type: string
mutationAnnotations:
description: Sets the --mutation-annotations flag which adds the
gatekeeper.sh/mutation-id and gatekeeper.sh/mutations annotations
on mutated objects. This defaults to Disabled.
enum:
- Enabled
- Disabled
type: string
namespaceSelector:
description: A label selector is a label query over a set of resources.
The result of matchLabels and matchExpressions are ANDed. An
Expand Down
2 changes: 2 additions & 0 deletions config/samples/gatekeeper_e2e_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ spec:
admissionEventsInvolvedNamespace: Enabled
disabledBuiltins:
- http.send
logMutations: Enabled
mutationAnnotations: Enabled
27 changes: 27 additions & 0 deletions controllers/gatekeeper_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ const (
OperationMutationStatus = "mutation-status"
OperationMutationWebhook = "mutation-webhook"
DisabledBuiltinArg = "--disable-opa-builtin"
LogMutationsArg = "--log-mutations"
MutationAnnotationsArg = "--mutation-annotations"
)

var (
Expand Down Expand Up @@ -659,6 +661,10 @@ func webhookOverrides(obj *unstructured.Unstructured, webhook *operatorv1alpha1.
if err := setDisabledBuiltins(obj, webhook.DisabledBuiltins); err != nil {
return err
}

if err := setMutationFlags(obj, webhook); err != nil {
return err
}
}

return nil
Expand Down Expand Up @@ -833,6 +839,27 @@ func setLogLevel(obj *unstructured.Unstructured, logLevel *operatorv1alpha1.LogL
return nil
}

func setMutationFlags(obj *unstructured.Unstructured, webhookConfig *operatorv1alpha1.WebhookConfig) error {
if webhookConfig == nil {
return nil
}

if webhookConfig.LogMutations != nil && webhookConfig.LogMutations.ToBool() {
err := setContainerArg(obj, managerContainer, LogMutationsArg, webhookConfig.LogMutations.ToBoolString(), false)
if err != nil {
return err
}
}

if webhookConfig.MutationAnnotations != nil && webhookConfig.MutationAnnotations.ToBool() {
return setContainerArg(
obj, managerContainer, MutationAnnotationsArg, webhookConfig.MutationAnnotations.ToBoolString(), false,
)
}

return nil
}

func setAuditInterval(obj *unstructured.Unstructured, auditInterval *metav1.Duration) error {
if auditInterval != nil {
return setContainerArg(obj, managerContainer, AuditIntervalArg, fmt.Sprint(auditInterval.Round(time.Second).Seconds()), false)
Expand Down
25 changes: 23 additions & 2 deletions controllers/gatekeeper_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1321,13 +1321,17 @@ func TestAllWebhookArgs(t *testing.T) {
expectObjContainerArgument(g, managerContainer, webhookObj).NotTo(HaveKey(AdmissionEventsInvolvedNamespaceArg))
expectObjContainerArgument(g, managerContainer, webhookObj).NotTo(HaveKey(LogLevelArg))
expectObjContainerArgument(g, managerContainer, webhookObj).NotTo(HaveKey(EnableMutationArg))
expectObjContainerArgument(g, managerContainer, webhookObj).NotTo(HaveKey(LogMutationsArg))
expectObjContainerArgument(g, managerContainer, webhookObj).NotTo(HaveKey(MutationAnnotationsArg))
// test nil
err = crOverrides(gatekeeper, WebhookFile, webhookObj, namespace, false, false)
g.Expect(err).ToNot(HaveOccurred())
expectObjContainerArgument(g, managerContainer, webhookObj).NotTo(HaveKey(EmitAdmissionEventsArg))
expectObjContainerArgument(g, managerContainer, webhookObj).NotTo(HaveKey(AdmissionEventsInvolvedNamespaceArg))
expectObjContainerArgument(g, managerContainer, webhookObj).NotTo(HaveKey(LogLevelArg))
expectObjContainerArgument(g, managerContainer, webhookObj).NotTo(HaveKey(EnableMutationArg))
expectObjContainerArgument(g, managerContainer, webhookObj).NotTo(HaveKey(LogMutationsArg))
expectObjContainerArgument(g, managerContainer, webhookObj).NotTo(HaveKey(MutationAnnotationsArg))
// test override without mutation
gatekeeper.Spec.Webhook = &webhookOverride
err = crOverrides(gatekeeper, WebhookFile, webhookObj, namespace, false, false)
Expand All @@ -1336,15 +1340,32 @@ func TestAllWebhookArgs(t *testing.T) {
expectObjContainerArgument(g, managerContainer, webhookObj).To(HaveKeyWithValue(AdmissionEventsInvolvedNamespaceArg, "true"))
expectObjContainerArgument(g, managerContainer, webhookObj).To(HaveKeyWithValue(LogLevelArg, "DEBUG"))
expectObjContainerArgument(g, managerContainer, webhookObj).NotTo(HaveKey(EnableMutationArg))
expectObjContainerArgument(g, managerContainer, webhookObj).NotTo(HaveKey(LogMutationsArg))
expectObjContainerArgument(g, managerContainer, webhookObj).NotTo(HaveKey(MutationAnnotationsArg))
// test override with mutation
mutatingWebhook := operatorv1alpha1.Enabled
gatekeeper.Spec.MutatingWebhook = &mutatingWebhook
enabled := operatorv1alpha1.Enabled
gatekeeper.Spec.MutatingWebhook = &enabled
err = crOverrides(gatekeeper, WebhookFile, webhookObj, namespace, false, false)
g.Expect(err).ToNot(HaveOccurred())
expectObjContainerArgument(g, managerContainer, webhookObj).To(HaveKeyWithValue(EmitAdmissionEventsArg, "true"))
expectObjContainerArgument(g, managerContainer, webhookObj).To(HaveKeyWithValue(AdmissionEventsInvolvedNamespaceArg, "true"))
expectObjContainerArgument(g, managerContainer, webhookObj).To(HaveKeyWithValue(LogLevelArg, "DEBUG"))
expectObjContainerArgument(g, managerContainer, webhookObj).To(HaveKeyWithValue(OperationArg, OperationMutationWebhook))
expectObjContainerArgument(g, managerContainer, webhookObj).NotTo(HaveKey(LogMutationsArg))
expectObjContainerArgument(g, managerContainer, webhookObj).NotTo(HaveKey(MutationAnnotationsArg))

// test override with mutation flags
gatekeeper.Spec.MutatingWebhook = &enabled
gatekeeper.Spec.Webhook.LogMutations = &enabled
gatekeeper.Spec.Webhook.MutationAnnotations = &enabled
err = crOverrides(gatekeeper, WebhookFile, webhookObj, namespace, false, false)
g.Expect(err).ToNot(HaveOccurred())
expectObjContainerArgument(g, managerContainer, webhookObj).To(HaveKeyWithValue(EmitAdmissionEventsArg, "true"))
expectObjContainerArgument(g, managerContainer, webhookObj).To(HaveKeyWithValue(AdmissionEventsInvolvedNamespaceArg, "true"))
expectObjContainerArgument(g, managerContainer, webhookObj).To(HaveKeyWithValue(LogLevelArg, "DEBUG"))
expectObjContainerArgument(g, managerContainer, webhookObj).To(HaveKeyWithValue(OperationArg, OperationMutationWebhook))
expectObjContainerArgument(g, managerContainer, webhookObj).To(HaveKeyWithValue(LogMutationsArg, "true"))
expectObjContainerArgument(g, managerContainer, webhookObj).To(HaveKeyWithValue(MutationAnnotationsArg, "true"))
}

func expectObjContainerArgument(g *WithT, containerName string, obj *unstructured.Unstructured) Assertion {
Expand Down

0 comments on commit 660bc20

Please sign in to comment.