Skip to content

Commit

Permalink
Merge branch 'main' into EVEREST-1210-edit-advanced-configuration-ove…
Browse files Browse the repository at this point in the history
…rview-page
  • Loading branch information
solovevayaroslavna authored Oct 29, 2024
2 parents c5504b1 + e330f7e commit f84096a
Show file tree
Hide file tree
Showing 17 changed files with 342 additions and 40 deletions.
17 changes: 15 additions & 2 deletions commands/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
package commands

import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"

"github.com/percona/everest/pkg/install"
"github.com/percona/everest/pkg/kubernetes"
"github.com/percona/everest/pkg/output"
)

Expand Down Expand Up @@ -78,6 +80,12 @@ func initInstallFlags(cmd *cobra.Command) {
cmd.Flags().Bool(install.FlagOperatorMongoDB, true, "Install MongoDB operator")
cmd.Flags().Bool(install.FlagOperatorPostgresql, true, "Install PostgreSQL operator")
cmd.Flags().Bool(install.FlagOperatorXtraDBCluster, true, "Install XtraDB Cluster operator")

cmd.Flags().Bool(install.FlagSkipEnvDetection, false, "Skip detecting Kubernetes environment where Everest is installed")
cmd.Flags().String(install.FlagCatalogNamespace, kubernetes.OLMNamespace,
fmt.Sprintf("Namespace where Everest OLM catalog is installed. Implies --%s", install.FlagSkipEnvDetection),
)
cmd.Flags().Bool(install.FlagSkipOLM, false, fmt.Sprintf("Skip OLM installation. Implies --%s", install.FlagSkipEnvDetection))
}

func initInstallViperFlags(cmd *cobra.Command) {
Expand All @@ -93,6 +101,11 @@ func initInstallViperFlags(cmd *cobra.Command) {
viper.BindPFlag(install.FlagOperatorMongoDB, cmd.Flags().Lookup(install.FlagOperatorMongoDB)) //nolint:errcheck,gosec
viper.BindPFlag(install.FlagOperatorPostgresql, cmd.Flags().Lookup(install.FlagOperatorPostgresql)) //nolint:errcheck,gosec
viper.BindPFlag(install.FlagOperatorXtraDBCluster, cmd.Flags().Lookup(install.FlagOperatorXtraDBCluster)) //nolint:errcheck,gosec
viper.BindPFlag("verbose", cmd.Flags().Lookup("verbose")) //nolint:errcheck,gosec
viper.BindPFlag("json", cmd.Flags().Lookup("json")) //nolint:errcheck,gosec

viper.BindPFlag(install.FlagSkipEnvDetection, cmd.Flags().Lookup(install.FlagSkipEnvDetection)) //nolint:errcheck,gosec
viper.BindPFlag(install.FlagSkipOLM, cmd.Flags().Lookup(install.FlagSkipOLM)) //nolint:errcheck,gosec
viper.BindPFlag(install.FlagCatalogNamespace, cmd.Flags().Lookup(install.FlagCatalogNamespace)) //nolint:errcheck,gosec

viper.BindPFlag("verbose", cmd.Flags().Lookup("verbose")) //nolint:errcheck,gosec
viper.BindPFlag("json", cmd.Flags().Lookup("json")) //nolint:errcheck,gosec
}
12 changes: 12 additions & 0 deletions commands/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
package commands

import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"

"github.com/percona/everest/pkg/kubernetes"
"github.com/percona/everest/pkg/output"
"github.com/percona/everest/pkg/uninstall"
)
Expand Down Expand Up @@ -64,6 +66,12 @@ func newUninstallCmd(l *zap.SugaredLogger) *cobra.Command {
func initUninstallFlags(cmd *cobra.Command) {
cmd.Flags().BoolP("assume-yes", "y", false, "Assume yes to all questions")
cmd.Flags().BoolP("force", "f", false, "Force removal in case there are database clusters running")

cmd.Flags().Bool(uninstall.FlagSkipEnvDetection, false, "Skip detecting Kubernetes environment where Everest is installed")
cmd.Flags().String(uninstall.FlagCatalogNamespace, kubernetes.OLMNamespace,
fmt.Sprintf("Namespace where Everest OLM catalog is installed. Implies --%s", uninstall.FlagSkipEnvDetection),
)
cmd.Flags().Bool(uninstall.FlagSkipOLM, false, fmt.Sprintf("Skip OLM uninstallation. Implies --%s", uninstall.FlagSkipEnvDetection))
}

func initUninstallViperFlags(cmd *cobra.Command) {
Expand All @@ -73,6 +81,10 @@ func initUninstallViperFlags(cmd *cobra.Command) {
viper.BindPFlag("force", cmd.Flags().Lookup("force")) //nolint:errcheck,gosec
viper.BindPFlag("verbose", cmd.Flags().Lookup("verbose")) //nolint:errcheck,gosec
viper.BindPFlag("json", cmd.Flags().Lookup("json")) //nolint:errcheck,gosec

viper.BindPFlag(uninstall.FlagSkipEnvDetection, cmd.Flags().Lookup(uninstall.FlagSkipEnvDetection)) //nolint:errcheck,gosec
viper.BindPFlag(uninstall.FlagSkipOLM, cmd.Flags().Lookup(uninstall.FlagSkipOLM)) //nolint:errcheck,gosec
viper.BindPFlag(uninstall.FlagCatalogNamespace, cmd.Flags().Lookup(uninstall.FlagCatalogNamespace)) //nolint:errcheck,gosec
}

func parseClusterConfig() (*uninstall.Config, error) {
Expand Down
12 changes: 12 additions & 0 deletions commands/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
package commands

import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"

"github.com/percona/everest/pkg/kubernetes"
"github.com/percona/everest/pkg/output"
"github.com/percona/everest/pkg/upgrade"
)
Expand Down Expand Up @@ -71,6 +73,12 @@ 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(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),
)
cmd.Flags().Bool(upgrade.FlagSkipOLM, false, fmt.Sprintf("Skip OLM upgrade. Implies --%s", upgrade.FlagSkipEnvDetection))
}

func initUpgradeViperFlags(cmd *cobra.Command) {
Expand All @@ -80,6 +88,10 @@ 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(upgrade.FlagSkipEnvDetection, cmd.Flags().Lookup(upgrade.FlagSkipEnvDetection)) //nolint:errcheck,gosec
viper.BindPFlag(upgrade.FlagSkipOLM, cmd.Flags().Lookup(upgrade.FlagSkipOLM)) //nolint:errcheck,gosec
viper.BindPFlag(upgrade.FlagCatalogNamespace, cmd.Flags().Lookup(upgrade.FlagCatalogNamespace)) //nolint:errcheck,gosec
}

func parseUpgradeConfig() (*upgrade.Config, error) {
Expand Down
1 change: 0 additions & 1 deletion data/crds/olm/everest-catalog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ apiVersion: operators.coreos.com/v1alpha1
kind: CatalogSource
metadata:
name: everest-catalog
namespace: everest-olm
spec:
displayName: Everest Catalog
publisher: Percona
Expand Down
62 changes: 56 additions & 6 deletions pkg/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ const (
FlagVersion = "version"
// FlagSkipWizard represents the flag to skip the installation wizard.
FlagSkipWizard = "skip-wizard"
// FlagCatalogNamespace is the name of the catalog namespace flag.
FlagCatalogNamespace = "catalog-namespace"
// FlagSkipEnvDetection is the name of the skip env detection flag.
FlagSkipEnvDetection = "skip-env-detection"
// FlagSkipOLM is the name of the skip OLM flag.
FlagSkipOLM = "skip-olm"
// FlagDisableTelemetry disables telemetry.
FlagDisableTelemetry = "disable-telemetry"
)
Expand Down Expand Up @@ -149,6 +155,10 @@ type (
// DisableTelemetry disables telemetry.
DisableTelemetry bool `mapstructure:"disable-telemetry"`

SkipEnvDetection bool `mapstructure:"skip-env-detection"`
SkipOLM bool `mapstructure:"skip-olm"`
CatalogNamespace string `mapstructure:"catalog-namespace"`

Operator OperatorConfig

// If set, we will print the pretty output.
Expand Down Expand Up @@ -192,7 +202,7 @@ func NewInstall(c Config, l *zap.SugaredLogger, cmd *cobra.Command) (*Install, e
}

// Run runs the operators installation process.
func (o *Install) Run(ctx context.Context) error {
func (o *Install) Run(ctx context.Context) error { //nolint:funlen
// TODO: we shall probably split this into "install" and "add namespaces"
// Otherwise the logic is hard to maintain - we need to make sure not to,
// for example, install a different version of operators per namespace, if
Expand Down Expand Up @@ -229,7 +239,21 @@ func (o *Install) Run(ctx context.Context) error {
o.l.Debugf("Everest latest version available: %s", latest)
o.l.Debugf("Everest version information %#v", latestMeta)

installSteps = append(installSteps, o.provisionOLM(latest)...)
if o.config.SkipEnvDetection {
o.l.Debug("Skipping Kubernetes environment detection")
} else {
if err := o.detectEnvironment(ctx); err != nil {
return fmt.Errorf("could not detect Kubernetes environment: %w", err)
}
}

if o.config.SkipOLM {
o.l.Debug("Skipping OLM installation")
} else {
installSteps = append(installSteps, o.provisionOLM()...)
}

installSteps = append(installSteps, o.installCatalog(latest)...)
installSteps = append(installSteps, o.provisionMonitoringStack()...)
installSteps = append(installSteps, o.provisionEverestComponents(latest, recVer)...)

Expand Down Expand Up @@ -283,6 +307,13 @@ func (o *Install) populateConfig() error {
return ErrNoOperatorsSelected
}

if !o.config.SkipEnvDetection {
if o.config.CatalogNamespace != kubernetes.OLMNamespace || o.config.SkipOLM {
// Catalog namespace or Skip OLM implies disabled environment detection.
o.config.SkipEnvDetection = true
}
}

return nil
}

Expand Down Expand Up @@ -365,7 +396,7 @@ func (o *Install) installVMOperator(ctx context.Context) error {
Name: vmOperatorName,
OperatorGroup: monitoringOperatorGroup,
CatalogSource: catalogSource,
CatalogSourceNamespace: kubernetes.OLMNamespace,
CatalogSourceNamespace: o.config.CatalogNamespace,
Channel: vmOperatorChannel,
InstallPlanApproval: v1alpha1.ApprovalManual,
}
Expand Down Expand Up @@ -669,7 +700,21 @@ func (o *Install) createNamespace(ctx context.Context, namespace string) error {
return nil
}

func (o *Install) provisionOLM(v *goversion.Version) []common.Step {
func (o *Install) detectEnvironment(ctx context.Context) error {
o.l.Info("Detecting Kubernetes environment")
env, err := o.kubeClient.DetectEnvironment(ctx)
if err != nil {
return err
}

o.l.Infof("Detected %s Kubernetes environment", env.Env)
o.config.CatalogNamespace = env.CatalogNamespace
o.config.SkipOLM = env.SkipOLM

return nil
}

func (o *Install) provisionOLM() []common.Step {
result := []common.Step{}
result = append(result, common.Step{
Desc: "Install Operator Lifecycle Manager",
Expand All @@ -685,10 +730,15 @@ func (o *Install) provisionOLM(v *goversion.Version) []common.Step {
},
})

return result
}

func (o *Install) installCatalog(v *goversion.Version) []common.Step {
result := []common.Step{}
result = append(result, common.Step{
Desc: "Install Percona OLM Catalog",
F: func(ctx context.Context) error {
if err := o.kubeClient.InstallPerconaCatalog(ctx, v); err != nil {
if err := o.kubeClient.InstallPerconaCatalog(ctx, v, o.config.CatalogNamespace); err != nil {
o.l.Errorf("failed installing OLM catalog: %v", err)
return err
}
Expand Down Expand Up @@ -759,7 +809,7 @@ func (o *Install) installOperator(ctx context.Context, channel, operatorName, na
Name: operatorName,
OperatorGroup: systemOperatorGroup,
CatalogSource: catalogSource,
CatalogSourceNamespace: kubernetes.OLMNamespace,
CatalogSourceNamespace: o.config.CatalogNamespace,
Channel: channel,
InstallPlanApproval: v1alpha1.ApprovalManual,
StartingCSV: startingCSV,
Expand Down
8 changes: 8 additions & 0 deletions pkg/kubernetes/catalog_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package kubernetes

import "context"

// DeleteCatalogSource deletes a catalog source.
func (k *Kubernetes) DeleteCatalogSource(ctx context.Context, namespace, name string) error {
return k.client.DeleteCatalogSource(ctx, namespace, name)
}
12 changes: 12 additions & 0 deletions pkg/kubernetes/client/catalog_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package client

import (
"context"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// DeleteCatalogSource deletes a catalog source.
func (c *Client) DeleteCatalogSource(ctx context.Context, namespace string, name string) error {
return c.olmClientset.OperatorsV1alpha1().CatalogSources(namespace).Delete(ctx, name, metav1.DeleteOptions{})
}
2 changes: 1 addition & 1 deletion pkg/kubernetes/client/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@

package client

//go:generate ../../../bin/ifacemaker -f backup_storage.go -f configmap.go -f client.go -f cluster_service_version.go -f ctl.go -f database_cluster.go -f database_cluster_backup.go -f database_cluster_restore.go -f database_engine.go -f deployment.go -f install_plan.go -f monitoring.go -f monitoring_config.go -f namespace.go -f olm.go -f node.go -f pod.go -f secret.go -f storage.go -f writer -s Client -i KubeClientConnector -p client -o kubeclient_interface.go
//go:generate ../../../bin/ifacemaker -f backup_storage.go -f catalog_source.go -f configmap.go -f client.go -f cluster_service_version.go -f ctl.go -f database_cluster.go -f database_cluster_backup.go -f database_cluster_restore.go -f database_engine.go -f deployment.go -f install_plan.go -f monitoring.go -f monitoring_config.go -f namespace.go -f olm.go -f node.go -f pod.go -f secret.go -f storage.go -f writer -s Client -i KubeClientConnector -p client -o kubeclient_interface.go
//go:generate ../../../bin/mockery --name=KubeClientConnector --case=snake --inpackage
2 changes: 2 additions & 0 deletions pkg/kubernetes/client/kubeclient_interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions pkg/kubernetes/client/mock_kube_client_connector.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions pkg/kubernetes/environment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package kubernetes

import (
"context"

k8serrors "k8s.io/apimachinery/pkg/api/errors"
)

// K8sEnv represents type of Kubernetes environment.
type K8sEnv string

const (
// EnvStandard is a standard k8s environment.
EnvStandard K8sEnv = "Standard"
// EnvOpenShift is an OpenShift k8s environment.
EnvOpenShift K8sEnv = "OpenShift"

// OpenShiftCatalogNamespace is a namespace where catalogs in an OpenShift cluster are stored.
OpenShiftCatalogNamespace = "openshift-marketplace"
)

//nolint:gochecknoglobals
var (
openShiftEnv = &Environment{
Env: EnvOpenShift,
CatalogNamespace: OpenShiftCatalogNamespace,
SkipOLM: true,
}
standardEnv = &Environment{
Env: EnvStandard,
CatalogNamespace: OLMNamespace,
SkipOLM: false,
}
)

// Environment represents a Kubernetes environment.
type Environment struct {
Env K8sEnv
CatalogNamespace string
SkipOLM bool
}

// DetectEnvironment detects Kubernetes environment.
func (k *Kubernetes) DetectEnvironment(ctx context.Context) (*Environment, error) {
env, ok, err := k.detectOpenShift(ctx)
if err != nil {
return nil, err
}
if ok {
return env, nil
}
return standardEnv, nil
}

func (k *Kubernetes) detectOpenShift(ctx context.Context) (*Environment, bool, error) {
k.l.Debug("Detecting OpenShift")
_, err := k.GetNamespace(ctx, OpenShiftCatalogNamespace)
if err != nil {
if k8serrors.IsNotFound(err) {
k.l.Info("Detected standard Kubernetes environment")
return nil, false, nil
}
k.l.Info("Failed to detect Kubernetes environment")
return nil, false, err
}

// If the OpenShift Marketplace namespace exists, we consider this an OpenShift cluster.
k.l.Debug("Detected OpenShift Kubernetes environment")
return openShiftEnv, true, nil
}
2 changes: 1 addition & 1 deletion pkg/kubernetes/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@

package kubernetes

//go:generate ../../bin/ifacemaker -f accounts.go -f backup_storage.go -f cluster_service_version.go -f configmap.go -f deployment.go -f install_plan.go -f kubernetes.go -f monitoring_config.go -f namespace.go -f operator.go -f jwt.go -f oidc.go -f secret.go -s Kubernetes -i KubernetesConnector -p kubernetes -o kubernetes_interface.go
//go:generate ../../bin/ifacemaker -f accounts.go -f backup_storage.go -f cluster_service_version.go -f configmap.go -f deployment.go -f environment.go -f install_plan.go -f kubernetes.go -f monitoring_config.go -f namespace.go -f operator.go -f jwt.go -f oidc.go -f secret.go -s Kubernetes -i KubernetesConnector -p kubernetes -o kubernetes_interface.go
//go:generate ../../bin/mockery --name=KubernetesConnector --case=snake --inpackage
Loading

0 comments on commit f84096a

Please sign in to comment.