-
Notifications
You must be signed in to change notification settings - Fork 988
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
Remove default token volume and volume_mount from Pod state #1096
Conversation
kubernetes/structures_pod.go
Outdated
if nameMatchesDefaultToken && volume.Secret != nil { | ||
for _, secretVolume := range volume.Secret.Items { | ||
if secretVolume.Path == "/var/run/secrets/kubernetes.io/serviceaccount" { | ||
removeVolume(i, in.Volumes) |
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.
The reason this isn't working is because your removeVolume
func returns a new slice, so you need to assign that new slice to in.Volumes
.
kubernetes/structures_pod.go
Outdated
// we need to remove it prior to recording the state. | ||
if in.AutomountServiceAccountToken != nil && *in.AutomountServiceAccountToken == true { | ||
for i, volume := range in.Volumes { | ||
nameMatchesDefaultToken, err := regexp.MatchString("default-token-([a-z0-9]{5})", volume.Name) |
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.
What I would do is put this logic here in the loop that expands the volumes and use a continue
statement to skip it, rather than removing it here.
edit: we'll have to change that code to use append
rather than creating a fixed size array
Hmm, well this does look neater. And it successfully skips adding both the volume and volumeMount to state... well, almost. It adds them as empty blocks if I put the logic into However, if I remove these at the PodSpec level, it omits the blocks entirely, which prevents the diff. I'm going to explore that route next. |
Got it working! Pods are no longer destroyed on each apply.
|
kubernetes/structures_pod.go
Outdated
@@ -107,6 +108,17 @@ func flattenPodSpec(in v1.PodSpec) ([]interface{}, error) { | |||
} | |||
|
|||
if len(in.Volumes) > 0 { | |||
for i, volume := range in.Volumes { | |||
// To avoid perpetual diff, remove the default service account token volume from PodSpec. | |||
nameMatchesDefaultToken, err := regexp.MatchString("default-token-([a-z0-9]{5})", volume.Name) |
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.
Ah, I just realized we are going to have to be a bit more clever than this.
When the token get's auto-mounted it takes the name from the service account, so only if they don't supply a service account will it be called default
, otherwise it will be the name of the service account. You can see how it does this in the admission controller code for it: kubernetes/blob/master/plugin/pkg/admission/serviceaccount/admission.go#L46
So we'll have to actually pass down the serviceAccount name and make the regex match that. 😢
We could just match on -token-
but that creates a surprising edge case for someone to discover.
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.
oh, nice! That is really helpful. I hadn't thought to test with a service account specified. I'll put in a test to catch these cases and make it clever enough to handle them. Thanks!
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.
Looks good just a couple of nits.
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 going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 [email protected]. Thanks! |
Fixes #1085
Acceptance tests
Output from acceptance testing:
Release Note
Release note for CHANGELOG:
References
Community Note