Skip to content

Commit

Permalink
feat: add vcluster activate & apply platform secret
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianKramm committed Apr 30, 2024
1 parent f0d5638 commit dead007
Show file tree
Hide file tree
Showing 42 changed files with 1,064 additions and 856 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/unit-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
uses: actions/checkout@v4
- name: Install Helm Unit Test Plugin
run: |
helm plugin install https://github.com/helm-unittest/helm-unittest
helm plugin install https://github.com/helm-unittest/helm-unittest --version v0.4.4
- name: Run Helm Unit Tests
run: |
helm unittest chart
Expand Down
12 changes: 0 additions & 12 deletions cmd/vcluster/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"os"
"runtime/debug"

"github.com/go-logr/logr"
vconfig "github.com/loft-sh/vcluster/config"
"github.com/loft-sh/vcluster/pkg/config"
"github.com/loft-sh/vcluster/pkg/leaderelection"
"github.com/loft-sh/vcluster/pkg/plugin"
Expand Down Expand Up @@ -53,16 +51,6 @@ func ExecuteStart(ctx context.Context, options *StartOptions) error {
return err
}

if vconfig.ShouldCheckForProFeatures() && vConfig.IsProFeatureEnabled() {
log, err := logr.FromContext(ctx)
if err != nil {
return err
}

log.Info("In order to use a Pro feature, please contact us at https://www.vcluster.com/pro-demo or downgrade by running `vcluster upgrade --version v0.19.5`")
os.Exit(1)
}

// get current namespace
vConfig.ControlPlaneConfig, vConfig.ControlPlaneNamespace, vConfig.ControlPlaneService, vConfig.WorkloadConfig, vConfig.WorkloadNamespace, vConfig.WorkloadService, err = pro.GetRemoteClient(vConfig)
if err != nil {
Expand Down
8 changes: 0 additions & 8 deletions cmd/vclusterctl/cmd/activate.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package cmd

import (
"os"

loftctlUtil "github.com/loft-sh/loftctl/v3/pkg/util"
"github.com/loft-sh/log"
platformcmd "github.com/loft-sh/vcluster/cmd/vclusterctl/cmd/platform"
"github.com/loft-sh/vcluster/config"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -35,11 +32,6 @@ vcluster activate my-vcluster --cluster connected-cluster \
Long: description,
Args: loftctlUtil.VClusterNameOnlyValidator,
RunE: func(cobraCmd *cobra.Command, args []string) error {
if config.ShouldCheckForProFeatures() {
cmd.Log.Warnf("In order to use a Pro feature, please contact us at https://www.vcluster.com/pro-demo or downgrade by running `vcluster upgrade --version v0.19.5`")
os.Exit(1)
}

return cmd.Run(cobraCmd.Context(), args)
},
}
Expand Down
1 change: 1 addition & 0 deletions cmd/vclusterctl/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ vcluster create test --namespace test
cobraCmd.Flags().BoolVar(&cmd.Upgrade, "upgrade", false, "If true will try to upgrade the vcluster instead of failing if it already exists")

// Platform flags
cobraCmd.Flags().BoolVar(&cmd.Activate, "activate", true, "[PLATFORM] Activate the vCluster automatically when using helm manager")
cobraCmd.Flags().StringVar(&cmd.Project, "project", "", "[PLATFORM] The vCluster platform project to use")
cobraCmd.Flags().StringSliceVarP(&cmd.Labels, "labels", "l", []string{}, "[PLATFORM] Comma separated labels to apply to the virtualclusterinstance")
cobraCmd.Flags().StringSliceVar(&cmd.Annotations, "annotations", []string{}, "[PLATFORM] Comma separated annotations to apply to the virtualclusterinstance")
Expand Down
7 changes: 0 additions & 7 deletions cmd/vclusterctl/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package cmd

import (
"fmt"
"os"

"github.com/loft-sh/api/v3/pkg/product"
loftctl "github.com/loft-sh/loftctl/v3/cmd/loftctl/cmd"
"github.com/loft-sh/log"
"github.com/loft-sh/vcluster/cmd/vclusterctl/cmd/use"
"github.com/loft-sh/vcluster/config"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/loft-sh/vcluster/pkg/platform"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -46,11 +44,6 @@ vcluster login https://my-vcluster-platform.com --access-key myaccesskey
Long: description,
Args: cobra.MaximumNArgs(1),
RunE: func(cobraCmd *cobra.Command, args []string) error {
if config.ShouldCheckForProFeatures() {
log.GetInstance().Warnf("In order to use a Platform feature, please contact us at https://www.vcluster.com/pro-demo or downgrade by running `vcluster upgrade --version v0.19.5`")
os.Exit(1)
}

loginCmd := &loftctl.LoginCmd{
GlobalFlags: loftGlobalFlags,

Expand Down
7 changes: 0 additions & 7 deletions cmd/vclusterctl/cmd/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package cmd

import (
"fmt"
"os"

loftctl "github.com/loft-sh/loftctl/v3/cmd/loftctl/cmd"
"github.com/loft-sh/log"
"github.com/loft-sh/vcluster/cmd/vclusterctl/cmd/use"
"github.com/loft-sh/vcluster/config"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/loft-sh/vcluster/pkg/platform"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -40,11 +38,6 @@ vcluster logout
Long: description,
Args: cobra.NoArgs,
RunE: func(cobraCmd *cobra.Command, args []string) error {
if config.ShouldCheckForProFeatures() {
cmd.Log.Warnf("In order to use a Pro feature, please contact us at https://www.vcluster.com/pro-demo or downgrade by running `vcluster upgrade --version v0.19.5`")
os.Exit(1)
}

_, err := platform.CreatePlatformClient()
if err != nil {
return err
Expand Down
9 changes: 1 addition & 8 deletions cmd/vclusterctl/cmd/platform/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package platform

import (
"context"
"os"

loftctlUtil "github.com/loft-sh/loftctl/v3/pkg/util"
"github.com/loft-sh/log"
"github.com/loft-sh/vcluster/config"
"github.com/loft-sh/vcluster/pkg/cli"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/loft-sh/vcluster/pkg/platform"
Expand Down Expand Up @@ -43,11 +41,6 @@ vcluster platform import my-vcluster --cluster connected-cluster \
Long: description,
Args: loftctlUtil.VClusterNameOnlyValidator,
RunE: func(cobraCmd *cobra.Command, args []string) error {
if config.ShouldCheckForProFeatures() {
cmd.Log.Warnf("In order to use a Pro feature, please contact us at https://www.vcluster.com/pro-demo or downgrade by running `vcluster upgrade --version v0.19.5`")
os.Exit(1)
}

return cmd.Run(cobraCmd.Context(), args)
},
}
Expand All @@ -73,5 +66,5 @@ func (cmd *ImportCmd) Run(ctx context.Context, args []string) error {
return cli.ActivatePlatform(ctx, &cmd.ActivateOptions, cmd.GlobalFlags, args[0], cmd.Log)
}

return cli.ActivateHelm()
return cli.ActivateHelm(ctx, &cmd.ActivateOptions, cmd.GlobalFlags, args[0], cmd.Log)
}
7 changes: 0 additions & 7 deletions cmd/vclusterctl/cmd/platform/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package platform
import (
"context"
"fmt"
"os"

loftctlflags "github.com/loft-sh/loftctl/v3/cmd/loftctl/flags"
"github.com/loft-sh/loftctl/v3/pkg/start"
"github.com/loft-sh/log"
"github.com/loft-sh/log/survey"
"github.com/loft-sh/log/terminal"
"github.com/loft-sh/vcluster/config"
"github.com/loft-sh/vcluster/pkg/cli/find"
"github.com/loft-sh/vcluster/pkg/platform"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -77,11 +75,6 @@ before running this command:
}

func (cmd *StartCmd) Run(ctx context.Context) error {
if config.ShouldCheckForProFeatures() {
cmd.Log.Warnf("In order to use a Pro feature, please contact us at https://www.vcluster.com/pro-demo or downgrade by running `vcluster upgrade --version v0.19.5`")
os.Exit(1)
}

// get version to deploy
if cmd.Version == "latest" || cmd.Version == "" {
cmd.Version = platform.MinimumVersionTag
Expand Down
7 changes: 0 additions & 7 deletions cmd/vclusterctl/cmd/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package cmd
import (
"errors"
"fmt"
"os"

loftctl "github.com/loft-sh/loftctl/v3/cmd/loftctl/cmd"
"github.com/loft-sh/log"
"github.com/loft-sh/vcluster/config"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/loft-sh/vcluster/pkg/platform"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -40,11 +38,6 @@ vcluster ui
Long: description,
Args: cobra.NoArgs,
RunE: func(cobraCmd *cobra.Command, args []string) error {
if config.ShouldCheckForProFeatures() {
cmd.Log.Warnf("In order to use a Pro feature, please contact us at https://www.vcluster.com/pro-demo or downgrade by running `vcluster upgrade --version v0.19.5`")
os.Exit(1)
}

err := cmd.Run(cobraCmd.Context(), args)
if err != nil {
if errors.Is(err, loftctl.ErrNoUrl) {
Expand Down
5 changes: 0 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"io"
"os"
"reflect"
"regexp"
"strings"
Expand Down Expand Up @@ -133,10 +132,6 @@ func (c *Config) Distro() string {
return K8SDistro
}

func ShouldCheckForProFeatures() bool {
return os.Getenv("FORCE_VCLUSTER_PRO") != "true"
}

func (c *Config) IsProFeatureEnabled() bool {
if len(c.Networking.ResolveDNS) > 0 {
return true
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ require (
github.com/invopop/jsonschema v0.12.0
github.com/kubernetes-csi/external-snapshotter/client/v4 v4.2.0
github.com/loft-sh/admin-apis v0.0.0-20240203010124-3600c1c582a8
github.com/loft-sh/agentapi/v3 v3.4.5
github.com/loft-sh/agentapi/v3 v3.4.6-beta.0
github.com/loft-sh/analytics-client v0.0.0-20240219162240-2f4c64b2494e
github.com/loft-sh/api/v3 v3.4.5
github.com/loft-sh/loftctl/v3 v3.5.0-alpha.0.0.20240425091019-24b84e25c22a
github.com/loft-sh/api/v3 v3.5.0-alpha.1.0.20240426152201-dfdce426ab92
github.com/loft-sh/loftctl/v3 v3.5.0-alpha.1.0.20240430082516-88fe061dbac1
github.com/loft-sh/utils v0.0.29
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d
github.com/mitchellh/go-homedir v1.1.0
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -634,18 +634,18 @@ github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de h1:9TO3cAIGXtEhn
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de/go.mod h1:zAbeS9B/r2mtpb6U+EI2rYA5OAXxsYw6wTamcNW+zcE=
github.com/loft-sh/admin-apis v0.0.0-20240203010124-3600c1c582a8 h1:nuY9Vgvabh2FlaTYp9yhzh3cBzY6jjFEFSsOvw9ZAJw=
github.com/loft-sh/admin-apis v0.0.0-20240203010124-3600c1c582a8/go.mod h1:MWczNwKvWssHo1KaeZKaWDdRLYSNbWqQBGsTLoCNd7U=
github.com/loft-sh/agentapi/v3 v3.4.5 h1:1IUMGU//43w+6xMYcUQZzWbeAwQTq55E9kdorrjrDzA=
github.com/loft-sh/agentapi/v3 v3.4.5/go.mod h1:H6z6N4cxhi6axLQGTIfBbWiFYaWHJIiPBAPywP7LiJ8=
github.com/loft-sh/agentapi/v3 v3.4.6-beta.0 h1:tZeKjzlVjc8KL5rVQyYQQVsX7P4lfwO1GZSNKBdKtrY=
github.com/loft-sh/agentapi/v3 v3.4.6-beta.0/go.mod h1:9lZuxYBmu4gE9Pkeh5+XObMvRpClOTAAdyPeuY5cAiY=
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.5 h1:PAB5Vv4J54jbfwjd5qKO6day1dCaHhRe6n/Gq1zcFsE=
github.com/loft-sh/api/v3 v3.4.5/go.mod h1:GYvob5nqzrApImDtxmPUzF1z21kwE2rtqsm6pu9EqIQ=
github.com/loft-sh/api/v3 v3.5.0-alpha.1.0.20240426152201-dfdce426ab92 h1:Ae2S28oZK48bWYDMv13qN+kmwwIcMa4O17eu/SYnIK4=
github.com/loft-sh/api/v3 v3.5.0-alpha.1.0.20240426152201-dfdce426ab92/go.mod h1:thDZXZ/bR/aDAGLrJvGnohMw1tU/y+OUvr4wSDJrJiQ=
github.com/loft-sh/apiserver v0.0.0-20240129130254-7b9a55ab1744 h1:1Yhs86ugpnEsex4kkFpyvCHNy42O5vtJjY7Loty9JkI=
github.com/loft-sh/apiserver v0.0.0-20240129130254-7b9a55ab1744/go.mod h1:/QBF5drEoeQ0pggzT6q29+cyirRg5/MOJCq39ghku+8=
github.com/loft-sh/jspolicy v0.2.2 h1:+7QqVumYm225mSJsGOjnbCGYzh9XyKYQab6FgLiHYJU=
github.com/loft-sh/jspolicy v0.2.2/go.mod h1:kbXD1CS17Ie9dqcq5iXrOhpSLESkmQpy4+F80YXOfUM=
github.com/loft-sh/loftctl/v3 v3.5.0-alpha.0.0.20240425091019-24b84e25c22a h1:7JtQrD39yYkFvGJ8s9tu+y+qDjxyEM9RIFuLpZmYmmE=
github.com/loft-sh/loftctl/v3 v3.5.0-alpha.0.0.20240425091019-24b84e25c22a/go.mod h1:IUXEhtAFeJj2GED0yyePY3ybpjOs8ZJ4pvkPIJ2/g7U=
github.com/loft-sh/loftctl/v3 v3.5.0-alpha.1.0.20240430082516-88fe061dbac1 h1:ecEb6OmBBs1eF6zxSIH1k+rEaWkqCjuApgbUCkAs220=
github.com/loft-sh/loftctl/v3 v3.5.0-alpha.1.0.20240430082516-88fe061dbac1/go.mod h1:IUXEhtAFeJj2GED0yyePY3ybpjOs8ZJ4pvkPIJ2/g7U=
github.com/loft-sh/log v0.0.0-20240219160058-26d83ffb46ac h1:Gz/7Lb7WgdgIv+KJz87ORA1zvQW52tUqKPGyunlp4dQ=
github.com/loft-sh/log v0.0.0-20240219160058-26d83ffb46ac/go.mod h1:YImeRjXH34Yf5E79T7UHBQpDZl9fIaaFRgyZ/bkY+UQ=
github.com/loft-sh/utils v0.0.29 h1:P/MObccXToAZy2QoJSQDJ+OJx1qHitpFHEVj3QBSNJs=
Expand Down
50 changes: 47 additions & 3 deletions pkg/cli/activate_helm.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
package cli

import "fmt"
import (
"context"
"fmt"

"github.com/loft-sh/log"
"github.com/loft-sh/vcluster/pkg/cli/find"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/loft-sh/vcluster/pkg/lifecycle"
"github.com/loft-sh/vcluster/pkg/platform"
"k8s.io/client-go/kubernetes"
)

type ActivateOptions struct {
Manager string
Expand All @@ -10,6 +20,40 @@ type ActivateOptions struct {
ImportName string
}

func ActivateHelm() error {
return fmt.Errorf("this function is currently not implemented")
func ActivateHelm(ctx context.Context, options *ActivateOptions, globalFlags *flags.GlobalFlags, vClusterName string, log log.Logger) error {
platformClient, err := platform.CreatePlatformClient()
if err != nil {
return err
}

// check if vCluster exists
vCluster, err := find.GetVCluster(ctx, globalFlags.Context, vClusterName, globalFlags.Namespace, log)
if err != nil {
return err
}

// create kube client
restConfig, err := vCluster.ClientFactory.ClientConfig()
if err != nil {
return err
}
kubeClient, err := kubernetes.NewForConfig(restConfig)
if err != nil {
return err
}

// apply platform secret
err = platformClient.ApplyPlatformSecret(ctx, kubeClient, vCluster.Name, vCluster.Namespace, options.Project)
if err != nil {
return err
}

// restart vCluster
err = lifecycle.DeletePods(ctx, kubeClient, "app=vcluster,release="+vCluster.Name, vCluster.Namespace, log)
if err != nil {
return fmt.Errorf("delete vcluster workloads: %w", err)
}

log.Donef("Successfully activated vCluster %s/%s", vCluster.Namespace, vCluster.Name)
return nil
}
27 changes: 23 additions & 4 deletions pkg/cli/create_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/loft-sh/vcluster/pkg/constants"
"github.com/loft-sh/vcluster/pkg/embed"
"github.com/loft-sh/vcluster/pkg/helm"
"github.com/loft-sh/vcluster/pkg/platform"
"github.com/loft-sh/vcluster/pkg/telemetry"
"github.com/loft-sh/vcluster/pkg/upgrade"
"github.com/loft-sh/vcluster/pkg/util"
Expand Down Expand Up @@ -65,6 +66,7 @@ type CreateOptions struct {
Upgrade bool

// Platform
Activate bool
Project string
Cluster string
Template string
Expand Down Expand Up @@ -159,6 +161,7 @@ func CreateHelm(ctx context.Context, options *CreateOptions, globalFlags *flags.
}
// TODO end

// build extra values
var newExtraValues []string
for _, value := range cmd.Values {
decodedString, err := getBase64DecodedString(value)
Expand Down Expand Up @@ -192,13 +195,17 @@ func CreateHelm(ctx context.Context, options *CreateOptions, globalFlags *flags.
}

// Check if the passed in values adhere to our config format.
hasPlatformConfiguration := false
for _, p := range newExtraValues {
f, err := os.Open(p)
if err != nil {
return err
}
defer f.Close()
defer func() {
_ = f.Close()
}()

// parse config
cfg := &config.Config{}
err = cfg.DecodeYAML(f)
if err != nil {
Expand All @@ -208,9 +215,8 @@ func CreateHelm(ctx context.Context, options *CreateOptions, globalFlags *flags.
return err
}

if config.ShouldCheckForProFeatures() && cfg.IsProFeatureEnabled() {
cmd.log.Warnf("In order to use a Pro feature, please contact us at https://www.vcluster.com/pro-demo or downgrade by running `vcluster upgrade --version v0.19.5`")
os.Exit(1)
if cfg.Platform.API.AccessKey != "" || cfg.Platform.API.SecretRef.Name != "" {
hasPlatformConfiguration = true
}

// TODO(johannesfrey): We would also need to validate here if the user is about to perform changes which would lead to distro/store changes
Expand Down Expand Up @@ -259,6 +265,19 @@ func CreateHelm(ctx context.Context, options *CreateOptions, globalFlags *flags.
}
}

// create platform secret
if !hasPlatformConfiguration && cmd.Activate {
platformClient, err := platform.CreatePlatformClient()
if err == nil {
err = platformClient.ApplyPlatformSecret(ctx, cmd.kubeClient, "vcluster-platform-api-key", cmd.Namespace, cmd.Project)
if err != nil {
return fmt.Errorf("apply platform secret: %w", err)
}
} else {
log.Debugf("Error creating platform client: %v", err)
}
}

// we have to upgrade / install the chart
err = cmd.deployChart(ctx, vClusterName, chartValues, helmBinaryPath)
if err != nil {
Expand Down
Loading

0 comments on commit dead007

Please sign in to comment.