Skip to content

Commit

Permalink
Merge pull request kosmos-io#272 from OrangeBao/fix_leafmanager
Browse files Browse the repository at this point in the history
feat: refactor the leaf resource manager
  • Loading branch information
kosmos-robot authored Nov 21, 2023
2 parents 0f957aa + 0fa00ca commit af81979
Show file tree
Hide file tree
Showing 16 changed files with 214 additions and 151 deletions.
14 changes: 10 additions & 4 deletions pkg/clustertree/cluster-manager/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,15 @@ func (c *ClusterController) clearClusterControllers(cluster *kosmosv1alpha1.Clus
c.GlobalLeafManager.RemoveLeafResource(cluster.Name)
}

func (c *ClusterController) setupControllers(mgr manager.Manager, cluster *kosmosv1alpha1.Cluster, nodes []*corev1.Node, clientDynamic *dynamic.DynamicClient, leafClientset kubernetes.Interface, kosmosClient kosmosversioned.Interface, leafRestConfig *rest.Config) error {
clusterName := c.LeafModelHandler.GetGlobalLeafManagerClusterName(cluster)
c.GlobalLeafManager.AddLeafResource(clusterName, &leafUtils.LeafResource{
func (c *ClusterController) setupControllers(
mgr manager.Manager,
cluster *kosmosv1alpha1.Cluster,
nodes []*corev1.Node,
clientDynamic *dynamic.DynamicClient,
leafClientset kubernetes.Interface,
kosmosClient kosmosversioned.Interface,
leafRestConfig *rest.Config) error {
c.GlobalLeafManager.AddLeafResource(&leafUtils.LeafResource{
Client: mgr.GetClient(),
DynamicClient: clientDynamic,
Clientset: leafClientset,
Expand All @@ -248,7 +254,7 @@ func (c *ClusterController) setupControllers(mgr manager.Manager, cluster *kosmo
IgnoreLabels: strings.Split("", ","),
EnableServiceAccount: true,
RestConfig: leafRestConfig,
}, cluster.Spec.ClusterTreeOptions.LeafModels, nodes)
}, cluster, nodes)

nodeResourcesController := controllers.NodeResourcesController{
Leaf: mgr.GetClient(),
Expand Down
18 changes: 9 additions & 9 deletions pkg/clustertree/cluster-manager/controllers/common_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type SyncResourcesReconciler struct {
}

func (r *SyncResourcesReconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
var owners []string
var clusters []string
rootobj, err := r.DynamicRootClient.Resource(r.GroupVersionResource).Namespace(request.Namespace).Get(ctx, request.Name, metav1.GetOptions{})
if err != nil && !errors.IsNotFound(err) {
klog.Errorf("get %s error: %v", request.NamespacedName, err)
Expand All @@ -53,16 +53,16 @@ func (r *SyncResourcesReconciler) Reconcile(ctx context.Context, request reconci

if err != nil && errors.IsNotFound(err) {
// delete all
owners = r.GlobalLeafManager.ListNodeNames()
clusters = r.GlobalLeafManager.ListClusters()
} else {
owners = utils.ListResourceOwnersAnnotations(rootobj.GetAnnotations())
clusters = utils.ListResourceClusters(rootobj.GetAnnotations())
}

for _, owner := range owners {
if r.GlobalLeafManager.Has(owner) {
lr, err := r.GlobalLeafManager.GetLeafResource(owner)
for _, cluster := range clusters {
if r.GlobalLeafManager.HasCluster(cluster) {
lr, err := r.GlobalLeafManager.GetLeafResource(cluster)
if err != nil {
klog.Errorf("get lr(owner: %s) err: %v", owner, err)
klog.Errorf("get lr(cluster: %s) err: %v", cluster, err)
return reconcile.Result{RequeueAfter: SyncResourcesRequeueTime}, nil
}
if err = r.SyncResource(ctx, request, lr); err != nil {
Expand Down Expand Up @@ -115,7 +115,7 @@ func (r *SyncResourcesReconciler) SetupWithManager(mgr manager.Manager, gvr sche
}

func (r *SyncResourcesReconciler) SyncResource(ctx context.Context, request reconcile.Request, lr *leafUtils.LeafResource) error {
klog.V(5).Infof("Started sync resource processing, ns: %s, name: %s", request.Namespace, request.Name)
klog.V(4).Infof("Started sync resource processing, ns: %s, name: %s", request.Namespace, request.Name)

deleteSecretInClient := false

Expand Down Expand Up @@ -146,7 +146,7 @@ func (r *SyncResourcesReconciler) SyncResource(ctx context.Context, request reco
}
return err
}
klog.V(5).Infof("%s %q deleted", r.GroupVersionResource.Resource, request.Name)
klog.V(4).Infof("%s %q deleted", r.GroupVersionResource.Resource, request.Name)
return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,9 @@ func (c *AutoCreateMCSController) cleanUpMcsResources(ctx context.Context, names
continue
}

clusterName := clustertreeutils.GetLeafResourceClusterName(newCluster)
leafManager, err := c.GlobalLeafManager.GetLeafResource(clusterName)
leafManager, err := c.GlobalLeafManager.GetLeafResource(cluster.Name)
if err != nil {
klog.Errorf("get leafManager for cluster %s failed,Error: %v", clusterName, err)
klog.Errorf("get leafManager for cluster %s failed,Error: %v", cluster.Name, err)
return err
}
if err = leafManager.KosmosClient.MulticlusterV1alpha1().ServiceImports(namespace).Delete(ctx, name, metav1.DeleteOptions{}); err != nil {
Expand Down Expand Up @@ -255,10 +254,9 @@ func (c *AutoCreateMCSController) autoCreateMcsResources(ctx context.Context, se
continue
}

clusterName := clustertreeutils.GetLeafResourceClusterName(newCluster)
leafManager, err := c.GlobalLeafManager.GetLeafResource(clusterName)
leafManager, err := c.GlobalLeafManager.GetLeafResource(cluster.Name)
if err != nil {
klog.Errorf("get leafManager for cluster %s failed,Error: %v", clusterName, err)
klog.Errorf("get leafManager for cluster %s failed,Error: %v", cluster.Name, err)
return err
}
serviceImport := &mcsv1alpha1.ServiceImport{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (r *LeafPodReconciler) Reconcile(ctx context.Context, request reconcile.Req
podutils.FitObjectMeta(&podCopy.ObjectMeta)
podCopy.ResourceVersion = "0"
if err := r.RootClient.Status().Update(ctx, podCopy); err != nil && !apierrors.IsNotFound(err) {
klog.V(5).Info(errors.Wrap(err, "error while updating pod status in kubernetes"))
klog.V(4).Info(errors.Wrap(err, "error while updating pod status in kubernetes"))
return reconcile.Result{RequeueAfter: LeafPodRequeueTime}, nil
}
}
Expand Down
Loading

0 comments on commit af81979

Please sign in to comment.