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

Implement reconciliation for ManagedCluster and create ManagedClusterView #219

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ metadata:
]
capabilities: Basic Install
console.openshift.io/plugins: '["odf-multicluster-console"]'
createdAt: "2024-07-12T13:14:27Z"
createdAt: "2024-07-16T05:37:17Z"
olm.skipRange: ""
operators.openshift.io/infrastructure-features: '["disconnected"]'
operators.operatorframework.io/builder: operator-sdk-v1.34.1
Expand Down
12 changes: 3 additions & 9 deletions 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 All @@ -28,7 +29,6 @@ const (
OdfInfoClusterClaimNamespacedName = "odfinfo.odf.openshift.io"
)

vbnrh marked this conversation as resolved.
Show resolved Hide resolved
// +kubebuilder:rbac:groups=view.open-cluster-management.io,resources=managedclusterviews,verbs=get;list;watch;create;update
func (r *ManagedClusterReconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
logger := r.Logger.With("ManagedCluster", req.NamespacedName)
logger.Info("Reconciling ManagedCluster")
Expand All @@ -46,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 @@ -78,18 +78,12 @@ func (r *ManagedClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
}
return hasRequiredODFKey(obj)
},

DeleteFunc: func(e event.DeleteEvent) bool {
return false
},
GenericFunc: func(e event.GenericEvent) bool {
return false
},
}

return ctrl.NewControllerManagedBy(mgr).
For(&clusterv1.ManagedCluster{}, builder.WithPredicates(managedClusterPredicate, predicate.ResourceVersionChangedPredicate{})).
Owns(&viewv1beta1.ManagedClusterView{}).
Owns(&corev1.ConfigMap{}).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any need to reconcile this on the owned ConfigMaps ?? It's not like we are managing cleanup of MCV or anything here, was there some other reason ??

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is done to ensure that MC reconcile is triggered whenever there are events for the ConfigMap. To ensure the desired ConfigMap is always present..

Copy link
Contributor

@SanjalKatiyar SanjalKatiyar Jul 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ConfigMap is created by MCV controller, not by MC controller, right ?? my question was, if any event occurs on that ConfigMap, why we need to reconcile MC controller (which creates MCV)... which edge case am I missing ??

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Configmap cannot be owned by MCV as they will be in different namespaces. It can be owned by cluster scoped resource like MC. Which will propagate events to eventually reconcile the configmap to desired state

Complete(r)
}

Expand Down
199 changes: 199 additions & 0 deletions controllers/managedclusterview_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
package controllers

import (
"context"
"encoding/json"
"fmt"
"log/slog"
"os"

ocsv1alpha1 "github.com/red-hat-storage/ocs-operator/api/v4/v1alpha1"
"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"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/yaml"
)

type ManagedClusterViewReconciler struct {
Client client.Client
Logger *slog.Logger
}

const (
ODFInfoConfigMapName = "odf-info"
ConfigMapResourceType = "ConfigMap"
ClientInfoConfigMapName = "odf-client-info"
vbnrh marked this conversation as resolved.
Show resolved Hide resolved
)

type ProviderInfo struct {
Version string `json:"version"`
DeploymentType string `json:"deploymentType"`
StorageSystemName string `json:"storageSystemName"`
ProviderManagedClusterName string `json:"providerManagedClusterName"`
NamespacedName types.NamespacedName `json:"namespacedName"`
StorageProviderEndpoint string `json:"storageProviderEndpoint"`
CephClusterFSID string `json:"cephClusterFSID"`
}

type ClientInfo struct {
ClusterID string `json:"clusterId"`
Name string `json:"name"`
ProviderInfo ProviderInfo `json:"providerInfo,omitempty"`
ClientManagedClusterName string `json:"clientManagedClusterName,omitempty"`
}

func (r *ManagedClusterViewReconciler) SetupWithManager(mgr ctrl.Manager) error {
r.Logger.Info("Setting up ManagedClusterViewReconciler with manager")
managedClusterViewPredicate := predicate.Funcs{
UpdateFunc: func(e event.UpdateEvent) bool {
obj, ok := e.ObjectNew.(*viewv1beta1.ManagedClusterView)
if !ok {
return false
}
return hasODFInfoInScope(obj)
},
CreateFunc: func(e event.CreateEvent) bool {
obj, ok := e.Object.(*viewv1beta1.ManagedClusterView)
if !ok {
return false
}
return hasODFInfoInScope(obj)
},
}

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

func hasODFInfoInScope(mc *viewv1beta1.ManagedClusterView) bool {
if mc.Spec.Scope.Name == ODFInfoConfigMapName && mc.Spec.Scope.Kind == ConfigMapResourceType {
return true
}
return false
}

func (r *ManagedClusterViewReconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
logger := r.Logger.With("ManagedClusterView", req.NamespacedName)
logger.Info("Reconciling ManagedClusterView")

var managedClusterView viewv1beta1.ManagedClusterView
if err := r.Client.Get(ctx, req.NamespacedName, &managedClusterView); err != nil {
if client.IgnoreNotFound(err) != nil {
logger.Error("Failed to get ManagedClusterView", "error", err)
}
return ctrl.Result{}, client.IgnoreNotFound(err)
}
vbnrh marked this conversation as resolved.
Show resolved Hide resolved

if err := createOrUpdateConfigMap(ctx, r.Client, managedClusterView, r.Logger); err != nil {
logger.Error("Failed to create or update ConfigMap for ManagedClusterView", "error", err)
return ctrl.Result{}, err
}

logger.Info("Successfully reconciled ManagedClusterView")

return ctrl.Result{}, nil
}

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

for _, value := range resultData {
var odfInfo ocsv1alpha1.OdfInfoData
err := yaml.Unmarshal([]byte(value), &odfInfo)
if err != nil {
return fmt.Errorf("failed to unmarshal ODF info data. %w", err)
}

providerInfo := ProviderInfo{
Version: odfInfo.Version,
DeploymentType: odfInfo.DeploymentType,
CephClusterFSID: odfInfo.StorageCluster.CephClusterFSID,
StorageProviderEndpoint: odfInfo.StorageCluster.StorageProviderEndpoint,
NamespacedName: odfInfo.StorageCluster.NamespacedName,
StorageSystemName: odfInfo.StorageSystemName,
ProviderManagedClusterName: managedClusterView.Namespace,
}

for _, client := range odfInfo.Clients {
managedCluster, err := utils.GetManagedClusterById(ctx, c, client.ClusterID)
if err != nil {
return err
}
clientInfo := ClientInfo{
ClusterID: client.ClusterID,
Name: client.Name,
ProviderInfo: providerInfo,
ClientManagedClusterName: managedCluster.Name,
}
clientInfoMap[fmt.Sprintf("%s/%s", managedCluster.Name, client.Name)] = clientInfo
}
}
Comment on lines +136 to +149
Copy link
Contributor

@SanjalKatiyar SanjalKatiyar Jul 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What will happen in case of Multiple StorageCluster scenario (normal 1 internal + 1 external on same OCP) (no Provider-Client) ??

  1. Won't fmt.Sprintf("%s/%s", managedCluster.Name, client.Name) give exact same "key" in both cases ??
  2. Assuming that there will always be one client for internal (somehow), what happens to external mode cluster ?? This ConfigMap won't store info about external cluster, right ??

Copy link
Contributor

@SanjalKatiyar SanjalKatiyar Jul 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know if this ConfigMap is only for Provider-Client use case and not for other deployments. Because it make sense if so, but I am not sure why to make it so specific.


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)
}

if configMap.Data == nil {
configMap.Data = make(map[string]string)
}

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)
}

mcvOwnerRefs := managedClusterView.GetOwnerReferences()
for _, mcvOwnerRef := range mcvOwnerRefs {
exists := false
for _, existingOwnerRef := range configMap.OwnerReferences {
if existingOwnerRef.UID == mcvOwnerRef.UID {
exists = true
break
}
}
if !exists {
configMap.OwnerReferences = append(configMap.OwnerReferences, mcvOwnerRef)
}
}
return nil
})

if err != nil {
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))

return nil
}
Loading
Loading