Skip to content

Commit

Permalink
fix: pv and pvc annotations duplicated error
Browse files Browse the repository at this point in the history
Signed-off-by: OrangeBao <[email protected]>
  • Loading branch information
OrangeBao committed Nov 12, 2023
1 parent a100756 commit da08e85
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
20 changes: 12 additions & 8 deletions pkg/clustertree/cluster-manager/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,15 @@ func (c *ClusterController) clearClusterControllers(cluster *kosmosv1alpha1.Clus
}

func (c *ClusterController) setupControllers(mgr manager.Manager, cluster *kosmosv1alpha1.Cluster, nodes []*corev1.Node, clientDynamic *dynamic.DynamicClient, leafClient kubernetes.Interface, kosmosClient kosmosversioned.Interface) error {
isNode2NodeFunc := func(cluster *kosmosv1alpha1.Cluster) bool {
return cluster.Spec.ClusterTreeOptions.LeafModels != nil
}

clusterName := fmt.Sprintf("%s%s", utils.KosmosNodePrefix, cluster.Name)
if isNode2NodeFunc(cluster) {
clusterName = cluster.Name
}

c.GlobalLeafManager.AddLeafResource(clusterName, &leafUtils.LeafResource{
Client: mgr.GetClient(),
DynamicClient: clientDynamic,
Expand All @@ -244,10 +252,6 @@ func (c *ClusterController) setupControllers(mgr manager.Manager, cluster *kosmo
EnableServiceAccount: true,
}, cluster.Spec.ClusterTreeOptions.LeafModels, nodes)

isNode2NodeFunc := func(cluster *kosmosv1alpha1.Cluster) bool {
return cluster.Spec.ClusterTreeOptions.LeafModels != nil
}

nodeResourcesController := controllers.NodeResourcesController{
Leaf: mgr.GetClient(),
GlobalLeafManager: c.GlobalLeafManager,
Expand Down Expand Up @@ -289,20 +293,20 @@ func (c *ClusterController) setupControllers(mgr manager.Manager, cluster *kosmo
return fmt.Errorf("error starting podUpstreamReconciler %s: %v", podcontrollers.LeafPodControllerName, err)
}

err := c.setupStorageControllers(mgr, nodes, leafClient)
err := c.setupStorageControllers(mgr, nodes, leafClient, cluster.Name)
if err != nil {
return err
}

return nil
}

func (c *ClusterController) setupStorageControllers(mgr manager.Manager, nodes []*corev1.Node, leafClient kubernetes.Interface) error {
func (c *ClusterController) setupStorageControllers(mgr manager.Manager, nodes []*corev1.Node, leafClient kubernetes.Interface, clustername string) error {
leafPVCController := pvc.LeafPVCController{
LeafClient: mgr.GetClient(),
RootClient: c.Root,
RootClientSet: c.RootClient,
NodeName: nodes[0].Name,
ClusterName: clustername,
}
if err := leafPVCController.SetupWithManager(mgr); err != nil {
return fmt.Errorf("error starting leaf pvc controller %v", err)
Expand All @@ -312,7 +316,7 @@ func (c *ClusterController) setupStorageControllers(mgr manager.Manager, nodes [
LeafClient: mgr.GetClient(),
RootClient: c.Root,
RootClientSet: c.RootClient,
NodeName: nodes[0].Name,
ClusterName: clustername,
}
if err := leafPVController.SetupWithManager(mgr); err != nil {
return fmt.Errorf("error starting leaf pv controller %v", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type LeafPVController struct {
LeafClient client.Client
RootClient client.Client
RootClientSet kubernetes.Interface
NodeName string
ClusterName string
}

func (l *LeafPVController) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
Expand Down Expand Up @@ -84,7 +84,7 @@ func (l *LeafPVController) Reconcile(ctx context.Context, request reconcile.Requ
}

rootPV = pv.DeepCopy()
filterPV(rootPV, l.NodeName)
filterPV(rootPV, l.ClusterName)
nn := types.NamespacedName{
Name: rootPV.Spec.ClaimRef.Name,
Namespace: rootPV.Spec.ClaimRef.Namespace,
Expand All @@ -101,7 +101,7 @@ func (l *LeafPVController) Reconcile(ctx context.Context, request reconcile.Requ

rootPV.Spec.ClaimRef.UID = rootPVC.UID
rootPV.Spec.ClaimRef.ResourceVersion = rootPVC.ResourceVersion
utils.AddResourceOwnersAnnotations(rootPV.Annotations, l.NodeName)
utils.AddResourceOwnersAnnotations(rootPV.Annotations, l.ClusterName)

rootPV, err = l.RootClientSet.CoreV1().PersistentVolumes().Create(ctx, rootPV, metav1.CreateOptions{})
if err != nil || rootPV == nil {
Expand All @@ -112,7 +112,7 @@ func (l *LeafPVController) Reconcile(ctx context.Context, request reconcile.Requ
return reconcile.Result{}, nil
}

if !utils.HasResourceOwnersAnnotations(rootPV.Annotations, l.NodeName) {
if !utils.HasResourceOwnersAnnotations(rootPV.Annotations, l.ClusterName) {
klog.Errorf("meet the same name root pv name: %q !", request.NamespacedName.Name)
return reconcile.Result{}, nil
}
Expand All @@ -128,7 +128,7 @@ func (l *LeafPVController) Reconcile(ctx context.Context, request reconcile.Requ
return reconcile.Result{}, nil
}

filterPV(rootPV, l.NodeName)
filterPV(rootPV, l.ClusterName)
if pvCopy.Spec.ClaimRef != nil || rootPV.Spec.ClaimRef == nil {
nn := types.NamespacedName{
Name: pvCopy.Spec.ClaimRef.Name,
Expand All @@ -151,7 +151,7 @@ func (l *LeafPVController) Reconcile(ctx context.Context, request reconcile.Requ
pvCopy.Spec.NodeAffinity = rootPV.Spec.NodeAffinity
pvCopy.UID = rootPV.UID
pvCopy.ResourceVersion = rootPV.ResourceVersion
utils.AddResourceOwnersAnnotations(pvCopy.Annotations, l.NodeName)
utils.AddResourceOwnersAnnotations(pvCopy.Annotations, l.ClusterName)

if utils.IsPVEqual(rootPV, pvCopy) {
return reconcile.Result{}, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type LeafPVCController struct {
LeafClient client.Client
RootClient client.Client
RootClientSet kubernetes.Interface
NodeName string
ClusterName string
}

func (l *LeafPVCController) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
Expand Down Expand Up @@ -65,14 +65,14 @@ func (l *LeafPVCController) Reconcile(ctx context.Context, request reconcile.Req
if reflect.DeepEqual(rootPVC.Status, pvcCopy.Status) {
return reconcile.Result{}, nil
}
if err = filterPVC(pvcCopy, l.NodeName); err != nil {
if err = filterPVC(pvcCopy, l.ClusterName); err != nil {
return reconcile.Result{}, nil
}

delete(pvcCopy.Annotations, utils.PVCSelectedNodeKey)
pvcCopy.ResourceVersion = rootPVC.ResourceVersion
pvcCopy.OwnerReferences = rootPVC.OwnerReferences
utils.AddResourceOwnersAnnotations(pvcCopy.Annotations, l.NodeName)
utils.AddResourceOwnersAnnotations(pvcCopy.Annotations, l.ClusterName)
pvcCopy.Spec = rootPVC.Spec
klog.V(4).Infof("rootPVC %+v\n, pvc %+v", rootPVC, pvcCopy)

Expand Down

0 comments on commit da08e85

Please sign in to comment.