Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kale-amruta committed Feb 12, 2025
1 parent a3c5b8a commit 935284f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 21 deletions.
17 changes: 9 additions & 8 deletions pkg/controllers/resources/csinodes/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package csinodes

import (
"fmt"
"time"

"github.com/loft-sh/vcluster/pkg/mappings/generic"
"github.com/loft-sh/vcluster/pkg/patcher"
Expand Down Expand Up @@ -98,21 +97,23 @@ func (s *csinodeSyncer) Sync(ctx *synccontext.SyncContext, event *synccontext.Sy
event.Virtual.Annotations = event.Host.Annotations
event.Virtual.Labels = event.Host.Labels
event.Host.Spec.DeepCopyInto(&event.Virtual.Spec)

// Set the marker of managed-by vcluster so that
// we skip deleting the nodes which are not managed
// by vcluster in `SyncToHost` function
if len(event.Virtual.Labels) == 0 {
event.Virtual.Labels = map[string]string{}
}
event.Virtual.Labels[translate.ManagedCSINodeLabel] = "true"
event.Virtual.Labels[translate.MarkerLabel] = translate.MarkerLabelValue

return ctrl.Result{}, nil
}

func (s *csinodeSyncer) SyncToHost(ctx *synccontext.SyncContext, event *synccontext.SyncToHostEvent[*storagev1.CSINode]) (ctrl.Result, error) {
if event.HostOld == nil {
ManagedCSINodeLabelsExists := event.Virtual.GetLabels() != nil && event.Virtual.GetLabels()[translate.ManagedCSINodeLabel] != ""
if !ManagedCSINodeLabelsExists {
// Delaying the deletion
if time.Since(event.Virtual.CreationTimestamp.Time) < 30*time.Second {
return ctrl.Result{RequeueAfter: time.Second * 2}, nil
}
ManagedByLabelDoesNotExist := event.Virtual.GetLabels() == nil || (event.Virtual.GetLabels() != nil && event.Virtual.GetLabels()[translate.MarkerLabel] != translate.MarkerLabelValue)
if ManagedByLabelDoesNotExist {
return ctrl.Result{}, nil
}
}
ctx.Log.Infof("delete virtual CSINode %s, because physical object is missing", event.Virtual.Name)
Expand Down
15 changes: 4 additions & 11 deletions pkg/controllers/resources/nodes/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package nodes
import (
"context"
"fmt"
"time"

"github.com/loft-sh/vcluster/pkg/mappings"
"github.com/loft-sh/vcluster/pkg/patcher"
Expand Down Expand Up @@ -34,9 +33,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/source"
)

// Default grace period in seconds
var minimumGracePeriodInSeconds int64 = 30

func NewSyncer(ctx *synccontext.RegisterContext, nodeServiceProvider nodeservice.Provider) (syncertypes.Object, error) {
var nodeSelector labels.Selector
if ctx.Config.Sync.FromHost.Nodes.Selector.All {
Expand Down Expand Up @@ -277,17 +273,14 @@ func registerIndices(ctx *synccontext.RegisterContext) error {

func (s *nodeSyncer) SyncToHost(ctx *synccontext.SyncContext, event *synccontext.SyncToHostEvent[*corev1.Node]) (ctrl.Result, error) {
if event.HostOld == nil {
fakeNodeLabelExists := event.Virtual.GetLabels() != nil && event.Virtual.GetLabels()[translate.ManagedFakeNodeLabel] != ""
ManagedLabelsAnnotationExists := event.Virtual.GetAnnotations() != nil && event.Virtual.GetAnnotations()[translate.ManagedLabelsAnnotation] != ""
if !fakeNodeLabelExists && !ManagedLabelsAnnotationExists {
// Delaying the deletion
if time.Since(event.Virtual.CreationTimestamp.Time) < 30*time.Second {
return ctrl.Result{RequeueAfter: time.Second * 2}, nil
}
ManagedByLabelDoesNotExist := event.Virtual.GetLabels() == nil || (event.Virtual.GetLabels() != nil && event.Virtual.GetLabels()[translate.MarkerLabel] != translate.MarkerLabelValue)
if ManagedByLabelDoesNotExist {
return ctrl.Result{}, nil
}
}
ctx.Log.Infof("delete virtual node %s, because it is not needed anymore", event.Virtual.Name)
return ctrl.Result{}, ctx.VirtualClient.Delete(ctx, event.Virtual)

}

func (s *nodeSyncer) Sync(ctx *synccontext.SyncContext, event *synccontext.SyncEvent[*corev1.Node]) (_ ctrl.Result, retErr error) {
Expand Down
5 changes: 5 additions & 0 deletions pkg/controllers/resources/nodes/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ func (s *nodeSyncer) translateUpdateBackwards(pNode *corev1.Node, vNode *corev1.
annotations["cluster-autoscaler.kubernetes.io/scale-down-disabled"] = "true"
}

// Set the marker of managed-by vcluster so that
// we skip deleting the nodes which are not managed
// by vcluster in `SyncToHost` function
labels[translate.MarkerLabel] = translate.MarkerLabelValue

// set annotations, spec & labels
vNode.Spec = *translatedSpec
vNode.Annotations = annotations
Expand Down
3 changes: 1 addition & 2 deletions pkg/util/translate/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ var (

ManagedAnnotationsAnnotation = "vcluster.loft.sh/managed-annotations"
ManagedLabelsAnnotation = "vcluster.loft.sh/managed-labels"
ManagedFakeNodeLabel = "vcluster.loft.sh/fake-node"
ManagedCSINodeLabel = "vcluster.loft.sh/csi-node"
MarkerLabelValue = "vcluster"
)

var Default Translator = &singleNamespace{}
Expand Down

0 comments on commit 935284f

Please sign in to comment.