Skip to content

Commit

Permalink
add labelSelectors for syncToHost operation
Browse files Browse the repository at this point in the history
Signed-off-by: abhay.tomar <[email protected]>

Signed-off-by: abhay.tomar <[email protected]>
  • Loading branch information
ApsTomar committed Jan 6, 2025
1 parent 3e094c5 commit 9907de7
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
4 changes: 4 additions & 0 deletions chart/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,10 @@
"type": "boolean",
"description": "Enabled defines if this option should be enabled."
},
"selector": {
"$ref": "#/$defs/Selector",
"description": "Selector defines the labelSelector based filtering for syncing the resources to the host cluster."
},
"patches": {
"items": {
"$ref": "#/$defs/TranslatePatch"
Expand Down
2 changes: 2 additions & 0 deletions chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ sync:
services:
# Enabled defines if this option should be enabled.
enabled: true
# Selector defines the labelSelector based filtering for syncing the resources to the host cluster.
selector: {}
# Endpoints defines if endpoints created within the virtual cluster should get synced to the host cluster.
endpoints:
# Enabled defines if this option should be enabled.
Expand Down
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,9 @@ type EnableSwitchWithPatches struct {
// Enabled defines if this option should be enabled.
Enabled bool `json:"enabled,omitempty"`

// Selector defines the labelSelector based filtering for syncing the resources to the host cluster.
Selector *Selector `json:"selector,omitempty"`

// Patches patch the resource according to the provided specification.
Patches []TranslatePatch `json:"patches,omitempty"`
}
Expand Down
2 changes: 2 additions & 0 deletions config/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ sync:
toHost:
services:
enabled: true
# selector defines the label and expression based filtering before syncing the services to the host cluster.
selector: {}
endpoints:
enabled: true
persistentVolumeClaims:
Expand Down
26 changes: 25 additions & 1 deletion pkg/controllers/resources/services/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package services
import (
"errors"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"time"

"github.com/loft-sh/vcluster/pkg/mappings"
Expand Down Expand Up @@ -76,8 +78,30 @@ func (s *serviceSyncer) SyncToHost(ctx *synccontext.SyncContext, event *synccont
return patcher.DeleteVirtualObject(ctx, event.Virtual, event.HostOld, "host object was deleted")
}

// fetch the label selector present in the config.
configLabelSelector := ctx.Config.Sync.ToHost.Services.Selector
var selector labels.Selector
var err error
if configLabelSelector != nil {
selector, err = metav1.LabelSelectorAsSelector(metav1.SetAsLabelSelector(configLabelSelector.LabelSelector))
if err != nil {
return ctrl.Result{}, err
}
}

pObj := s.translate(ctx, event.Virtual)
err := pro.ApplyPatchesHostObject(ctx, nil, pObj, event.Virtual, ctx.Config.Sync.ToHost.Services.Patches, false)

// fetch the labels of the input service and if they match with config selector
// then continue with the sync operation else return.
serviceLabels := pObj.GetLabels()
if serviceLabels != nil {
if !selector.Matches(labels.Set(serviceLabels)) {
ctx.Log.Infof("The labels of the service %s don't match with the labelSelector provided, hence the sync operation is cancelled", pObj.Name)
return ctrl.Result{}, nil
}
}

err = pro.ApplyPatchesHostObject(ctx, nil, pObj, event.Virtual, ctx.Config.Sync.ToHost.Services.Patches, false)
if err != nil {
return ctrl.Result{}, err
}
Expand Down

0 comments on commit 9907de7

Please sign in to comment.