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 30, 2024
1 parent 3922aee commit bfa9e5d
Show file tree
Hide file tree
Showing 122 changed files with 917 additions and 11,226 deletions.
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
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
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
11 changes: 5 additions & 6 deletions pkg/controllers/generic/import_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,8 @@ func createImporter(ctx *synccontext.RegisterContext, config *vclusterconfig.Imp

name: controllerID,
syncerOptions: &syncertypes.Options{
DisableUIDDeletion: true,
IsClusterScopedCRD: isClusterScoped,
HasStatusSubresource: hasStatusSubresource,
DisableUIDDeletion: true,
IsClusterScopedCRD: isClusterScoped,
},
}, nil
}
Expand Down Expand Up @@ -347,7 +346,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 +355,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 +373,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
8 changes: 4 additions & 4 deletions pkg/controllers/resources/endpoints/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ func TestExistingEndpoints(t *testing.T) {
}
pEndpoints := &corev1.Endpoints{
ObjectMeta: metav1.ObjectMeta{
Name: translate.Default.HostName(vEndpoints.Name, vEndpoints.Namespace),
Name: translate.Default.HostName(nil, vEndpoints.Name, vEndpoints.Namespace),
Namespace: "test",
},
}
pService := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: translate.Default.HostName(vEndpoints.Name, vEndpoints.Namespace),
Name: translate.Default.HostName(nil, vEndpoints.Name, vEndpoints.Namespace),
Namespace: "test",
Annotations: map[string]string{
translate.NameAnnotation: vEndpoints.Name,
Expand All @@ -69,7 +69,7 @@ func TestExistingEndpoints(t *testing.T) {
}
expectedEndpoints := &corev1.Endpoints{
ObjectMeta: metav1.ObjectMeta{
Name: translate.Default.HostName(vEndpoints.Name, vEndpoints.Namespace),
Name: translate.Default.HostName(nil, vEndpoints.Name, vEndpoints.Namespace),
Namespace: "test",
Annotations: map[string]string{
translate.NameAnnotation: vEndpoints.Name,
Expand Down Expand Up @@ -148,7 +148,7 @@ func TestSync(t *testing.T) {
}
syncedEndpoints := &corev1.Endpoints{
ObjectMeta: metav1.ObjectMeta{
Name: translate.Default.HostName(baseEndpoints.Name, baseEndpoints.Namespace),
Name: translate.Default.HostName(nil, baseEndpoints.Name, baseEndpoints.Namespace),
Namespace: "test",
Annotations: map[string]string{
translate.NameAnnotation: baseEndpoints.Name,
Expand Down
8 changes: 8 additions & 0 deletions pkg/controllers/resources/events/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ func (s *eventSyncer) Syncer() syncertypes.Sync[client.Object] {
return syncer.ToGenericSyncer[*corev1.Event](s)
}

var _ syncertypes.OptionsProvider = &eventSyncer{}

func (s *eventSyncer) Options() *syncertypes.Options {
return &syncertypes.Options{
SkipMappingsRecording: true,
}
}

func (s *eventSyncer) SyncToHost(ctx *synccontext.SyncContext, event *synccontext.SyncToHostEvent[*corev1.Event]) (ctrl.Result, error) {
// check if delete event
if event.IsDelete() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/resources/events/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestSync(t *testing.T) {
}
pPod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: translate.Default.HostName(vPod.Name, vPod.Namespace),
Name: translate.Default.HostName(nil, vPod.Name, vPod.Namespace),
Namespace: syncertesting.DefaultTestTargetNamespace,
},
}
Expand Down
Loading

0 comments on commit bfa9e5d

Please sign in to comment.