Skip to content

Commit 695f78a

Browse files
authored
Remove default token volume and volume_mount from Pod state (#1096)
To avoid perpetual diff when using `automount_service_account_token=true`, skip adding the service account's volume to state.
1 parent 0692409 commit 695f78a

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

kubernetes/structures_container.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
v1 "k8s.io/api/core/v1"
77
"k8s.io/apimachinery/pkg/api/resource"
88
"k8s.io/apimachinery/pkg/util/intstr"
9+
"regexp"
910
)
1011

1112
func flattenCapability(in []v1.Capability) []string {
@@ -261,6 +262,7 @@ func flattenValueFrom(in *v1.EnvVarSource) []interface{} {
261262

262263
func flattenContainerVolumeMounts(in []v1.VolumeMount) ([]interface{}, error) {
263264
att := make([]interface{}, len(in))
265+
264266
for i, v := range in {
265267
m := map[string]interface{}{}
266268
m["read_only"] = v.ReadOnly
@@ -353,7 +355,7 @@ func flattenContainerResourceRequirements(in v1.ResourceRequirements) ([]interfa
353355
return []interface{}{att}, nil
354356
}
355357

356-
func flattenContainers(in []v1.Container) ([]interface{}, error) {
358+
func flattenContainers(in []v1.Container, serviceAccountRegex string) ([]interface{}, error) {
357359
att := make([]interface{}, len(in))
358360
for i, v := range in {
359361
c := make(map[string]interface{})
@@ -406,6 +408,18 @@ func flattenContainers(in []v1.Container) ([]interface{}, error) {
406408
}
407409

408410
if len(v.VolumeMounts) > 0 {
411+
for num, m := range v.VolumeMounts {
412+
// To avoid perpetual diff, remove the default service account token volume from the container's list of volumeMounts.
413+
nameMatchesDefaultToken, err := regexp.MatchString(serviceAccountRegex, m.Name)
414+
if err != nil {
415+
return att, err
416+
}
417+
if nameMatchesDefaultToken {
418+
v.VolumeMounts = removeVolumeMountFromContainer(num, v.VolumeMounts)
419+
break
420+
}
421+
}
422+
409423
volumeMounts, err := flattenContainerVolumeMounts(v.VolumeMounts)
410424
if err != nil {
411425
return nil, err
@@ -417,6 +431,11 @@ func flattenContainers(in []v1.Container) ([]interface{}, error) {
417431
return att, nil
418432
}
419433

434+
// removeVolumeMountFromContainer removes the specified VolumeMount index (i) from the given list of VolumeMounts.
435+
func removeVolumeMountFromContainer(i int, v []v1.VolumeMount) []v1.VolumeMount {
436+
return append(v[:i], v[i+1:]...)
437+
}
438+
420439
func expandContainers(ctrs []interface{}) ([]v1.Container, error) {
421440
if len(ctrs) == 0 {
422441
return []v1.Container{}, nil

kubernetes/structures_pod.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package kubernetes
33
import (
44
"fmt"
55
"log"
6+
"regexp"
67
"strconv"
78
"strings"
89

@@ -27,7 +28,14 @@ func flattenPodSpec(in v1.PodSpec) ([]interface{}, error) {
2728
att["automount_service_account_token"] = *in.AutomountServiceAccountToken
2829
}
2930

30-
containers, err := flattenContainers(in.Containers)
31+
// To avoid perpetual diff, remove the service account token volume from PodSpec.
32+
serviceAccountName := "default"
33+
if in.ServiceAccountName != "" {
34+
serviceAccountName = in.ServiceAccountName
35+
}
36+
serviceAccountRegex := fmt.Sprintf("%s-token-([a-z0-9]{5})", serviceAccountName)
37+
38+
containers, err := flattenContainers(in.Containers, serviceAccountRegex)
3139
if err != nil {
3240
return nil, err
3341
}
@@ -39,7 +47,7 @@ func flattenPodSpec(in v1.PodSpec) ([]interface{}, error) {
3947
}
4048
att["readiness_gate"] = gates
4149

42-
initContainers, err := flattenContainers(in.InitContainers)
50+
initContainers, err := flattenContainers(in.InitContainers, serviceAccountRegex)
4351
if err != nil {
4452
return nil, err
4553
}
@@ -87,6 +95,7 @@ func flattenPodSpec(in v1.PodSpec) ([]interface{}, error) {
8795
if in.SecurityContext != nil {
8896
att["security_context"] = flattenPodSecurityContext(in.SecurityContext)
8997
}
98+
9099
if in.ServiceAccountName != "" {
91100
att["service_account_name"] = in.ServiceAccountName
92101
}
@@ -107,6 +116,18 @@ func flattenPodSpec(in v1.PodSpec) ([]interface{}, error) {
107116
}
108117

109118
if len(in.Volumes) > 0 {
119+
for i, volume := range in.Volumes {
120+
// To avoid perpetual diff, remove the service account token volume from PodSpec.
121+
nameMatchesDefaultToken, err := regexp.MatchString(serviceAccountRegex, volume.Name)
122+
if err != nil {
123+
return []interface{}{att}, err
124+
}
125+
if nameMatchesDefaultToken {
126+
in.Volumes = removeVolumeFromPodSpec(i, in.Volumes)
127+
break
128+
}
129+
}
130+
110131
v, err := flattenVolumes(in.Volumes)
111132
if err != nil {
112133
return []interface{}{att}, err
@@ -116,6 +137,11 @@ func flattenPodSpec(in v1.PodSpec) ([]interface{}, error) {
116137
return []interface{}{att}, nil
117138
}
118139

140+
// removeVolumeFromPodSpec removes the specified Volume index (i) from the given list of Volumes.
141+
func removeVolumeFromPodSpec(i int, v []v1.Volume) []v1.Volume {
142+
return append(v[:i], v[i+1:]...)
143+
}
144+
119145
func flattenPodDNSConfig(in *v1.PodDNSConfig) ([]interface{}, error) {
120146
att := make(map[string]interface{})
121147

0 commit comments

Comments
 (0)