Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release-4.17] Bug 2308144: Correct ConfigMap creation logic and update unit tests #233

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
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
Loading