Skip to content
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

Merged
merged 7 commits into from
Dec 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion kubernetes/structures_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/util/intstr"
"regexp"
)

func flattenCapability(in []v1.Capability) []string {
Expand Down Expand Up @@ -259,6 +260,7 @@ func flattenValueFrom(in *v1.EnvVarSource) []interface{} {

func flattenContainerVolumeMounts(in []v1.VolumeMount) ([]interface{}, error) {
att := make([]interface{}, len(in))

for i, v := range in {
m := map[string]interface{}{}
m["read_only"] = v.ReadOnly
Expand Down Expand Up @@ -351,7 +353,7 @@ func flattenContainerResourceRequirements(in v1.ResourceRequirements) ([]interfa
return []interface{}{att}, nil
}

func flattenContainers(in []v1.Container) ([]interface{}, error) {
func flattenContainers(in []v1.Container, serviceAccountRegex string) ([]interface{}, error) {
att := make([]interface{}, len(in))
for i, v := range in {
c := make(map[string]interface{})
Expand Down Expand Up @@ -404,6 +406,18 @@ func flattenContainers(in []v1.Container) ([]interface{}, error) {
}

if len(v.VolumeMounts) > 0 {
for num, m := range v.VolumeMounts {
// To avoid perpetual diff, remove the default service account token volume from the container's list of volumeMounts.
nameMatchesDefaultToken, err := regexp.MatchString(serviceAccountRegex, m.Name)
if err != nil {
return att, err
}
if nameMatchesDefaultToken {
v.VolumeMounts = removeVolumeMountFromContainer(num, v.VolumeMounts)
dak1n1 marked this conversation as resolved.
Show resolved Hide resolved
break
}
}

volumeMounts, err := flattenContainerVolumeMounts(v.VolumeMounts)
if err != nil {
return nil, err
Expand All @@ -415,6 +429,11 @@ func flattenContainers(in []v1.Container) ([]interface{}, error) {
return att, nil
}

// removeVolumeMountFromContainer removes the specified VolumeMount index (i) from the given list of VolumeMounts.
func removeVolumeMountFromContainer(i int, v []v1.VolumeMount) []v1.VolumeMount {
return append(v[:i], v[i+1:]...)
}

func expandContainers(ctrs []interface{}) ([]v1.Container, error) {
if len(ctrs) == 0 {
return []v1.Container{}, nil
Expand Down
30 changes: 28 additions & 2 deletions kubernetes/structures_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kubernetes
import (
"fmt"
"log"
"regexp"
"strconv"
"strings"

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

containers, err := flattenContainers(in.Containers)
// To avoid perpetual diff, remove the service account token volume from PodSpec.
serviceAccountName := "default"
if in.ServiceAccountName != "" {
serviceAccountName = in.ServiceAccountName
}
serviceAccountRegex := fmt.Sprintf("%s-token-([a-z0-9]{5})", serviceAccountName)

containers, err := flattenContainers(in.Containers, serviceAccountRegex)
if err != nil {
return nil, err
}
Expand All @@ -39,7 +47,7 @@ func flattenPodSpec(in v1.PodSpec) ([]interface{}, error) {
}
att["readiness_gate"] = gates

initContainers, err := flattenContainers(in.InitContainers)
initContainers, err := flattenContainers(in.InitContainers, serviceAccountRegex)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -87,6 +95,7 @@ func flattenPodSpec(in v1.PodSpec) ([]interface{}, error) {
if in.SecurityContext != nil {
att["security_context"] = flattenPodSecurityContext(in.SecurityContext)
}

if in.ServiceAccountName != "" {
att["service_account_name"] = in.ServiceAccountName
}
Expand All @@ -107,6 +116,18 @@ func flattenPodSpec(in v1.PodSpec) ([]interface{}, error) {
}

if len(in.Volumes) > 0 {
for i, volume := range in.Volumes {
// To avoid perpetual diff, remove the service account token volume from PodSpec.
nameMatchesDefaultToken, err := regexp.MatchString(serviceAccountRegex, volume.Name)
if err != nil {
return []interface{}{att}, err
}
if nameMatchesDefaultToken {
in.Volumes = removeVolumeFromPodSpec(i, in.Volumes)
dak1n1 marked this conversation as resolved.
Show resolved Hide resolved
break
}
}

v, err := flattenVolumes(in.Volumes)
if err != nil {
return []interface{}{att}, err
Expand All @@ -116,6 +137,11 @@ func flattenPodSpec(in v1.PodSpec) ([]interface{}, error) {
return []interface{}{att}, nil
}

// removeVolumeFromPodSpec removes the specified Volume index (i) from the given list of Volumes.
func removeVolumeFromPodSpec(i int, v []v1.Volume) []v1.Volume {
return append(v[:i], v[i+1:]...)
}

func flattenPodDNSConfig(in *v1.PodDNSConfig) ([]interface{}, error) {
att := make(map[string]interface{})

Expand Down