Skip to content

Commit

Permalink
Handle Service Account auto mount with Deployment Spec token auto mount
Browse files Browse the repository at this point in the history
The Deployment Spec token auto mount takes precendese over Service Account token
auto mount. Now both token auto mount properties should be set to true in order
to not mount volume.
  • Loading branch information
RafalKorepta committed Dec 4, 2024
1 parent 05d3182 commit f59aae6
Show file tree
Hide file tree
Showing 5 changed files with 1,718 additions and 229 deletions.
17 changes: 17 additions & 0 deletions charts/operator/ci/03-enable-automount-values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
---
serviceAccount:
automountServiceAccountToken: true
6 changes: 3 additions & 3 deletions charts/operator/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func operatorContainers(dot *helmette.Dot, podTerminationGracePeriodSeconds *int
func kubeRBACProxyVolumeMounts(dot *helmette.Dot) []corev1.VolumeMount {
values := helmette.Unwrap[Values](dot.Values)

if !(values.ServiceAccount.Create && !ptr.Deref(values.ServiceAccount.AutomountServiceAccountToken, false)) {
if !(values.ServiceAccount.Create && (!ptr.Deref(values.ServiceAccount.AutomountServiceAccountToken, false)) || !ptr.Deref(values.PodTemplate.Spec.AutomountServiceAccountToken, false)) {
return nil
}

Expand Down Expand Up @@ -259,7 +259,7 @@ func operatorPodVolumes(dot *helmette.Dot) []corev1.Volume {

vol := []corev1.Volume{}

if values.ServiceAccount.Create && !ptr.Deref(values.ServiceAccount.AutomountServiceAccountToken, false) {
if values.ServiceAccount.Create && (!ptr.Deref(values.ServiceAccount.AutomountServiceAccountToken, false) || !ptr.Deref(values.PodTemplate.Spec.AutomountServiceAccountToken, false)) {
vol = append(vol, kubeTokenAPIVolume(ServiceAccountVolumeName))
}

Expand Down Expand Up @@ -335,7 +335,7 @@ func operatorPodVolumesMounts(dot *helmette.Dot) []corev1.VolumeMount {

volMount := []corev1.VolumeMount{}

if values.ServiceAccount.Create && !ptr.Deref(values.ServiceAccount.AutomountServiceAccountToken, false) {
if values.ServiceAccount.Create && (!ptr.Deref(values.ServiceAccount.AutomountServiceAccountToken, false) || !ptr.Deref(values.PodTemplate.Spec.AutomountServiceAccountToken, false)) {
mountName := ServiceAccountVolumeName
for _, vol := range operatorPodVolumes(dot) {
if strings.HasPrefix(ServiceAccountVolumeName+"-", vol.Name) {
Expand Down
6 changes: 3 additions & 3 deletions charts/operator/templates/_deployment.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
{{- range $_ := (list 1) -}}
{{- $_is_returning := false -}}
{{- $values := $dot.Values.AsMap -}}
{{- if (not ((and $values.serviceAccount.create (not (get (fromJson (include "_shims.ptr_Deref" (dict "a" (list $values.serviceAccount.automountServiceAccountToken false) ))) "r"))))) -}}
{{- if (not ((or (and $values.serviceAccount.create ((not (get (fromJson (include "_shims.ptr_Deref" (dict "a" (list $values.serviceAccount.automountServiceAccountToken false) ))) "r")))) (not (get (fromJson (include "_shims.ptr_Deref" (dict "a" (list $values.podTemplate.spec.automountServiceAccountToken false) ))) "r"))))) -}}
{{- $_is_returning = true -}}
{{- (dict "r" (coalesce nil)) | toJson -}}
{{- break -}}
Expand Down Expand Up @@ -134,7 +134,7 @@
{{- $_is_returning := false -}}
{{- $values := $dot.Values.AsMap -}}
{{- $vol := (list ) -}}
{{- if (and $values.serviceAccount.create (not (get (fromJson (include "_shims.ptr_Deref" (dict "a" (list $values.serviceAccount.automountServiceAccountToken false) ))) "r"))) -}}
{{- if (and $values.serviceAccount.create ((or (not (get (fromJson (include "_shims.ptr_Deref" (dict "a" (list $values.serviceAccount.automountServiceAccountToken false) ))) "r")) (not (get (fromJson (include "_shims.ptr_Deref" (dict "a" (list $values.podTemplate.spec.automountServiceAccountToken false) ))) "r"))))) -}}
{{- $vol = (concat (default (list ) $vol) (list (get (fromJson (include "operator.kubeTokenAPIVolume" (dict "a" (list "kube-api-access") ))) "r"))) -}}
{{- end -}}
{{- if (not $values.webhook.enabled) -}}
Expand Down Expand Up @@ -165,7 +165,7 @@
{{- $_is_returning := false -}}
{{- $values := $dot.Values.AsMap -}}
{{- $volMount := (list ) -}}
{{- if (and $values.serviceAccount.create (not (get (fromJson (include "_shims.ptr_Deref" (dict "a" (list $values.serviceAccount.automountServiceAccountToken false) ))) "r"))) -}}
{{- if (and $values.serviceAccount.create ((or (not (get (fromJson (include "_shims.ptr_Deref" (dict "a" (list $values.serviceAccount.automountServiceAccountToken false) ))) "r")) (not (get (fromJson (include "_shims.ptr_Deref" (dict "a" (list $values.podTemplate.spec.automountServiceAccountToken false) ))) "r"))))) -}}
{{- $mountName := "kube-api-access" -}}
{{- range $_, $vol := (get (fromJson (include "operator.operatorPodVolumes" (dict "a" (list $dot) ))) "r") -}}
{{- if (hasPrefix $vol.name (printf "%s%s" "kube-api-access" "-")) -}}
Expand Down
Loading

0 comments on commit f59aae6

Please sign in to comment.