-
Notifications
You must be signed in to change notification settings - Fork 2
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
base: main
Are you sure you want to change the base?
Changes from all 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,74 @@ 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() | ||
|
|
||
| handleVolumeMount := func(cs []corev1.Container) bool { | ||
| // stores information if any container was modified | ||
| var result bool | ||
|
|
||
| for i, c := range cs { | ||
| index := slices.IndexFunc(c.VolumeMounts, func(vm corev1.VolumeMount) bool { | ||
| return vm.Name == mapping.VolumeName | ||
| }) | ||
|
|
||
| if index == -1 { | ||
| vm := mapping.VolumeMount() | ||
| cs[i].VolumeMounts = append(c.VolumeMounts, vm) | ||
| result = true | ||
| slog.Debug("volume mount added") | ||
| continue | ||
| } | ||
|
|
||
| if reflect.DeepEqual(c.VolumeMounts[index], vol) { | ||
| slog.Debug("volume already mounted, nothing to do") | ||
| continue | ||
| } | ||
|
|
||
| vm := mapping.VolumeMount() | ||
| cs[i].VolumeMounts[index] = vm | ||
| slog.Debug("volume mount replaced") | ||
| result = true | ||
| } | ||
|
|
||
| return result | ||
| } | ||
|
|
||
| handleVolumeMounts := func(modified bool, p *corev1.Pod) bool { | ||
| for _, cs := range [][]corev1.Container{p.Spec.Containers, p.Spec.InitContainers} { | ||
|
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. In case we will have two or more containers the volume mounts will be modified only for the first one. I think the code should be modified as follows: if handleVolumeMount(cs) {
result = true
} |
||
| if handleVolumeMount(cs) { | ||
| modified = true | ||
| } | ||
| } | ||
| return modified | ||
| } | ||
|
|
||
| handleClusterTrustBundle := func(p *corev1.Pod) bool { | ||
| index := slices.IndexFunc(p.Spec.Volumes, func(v corev1.Volume) bool { | ||
| return v.Name == mapping.VolumeName | ||
| }) | ||
|
|
||
| if index == -1 { | ||
| // volume does not exist, add it | ||
| p.Spec.Volumes = append(p.Spec.Volumes, vol) | ||
| slog.Debug("volume added") | ||
| return handleVolumeMounts(true, p) | ||
| } | ||
|
|
||
| if reflect.DeepEqual(p.Spec.Volumes[index], vol) { | ||
| slog.Debug("volume already added, nothing to do") | ||
| return handleVolumeMounts(false, p) | ||
| } | ||
|
|
||
| p.Spec.Volumes[index] = vol | ||
| slog.Debug("volume replaced") | ||
|
|
||
| return handleVolumeMounts(true, p) | ||
| } | ||
|
|
||
| return defaultPod(handleClusterTrustBundle, 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.