Skip to content

Commit

Permalink
feat(mutation/helm) - Add mutation to helm chart (#1220)
Browse files Browse the repository at this point in the history
Signed-off-by: Rita Zhang <rita.z.zhang@gmail.com>
  • Loading branch information
ritazh committed Apr 6, 2021
1 parent 0823900 commit e1021ca
Show file tree
Hide file tree
Showing 14 changed files with 427 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ bin

# Manager image patch file
config/overlays/dev/manager_image_patch.yaml
config/overlays/dev_mutation/manager_image_patch.yaml

# Kubernetes Generated files - skip generated files, except for vendored files

Expand Down
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ install: manifests
kustomize build config/crd | kubectl apply -f -

deploy-mutation: patch-image
@grep -q -v 'enable-mutation' ./config/overlays/dev/manager_image_patch.yaml && sed -i '/- --operation=webhook/a \ \ \ \ \ \ \ \ - --enable-mutation=true' ./config/overlays/dev/manager_image_patch.yaml
kustomize build config/overlays/mutation_webhook | kubectl apply -f -
@grep -q -v 'enable-mutation' ./config/overlays/dev_mutation/manager_image_patch.yaml && sed -i '/- --operation=webhook/a \ \ \ \ \ \ \ \ - --enable-mutation=true' ./config/overlays/dev_mutation/manager_image_patch.yaml
kustomize build config/overlays/dev_mutation | kubectl apply -f -
kustomize build --load_restrictor LoadRestrictionsNone config/overlays/mutation | kubectl apply -f -

# Deploy controller in the configured Kubernetes cluster in ~/.kube/config
Expand Down Expand Up @@ -258,10 +258,13 @@ docker-buildx-release:
patch-image:
@echo "updating kustomize image patch file for manager resource"
@bash -c 'echo -e ${MANAGER_IMAGE_PATCH} > ./config/overlays/dev/manager_image_patch.yaml'
cp ./config/overlays/dev/manager_image_patch.yaml ./config/overlays/dev_mutation/manager_image_patch.yaml
ifeq ($(USE_LOCAL_IMG),true)
@sed -i '/^ name: manager/a \ \ \ \ \ \ \ \ imagePullPolicy: IfNotPresent' ./config/overlays/dev/manager_image_patch.yaml
@sed -i '/^ name: manager/a \ \ \ \ \ \ \ \ imagePullPolicy: IfNotPresent' ./config/overlays/dev_mutation/manager_image_patch.yaml
endif
@sed -i'' -e 's@image: .*@image: '"${IMG}"'@' ./config/overlays/dev/manager_image_patch.yaml
@sed -i'' -e 's@image: .*@image: '"${IMG}"'@' ./config/overlays/dev_mutation/manager_image_patch.yaml

# Rebuild pkg/target/target_template_source.go to pull in pkg/target/regolib/src.rego
target-template-source:
Expand Down
14 changes: 13 additions & 1 deletion cmd/build/helmify/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ commonLabels:
release: "{{ .Release.Name }}"
heritage: "{{ .Release.Service }}"
bases:
- "../../../config/default"
- "../../../config/overlays/mutation_webhook" # calls ../../default
patchesStrategicMerge:
- kustomize-for-helm.yaml
patchesJson6902:
Expand Down Expand Up @@ -33,6 +33,18 @@ patchesJson6902:
kind: CustomResourceDefinition
name: configs.config.gatekeeper.sh
path: labels_patch.yaml
- target:
group: apiextensions.k8s.io
version: v1beta1
kind: CustomResourceDefinition
name: assignmetadata.mutations.gatekeeper.sh
path: labels_patch.yaml
- target:
group: apiextensions.k8s.io
version: v1beta1
kind: CustomResourceDefinition
name: assign.mutations.gatekeeper.sh
path: labels_patch.yaml
# these are defined in the chart values rather than hard-coded
- target:
kind: Deployment
Expand Down
19 changes: 19 additions & 0 deletions cmd/build/helmify/kustomize-for-helm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ metadata:
helm.sh/hook-delete-policy: before-hook-creation
status: null
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: assignmetadata.mutations.gatekeeper.sh
annotations:
helm.sh/hook: crd-install
helm.sh/hook-delete-policy: before-hook-creation
status: null
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: assign.mutations.gatekeeper.sh
annotations:
helm.sh/hook: crd-install
helm.sh/hook-delete-policy: before-hook-creation
status: null
---
apiVersion: apps/v1
kind: Deployment
metadata:
Expand All @@ -66,6 +84,7 @@ spec:
- --log-level={{ .Values.logLevel }}
- --exempt-namespace={{ include "gatekeeper.namespace" . }}
- --operation=webhook
- --enable-mutation={{ .Values.experimentalEnableMutation}}
imagePullPolicy: "{{ .Values.image.pullPolicy }}"
image: "{{ .Values.image.repository }}:{{ .Values.image.release }}"
resources:
Expand Down
4 changes: 4 additions & 0 deletions cmd/build/helmify/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ func (ks *kindSet) Write() error {
obj = "{{- if not .Values.disableValidatingWebhook }}\n" + obj + "{{- end }}\n"
}

if name == "gatekeeper-mutating-webhook-configuration" {
obj = "{{- if .Values.experimentalEnableMutation }}\n" + obj + "{{- end }}\n"
}

if name == "gatekeeper-system" {
obj = "{{- if .Values.createNamespace }}\n" + obj + "{{- end }}\n"
}
Expand Down
1 change: 1 addition & 0 deletions cmd/build/helmify/static/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ createNamespace: true
disableValidatingWebhook: false
validatingWebhookTimeoutSeconds: 3
enableDeleteOperations: false
experimentalEnableMutation: false
auditChunkSize: 0
logLevel: INFO
logDenies: false
Expand Down
8 changes: 8 additions & 0 deletions config/overlays/dev_mutation/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Adds namespace to all resources.
namespace: gatekeeper-system

resources:
- ../mutation_webhook

patchesStrategicMerge:
- manager_image_patch.yaml
2 changes: 1 addition & 1 deletion config/overlays/mutation_webhook/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace: gatekeeper-system

bases:
- ../dev
- ../../default

resources:
- webhook.yaml
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.3.0
helm.sh/hook: crd-install
helm.sh/hook-delete-policy: before-hook-creation
labels:
gatekeeper.sh/system: "yes"
name: assign.mutations.gatekeeper.sh
spec:
group: mutations.gatekeeper.sh
names:
kind: Assign
listKind: AssignList
plural: assign
singular: assign
scope: Cluster
validation:
openAPIV3Schema:
description: Assign is the Schema for the assign API
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
spec:
description: AssignSpec defines the desired state of Assign
properties:
applyTo:
description: 'INSERT ADDITIONAL SPEC FIELDS - desired state of cluster Important: Run "make" to regenerate code after modifying this file'
items:
description: ApplyTo determines what GVKs items the mutation should apply to. Globs are not allowed.
properties:
groups:
items:
type: string
type: array
kinds:
items:
type: string
type: array
versions:
items:
type: string
type: array
type: object
type: array
location:
type: string
match:
properties:
excludedNamespaces:
items:
type: string
type: array
kinds:
items:
description: Kinds accepts a list of objects with apiGroups and kinds fields that list the groups/kinds of objects to which the mutation will apply. If multiple groups/kinds objects are specified, only one match is needed for the resource to be in scope.
properties:
apiGroups:
description: APIGroups is the API groups the resources belong to. '*' is all groups. If '*' is present, the length of the slice must be one. Required.
items:
type: string
type: array
kinds:
items:
type: string
type: array
type: object
type: array
labelSelector:
description: A label selector is a label query over a set of resources. The result of matchLabels and matchExpressions are ANDed. An empty label selector matches all objects. A null label selector matches no objects.
properties:
matchExpressions:
description: matchExpressions is a list of label selector requirements. The requirements are ANDed.
items:
description: A label selector requirement is a selector that contains values, a key, and an operator that relates the key and values.
properties:
key:
description: key is the label key that the selector applies to.
type: string
operator:
description: operator represents a key's relationship to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist.
type: string
values:
description: values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. If the operator is Exists or DoesNotExist, the values array must be empty. This array is replaced during a strategic merge patch.
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
matchLabels:
additionalProperties:
type: string
description: matchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels map is equivalent to an element of matchExpressions, whose key field is "key", the operator is "In", and the values array contains only "value". The requirements are ANDed.
type: object
type: object
namespaceSelector:
description: A label selector is a label query over a set of resources. The result of matchLabels and matchExpressions are ANDed. An empty label selector matches all objects. A null label selector matches no objects.
properties:
matchExpressions:
description: matchExpressions is a list of label selector requirements. The requirements are ANDed.
items:
description: A label selector requirement is a selector that contains values, a key, and an operator that relates the key and values.
properties:
key:
description: key is the label key that the selector applies to.
type: string
operator:
description: operator represents a key's relationship to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist.
type: string
values:
description: values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. If the operator is Exists or DoesNotExist, the values array must be empty. This array is replaced during a strategic merge patch.
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
matchLabels:
additionalProperties:
type: string
description: matchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels map is equivalent to an element of matchExpressions, whose key field is "key", the operator is "In", and the values array contains only "value". The requirements are ANDed.
type: object
type: object
namespaces:
items:
type: string
type: array
scope:
description: ResourceScope is an enum defining the different scopes available to a custom resource
type: string
required:
- scope
type: object
parameters:
properties:
assign:
description: Assign.value holds the value to be assigned
type: object
x-kubernetes-preserve-unknown-fields: true
ifIn:
description: IfIn Only mutate if the current value is in the supplied list
items:
type: string
type: array
ifNotIn:
description: IfNotIn Only mutate if the current value is NOT in the supplied list
items:
type: string
type: array
pathTests:
items:
description: "PathTests allows the user to customize how the mutation works if parent paths are missing. It traverses the list in order. All sub paths are tested against the provided condition, if the test fails, the mutation is not applied. All `subPath` entries must be a prefix of `location`. Any glob characters will take on the same value as was used to expand the matching glob in `location`. \n Available Tests: * MustExist - the path must exist or do not mutate * MustNotExist - the path must not exist or do not mutate"
properties:
condition:
enum:
- MustExist
- MustNotExist
type: string
subPath:
type: string
type: object
type: array
type: object
type: object
status:
description: AssignStatus defines the observed state of Assign
type: object
type: object
version: v1alpha1
versions:
- name: v1alpha1
served: true
storage: true
Loading

0 comments on commit e1021ca

Please sign in to comment.