-
Notifications
You must be signed in to change notification settings - Fork 4
add cluster-trust-bundle feature #51
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
Changes from 3 commits
cd2bd1a
09834a1
6cdc5d1
9c52474
3141a7c
05e0095
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| apiVersion: certificates.k8s.io/v1beta1 | ||
| kind: ClusterTrustBundle | ||
| metadata: | ||
| name: k3d.test:ctb:1 | ||
| spec: | ||
| signerName: rt-bootstrapper-k3d.test/ctb | ||
| trustBundle: | | ||
| -----BEGIN CERTIFICATE----- | ||
| MIIBdzCCAR2gAwIBAgIBADAKBggqhkjOPQQDAjAjMSEwHwYDVQQDDBhrM3Mtc2Vy | ||
| dmVyLWNhQDE3NjcwMTMxNjUwHhcNMjUxMjI5MTI1OTI1WhcNMzUxMjI3MTI1OTI1 | ||
| WjAjMSEwHwYDVQQDDBhrM3Mtc2VydmVyLWNhQDE3NjcwMTMxNjUwWTATBgcqhkjO | ||
| PQIBBggqhkjOPQMBBwNCAASDZGb8hHA4r7/tLECdLLLtOQpfA0W+5FXdc4xJI7Zi | ||
| dwXz4WiliqVIxi77ow+c39EOe29X8yuNtbOouWsqn1Vho0IwQDAOBgNVHQ8BAf8E | ||
| BAMCAqQwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUoqr1Zk9sh+WqFtLhFgUe | ||
| e0m5zGEwCgYIKoZIzj0EAwIDSAAwRQIhAK5eY2h5Ui8OivvqqpmWPx7rJYiEWR1g | ||
| +K3J/5+FXUv2AiBQUtMXc/FlAHWT3u4j98v4XukRZftEVbrVK6+zn6EaFQ== | ||
| -----END CERTIFICATE----- | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ package v1 | |
|
|
||
| import ( | ||
| "log/slog" | ||
| "reflect" | ||
| "slices" | ||
|
|
||
| "github.com/kyma-project/rt-bootstrapper/internal/webhook/k8s" | ||
|
|
@@ -18,6 +19,9 @@ var ( | |
| annotationsSetPullSecret = map[string]string{ | ||
| apiv1.AnnotationSetPullSecret: "false", | ||
| } | ||
| annotationAddClusterTrustBundle = map[string]string{ | ||
| apiv1.AnnotationAddClusterTrustBundle: "false", | ||
| } | ||
| ) | ||
|
|
||
| func defaultPod(update func(*corev1.Pod) bool, features map[string]string) PodDefaulter { | ||
|
|
@@ -96,3 +100,67 @@ func BuildPodDefaulterAddImagePullSecrets(secretName string) PodDefaulter { | |
|
|
||
| return defaultPod(addImgPullSecret, annotationsSetPullSecret) | ||
| } | ||
|
|
||
| func BuildDefaulterAddClusterTrustBundle(mapping k8s.ClusterTrustBundleMapping) PodDefaulter { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a general remark: the defaulters could be tested by unit tests. I don't think we must add such unit tests right now, but it would nicely fit into the entire testing idea for the webhook.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fully agree, we will add tests for all defaulters. |
||
| slog.Debug("building volume", mapping.KeysAndValues()...) | ||
|
|
||
| vol := mapping.ClusterTrustedBundle() | ||
|
|
||
| addVolumeMount := func(modified bool, p *corev1.Pod) bool { | ||
|
|
||
| size := len(p.Spec.Containers) | ||
| results := make([]bool, size) | ||
|
|
||
| for i, c := range p.Spec.Containers { | ||
| index := slices.IndexFunc(c.VolumeMounts, func(vm corev1.VolumeMount) bool { | ||
| return vm.Name == mapping.Name | ||
| }) | ||
|
|
||
| if index == -1 { | ||
| // volume mount does not exist, add it | ||
| vm := mapping.VolumeMount() | ||
| p.Spec.Containers[i].VolumeMounts = append(c.VolumeMounts, vm) | ||
| results[i] = true | ||
| slog.Debug("volume mount added") | ||
| continue | ||
| } | ||
|
|
||
| if reflect.DeepEqual(c.VolumeMounts[index], vol) { | ||
| results[i] = false | ||
| slog.Debug("volume already mounted, nothing to do") | ||
| continue | ||
| } | ||
|
|
||
| p.Spec.Volumes[index] = vol | ||
| slog.Debug("volume mount replaced") | ||
| results[i] = true | ||
| } | ||
|
|
||
| return modified || slices.Contains(results, true) | ||
| } | ||
|
|
||
| addClusterTrustBundle := func(p *corev1.Pod) bool { | ||
| index := slices.IndexFunc(p.Spec.Volumes, func(v corev1.Volume) bool { | ||
| return v.Name == mapping.Name | ||
| }) | ||
|
|
||
| if index == -1 { | ||
| // volume does not exist, add it | ||
| p.Spec.Volumes = append(p.Spec.Volumes, vol) | ||
| slog.Debug("volume added") | ||
| return addVolumeMount(true, p) | ||
| } | ||
|
|
||
| if reflect.DeepEqual(p.Spec.Volumes[index], vol) { | ||
| slog.Debug("equal volume found, nothing to do") | ||
m00g3n marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return addVolumeMount(false, p) | ||
| } | ||
|
|
||
| p.Spec.Volumes[index] = vol | ||
| slog.Debug("volume replaced") | ||
|
|
||
| return addVolumeMount(true, p) | ||
| } | ||
|
|
||
| return defaultPod(addClusterTrustBundle, annotationAddClusterTrustBundle) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| apiVersion: apps/v1 | ||
| kind: Deployment | ||
| metadata: | ||
| name: pause-test4 | ||
| namespace: rt-bootstrapper-test1 | ||
| labels: | ||
| app: pause-test4 | ||
| spec: | ||
| replicas: 1 | ||
| selector: | ||
| matchLabels: | ||
| app: pause-test4 | ||
| template: | ||
| metadata: | ||
| annotations: | ||
| rt-cfg.kyma-project.io/add-img-pull-secret: "false" | ||
| rt-cfg.kyma-project.io/add-add-cluster-trust-bundle: "false" | ||
| labels: | ||
| app: pause-test4 | ||
| spec: | ||
| containers: | ||
| - name: pause | ||
| image: k8s.gcr.io/pause:latest | ||
|
|
||
|
|
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.
I'm not fully convinced to the name of the element. Instead of using
clusterTrustBundleMappingwe could do something like: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.
Lets do this refactor in the this PR just to avoid collisions.