Skip to content

Commit

Permalink
Merge pull request #1221 from loft-sh/thomaskosiewski/eng-2092-vclust…
Browse files Browse the repository at this point in the history
…er-login-logout

Added logout command
  • Loading branch information
FabianKramm authored Sep 22, 2023
2 parents 4167783 + 4501dd1 commit 1fb3fe3
Show file tree
Hide file tree
Showing 25 changed files with 310 additions and 455 deletions.
50 changes: 1 addition & 49 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,43 +71,6 @@ builds:
- -X github.com/loft-sh/vcluster/pkg/telemetry.SyncerVersion={{.Version}}
- -X github.com/loft-sh/vcluster/pkg/telemetry.telemetryPrivateKey={{.Env.TELEMETRY_PRIVATE_KEY}}

- id: vcluster-cli-experimental
env:
- CGO_ENABLED=0
- GO111MODULE=on
goos:
- darwin
- linux
- windows
goarch:
- amd64
- arm64
- arm
goarm:
- "6"
ignore:
- goos: darwin
goarch: arm
- goos: windows
goarch: arm
- goos: windows
goarch: arm64
binary: vcluster
main: ./cmd/vclusterctl
dir: .
flags:
- -trimpath
- -mod
- vendor
tags:
- embed_charts
- pro
ldflags:
- -s -w
- -X main.version={{.Version}}
- -X github.com/loft-sh/vcluster/pkg/telemetry.SyncerVersion={{.Version}}
- -X github.com/loft-sh/vcluster/pkg/telemetry.telemetryPrivateKey={{.Env.TELEMETRY_PRIVATE_KEY}}

archives:
- id: vcluster_cli_archives
format: binary
Expand All @@ -120,17 +83,6 @@ archives:
files:
- README.md
- LICENSE
- id: vcluster_experimental_cli_archives
format: binary
builds:
- vcluster-cli-experimental
name_template: "{{ .ProjectName }}-experimental-{{ .Os }}-{{ .Arch }}"
builds_info:
group: root
owner: root
files:
- README.md
- LICENSE

sboms:
- id: archive_sbom
Expand Down Expand Up @@ -195,7 +147,7 @@ brews:
skip_upload: auto
- name: vcluster-experimental
ids:
- vcluster_experimental_cli_archives
- vcluster_cli_archives
dependencies:
- name: helm
- name: kubernetes-cli
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
//go:build pro
// +build pro

package login
package cmd

import (
"fmt"
Expand All @@ -17,14 +14,17 @@ func NewLoginCmd(globalFlags *flags.GlobalFlags) (*cobra.Command, error) {
loftctlGlobalFlags := &loftctlflags.GlobalFlags{
Silent: globalFlags.Silent,
Debug: globalFlags.Debug,
Config: globalFlags.Config,
LogOutput: globalFlags.LogOutput,
}

var err error
loftctlGlobalFlags.Config, err = pro.GetConfigFilePath()
if err != nil {
return nil, fmt.Errorf("failed to get vcluster pro configuration file path: %w", err)
if globalFlags.Config != "" {
loftctlGlobalFlags.Config = globalFlags.Config
} else {
var err error
loftctlGlobalFlags.Config, err = pro.GetLoftConfigFilePath()
if err != nil {
return nil, fmt.Errorf("failed to get vcluster pro configuration file path: %w", err)
}
}

loginCmd := loftctl.NewLoginCmd(loftctlGlobalFlags)
Expand Down
13 changes: 0 additions & 13 deletions cmd/vclusterctl/cmd/login/notpro.go

This file was deleted.

42 changes: 42 additions & 0 deletions cmd/vclusterctl/cmd/logout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cmd

import (
"fmt"

loftctl "github.com/loft-sh/loftctl/v3/cmd/loftctl/cmd"
loftctlflags "github.com/loft-sh/loftctl/v3/cmd/loftctl/flags"
"github.com/loft-sh/vcluster/cmd/vclusterctl/flags"
"github.com/loft-sh/vcluster/pkg/pro"
"github.com/spf13/cobra"
)

func NewLogoutCmd(globalFlags *flags.GlobalFlags) (*cobra.Command, error) {
loftctlGlobalFlags := &loftctlflags.GlobalFlags{
Silent: globalFlags.Silent,
Debug: globalFlags.Debug,
LogOutput: globalFlags.LogOutput,
}

if globalFlags.Config != "" {
loftctlGlobalFlags.Config = globalFlags.Config
} else {
var err error
loftctlGlobalFlags.Config, err = pro.GetLoftConfigFilePath()
if err != nil {
return nil, fmt.Errorf("failed to get vcluster pro configuration file path: %w", err)
}
}

logoutCmd := loftctl.NewLogoutCmd(loftctlGlobalFlags)

logoutCmd.Use = "logout"
logoutCmd.Long = `########################################################
Log out of vCluster.Pro
Example:
vcluster logout
########################################################
`

return logoutCmd, nil
}
13 changes: 0 additions & 13 deletions cmd/vclusterctl/cmd/pro/notpro.go

This file was deleted.

37 changes: 25 additions & 12 deletions cmd/vclusterctl/cmd/pro/pro.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
//go:build pro
// +build pro

package pro

import (
"fmt"

loftctl "github.com/loft-sh/loftctl/v3/cmd/loftctl/cmd"
loftctlreset "github.com/loft-sh/loftctl/v3/cmd/loftctl/cmd/reset"
loftctlflags "github.com/loft-sh/loftctl/v3/cmd/loftctl/flags"
"github.com/loft-sh/log"
"github.com/loft-sh/vcluster/cmd/vclusterctl/flags"
Expand All @@ -28,34 +26,44 @@ func NewProCmd(globalFlags *flags.GlobalFlags) (*cobra.Command, error) {
loftctlGlobalFlags := &loftctlflags.GlobalFlags{
Silent: globalFlags.Silent,
Debug: globalFlags.Debug,
Config: globalFlags.Config,
LogOutput: globalFlags.LogOutput,
}

if globalFlags.Config != "" {
loftctlGlobalFlags.Config = globalFlags.Config
} else {
var err error
loftctlGlobalFlags.Config, err = pro.GetLoftConfigFilePath()
if err != nil {
return nil, fmt.Errorf("failed to get vcluster pro configuration file path: %w", err)
}
}

startCmd, err := NewStartCmd(loftctlGlobalFlags)
if err != nil {
return nil, fmt.Errorf("failed to create vcluster pro start command: %w", err)
}

proCmd.AddCommand(startCmd)
proCmd.AddCommand(loftctlreset.NewResetCmd(loftctlGlobalFlags))

return proCmd, nil
}

func NewStartCmd(loftctlGlobalFlags *loftctlflags.GlobalFlags) (*cobra.Command, error) {
starCmd := loftctl.NewStartCmd(loftctlGlobalFlags)

configPath, err := pro.GetConfigFilePath()
err := starCmd.Flags().Set("product", "vcluster-pro")
if err != nil {
return nil, fmt.Errorf("failed to get vcluster pro configuration file path: %w", err)
return nil, fmt.Errorf("failed to set product flag: %w", err)
}

starCmd.Flags().Set("config", configPath)

starCmd.Flags().Set("product", "vcluster-pro")
starCmd.Flags().Set("chart-name", "vcluster-control-plane")
err = starCmd.Flags().Set("chart-name", "vcluster-control-plane")
if err != nil {
return nil, fmt.Errorf("failed to set chart-name flag: %w", err)
}

starCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
starCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
version := pro.MinimumVersionTag

latestVersion, err := pro.LatestCompatibleVersion(cmd.Context())
Expand All @@ -65,7 +73,12 @@ func NewStartCmd(loftctlGlobalFlags *loftctlflags.GlobalFlags) (*cobra.Command,
version = latestVersion
}

starCmd.Flags().Set("version", version)
err = starCmd.Flags().Set("version", version)
if err != nil {
return fmt.Errorf("failed to set version flag: %w", err)
}

return nil
}

return starCmd, nil
Expand Down
17 changes: 17 additions & 0 deletions cmd/vclusterctl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,23 @@ func BuildRoot(log log.Logger) (*cobra.Command, error) {
rootCmd.AddCommand(proCmd)
}

loginCmd, err := NewLoginCmd(globalFlags)
if err != nil {
return nil, fmt.Errorf("failed to create login command: %w", err)
}
if loginCmd != nil {
rootCmd.AddCommand(loginCmd)
}

logoutCmd, err := NewLogoutCmd(globalFlags)
if err != nil {
return nil, fmt.Errorf("failed to create logout command: %w", err)
}
if logoutCmd != nil {
rootCmd.AddCommand(logoutCmd)
}

// add completion command
err = rootCmd.RegisterFlagCompletionFunc("namespace", newNamespaceCompletionFunc(rootCmd.Context()))
if err != nil {
return rootCmd, fmt.Errorf("failed to register completion for namespace: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/hashicorp/golang-lru/v2 v2.0.2
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213
github.com/kubernetes-csi/external-snapshotter/client/v4 v4.2.0
github.com/loft-sh/loftctl/v3 v3.0.0-20230921095454-4a09cdaacca3
github.com/loft-sh/loftctl/v3 v3.0.0-20230921143437-669b265e3ecf
github.com/loft-sh/utils v0.0.25
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d
github.com/mitchellh/go-homedir v1.1.0
Expand Down Expand Up @@ -76,7 +76,7 @@ require (
github.com/google/gnostic-models v0.6.8 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
github.com/loft-sh/agentapi/v3 v3.3.0-ci.1.0.20230921083523-e1d74f6f8fd1 // indirect
github.com/loft-sh/api/v3 v3.3.0-alpha.21 // indirect
github.com/loft-sh/api/v3 v3.0.0-20230921143328-114580f85fdd // indirect
github.com/loft-sh/apiserver v0.0.0-20230628051307-f26967fbb40f // indirect
github.com/loft-sh/external-types v0.0.2-0.20230301201552-ec939da949b4 // indirect
github.com/loft-sh/jspolicy v0.1.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -550,16 +550,16 @@ 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/agentapi/v3 v3.3.0-ci.1.0.20230921083523-e1d74f6f8fd1 h1:XE+JbvcExJd45Oh3Jm2d62uPmDP2luPf0mQ3NZZMMuY=
github.com/loft-sh/agentapi/v3 v3.3.0-ci.1.0.20230921083523-e1d74f6f8fd1/go.mod h1:8jmVWFBcLjwh6Tv2KPVxkIjUJpVxUMROcbYYVYxR1pI=
github.com/loft-sh/api/v3 v3.3.0-alpha.21 h1:akI1V79u/aPePeQue6UsAff67pvS6ok0/NxbmND8wHw=
github.com/loft-sh/api/v3 v3.3.0-alpha.21/go.mod h1:eyXGuTGTpJmWAlmXM1QQMHwFc6t9jhj+imrrPdj9syA=
github.com/loft-sh/api/v3 v3.0.0-20230921143328-114580f85fdd h1:8UQwV6TK1QAHlsdrL8we59IKrPH1z8nSO6+mI2lziXs=
github.com/loft-sh/api/v3 v3.0.0-20230921143328-114580f85fdd/go.mod h1:eIbt+Ze6F2fW651JE3VTlFsool+S/hX603aFG5s/AZg=
github.com/loft-sh/apiserver v0.0.0-20230628051307-f26967fbb40f h1:eeLBYlrGOvzJK3h7JYdBGpRvdjzD/sTCVPL2lIQsXlg=
github.com/loft-sh/apiserver v0.0.0-20230628051307-f26967fbb40f/go.mod h1:Mlbl/QTA48cbKpQcmYMA56TQ+Z6Nna7t7ocbCoCmf5w=
github.com/loft-sh/external-types v0.0.2-0.20230301201552-ec939da949b4 h1:eSuBR+6j7MWmab6AU8WGd4EYQiRCZKupxThUxqf38C8=
github.com/loft-sh/external-types v0.0.2-0.20230301201552-ec939da949b4/go.mod h1:qnerIOkD67CmTkV8HsXQit6BfB7Qiw45bSUy1oefIXU=
github.com/loft-sh/jspolicy v0.1.0 h1:FNAWR6tRX5NRWxAf9RI/86NRH83NXYbobLHSZyMND74=
github.com/loft-sh/jspolicy v0.1.0/go.mod h1:4Zi38iEB0JvhnkrNHPpoueSUWQ1OlHMNB9JHTGEsPO0=
github.com/loft-sh/loftctl/v3 v3.0.0-20230921095454-4a09cdaacca3 h1:NVjFE4y/QD2Ol3tkdhTnv+iV8baktqGmxodpr2vZVQo=
github.com/loft-sh/loftctl/v3 v3.0.0-20230921095454-4a09cdaacca3/go.mod h1:iN3Q75vfQDNJrJLKK7YmPVYJhNGPZcV7J0fbsrosnQA=
github.com/loft-sh/loftctl/v3 v3.0.0-20230921143437-669b265e3ecf h1:SwIal5P1coM01bw3OYz0PLokpu4iTaYOMlPH1WfWBjk=
github.com/loft-sh/loftctl/v3 v3.0.0-20230921143437-669b265e3ecf/go.mod h1:I+dMG4zKZWOn+CcteN0cyPGOclHlqqmXBC6THcUwj60=
github.com/loft-sh/log v0.0.0-20230824104949-bd516c25712a h1:/gqqjKpcHEdFXIX41lx1Y/FBqT/72gbPpf7sa20tyM8=
github.com/loft-sh/log v0.0.0-20230824104949-bd516c25712a/go.mod h1:YImeRjXH34Yf5E79T7UHBQpDZl9fIaaFRgyZ/bkY+UQ=
github.com/loft-sh/utils v0.0.25 h1:JbbRJfXO1Rd34fQcaoDSmwyPBEzsrLwBSR21C90hHuk=
Expand Down
21 changes: 15 additions & 6 deletions pkg/pro/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (
"path/filepath"
"time"

"github.com/loft-sh/loftctl/v3/pkg/client"
"github.com/loft-sh/vcluster/pkg/util/cliconfig"
homedir "github.com/mitchellh/go-homedir"
)

const (
VclusterProFolder = "pro"

LoftctlConfigFileName = "creds.json"
)

var (
Expand All @@ -25,8 +26,6 @@ var (

// CLIConfig is the config of the CLI
type CLIConfig struct {
*client.Config `json:",inline"`

LatestVersion string `json:"latestVersion,omitempty"`
LatestCheckAt time.Time `json:"latestCheck,omitempty"`
}
Expand All @@ -36,8 +35,18 @@ func getDefaultCLIConfig() *CLIConfig {
return &CLIConfig{}
}

// GetLoftConfigFilePath returns the path to the loft config file
func GetLoftConfigFilePath() (string, error) {
home, err := homedir.Dir()
if err != nil {
return "", fmt.Errorf("failed to open vcluster pro configuration file from, unable to detect $HOME directory, falling back to default configuration, following error occurred: %w", err)
}

return filepath.Join(home, cliconfig.VclusterFolder, VclusterProFolder, LoftctlConfigFileName), nil
}

// getConfigFilePath returns the path to the config file
func GetConfigFilePath() (string, error) {
func getConfigFilePath() (string, error) {
home, err := homedir.Dir()
if err != nil {
return "", fmt.Errorf("failed to open vcluster pro configuration file from, unable to detect $HOME directory, falling back to default configuration, following error occurred: %w", err)
Expand All @@ -48,7 +57,7 @@ func GetConfigFilePath() (string, error) {

// GetConfig returns the config from the config file
func GetConfig() (*CLIConfig, error) {
path, err := GetConfigFilePath()
path, err := getConfigFilePath()
if err != nil {
return getDefaultCLIConfig(), fmt.Errorf("failed to get vcluster pro configuration file path: %w", err)
}
Expand Down Expand Up @@ -80,7 +89,7 @@ func GetConfig() (*CLIConfig, error) {

// WriteConfig writes the given config to the config file
func WriteConfig(c *CLIConfig) error {
path, err := GetConfigFilePath()
path, err := getConfigFilePath()
if err != nil {
return fmt.Errorf("failed to get vcluster configuration file path: %w", err)
}
Expand Down
Loading

0 comments on commit 1fb3fe3

Please sign in to comment.