Merge pull request #3983 from zishen/transform-zone

transform zone to zones
This commit is contained in:
karmada-bot 2023-08-25 10:35:11 +08:00 committed by GitHub
commit edcb431828
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 10 deletions

View File

@ -18,11 +18,20 @@ const (
// MutateCluster mutates required fields of the Cluster.
func MutateCluster(cluster *clusterapis.Cluster) {
MutateClusterTaints(cluster.Spec.Taints)
mutateClusterTaints(cluster.Spec.Taints)
migrateZoneToZones(cluster)
}
// MutateClusterTaints add TimeAdded field for cluster NoExecute taints only if TimeAdded not set.
func MutateClusterTaints(taints []corev1.Taint) {
// migrateZoneToZones add zones field for cluster if Zones not set but Zone set only.
func migrateZoneToZones(cluster *clusterapis.Cluster) {
if cluster.Spec.Zone != "" && len(cluster.Spec.Zones) == 0 {
cluster.Spec.Zones = append(cluster.Spec.Zones, cluster.Spec.Zone)
cluster.Spec.Zone = ""
}
}
// mutateClusterTaints add TimeAdded field for cluster NoExecute taints only if TimeAdded not set.
func mutateClusterTaints(taints []corev1.Taint) {
for i := range taints {
if taints[i].Effect == corev1.TaintEffectNoExecute && taints[i].TimeAdded == nil {
now := metav1.Now()

View File

@ -1,6 +1,7 @@
package mutation
import (
"fmt"
"math"
"reflect"
"testing"
@ -18,9 +19,10 @@ func TestMutateCluster(t *testing.T) {
tests := []struct {
name string
args args
fun func(args) error
}{
{
name: "test mutate cluster",
name: "test mutate cluster Taints",
args: args{
cluster: &clusterapis.Cluster{
Spec: clusterapis.ClusterSpec{
@ -33,20 +35,39 @@ func TestMutateCluster(t *testing.T) {
{
Key: "bar",
Effect: corev1.TaintEffectNoExecute,
},
},
}}}},
},
fun: func(data args) error {
for i := range data.cluster.Spec.Taints {
if data.cluster.Spec.Taints[i].Effect == corev1.TaintEffectNoExecute && data.cluster.Spec.Taints[i].TimeAdded == nil {
return fmt.Errorf("failed to mutate cluster, taints TimeAdded should not be nil")
}
}
return nil
},
},
{
name: "test mutate cluster Zone",
args: args{
cluster: &clusterapis.Cluster{
Spec: clusterapis.ClusterSpec{
Zone: "zone1",
},
},
},
fun: func(data args) error {
if data.cluster.Spec.Zone != "" && len(data.cluster.Spec.Zones) == 0 {
return fmt.Errorf("failed to mutate cluster, zones should not be nil")
}
return nil
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
MutateCluster(tt.args.cluster)
for i := range tt.args.cluster.Spec.Taints {
if tt.args.cluster.Spec.Taints[i].Effect == corev1.TaintEffectNoExecute && tt.args.cluster.Spec.Taints[i].TimeAdded == nil {
t.Error("failed to mutate cluster, taints TimeAdded should not be nil")
}
if err := tt.fun(tt.args); err != nil {
t.Error(err)
}
})
}