Skip to content

Commit

Permalink
Fix: Correct ConfigMap creation logic and update unit tests
Browse files Browse the repository at this point in the history
- Updated the `createOrUpdateConfigMap` function to handle nested data structures correctly, ensuring that YAML content is properly extracted and parsed.
- Adjusted the logic to filter for YAML keys in the ManagedClusterView data and marshal the parsed data into JSON for storage in the ConfigMap.
- Ensured that the `Controller` field in the OwnerReferences is set to `false` where necessary to prevent conflicts.
- Modified unit tests to reflect the changes in the data structure and logic, ensuring accurate validation of the ConfigMap content and OwnerReferences.
- Fixed a bug where the `Scope` check in the `hasODFInfoInScope` function was incorrectly using `Kind` instead of `Resource`.

Signed-off-by: vbadrina <[email protected]>
  • Loading branch information
vbnrh committed Aug 27, 2024
1 parent b3df88c commit 2f2303e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
42 changes: 30 additions & 12 deletions controllers/managedclusterview_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log/slog"
"os"
"strings"

ocsv1alpha1 "github.com/red-hat-storage/ocs-operator/api/v4/v1alpha1"
"github.com/red-hat-storage/odf-multicluster-orchestrator/controllers/utils"
Expand Down Expand Up @@ -77,7 +78,7 @@ func (r *ManagedClusterViewReconciler) SetupWithManager(mgr ctrl.Manager) error
}

func hasODFInfoInScope(mc *viewv1beta1.ManagedClusterView) bool {
if mc.Spec.Scope.Name == ODFInfoConfigMapName && mc.Spec.Scope.Kind == ConfigMapResourceType {
if mc.Spec.Scope.Name == ODFInfoConfigMapName && mc.Spec.Scope.Resource == ConfigMapResourceType {
return true
}
return false
Expand Down Expand Up @@ -108,19 +109,33 @@ func (r *ManagedClusterViewReconciler) Reconcile(ctx context.Context, req reconc
func createOrUpdateConfigMap(ctx context.Context, c client.Client, managedClusterView viewv1beta1.ManagedClusterView, logger *slog.Logger) error {
logger = logger.With("ManagedClusterView", managedClusterView.Name, "Namespace", managedClusterView.Namespace)

var resultData map[string]string
// Initialize an empty map to hold the result data.
var resultData map[string]interface{}
err := json.Unmarshal(managedClusterView.Status.Result.Raw, &resultData)
if err != nil {
return fmt.Errorf("failed to unmarshal result data. %w", err)
}

clientInfoMap := make(map[string]ClientInfo)
data, ok := resultData["data"].(map[string]interface{})
if !ok {
return fmt.Errorf("unexpected data format in result: %v", resultData["data"])
}

clientInfoMap := make(map[string]string)

for key, value := range data {
if !strings.Contains(key, ".yaml") {
continue
}

for _, value := range resultData {
yamlContent, ok := value.(string)
if !ok {
return fmt.Errorf("unexpected value format in data for key %s: expected string, got %T", key, value)
}
var odfInfo ocsv1alpha1.OdfInfoData
err := yaml.Unmarshal([]byte(value), &odfInfo)
err := yaml.Unmarshal([]byte(yamlContent), &odfInfo)
if err != nil {
return fmt.Errorf("failed to unmarshal ODF info data. %w", err)
return fmt.Errorf("failed to unmarshal ODF info data for key %s: %w", key, err)
}

providerInfo := ProviderInfo{
Expand All @@ -144,7 +159,12 @@ func createOrUpdateConfigMap(ctx context.Context, c client.Client, managedCluste
ProviderInfo: providerInfo,
ClientManagedClusterName: managedCluster.Name,
}
clientInfoMap[fmt.Sprintf("%s/%s", managedCluster.Name, client.Name)] = clientInfo
clientInfoJSON, err := json.Marshal(clientInfo)
if err != nil {
return fmt.Errorf("failed to marshal client info for key %s: %w", key, err)
}

clientInfoMap[fmt.Sprintf("%s/%s", managedCluster.Name, client.Name)] = string(clientInfoJSON)
}
}

Expand All @@ -166,11 +186,7 @@ func createOrUpdateConfigMap(ctx context.Context, c client.Client, managedCluste

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)
configMap.Data[clientKey] = clientInfo
}

mcvOwnerRefs := managedClusterView.GetOwnerReferences()
Expand All @@ -183,6 +199,8 @@ func createOrUpdateConfigMap(ctx context.Context, c client.Client, managedCluste
}
}
if !exists {
falseValue := false
mcvOwnerRef.Controller = &falseValue
configMap.OwnerReferences = append(configMap.OwnerReferences, mcvOwnerRef)
}
}
Expand Down
4 changes: 3 additions & 1 deletion controllers/managedclusterview_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ func TestCreateOrUpdateConfigMap(t *testing.T) {
logger := utils.GetLogger(utils.GetZapLogger(true))

createManagedClusterView := func(name, namespace string, data map[string]string, ownerRefs []metav1.OwnerReference) *viewv1beta1.ManagedClusterView {
raw, _ := json.Marshal(data)
raw, _ := json.Marshal(map[string]interface{}{
"data": data,
})
return &viewv1beta1.ManagedClusterView{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand Down

0 comments on commit 2f2303e

Please sign in to comment.