Skip to content

Commit

Permalink
refactor: clean up & add synccontext to HostName
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianKramm committed Jul 29, 2024
1 parent 3922aee commit 82ac24b
Show file tree
Hide file tree
Showing 114 changed files with 613 additions and 11,136 deletions.
21 changes: 21 additions & 0 deletions chart/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,13 @@
"description": "DisableSync will not sync any resources and disable most control plane functionality.",
"pro": true
},
"namespaceMappings": {
"items": {
"$ref": "#/$defs/NamespaceMapping"
},
"type": "array",
"description": "NamespaceMappings are direct namespace mappings"
},
"rewriteKubernetesService": {
"type": "boolean",
"description": "RewriteKubernetesService will rewrite the Kubernetes service to point to the vCluster service if disableSync is enabled",
Expand Down Expand Up @@ -1925,6 +1932,20 @@
"additionalProperties": false,
"type": "object"
},
"NamespaceMapping": {
"properties": {
"host": {
"type": "string",
"description": "Host namespace to map into the vCluster"
},
"virtual": {
"type": "string",
"description": "Virtual namespace to map into the vCluster"
}
},
"additionalProperties": false,
"type": "object"
},
"NetworkPolicy": {
"properties": {
"enabled": {
Expand Down
2 changes: 2 additions & 0 deletions cmd/vcluster/cmd/debug/debug.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package debug

import (
"github.com/loft-sh/vcluster/cmd/vcluster/cmd/debug/etcd"
"github.com/loft-sh/vcluster/cmd/vcluster/cmd/debug/mappings"
"github.com/spf13/cobra"
)
Expand All @@ -17,5 +18,6 @@ func NewDebugCmd() *cobra.Command {
}

debugCmd.AddCommand(mappings.NewMappingsCmd())
debugCmd.AddCommand(etcd.NewEtcdCmd())
return debugCmd
}
20 changes: 20 additions & 0 deletions cmd/vcluster/cmd/debug/etcd/etcd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package etcd

import (
"github.com/spf13/cobra"
)

func NewEtcdCmd() *cobra.Command {
debugCmd := &cobra.Command{
Use: "etcd",
Short: "vCluster etcd subcommand",
Long: `#######################################################
############### vcluster debug etcd ###############
#######################################################
`,
Args: cobra.NoArgs,
}

debugCmd.AddCommand(NewKeysCommand())
return debugCmd
}
61 changes: 61 additions & 0 deletions cmd/vcluster/cmd/debug/etcd/keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package etcd

import (
"context"
"fmt"
"os"

"github.com/loft-sh/vcluster/pkg/config"
"github.com/loft-sh/vcluster/pkg/constants"
"github.com/loft-sh/vcluster/pkg/etcd"
"github.com/spf13/cobra"
)

type KeysOptions struct {
Config string

Prefix string
}

func NewKeysCommand() *cobra.Command {
options := &KeysOptions{}
cmd := &cobra.Command{
Use: "keys",
Short: "Dump the vCluster etcd stored keys",
Args: cobra.NoArgs,
RunE: func(cobraCommand *cobra.Command, _ []string) (err error) {
return ExecuteKeys(cobraCommand.Context(), options)
},
}

cmd.Flags().StringVar(&options.Config, "config", constants.DefaultVClusterConfigLocation, "The path where to find the vCluster config to load")
cmd.Flags().StringVar(&options.Prefix, "prefix", "/", "The prefix to use for listing the keys")
return cmd
}

func ExecuteKeys(ctx context.Context, options *KeysOptions) error {
// parse vCluster config
vConfig, err := config.ParseConfig(options.Config, os.Getenv("VCLUSTER_NAME"), nil)
if err != nil {
return err
}

// create new etcd client
etcdClient, err := etcd.NewFromConfig(ctx, vConfig)
if err != nil {
return err
}

// create new etcd backend & list mappings
keyValues, err := etcdClient.List(ctx, options.Prefix, 0)
if err != nil {
return err
}

// print mappings
for _, keyValue := range keyValues {
fmt.Println(string(keyValue.Key))
}

return nil
}
2 changes: 1 addition & 1 deletion cmd/vcluster/cmd/debug/mappings/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewAddCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "add",
Short: "Adds a custom mapping to the vCluster stored mappings",
RunE: func(cobraCommand *cobra.Command, args []string) (err error) {
RunE: func(cobraCommand *cobra.Command, _ []string) (err error) {
return ExecuteSave(cobraCommand.Context(), options)
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/vcluster/cmd/debug/mappings/clear.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewClearCommand() *cobra.Command {
Use: "clear",
Short: "Empty the vCluster stored mappings",
Args: cobra.NoArgs,
RunE: func(cobraCommand *cobra.Command, args []string) (err error) {
RunE: func(cobraCommand *cobra.Command, _ []string) (err error) {
return ExecuteClear(cobraCommand.Context(), options)
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/vcluster/cmd/debug/mappings/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewDeleteCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Short: "Deletes a custom mapping to the vCluster stored mappings",
RunE: func(cobraCommand *cobra.Command, args []string) (err error) {
RunE: func(cobraCommand *cobra.Command, _ []string) (err error) {
return ExecuteDelete(cobraCommand.Context(), options)
},
}
Expand Down
19 changes: 18 additions & 1 deletion cmd/vcluster/cmd/debug/mappings/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (

type ListOptions struct {
Config string

Kind string
}

func NewListCommand() *cobra.Command {
Expand All @@ -23,14 +25,16 @@ func NewListCommand() *cobra.Command {
Use: "list",
Short: "Dump the vCluster stored mappings",
Args: cobra.NoArgs,
RunE: func(cobraCommand *cobra.Command, args []string) (err error) {
RunE: func(cobraCommand *cobra.Command, _ []string) (err error) {
return ExecuteList(cobraCommand.Context(), options)
},
}

cmd.Flags().StringVar(&options.Config, "config", constants.DefaultVClusterConfigLocation, "The path where to find the vCluster config to load")
cmd.Flags().StringVar(&options.Kind, "kind", "", "The kind of objects to list")
return cmd
}

func ExecuteList(ctx context.Context, options *ListOptions) error {
// parse vCluster config
vConfig, err := config.ParseConfig(options.Config, os.Getenv("VCLUSTER_NAME"), nil)
Expand All @@ -50,6 +54,19 @@ func ExecuteList(ctx context.Context, options *ListOptions) error {
return fmt.Errorf("list mappings: %w", err)
}

// filter if kind is specified
if options.Kind != "" {
newMappings := make([]*store.Mapping, 0, len(mappings))
for _, mapping := range mappings {
if mapping.Kind != options.Kind {
continue
}

newMappings = append(newMappings, mapping)
}
mappings = newMappings
}

// print mappings
raw, err := json.MarshalIndent(mappings, "", " ")
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1704,6 +1704,9 @@ type ExperimentalSyncSettings struct {
// DisableSync will not sync any resources and disable most control plane functionality.
DisableSync bool `json:"disableSync,omitempty" product:"pro"`

// NamespaceMappings are direct namespace mappings
NamespaceMappings []NamespaceMapping `json:"namespaceMappings,omitempty"`

// RewriteKubernetesService will rewrite the Kubernetes service to point to the vCluster service if disableSync is enabled
RewriteKubernetesService bool `json:"rewriteKubernetesService,omitempty" product:"pro"`

Expand All @@ -1723,6 +1726,14 @@ type ExperimentalSyncSettings struct {
VirtualMetricsBindAddress string `json:"virtualMetricsBindAddress,omitempty"`
}

type NamespaceMapping struct {
// Host namespace to map into the vCluster
Host string `json:"host,omitempty"`

// Virtual namespace to map into the vCluster
Virtual string `json:"virtual,omitempty"`
}

func (e ExperimentalSyncSettings) JSONSchemaExtend(base *jsonschema.Schema) {
addProToJSONSchema(base, reflect.TypeOf(e))
}
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ require (
golang.org/x/sync v0.7.0
google.golang.org/grpc v1.64.0
google.golang.org/protobuf v1.34.1
gopkg.in/square/go-jose.v2 v2.6.0
gopkg.in/yaml.v3 v3.0.1
gotest.tools v2.2.0+incompatible
gotest.tools/v3 v3.5.1
Expand Down Expand Up @@ -176,7 +175,7 @@ require (
github.com/tcnksm/go-gitconfig v0.1.2 // indirect
github.com/ulikunitz/xz v0.5.11 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
go.etcd.io/etcd/api/v3 v3.5.14 // indirect
go.etcd.io/etcd/api/v3 v3.5.14
go.etcd.io/etcd/client/pkg/v3 v3.5.14 // indirect
go.etcd.io/etcd/client/v3 v3.5.14
go.mongodb.org/mongo-driver v1.10.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -813,8 +813,6 @@ gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
gopkg.in/square/go-jose.v2 v2.6.0 h1:NGk74WTnPKBNUhNzQX7PYcTLUjoq7mzKk2OKbvwk2iI=
gopkg.in/square/go-jose.v2 v2.6.0/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
20 changes: 20 additions & 0 deletions pkg/config/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,26 @@ func ValidateConfigAndSetDefaults(config *VirtualClusterConfig) error {
config.Sync.FromHost.Nodes.Selector.All = true
}

// if virtual is empty we set it to host
virtual := map[string]bool{}
host := map[string]bool{}
for i, mapping := range config.Experimental.SyncSettings.NamespaceMappings {
if mapping.Host == "" {
return fmt.Errorf("host needs to be set in namespace mapping")
} else if host[mapping.Host] {
return fmt.Errorf("multiple mappings found for host namespace %s", mapping.Host)
}
host[mapping.Host] = true

if mapping.Virtual == "" {
config.Experimental.SyncSettings.NamespaceMappings[i].Virtual = mapping.Host
}
if virtual[config.Experimental.SyncSettings.NamespaceMappings[i].Virtual] {
return fmt.Errorf("multiple mappings found for virtual namespace %s", config.Experimental.SyncSettings.NamespaceMappings[i].Virtual)
}
virtual[config.Experimental.SyncSettings.NamespaceMappings[i].Virtual] = true
}

// enable additional controllers required for scheduling with storage
if config.ControlPlane.Advanced.VirtualScheduler.Enabled && config.Sync.ToHost.PersistentVolumeClaims.Enabled {
if config.Sync.FromHost.CSINodes.Enabled == "auto" {
Expand Down
14 changes: 7 additions & 7 deletions pkg/controllers/generic/export_patcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (e *exportPatcher) ServerSideApply(ctx *synccontext.SyncContext, fromObj, d
syncContext: ctx,

namespace: fromObj.GetNamespace(),
targetNamespace: translate.Default.HostNamespace(fromObj.GetNamespace()),
targetNamespace: translate.Default.HostNamespace(ctx, fromObj.GetNamespace()),
})
}

Expand Down Expand Up @@ -58,33 +58,33 @@ func (r *virtualToHostNameResolver) TranslateNameWithNamespace(name string, name
}

return types.NamespacedName{
Namespace: translate.Default.HostNamespace(namespace),
Name: translate.Default.HostName(name, ns),
Namespace: translate.Default.HostNamespace(r.syncContext, namespace),
Name: translate.Default.HostName(r.syncContext, name, ns),
}
}), nil
}

return translate.Default.HostName(name, namespace), nil
return translate.Default.HostName(r.syncContext, name, namespace), nil
}

func (r *virtualToHostNameResolver) TranslateLabelExpressionsSelector(selector *metav1.LabelSelector) (*metav1.LabelSelector, error) {
return translate.HostLabelSelectorCluster(r.syncContext, selector), nil
}

func (r *virtualToHostNameResolver) TranslateLabelKey(key string) (string, error) {
return translate.Default.HostLabel(r.syncContext, key), nil
return translate.Default.HostLabel(r.syncContext, key, ""), nil
}

func (r *virtualToHostNameResolver) TranslateLabelSelector(selector map[string]string) (map[string]string, error) {
labelSelector := &metav1.LabelSelector{
MatchLabels: selector,
}

return metav1.LabelSelectorAsMap(translate.HostLabelSelector(r.syncContext, labelSelector))
return metav1.LabelSelectorAsMap(translate.HostLabelSelector(r.syncContext, labelSelector, ""))
}

func (r *virtualToHostNameResolver) TranslateNamespaceRef(namespace string) (string, error) {
return translate.Default.HostNamespace(namespace), nil
return translate.Default.HostNamespace(r.syncContext, namespace), nil
}

func validateExportConfig(config *vclusterconfig.Export) error {
Expand Down
6 changes: 3 additions & 3 deletions pkg/controllers/generic/import_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ func (s *importer) isVirtualManaged(vObj client.Object) bool {
return vObj.GetAnnotations() != nil && vObj.GetAnnotations()[translate.ControllerLabel] != "" && vObj.GetAnnotations()[translate.ControllerLabel] == s.Name()
}

func (s *importer) IsManaged(_ *synccontext.SyncContext, pObj client.Object) (bool, error) {
func (s *importer) IsManaged(ctx *synccontext.SyncContext, pObj client.Object) (bool, error) {
if s.syncerOptions.IsClusterScopedCRD {
return true, nil
}
Expand All @@ -356,7 +356,7 @@ func (s *importer) IsManaged(_ *synccontext.SyncContext, pObj client.Object) (bo
}

// check if the pObj belong to a namespace managed by this vcluster
if !translate.Default.IsTargetedNamespace(pObj.GetNamespace()) {
if !translate.Default.IsTargetedNamespace(ctx, pObj.GetNamespace()) {
return false, nil
}

Expand All @@ -374,7 +374,7 @@ func (s *importer) VirtualToHost(ctx *synccontext.SyncContext, req types.Namespa
return s.virtualToHost(ctx, req, vObj)
}

return types.NamespacedName{Name: translate.Default.HostName(req.Name, req.Namespace), Namespace: translate.Default.HostNamespace(req.Namespace)}
return types.NamespacedName{Name: translate.Default.HostName(ctx, req.Name, req.Namespace), Namespace: translate.Default.HostNamespace(ctx, req.Namespace)}
}

func (s *importer) HostToVirtual(ctx *synccontext.SyncContext, req types.NamespacedName, pObj client.Object) types.NamespacedName {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/resources/configmaps/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestSync(t *testing.T) {
}
syncedConfigMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: translate.Default.HostName(baseConfigMap.Name, baseConfigMap.Namespace),
Name: translate.Default.HostName(nil, baseConfigMap.Name, baseConfigMap.Namespace),
Namespace: "test",
Annotations: map[string]string{
translate.NameAnnotation: baseConfigMap.Name,
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/resources/csistoragecapacities/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (s *csiStorageCapacitiesMapper) HostToVirtual(_ *synccontext.SyncContext, r
return types.NamespacedName{Name: translate.SafeConcatName(req.Name, "x", req.Namespace), Namespace: "kube-system"}
}

func (s *csiStorageCapacitiesMapper) VirtualToHost(ctx *synccontext.SyncContext, req types.NamespacedName, vObj client.Object) types.NamespacedName {
func (s *csiStorageCapacitiesMapper) VirtualToHost(_ *synccontext.SyncContext, _ types.NamespacedName, vObj client.Object) types.NamespacedName {
// if the virtual object is annotated with the physical name and namespace, return that
if vObj != nil {
vAnnotations := vObj.GetAnnotations()
Expand Down
Loading

0 comments on commit 82ac24b

Please sign in to comment.