Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EVEREST-1512 | Allow running upgrade CLI using in-cluster config #800

Merged
merged 6 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion commands/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func initUpgradeFlags(cmd *cobra.Command) {
cmd.Flags().String("version-metadata-url", "https://check.percona.com", "URL to retrieve version metadata information from")
cmd.Flags().BoolP("logs", "l", false, "If set, logs are printed during the upgrade process")
cmd.Flags().Bool("dry-run", false, "If set, only executes the pre-upgrade checks")

cmd.Flags().Bool("in-cluster", false, "If set, uses the in-cluster Kubernetes client configuration")
cmd.Flags().Bool(upgrade.FlagSkipEnvDetection, false, "Skip detecting Kubernetes environment where Everest is installed")
cmd.Flags().String(upgrade.FlagCatalogNamespace, kubernetes.OLMNamespace,
fmt.Sprintf("Namespace where Everest OLM catalog is installed. Implies --%s", upgrade.FlagSkipEnvDetection),
Expand All @@ -88,6 +88,7 @@ func initUpgradeViperFlags(cmd *cobra.Command) {
viper.BindPFlag("verbose", cmd.Flags().Lookup("verbose")) //nolint:errcheck,gosec
viper.BindPFlag("json", cmd.Flags().Lookup("json")) //nolint:errcheck,gosec
viper.BindPFlag("dry-run", cmd.Flags().Lookup("dry-run")) //nolint:errcheck,gosec
viper.BindPFlag("in-cluster", cmd.Flags().Lookup("in-cluster")) //nolint:errcheck,gosec

viper.BindPFlag(upgrade.FlagSkipEnvDetection, cmd.Flags().Lookup(upgrade.FlagSkipEnvDetection)) //nolint:errcheck,gosec
viper.BindPFlag(upgrade.FlagSkipOLM, cmd.Flags().Lookup(upgrade.FlagSkipOLM)) //nolint:errcheck,gosec
Expand Down
41 changes: 29 additions & 12 deletions pkg/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ type (
Config struct {
// KubeconfigPath is a path to a kubeconfig
KubeconfigPath string `mapstructure:"kubeconfig"`
// InCluster is set if the upgrade process should use in-cluster configuration.
InCluster bool `mapstructure:"in-cluster"`
// VersionMetadataURL stores hostname to retrieve version metadata information from.
VersionMetadataURL string `mapstructure:"version-metadata-url"`
// DryRun is set if the upgrade process should only perform pre-upgrade checks and not perform the actual upgrade.
Expand Down Expand Up @@ -133,17 +135,32 @@ func NewUpgrade(cfg *Config, l *zap.SugaredLogger) (*Upgrade, error) {
cli.l = zap.NewNop().Sugar()
}

k, err := kubernetes.New(cfg.KubeconfigPath, cli.l)
if err != nil {
var u *url.Error
if errors.As(err, &u) {
l.Error("Could not connect to Kubernetes. " +
"Make sure Kubernetes is running and is accessible from this computer/server.")
var kubeClient kubernetes.KubernetesConnector
if cfg.InCluster {
k, err := kubernetes.NewInCluster(cli.l)
if err != nil {
return nil, fmt.Errorf("could not create in-cluster kubernetes client: %w", err)
}
kubeClient = k
}
if cfg.KubeconfigPath != "" {
k, err := kubernetes.New(cfg.KubeconfigPath, cli.l)
if err != nil {
var u *url.Error
if errors.As(err, &u) {
l.Error("Could not connect to Kubernetes. " +
"Make sure Kubernetes is running and is accessible from this computer/server.")
}
return nil, err
}
return nil, err
kubeClient = k
}
if kubeClient == nil {
return nil, errors.New("must provide kubeconfig path or run in-cluster")
}

cli.dryRun = cfg.DryRun
cli.kubeClient = k
cli.kubeClient = kubeClient
cli.versionService = versionservice.New(cfg.VersionMetadataURL)
return cli, nil
}
Expand Down Expand Up @@ -174,6 +191,10 @@ func (u *Upgrade) Run(ctx context.Context) error {
return err
}

if u.dryRun {
return nil
}

if !u.config.SkipEnvDetection {
// Catalog namespace or Skip OLM implies disabled environment detection.
if u.config.SkipOLM || u.config.CatalogNamespace != kubernetes.OLMNamespace {
Expand All @@ -193,10 +214,6 @@ func (u *Upgrade) Run(ctx context.Context) error {
u.config.CatalogNamespace = env.CatalogNamespace
}

if u.dryRun {
return nil
}

upgradeSteps := []common.Step{}

// Start upgrade.
Expand Down
Loading