Skip to content
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
3 changes: 2 additions & 1 deletion pkg/operator/kube_cloud_config/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func (c KubeCloudConfigController) sync(ctx context.Context, syncCtx factory.Syn
}

sourceCloudConfigMap := currentInfra.Spec.CloudConfig.Name
sourceCloudConfigKey := currentInfra.Spec.CloudConfig.Key

source := &corev1.ConfigMap{}
if len(sourceCloudConfigMap) > 0 {
Expand All @@ -106,7 +107,7 @@ func (c KubeCloudConfigController) sync(ctx context.Context, syncCtx factory.Syn
cloudConfigTransformerFn = asIsTransformer
}

target, err := cloudConfigTransformerFn(source, targetConfigKey, currentInfra)
target, err := cloudConfigTransformerFn(source, sourceCloudConfigKey, currentInfra)
if err != nil {
return err
}
Expand Down
157 changes: 157 additions & 0 deletions pkg/operator/kube_cloud_config/controller_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package kube_cloud_config

import (
"context"
"testing"

configv1 "github.com/openshift/api/config/v1"
configfakeclient "github.com/openshift/client-go/config/clientset/versioned/fake"
configv1listers "github.com/openshift/client-go/config/listers/config/v1"
"github.com/openshift/cluster-config-operator/pkg/operator/operatorclient"
"github.com/openshift/library-go/pkg/controller/factory"
"github.com/openshift/library-go/pkg/operator/events"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes/fake"
ktesting "k8s.io/client-go/testing"
"k8s.io/client-go/tools/cache"
)

func Test_asIsTransformer(t *testing.T) {
Expand Down Expand Up @@ -77,3 +87,150 @@ func Test_asIsTransformer(t *testing.T) {
})
}
}

func Test_sync(t *testing.T) {
cases := []struct {
inputinfra *configv1.Infrastructure
inputdata string

outputdata map[string]string
err string
actions []ktesting.Action
}{{
inputinfra: &configv1.Infrastructure{Status: configv1.InfrastructureStatus{PlatformStatus: &configv1.PlatformStatus{Type: configv1.AzurePlatformType}}},
inputdata: `{"somekey": "somevalue"}`,

outputdata: map[string]string{"cloud.conf": `{"somekey": "somevalue"}`},
actions: []ktesting.Action{
ktesting.NewGetAction(schema.GroupVersionResource{Resource: "configmaps"}, "openshift-config", "cluster-config-v1"),
ktesting.NewGetAction(schema.GroupVersionResource{Resource: "configmaps"}, "openshift-config-managed", "kube-cloud-config"),
ktesting.NewUpdateAction(schema.GroupVersionResource{Resource: "configmaps"}, "openshift-config-managed", nil),
},
}, {
inputinfra: &configv1.Infrastructure{Status: configv1.InfrastructureStatus{PlatformStatus: &configv1.PlatformStatus{Type: configv1.GCPPlatformType}}},
inputdata: `[global]
somekey = somevalue`,

outputdata: map[string]string{"cloud.conf": `[global]
somekey = somevalue`},
actions: []ktesting.Action{
ktesting.NewGetAction(schema.GroupVersionResource{Resource: "configmaps"}, "openshift-config", "cluster-config-v1"),
ktesting.NewGetAction(schema.GroupVersionResource{Resource: "configmaps"}, "openshift-config-managed", "kube-cloud-config"),
ktesting.NewUpdateAction(schema.GroupVersionResource{Resource: "configmaps"}, "openshift-config-managed", nil),
},
}, {
inputinfra: &configv1.Infrastructure{Status: configv1.InfrastructureStatus{PlatformStatus: &configv1.PlatformStatus{Type: configv1.NonePlatformType}}},

actions: []ktesting.Action{
ktesting.NewDeleteAction(schema.GroupVersionResource{Resource: "configmaps"}, "openshift-config-managed", "kube-cloud-config"),
},
}, {
inputinfra: &configv1.Infrastructure{Status: configv1.InfrastructureStatus{PlatformStatus: &configv1.PlatformStatus{Type: configv1.AWSPlatformType, AWS: &configv1.AWSPlatformStatus{Region: "test-region"}}}},

actions: []ktesting.Action{
ktesting.NewDeleteAction(schema.GroupVersionResource{Resource: "configmaps"}, "openshift-config-managed", "kube-cloud-config"),
},
}, {
inputinfra: &configv1.Infrastructure{Status: configv1.InfrastructureStatus{PlatformStatus: &configv1.PlatformStatus{Type: configv1.AWSPlatformType, AWS: &configv1.AWSPlatformStatus{Region: "test-region", ServiceEndpoints: []configv1.AWSServiceEndpoint{{Name: "ec2", URL: "https://ec2.local"}}}}}},

outputdata: map[string]string{"cloud.conf": `
[ServiceOverride "0"]
Service = ec2
Region = test-region
URL = https://ec2.local
SigningRegion = test-region
`},
actions: []ktesting.Action{
ktesting.NewGetAction(schema.GroupVersionResource{Resource: "configmaps"}, "openshift-config-managed", "kube-cloud-config"),
ktesting.NewUpdateAction(schema.GroupVersionResource{Resource: "configmaps"}, "openshift-config-managed", nil),
},
}, {
inputinfra: &configv1.Infrastructure{Status: configv1.InfrastructureStatus{PlatformStatus: &configv1.PlatformStatus{Type: configv1.AWSPlatformType, AWS: &configv1.AWSPlatformStatus{Region: "test-region"}}}},
inputdata: `[Global]
VPC = vpc-test
SubnetID = subnet-test`,

outputdata: map[string]string{"cloud.conf": `[Global]
VPC = vpc-test
SubnetID = subnet-test`},
actions: []ktesting.Action{
ktesting.NewGetAction(schema.GroupVersionResource{Resource: "configmaps"}, "openshift-config", "cluster-config-v1"),
ktesting.NewGetAction(schema.GroupVersionResource{Resource: "configmaps"}, "openshift-config-managed", "kube-cloud-config"),
ktesting.NewUpdateAction(schema.GroupVersionResource{Resource: "configmaps"}, "openshift-config-managed", nil),
},
}, {
inputinfra: &configv1.Infrastructure{Status: configv1.InfrastructureStatus{PlatformStatus: &configv1.PlatformStatus{Type: configv1.AWSPlatformType, AWS: &configv1.AWSPlatformStatus{Region: "test-region", ServiceEndpoints: []configv1.AWSServiceEndpoint{{Name: "ec2", URL: "https://ec2.local"}}}}}},
inputdata: `[Global]
VPC = vpc-test
SubnetID = subnet-test`,

outputdata: map[string]string{"cloud.conf": `[Global]
VPC = vpc-test
SubnetID = subnet-test
[ServiceOverride "0"]
Service = ec2
Region = test-region
URL = https://ec2.local
SigningRegion = test-region
`},
actions: []ktesting.Action{
ktesting.NewGetAction(schema.GroupVersionResource{Resource: "configmaps"}, "openshift-config", "cluster-config-v1"),
ktesting.NewGetAction(schema.GroupVersionResource{Resource: "configmaps"}, "openshift-config-managed", "kube-cloud-config"),
ktesting.NewUpdateAction(schema.GroupVersionResource{Resource: "configmaps"}, "openshift-config-managed", nil),
},
}}
for _, test := range cases {
t.Run("", func(t *testing.T) {
fake := fake.NewSimpleClientset()
if len(test.inputdata) > 0 {
fake.Tracker().Add(&corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: "cluster-config-v1", Namespace: "openshift-config"}, Data: map[string]string{"config": test.inputdata}})
test.inputinfra.Spec.CloudConfig = configv1.ConfigMapFileReference{Name: "cluster-config-v1", Key: "config"}
}
fake.Tracker().Add(&corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: "kube-cloud-config", Namespace: "openshift-config-managed"}})

test.inputinfra.ObjectMeta = metav1.ObjectMeta{Name: "cluster"}
indexerInfra := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{})
if err := indexerInfra.Add(test.inputinfra); err != nil {
t.Fatal(err.Error())
}
fakeConfig := configfakeclient.NewSimpleClientset(test.inputinfra)

ctrl := KubeCloudConfigController{
infraClient: fakeConfig.ConfigV1().Infrastructures(),
infraLister: configv1listers.NewInfrastructureLister(indexerInfra),
configMapClient: fake.CoreV1(),
cloudConfigTransformers: map[configv1.PlatformType]cloudConfigTransformer{
configv1.AWSPlatformType: awsTransformer,
},
}

err := ctrl.sync(context.TODO(),
factory.NewSyncContext("KubeCloudConfigController", events.NewInMemoryRecorder("KubeCloudConfigController")))
if test.err == "" {
assert.NoError(t, err)
assert.Equal(t, len(test.actions), len(fake.Actions()))

for idx, a := range fake.Actions() {
actionGot := a.(ktesting.Action)
actionExp := test.actions[idx].(ktesting.Action)
assert.Equalf(t, actionExp.GetVerb(), actionGot.GetVerb(), "mismatch verb at action %d", idx)
assert.Equalf(t, actionExp.GetNamespace(), actionGot.GetNamespace(), "mismatch namespace at action %d", idx)
assert.Equalf(t, actionExp.GetResource().Resource, actionGot.GetResource().Resource, "mismatch resource at action %d", idx)

switch obj := a.(type) {
case ktesting.GetAction:
getExp := actionExp.(ktesting.GetAction)
assert.Equalf(t, getExp.GetName(), obj.GetName(), "mismatch name at action %d", idx)
case ktesting.UpdateAction:
assert.Equalf(t, test.outputdata, obj.GetObject().(*corev1.ConfigMap).Data, "mismatch Update data at action %d", idx)
case ktesting.DeleteAction:
deleteExp := actionExp.(ktesting.DeleteAction)
assert.Equalf(t, deleteExp.GetName(), obj.GetName(), "mismatch name at action %d", idx)
}
}
} else {
assert.Regexp(t, test.err, err.Error())
}
})
}
}
Loading