@@ -3,6 +3,7 @@ package kubernetes
33import (
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+
119145func flattenPodDNSConfig (in * v1.PodDNSConfig ) ([]interface {}, error ) {
120146 att := make (map [string ]interface {})
121147
0 commit comments