Skip to content

Commit

Permalink
Fix the configmap function aggregating logic
Browse files Browse the repository at this point in the history
Signed-off-by: vbadrina <[email protected]>
  • Loading branch information
vbnrh committed Jul 18, 2024
1 parent c49600c commit b9dbed4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 48 deletions.
4 changes: 3 additions & 1 deletion controllers/managedcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/red-hat-storage/odf-multicluster-orchestrator/controllers/utils"
viewv1beta1 "github.com/stolostron/multicloud-operators-foundation/pkg/apis/view/v1beta1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
clusterv1 "open-cluster-management.io/api/cluster/v1"
Expand Down Expand Up @@ -45,7 +46,7 @@ func (r *ManagedClusterReconciler) Reconcile(ctx context.Context, req reconcile.
return ctrl.Result{}, err
}

logger.Info("Successfully reconciled ManagedCluster", "name", managedCluster.Name)
logger.Info("Successfully reconciled ManagedCluster")

return ctrl.Result{}, nil
}
Expand Down Expand Up @@ -82,6 +83,7 @@ func (r *ManagedClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&clusterv1.ManagedCluster{}, builder.WithPredicates(managedClusterPredicate, predicate.GenerationChangedPredicate{})).
Owns(&viewv1beta1.ManagedClusterView{}).
Owns(&corev1.ConfigMap{}).
Complete(r)
}

Expand Down
46 changes: 19 additions & 27 deletions controllers/managedclusterview_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/red-hat-storage/odf-multicluster-orchestrator/controllers/utils"
viewv1beta1 "github.com/stolostron/multicloud-operators-foundation/pkg/apis/view/v1beta1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -72,7 +73,6 @@ func (r *ManagedClusterViewReconciler) SetupWithManager(mgr ctrl.Manager) error

return ctrl.NewControllerManagedBy(mgr).
For(&viewv1beta1.ManagedClusterView{}, builder.WithPredicates(managedClusterViewPredicate, predicate.ResourceVersionChangedPredicate{}, predicate.GenerationChangedPredicate{})).
Owns(&corev1.ConfigMap{}).
Complete(r)
}

Expand Down Expand Up @@ -100,7 +100,7 @@ func (r *ManagedClusterViewReconciler) Reconcile(ctx context.Context, req reconc
return ctrl.Result{}, err
}

logger.Info("Successfully reconciled ManagedClusterView", "name", managedClusterView.Name)
logger.Info("Successfully reconciled ManagedClusterView")

return ctrl.Result{}, nil
}
Expand All @@ -111,7 +111,7 @@ func createOrUpdateConfigMap(ctx context.Context, c client.Client, managedCluste
var resultData map[string]string
err := json.Unmarshal(managedClusterView.Status.Result.Raw, &resultData)
if err != nil {
return fmt.Errorf("failed to unmarshal result data: %v", err)
return fmt.Errorf("failed to unmarshal result data. %w", err)
}

clientInfoMap := make(map[string]ClientInfo)
Expand All @@ -120,7 +120,7 @@ func createOrUpdateConfigMap(ctx context.Context, c client.Client, managedCluste
var odfInfo ocsv1alpha1.OdfInfoData
err := yaml.Unmarshal([]byte(value), &odfInfo)
if err != nil {
return fmt.Errorf("failed to unmarshal ODF info data: %v", err)
return fmt.Errorf("failed to unmarshal ODF info data. %w", err)
}

providerInfo := ProviderInfo{
Expand Down Expand Up @@ -148,45 +148,37 @@ func createOrUpdateConfigMap(ctx context.Context, c client.Client, managedCluste
}
}

configMapData := make(map[string]string)
for clientName, clientInfo := range clientInfoMap {
clientInfoJSON, err := json.Marshal(clientInfo)
if err != nil {
return fmt.Errorf("failed to marshal client info: %v", err)
}
configMapData[clientName] = string(clientInfoJSON)
}

operatorNamespace := os.Getenv("POD_NAMESPACE")
configMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: ClientInfoConfigMapName,
Namespace: operatorNamespace,
},
}
err = c.Get(ctx, types.NamespacedName{Name: ClientInfoConfigMapName, Namespace: operatorNamespace}, configMap)
if err != nil && !errors.IsNotFound(err) {
return fmt.Errorf("failed to get ConfigMap. %w", err)
}

op, err := controllerutil.CreateOrUpdate(ctx, c, configMap, func() error {
configMap.Data = configMapData
if configMap.Data == nil {
configMap.Data = make(map[string]string)
}

ownerExists := false
for _, ownerRef := range configMap.OwnerReferences {
if ownerRef.UID == managedClusterView.UID {
ownerExists = true
break
op, err := controllerutil.CreateOrUpdate(ctx, c, configMap, func() error {
for clientKey, clientInfo := range clientInfoMap {
clientInfoJSON, err := json.Marshal(clientInfo)
if err != nil {
return fmt.Errorf("failed to marshal client info. %w", err)
}
configMap.Data[clientKey] = string(clientInfoJSON)
}

if !ownerExists {
ownerRef := *metav1.NewControllerRef(&managedClusterView, viewv1beta1.GroupVersion.WithKind("ManagedClusterView"))
logger.Info("OwnerRef added", "UID", string(ownerRef.UID))
configMap.OwnerReferences = append(configMap.OwnerReferences, ownerRef)
}

configMap.OwnerReferences = append(configMap.OwnerReferences, managedClusterView.GetOwnerReferences()...)
return nil
})

if err != nil {
return fmt.Errorf("failed to create or update ConfigMap: %v", err)
return fmt.Errorf("failed to create or update ConfigMap. %w", err)
}

logger.Info(fmt.Sprintf("ConfigMap %s in namespace %s has been %s", ClientInfoConfigMapName, operatorNamespace, op))
Expand Down
43 changes: 23 additions & 20 deletions controllers/managedclusterview_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ func TestCreateOrUpdateConfigMap(t *testing.T) {
os.Setenv("POD_NAMESPACE", "openshift-operators")
logger := utils.GetLogger(utils.GetZapLogger(true))

createManagedClusterView := func(name, namespace string, data map[string]string) *viewv1beta1.ManagedClusterView {
createManagedClusterView := func(name, namespace string, data map[string]string, ownerRefs []metav1.OwnerReference) *viewv1beta1.ManagedClusterView {
raw, _ := json.Marshal(data)
return &viewv1beta1.ManagedClusterView{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
UID: types.UID(uuid.New().String()),
Name: name,
Namespace: namespace,
UID: types.UID(uuid.New().String()),
OwnerReferences: ownerRefs,
},
Status: viewv1beta1.ViewStatus{
Result: runtime.RawExtension{Raw: raw},
Expand All @@ -58,7 +59,7 @@ func TestCreateOrUpdateConfigMap(t *testing.T) {
}
}

t.Run("Create ConfigMap", func(t *testing.T) {
t.Run("Create ConfigMap with MCV in cluster1", func(t *testing.T) {
mc1 := createManagedCluster("cluster1-name", "cluster1")
mc2 := createManagedCluster("cluster2-name", "cluster2")
err := c.Create(context.TODO(), mc1)
Expand All @@ -73,8 +74,6 @@ func TestCreateOrUpdateConfigMap(t *testing.T) {
clients:
- name: "client1"
clusterId: "cluster1"
- name: "client2"
clusterId: "cluster2"
storageCluster:
namespacedName:
name: "ocs-storagecluster"
Expand All @@ -84,7 +83,10 @@ func TestCreateOrUpdateConfigMap(t *testing.T) {
storageSystemName: "ocs-storagecluster-storagesystem"
`,
}
mcv := createManagedClusterView("test-view", "local-cluster", data)
ownerRefs := []metav1.OwnerReference{
*metav1.NewControllerRef(mc1, clusterv1.SchemeGroupVersion.WithKind("ManagedCluster")),
}
mcv := createManagedClusterView("test-view", "cluster1", data, ownerRefs)

ctx := context.TODO()
err = c.Create(ctx, mcv)
Expand All @@ -99,39 +101,40 @@ func TestCreateOrUpdateConfigMap(t *testing.T) {
assert.NotNil(t, cm)

expectedData := map[string]string{
"cluster1-name/client1": `{"clusterId":"cluster1","name":"client1","providerInfo":{"version":"4.Y.Z","deploymentType":"internal","storageSystemName":"ocs-storagecluster-storagesystem","providerManagedClusterName":"local-cluster","namespacedName":{"Namespace":"openshift-storage","Name":"ocs-storagecluster"},"storageProviderEndpoint":"","cephClusterFSID":"7a3d6b81-a55d-44fe-84d0-46c67cd395ca"},"clientManagedClusterName":"cluster1-name"}`,
"cluster2-name/client2": `{"clusterId":"cluster2","name":"client2","providerInfo":{"version":"4.Y.Z","deploymentType":"internal","storageSystemName":"ocs-storagecluster-storagesystem","providerManagedClusterName":"local-cluster","namespacedName":{"Namespace":"openshift-storage","Name":"ocs-storagecluster"},"storageProviderEndpoint":"","cephClusterFSID":"7a3d6b81-a55d-44fe-84d0-46c67cd395ca"},"clientManagedClusterName":"cluster2-name"}`,
"cluster1-name/client1": `{"clusterId":"cluster1","name":"client1","providerInfo":{"version":"4.Y.Z","deploymentType":"internal","storageSystemName":"ocs-storagecluster-storagesystem","providerManagedClusterName":"cluster1","namespacedName":{"Namespace":"openshift-storage","Name":"ocs-storagecluster"},"storageProviderEndpoint":"","cephClusterFSID":"7a3d6b81-a55d-44fe-84d0-46c67cd395ca"},"clientManagedClusterName":"cluster1-name"}`,
}

assert.Equal(t, expectedData, cm.Data)
assert.Equal(t, 1, len(cm.OwnerReferences))
assert.Equal(t, mcv.Name, cm.OwnerReferences[0].Name)
assert.Equal(t, "ManagedClusterView", cm.OwnerReferences[0].Kind)
assert.Equal(t, viewv1beta1.GroupVersion.String(), cm.OwnerReferences[0].APIVersion)
assert.Equal(t, mc1.Name, cm.OwnerReferences[0].Name)
assert.Equal(t, "ManagedCluster", cm.OwnerReferences[0].Kind)
assert.Equal(t, clusterv1.GroupVersion.String(), cm.OwnerReferences[0].APIVersion)

})

t.Run("Update ConfigMap with additional owner reference", func(t *testing.T) {
t.Run("Update ConfigMap with MCV in cluster2", func(t *testing.T) {
mc2 := createManagedCluster("cluster2-name", "cluster2")
ctx := context.TODO()
data := map[string]string{
"openshift-storage_ocs-storagecluster.config.yaml": `
version: "4.Y.Z"
deploymentType: "internal"
clients:
- name: "client1"
clusterId: "cluster1"
- name: "client2"
clusterId: "cluster2"
storageCluster:
namespacedName:
name: "ocs-storagecluster"
namespace: "openshift-storage"
storageProviderEndpoint: ""
cephClusterFSID: "7a3d6b81-a55d-44fe-84d0-46c67cd395ca"
cephClusterFSID: "8b3d6b81-b55d-55fe-94d0-56c67cd495ca"
storageSystemName: "ocs-storagecluster-storagesystem"
`,
}
mcv := createManagedClusterView("new-view", "local-cluster", data)
ownerRefs := []metav1.OwnerReference{
*metav1.NewControllerRef(mc2, clusterv1.SchemeGroupVersion.WithKind("ManagedCluster")),
}
mcv := createManagedClusterView("new-view", "cluster2", data, ownerRefs)

err := c.Create(ctx, mcv)
assert.NoError(t, err)
Expand All @@ -145,8 +148,8 @@ func TestCreateOrUpdateConfigMap(t *testing.T) {
assert.NotNil(t, cm)

expectedData := map[string]string{
"cluster1-name/client1": `{"clusterId":"cluster1","name":"client1","providerInfo":{"version":"4.Y.Z","deploymentType":"internal","storageSystemName":"ocs-storagecluster-storagesystem","providerManagedClusterName":"local-cluster","namespacedName":{"Namespace":"openshift-storage","Name":"ocs-storagecluster"},"storageProviderEndpoint":"","cephClusterFSID":"7a3d6b81-a55d-44fe-84d0-46c67cd395ca"},"clientManagedClusterName":"cluster1-name"}`,
"cluster2-name/client2": `{"clusterId":"cluster2","name":"client2","providerInfo":{"version":"4.Y.Z","deploymentType":"internal","storageSystemName":"ocs-storagecluster-storagesystem","providerManagedClusterName":"local-cluster","namespacedName":{"Namespace":"openshift-storage","Name":"ocs-storagecluster"},"storageProviderEndpoint":"","cephClusterFSID":"7a3d6b81-a55d-44fe-84d0-46c67cd395ca"},"clientManagedClusterName":"cluster2-name"}`,
"cluster1-name/client1": `{"clusterId":"cluster1","name":"client1","providerInfo":{"version":"4.Y.Z","deploymentType":"internal","storageSystemName":"ocs-storagecluster-storagesystem","providerManagedClusterName":"cluster1","namespacedName":{"Namespace":"openshift-storage","Name":"ocs-storagecluster"},"storageProviderEndpoint":"","cephClusterFSID":"7a3d6b81-a55d-44fe-84d0-46c67cd395ca"},"clientManagedClusterName":"cluster1-name"}`,
"cluster2-name/client2": `{"clusterId":"cluster2","name":"client2","providerInfo":{"version":"4.Y.Z","deploymentType":"internal","storageSystemName":"ocs-storagecluster-storagesystem","providerManagedClusterName":"cluster2","namespacedName":{"Namespace":"openshift-storage","Name":"ocs-storagecluster"},"storageProviderEndpoint":"","cephClusterFSID":"8b3d6b81-b55d-55fe-94d0-56c67cd495ca"},"clientManagedClusterName":"cluster2-name"}`,
}

assert.Equal(t, expectedData, cm.Data)
Expand Down

0 comments on commit b9dbed4

Please sign in to comment.