Skip to content

Commit

Permalink
refactor: use new vcluster dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianKramm committed Apr 3, 2024
1 parent dc3d86c commit 742feac
Show file tree
Hide file tree
Showing 153 changed files with 6,965 additions and 3,127 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ name: E2E CI

on:
pull_request:
types: [opened, synchronize, reopened, edited]
branches:
- main
paths:
Expand All @@ -17,7 +16,7 @@ concurrency:
cancel-in-progress: true

env:
VCLUSTER_VERSION: v0.19.0-beta.2
VCLUSTER_VERSION: v0.20.0-alpha.2
VCLUSTER_SUFFIX: vcluster
VCLUSTER_NAME: vcluster
VCLUSTER_NAMESPACE: vcluster
Expand Down
2 changes: 1 addition & 1 deletion e2e/test_plugin/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build the manager binary
FROM golang:1.21.5 as builder
FROM golang:1.22 as builder

# Make sure we use go modules
WORKDIR vcluster
Expand Down
6 changes: 0 additions & 6 deletions e2e/test_plugin/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,3 @@ plugin:
- apiGroups: ["apiextensions.k8s.io"]
resources: ["customresourcedefinitions"]
verbs: ["create", "update", "get", "list", "watch"]

# Make sure the cluster role is enabled or otherwise the plugin won't be able to watch custom
# resource definitions.
rbac:
clusterRole:
create: true
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
module github.com/loft-sh/vcluster-sdk

go 1.21.5
go 1.22.0

toolchain go1.22.1

require (
github.com/ghodss/yaml v1.0.0
github.com/hashicorp/go-plugin v1.6.0
github.com/loft-sh/log v0.0.0-20230824104949-bd516c25712a
github.com/loft-sh/vcluster v0.19.0-beta.2
github.com/loft-sh/vcluster v0.20.0-alpha.2.0.20240403130844-8bb987ed97b4
github.com/onsi/ginkgo/v2 v2.14.0
github.com/onsi/gomega v1.30.0
github.com/pkg/errors v0.9.1
Expand Down Expand Up @@ -98,7 +100,6 @@ require (
github.com/loft-sh/jspolicy v0.2.2 // indirect
github.com/loft-sh/loftctl/v3 v3.4.0-beta.11 // indirect
github.com/loft-sh/utils v0.0.29 // indirect
github.com/loft-sh/vcluster-values v0.0.0-20240207093538-4bbb24e9f699 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -614,8 +614,6 @@ github.com/loft-sh/admin-apis v0.0.0-20240203010124-3600c1c582a8 h1:nuY9Vgvabh2F
github.com/loft-sh/admin-apis v0.0.0-20240203010124-3600c1c582a8/go.mod h1:MWczNwKvWssHo1KaeZKaWDdRLYSNbWqQBGsTLoCNd7U=
github.com/loft-sh/agentapi/v3 v3.4.0-beta.12.0.20240207094449-7adc00ae1265 h1:SUAdOY4H2n04JlI2So/kyRaH/HEpnoH//cewI4C4vyY=
github.com/loft-sh/agentapi/v3 v3.4.0-beta.12.0.20240207094449-7adc00ae1265/go.mod h1:Ln2nKIXWwgCYqA+NflEu182x3r0Zb7KZnzJ4LHOMP7Y=
github.com/loft-sh/analytics-client v0.0.0-20240206084604-1302688c3d4c h1:WeOXXSGCdE6s0+fargq3Hs0aYx+TMZoF9Kb45fkubys=
github.com/loft-sh/analytics-client v0.0.0-20240206084604-1302688c3d4c/go.mod h1:FFWcGASyM2QlWTDTCG/WBVM/XYr8btqYt335TFNRCFg=
github.com/loft-sh/analytics-client v0.0.0-20240219162240-2f4c64b2494e h1:JcPnMaoczikvpasi8OJ47dCkWZjfgFubWa4V2SZo7h0=
github.com/loft-sh/analytics-client v0.0.0-20240219162240-2f4c64b2494e/go.mod h1:FFWcGASyM2QlWTDTCG/WBVM/XYr8btqYt335TFNRCFg=
github.com/loft-sh/api/v3 v3.4.0-beta.11 h1:kh9oqGdco1YAUGjgw5LK9Z++4KLw561QANvGko312C8=
Expand All @@ -630,10 +628,8 @@ github.com/loft-sh/log v0.0.0-20230824104949-bd516c25712a h1:/gqqjKpcHEdFXIX41lx
github.com/loft-sh/log v0.0.0-20230824104949-bd516c25712a/go.mod h1:YImeRjXH34Yf5E79T7UHBQpDZl9fIaaFRgyZ/bkY+UQ=
github.com/loft-sh/utils v0.0.29 h1:P/MObccXToAZy2QoJSQDJ+OJx1qHitpFHEVj3QBSNJs=
github.com/loft-sh/utils v0.0.29/go.mod h1:9hlX9cGpWHg3mNi/oBlv3X4ePGDMK66k8MbOZGFMDTI=
github.com/loft-sh/vcluster v0.19.0-beta.2 h1:fAHWadyS/8P7aUSbCPKujEAHbuTgaYBOVazkVhYyeS8=
github.com/loft-sh/vcluster v0.19.0-beta.2/go.mod h1:5PQdcVunYGZZKgkY1q8ibt0TLspMAFoVn3vNvczKNU8=
github.com/loft-sh/vcluster-values v0.0.0-20240207093538-4bbb24e9f699 h1:lbbIlSpl/sz1iY/Tl/VUOU8NtFe31uwvlUbANK04mDM=
github.com/loft-sh/vcluster-values v0.0.0-20240207093538-4bbb24e9f699/go.mod h1:J34xtWyMbjM+NRgVWxO0IVDB5XYUaX52jPQNqEAhu4M=
github.com/loft-sh/vcluster v0.20.0-alpha.2.0.20240403130844-8bb987ed97b4 h1:D486kUE5ZQSx2DDQQ5kf/gowKZG/dNw7As4vg9WR7dw=
github.com/loft-sh/vcluster v0.20.0-alpha.2.0.20240403130844-8bb987ed97b4/go.mod h1:Ty7km/e/U7wVF9kIqROOR51/XezgHEtS/za0QVPhKkU=
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/mailru/easyjson v0.0.0-20160728113105-d5b7844b561a/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
Expand Down
96 changes: 73 additions & 23 deletions plugin/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,24 @@ import (

"github.com/ghodss/yaml"
"github.com/loft-sh/log/logr"
config2 "github.com/loft-sh/vcluster/config"
"github.com/loft-sh/vcluster/pkg/config"
"github.com/loft-sh/vcluster/pkg/controllers/syncer"
synccontext "github.com/loft-sh/vcluster/pkg/controllers/syncer/context"
"github.com/loft-sh/vcluster/pkg/options"
"github.com/loft-sh/vcluster/pkg/plugin"
"github.com/loft-sh/vcluster/pkg/plugin/types"
v2 "github.com/loft-sh/vcluster/pkg/plugin/v2"
"github.com/loft-sh/vcluster/pkg/scheme"
"github.com/loft-sh/vcluster/pkg/setup"
syncertypes "github.com/loft-sh/vcluster/pkg/types"
"github.com/loft-sh/vcluster/pkg/util/clienthelper"
contextutil "github.com/loft-sh/vcluster/pkg/util/context"
"github.com/loft-sh/vcluster/pkg/util/translate"
"github.com/pkg/errors"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
ctrlmanager "sigs.k8s.io/controller-runtime/pkg/manager"
)

func newManager() Manager {
Expand All @@ -45,6 +48,8 @@ type manager struct {
syncers []syncertypes.Base

proConfig v2.InitConfigPro

options Options
}

func (m *manager) UnmarshalConfig(into interface{}) error {
Expand All @@ -67,14 +72,20 @@ func (m *manager) Init() (*synccontext.RegisterContext, error) {
return m.InitWithOptions(Options{})
}

func (m *manager) InitWithOptions(opts Options) (*synccontext.RegisterContext, error) {
func (m *manager) InitWithOptions(options Options) (*synccontext.RegisterContext, error) {
m.m.Lock()
defer m.m.Unlock()

if m.initialized {
return nil, fmt.Errorf("plugin manager is already initialized")
}
m.initialized = true
m.options = options

// set code globals
plugin.IsPlugin = true
setup.NewLocalManager = m.newLocalManager
setup.NewVirtualManager = m.newVirtualManager

// create a new plugin server
var err error
Expand Down Expand Up @@ -119,34 +130,40 @@ func (m *manager) InitWithOptions(opts Options) (*synccontext.RegisterContext, e
ctx := klog.NewContext(context.Background(), logger)

// now create register context
virtualClusterOptions := &options.VirtualClusterOptions{}
err = json.Unmarshal(initConfig.Options, virtualClusterOptions)
virtualClusterConfig := &config.VirtualClusterConfig{}
err = json.Unmarshal(initConfig.Config, virtualClusterConfig)
if err != nil {
return nil, errors.Wrap(err, "unmarshal vcluster options")
return nil, errors.Wrap(err, "unmarshal vCluster config")
}

// set vcluster name correctly
if virtualClusterOptions.Name != "" {
translate.VClusterName = virtualClusterOptions.Name
// parse workload & control plane client
virtualClusterConfig.WorkloadConfig, err = bytesToRestConfig(initConfig.WorkloadConfig)
if err != nil {
return nil, fmt.Errorf("parse workload config: %w", err)
}

// parse clients
physicalConfig, err := clientcmd.NewClientConfigFromBytes(initConfig.PhysicalClusterConfig)
virtualClusterConfig.ControlPlaneConfig, err = bytesToRestConfig(initConfig.ControlPlaneConfig)
if err != nil {
return nil, errors.Wrap(err, "parse physical kube config")
return nil, fmt.Errorf("parse control plane config: %w", err)
}
restPhysicalConfig, err := physicalConfig.ClientConfig()

// we disable plugin loading
virtualClusterConfig.Plugin = map[string]config2.Plugin{}
virtualClusterConfig.Plugins = map[string]config2.Plugins{}

// init virtual cluster config
err = setup.InitConfig(virtualClusterConfig)
if err != nil {
return nil, errors.Wrap(err, "parse physical kube config rest")
return nil, fmt.Errorf("init config: %w", err)
}

// parse clients
m.syncerConfig, err = clientcmd.NewClientConfigFromBytes(initConfig.SyncerConfig)
if err != nil {
return nil, errors.Wrap(err, "parse syncer kube config")
}

// we disable plugin loading and create a new controller context
virtualClusterOptions.DisablePlugins = true
controllerCtx, err := setup.NewControllerContext(ctx, virtualClusterOptions, initConfig.CurrentNamespace, restPhysicalConfig, scheme.Scheme, opts.NewClient, opts.NewClient)
// create new controller context
controllerCtx, err := setup.NewControllerContext(ctx, virtualClusterConfig)
if err != nil {
return nil, fmt.Errorf("create controller context: %w", err)
}
Expand Down Expand Up @@ -220,15 +237,17 @@ func (m *manager) start() error {
go func() {
err := m.context.PhysicalManager.Start(m.context.Context)
if err != nil {
panic(err)
klog.Error(err)
Exit(1)
}
}()

// start the virtual cluster manager
go func() {
err := m.context.VirtualManager.Start(m.context.Context)
if err != nil {
panic(err)
klog.Error(err)
Exit(1)
}
}()

Expand All @@ -241,9 +260,9 @@ func (m *manager) start() error {
m.context.Context,
m.context.CurrentNamespaceClient,
m.context.CurrentNamespace,
m.context.Options.TargetNamespace,
m.context.Options.SetOwner,
m.context.Options.ServiceName,
m.context.Config.WorkloadTargetNamespace,
m.context.Config.Experimental.SyncSettings.SetOwner,
m.context.Config.WorkloadService,
)
if err != nil {
return fmt.Errorf("error in setting owner reference %v", err)
Expand Down Expand Up @@ -378,3 +397,34 @@ func (m *manager) findAllHooks() (map[types.VersionKindType][]ClientHook, error)

return hooks, nil
}

func (m *manager) newLocalManager(config *rest.Config, options ctrlmanager.Options) (ctrlmanager.Manager, error) {
options.Metrics.BindAddress = ""
if m.options.ModifyHostManager != nil {
m.options.ModifyHostManager(&options)
}

return ctrlmanager.New(config, options)
}

func (m *manager) newVirtualManager(config *rest.Config, options ctrlmanager.Options) (ctrlmanager.Manager, error) {
options.Metrics.BindAddress = ""
if m.options.ModifyVirtualManager != nil {
m.options.ModifyVirtualManager(&options)
}

return ctrlmanager.New(config, options)
}

func bytesToRestConfig(rawBytes []byte) (*rest.Config, error) {
if len(rawBytes) == 0 {
return nil, fmt.Errorf("kube client config is empty")
}

parsedConfig, err := clientcmd.NewClientConfigFromBytes(rawBytes)
if err != nil {
return nil, fmt.Errorf("parse kube config: %w", err)
}

return parsedConfig.ClientConfig()
}
11 changes: 5 additions & 6 deletions plugin/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ import (
synccontext "github.com/loft-sh/vcluster/pkg/controllers/syncer/context"
v2 "github.com/loft-sh/vcluster/pkg/plugin/v2"
syncertypes "github.com/loft-sh/vcluster/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrlmanager "sigs.k8s.io/controller-runtime/pkg/manager"
)

type Options struct {
// NewClient allows a user to define how to create a client.
NewClient client.NewClientFunc
// ModifyVirtualManager modifies options for the virtual manager
ModifyVirtualManager func(options *ctrlmanager.Options)

// NewCache is the function that will create the cache to be used
// by the manager. If not set this will use the default new cache function.
NewCache cache.NewCacheFunc
// ModifyHostManager modifies options for the host manager
ModifyHostManager func(options *ctrlmanager.Options)
}

type Manager interface {
Expand Down
Loading

0 comments on commit 742feac

Please sign in to comment.