Merge pull request #3983 from zishen/transform-zone
transform zone to zones
This commit is contained in:
commit
edcb431828
|
@ -18,11 +18,20 @@ const (
|
||||||
|
|
||||||
// MutateCluster mutates required fields of the Cluster.
|
// MutateCluster mutates required fields of the Cluster.
|
||||||
func MutateCluster(cluster *clusterapis.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.
|
// migrateZoneToZones add zones field for cluster if Zones not set but Zone set only.
|
||||||
func MutateClusterTaints(taints []corev1.Taint) {
|
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 {
|
for i := range taints {
|
||||||
if taints[i].Effect == corev1.TaintEffectNoExecute && taints[i].TimeAdded == nil {
|
if taints[i].Effect == corev1.TaintEffectNoExecute && taints[i].TimeAdded == nil {
|
||||||
now := metav1.Now()
|
now := metav1.Now()
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package mutation
|
package mutation
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -18,9 +19,10 @@ func TestMutateCluster(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
args args
|
args args
|
||||||
|
fun func(args) error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "test mutate cluster",
|
name: "test mutate cluster Taints",
|
||||||
args: args{
|
args: args{
|
||||||
cluster: &clusterapis.Cluster{
|
cluster: &clusterapis.Cluster{
|
||||||
Spec: clusterapis.ClusterSpec{
|
Spec: clusterapis.ClusterSpec{
|
||||||
|
@ -33,20 +35,39 @@ func TestMutateCluster(t *testing.T) {
|
||||||
{
|
{
|
||||||
Key: "bar",
|
Key: "bar",
|
||||||
Effect: corev1.TaintEffectNoExecute,
|
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 {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
MutateCluster(tt.args.cluster)
|
MutateCluster(tt.args.cluster)
|
||||||
for i := range tt.args.cluster.Spec.Taints {
|
if err := tt.fun(tt.args); err != nil {
|
||||||
if tt.args.cluster.Spec.Taints[i].Effect == corev1.TaintEffectNoExecute && tt.args.cluster.Spec.Taints[i].TimeAdded == nil {
|
t.Error(err)
|
||||||
t.Error("failed to mutate cluster, taints TimeAdded should not be nil")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue