Skip to content

Commit

Permalink
fix vcluster config convert overwritting statefulSet affinity; do not…
Browse files Browse the repository at this point in the history
… prune ephemeral-storage null values

Signed-off-by: Paweł Bojanowski <[email protected]>
  • Loading branch information
hidalgopl committed Dec 13, 2024
1 parent 971a3f0 commit 0ebd42a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
11 changes: 10 additions & 1 deletion config/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ import (
// ErrUnsupportedType is returned if the type is not implemented
var ErrUnsupportedType = errors.New("unsupported type")

// includeNilValuesForKeys prevents removing certain keys with null values.
// This is useful when default value for a given field is not nil, user set this field for null in oldConfig,
// and we want to keep this field as null in the newConfig.
var includeNilValuesForKeys = map[string]struct{}{
"ephemeral-storage": {},
"requests.ephemeral-storage": {},
}

func Diff(fromConfig *Config, toConfig *Config) (string, error) {
// convert to map[string]interface{}
fromRaw := map[string]interface{}{}
Expand Down Expand Up @@ -112,7 +120,8 @@ func prune(in interface{}) interface{} {

for k, v := range inType {
inType[k] = prune(v)
if inType[k] == nil {
_, ok := includeNilValuesForKeys[k]
if inType[k] == nil && !ok {
delete(inType, k)
}
}
Expand Down
3 changes: 2 additions & 1 deletion config/legacyconfig/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,14 @@ func convertBaseValues(oldConfig BaseHelm, newConfig *config.Config) error {
}

newConfig.Networking.Advanced.FallbackHostCluster = oldConfig.FallbackHostDNS

newConfig.ControlPlane.StatefulSet.Labels = oldConfig.Labels
newConfig.ControlPlane.StatefulSet.Annotations = oldConfig.Annotations
newConfig.ControlPlane.StatefulSet.Pods.Labels = oldConfig.PodLabels
newConfig.ControlPlane.StatefulSet.Pods.Annotations = oldConfig.PodAnnotations
newConfig.ControlPlane.StatefulSet.Scheduling.Tolerations = oldConfig.Tolerations
newConfig.ControlPlane.StatefulSet.Scheduling.NodeSelector = oldConfig.NodeSelector
newConfig.ControlPlane.StatefulSet.Scheduling.Affinity = oldConfig.Affinity
newConfig.ControlPlane.StatefulSet.Scheduling.Affinity = mergeMaps(newConfig.ControlPlane.StatefulSet.Scheduling.Affinity, oldConfig.Affinity)
newConfig.ControlPlane.StatefulSet.Scheduling.PriorityClassName = oldConfig.PriorityClassName

newConfig.Networking.ReplicateServices.FromHost = oldConfig.MapServices.FromHost
Expand Down
50 changes: 26 additions & 24 deletions config/legacyconfig/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,16 +596,6 @@ api:
- -v=4
enabled: true
statefulSet:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- vcluster
topologyKey: "kubernetes.io/hostname"
highAvailability:
replicas: 3
persistence:
Expand All @@ -617,17 +607,27 @@ api:
cpu: "8"
memory: 10Gi
scheduling:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- vcluster
topologyKey: kubernetes.io/hostname
podManagementPolicy: OrderedReady
policies:
limitRange:
default:
cpu: 512m
ephemeral-storage: null
memory: 2Gi
ephemeral-storage: null
defaultRequest:
cpu: 24m
ephemeral-storage: null
memory: 32Mi
ephemeral-storage: null
enabled: true
podSecurityStandard: baseline
resourceQuota:
Expand All @@ -636,9 +636,9 @@ policies:
limits.cpu: 256
limits.memory: 1Ti
requests.cpu: 120
requests.ephemeral-storage: null
requests.memory: 128Gi
requests.storage: 10Ti
requests.ephemeral-storage: null
sync:
fromHost:
nodes:
Expand All @@ -653,18 +653,20 @@ sync:
}

for _, testCase := range testCases {
out, err := MigrateLegacyConfig(testCase.Distro, testCase.In)
if err != nil {
if testCase.ExpectedErr != "" && testCase.ExpectedErr == err.Error() {
continue
}
t.Run(testCase.Name, func(t *testing.T) {
out, err := MigrateLegacyConfig(testCase.Distro, testCase.In)
if err != nil {
if testCase.ExpectedErr != "" && testCase.ExpectedErr == err.Error() {
return
}

t.Fatalf("Test case %s failed with: %v", testCase.Name, err)
}
t.Fatalf("Test case %s failed with: %v", testCase.Name, err)
}

if strings.TrimSpace(testCase.Expected) != strings.TrimSpace(out) {
t.Log(out)
}
assert.Equal(t, strings.TrimSpace(testCase.Expected), strings.TrimSpace(out), testCase.Name)
if strings.TrimSpace(testCase.Expected) != strings.TrimSpace(out) {
t.Log(out)
}
assert.Equal(t, strings.TrimSpace(testCase.Expected), strings.TrimSpace(out), testCase.Name)
})
}
}

0 comments on commit 0ebd42a

Please sign in to comment.