diff --git a/cli/cmd/dev/dev.go b/cli/cmd/dev/dev.go index 03f6085b..8f65587d 100644 --- a/cli/cmd/dev/dev.go +++ b/cli/cmd/dev/dev.go @@ -105,18 +105,6 @@ func createMicroshift(kmd *cobra.Command, cliCtx cliutils.SoftwareFactoryConfigC msAnsibleMicroshiftRolePath = rootDir + "/ansible-microshift-role" ctrl.Log.Info("No path to ansible-microshift-role provided, the role will be cloned into " + msAnsibleMicroshiftRolePath) } - defaultSFOperatorRepositoryPath, _ := os.Getwd() - msSFOperatorRepositoryPath := cliCtx.Dev.SFOperatorRepositoryPath - if msSFOperatorRepositoryPath == "" { - if defaultSFOperatorRepositoryPath != "" { - msSFOperatorRepositoryPath = defaultSFOperatorRepositoryPath - ctrl.Log.Info("Using current working directory for sf-operator-repository-path: " + msSFOperatorRepositoryPath) - } else { - ctrl.Log.Error(errMissingArg, "The path to the sf-operator repository must be set in `dev` section of the configuration file") - os.Exit(1) - } - } - options := ms.MkAnsiblePlaybookOptions(msHost, msUser, msOpenshiftPullSecret, rootDir) varsFile := ms.MkTemporaryVarsFile( cliCtx.FQDN, msDiskFileSize, msAnsibleMicroshiftRolePath, rootDir, msEtcdOnRamdisk) @@ -124,27 +112,27 @@ func createMicroshift(kmd *cobra.Command, cliCtx cliutils.SoftwareFactoryConfigC // Ensure ansible-microshift-role is available ms.MkMicroshiftRoleSetupPlaybook(rootDir) if !dryRun { - ms.RunMicroshiftRoleSetup(rootDir, msSFOperatorRepositoryPath, msAnsibleMicroshiftRolePath, options) + ms.RunMicroshiftRoleSetup(rootDir, cliCtx.Dev.SFOperatorRepositoryPath, msAnsibleMicroshiftRolePath, options) } // Ensure tooling and prerequisites are installed if !skipLocalSetup { ms.MkLocalSetupPlaybook(rootDir) if !dryRun { - ms.RunLocalSetup(rootDir, msSFOperatorRepositoryPath, msAnsibleMicroshiftRolePath, options) + ms.RunLocalSetup(rootDir, cliCtx.Dev.SFOperatorRepositoryPath, msAnsibleMicroshiftRolePath, options) } } // Deploy MicroShift if !skipDeploy { ms.MkDeployMicroshiftPlaybook(rootDir) if !dryRun { - ms.RunDeploy(rootDir, msSFOperatorRepositoryPath, msAnsibleMicroshiftRolePath, options) + ms.RunDeploy(rootDir, cliCtx.Dev.SFOperatorRepositoryPath, msAnsibleMicroshiftRolePath, options) } } // Configure cluster for development and testing if !skipPostInstall { ms.MkPostInstallPlaybook(rootDir) if !dryRun { - ms.RunPostInstall(rootDir, msSFOperatorRepositoryPath, msAnsibleMicroshiftRolePath, options) + ms.RunPostInstall(rootDir, cliCtx.Dev.SFOperatorRepositoryPath, msAnsibleMicroshiftRolePath, options) } } if !dryRun { @@ -157,7 +145,6 @@ func createMicroshift(kmd *cobra.Command, cliCtx cliutils.SoftwareFactoryConfigC func devRunTests(kmd *cobra.Command, args []string) { cliCtx := cliutils.GetCLIctxOrDie(kmd, args, runTestsAllowedArgs) target := args[0] - sfOperatorRepositoryPath := cliCtx.Dev.SFOperatorRepositoryPath vars, _ := kmd.Flags().GetStringSlice("extra-var") extraVars := cliutils.VarListToMap(vars) if len(extraVars) == 0 { @@ -166,10 +153,6 @@ func devRunTests(kmd *cobra.Command, args []string) { if extraVars == nil { extraVars = make(map[string]string) } - if sfOperatorRepositoryPath == "" { - ctrl.Log.Error(errMissingArg, "The path to the sf-operator repository must be set in `dev` section of the configuration") - os.Exit(1) - } var verbosity string verbose, _ := kmd.Flags().GetBool("v") debug, _ := kmd.Flags().GetBool("vvv") @@ -196,7 +179,7 @@ func devRunTests(kmd *cobra.Command, args []string) { reposPath = "deploy" } extraVars["demo_repos_path"] = reposPath - createDemoEnv(env, restConfig, fqdn, reposPath, sfOperatorRepositoryPath, false) + createDemoEnv(env, restConfig, fqdn, reposPath, cliCtx.Dev.SFOperatorRepositoryPath, false) } // use config file and context for CLI calls in the tests var cliGlobalFlags string @@ -212,11 +195,11 @@ func devRunTests(kmd *cobra.Command, args []string) { } extraVars["cli_global_flags"] = cliGlobalFlags if target == "olm" { - runTestOLM(extraVars, sfOperatorRepositoryPath, verbosity) + runTestOLM(extraVars, cliCtx.Dev.SFOperatorRepositoryPath, verbosity) } else if target == "standalone" { - runTestStandalone(extraVars, sfOperatorRepositoryPath, verbosity) + runTestStandalone(extraVars, cliCtx.Dev.SFOperatorRepositoryPath, verbosity) } else if target == "upgrade" { - runTestUpgrade(extraVars, sfOperatorRepositoryPath, verbosity) + runTestUpgrade(extraVars, cliCtx.Dev.SFOperatorRepositoryPath, verbosity) } } @@ -260,13 +243,8 @@ func devCreate(kmd *cobra.Command, args []string) { ctrl.Log.Info("Demo repos path unset; repos will be cloned into ./deploy") reposPath = "deploy" } - sfOperatorRepositoryPath := cliCtx.Dev.SFOperatorRepositoryPath - if sfOperatorRepositoryPath == "" { - ctrl.Log.Error(errMissingArg, "The path to the sf-operator repository must be set in `dev` section of the configuration") - os.Exit(1) - } keepDemoTenantDefinition, _ := kmd.Flags().GetBool("keep-demo-tenant") - createDemoEnv(env, restConfig, fqdn, reposPath, sfOperatorRepositoryPath, keepDemoTenantDefinition) + createDemoEnv(env, restConfig, fqdn, reposPath, cliCtx.Dev.SFOperatorRepositoryPath, keepDemoTenantDefinition) } else { ctrl.Log.Error(errors.New("unsupported target"), "Invalid argument '"+target+"'") diff --git a/cli/cmd/utils/utils.go b/cli/cmd/utils/utils.go index f3f15626..513a7c16 100644 --- a/cli/cmd/utils/utils.go +++ b/cli/cmd/utils/utils.go @@ -28,6 +28,7 @@ import ( "reflect" "strings" + "go.uber.org/zap/zapcore" "gopkg.in/yaml.v3" apiv1 "k8s.io/api/core/v1" @@ -45,6 +46,7 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/log/zap" monitoring "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" @@ -119,7 +121,42 @@ func getContextFromFile(command *cobra.Command) (ctxName string, cliContext Soft return ctxName, cliContext, errors.New("context not found") } +// SetLogger enables the DEBUG LogLevel in the Logger when the debug flag is set +func SetLogger(command *cobra.Command) { + debug, _ := command.Flags().GetBool("debug") + logLevel := zapcore.InfoLevel + if debug { + logLevel = zapcore.DebugLevel + } + + opts := zap.Options{ + Development: true, + Level: logLevel, + DestWriter: os.Stderr, + } + ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) +} + +// logI logs a message with the INFO log Level +func logI(msg string) { + ctrl.Log.Info(msg) +} + +// logI logs a message with the DEBUG log Level +func logD(msg string) { + ctrl.Log.V(1).Info(msg) +} + +// logI logs a message with the Error log Level +func logE(err error, msg string) { + ctrl.Log.Error(err, msg) +} + func GetCLIContext(command *cobra.Command) (SoftwareFactoryConfigContext, error) { + + // This is usually called for every CLI command so here let's set the Logger settings + SetLogger(command) + var cliContext SoftwareFactoryConfigContext var ctxName string var err error @@ -127,9 +164,10 @@ func GetCLIContext(command *cobra.Command) (SoftwareFactoryConfigContext, error) if configPath != "" { ctxName, cliContext, err = getContextFromFile(command) if err != nil { - ctrl.Log.Error(err, "Could not load config file") + logE(err, "Could not load config file") + os.Exit(1) } else { - ctrl.Log.V(5).Info("Using configuration context " + ctxName) + logD("Using configuration context " + ctxName) } } // Override with defaults @@ -149,6 +187,16 @@ func GetCLIContext(command *cobra.Command) (SoftwareFactoryConfigContext, error) if cliContext.FQDN == "" { cliContext.FQDN = fqdn } + if cliContext.Dev.SFOperatorRepositoryPath == "" { + defaultSFOperatorRepositoryPath, getwdErr := os.Getwd() + if getwdErr != nil { + logE(getwdErr, + "sf-operator-repository-path is not set in `dev` section of the configuration file and unable to determine the current working directory") + os.Exit(1) + } + cliContext.Dev.SFOperatorRepositoryPath = defaultSFOperatorRepositoryPath + logD("Using current working directory for sf-operator-repository-path: " + cliContext.Dev.SFOperatorRepositoryPath) + } return cliContext, nil } @@ -332,8 +380,8 @@ func RunCmdWithEnvOrDie(environ []string, cmd string, args ...string) string { kmd.Env = append(os.Environ(), environ...) out, err := kmd.CombinedOutput() if err != nil { - ctrl.Log.Error(err, "Could not run command '"+cmd+"'") - ctrl.Log.Info("Captured output:\n" + string(out)) + logE(err, "Could not run command '"+cmd+"'") + logI("Captured output:\n" + string(out)) os.Exit(1) } return string(out) diff --git a/doc/developer/getting_started.md b/doc/developer/getting_started.md index 2cae812f..41c4e8c6 100644 --- a/doc/developer/getting_started.md +++ b/doc/developer/getting_started.md @@ -48,19 +48,18 @@ kubectl create namespace sf kubectl config set-context microshift --namespace=sf ``` -[Create a sf-operator configuration file](../reference/cli/index.md#config) to your liking: - -```sh -go run ./main.go init config --dev > /path/to/sfcli.yaml +Ensure the following minimal settings in your sf-operator CLI file: + +```yaml +contexts: + my-context: + namespace: sf + fqdn: sfop.me +default-context: my-context ``` -Then edit it as necessary, for example by setting up a custom FQDN. Below is an example you can copy and edit. - -??? abstract "CLI configuration example" - - ```yaml title="sfcli.yaml" - --8<-- "sfcli.yaml" - ``` +Consult [Create a sf-operator configuration file](../reference/cli/index.md#config) and +[The configuration file schema](../reference/cli/index.md#configuration-file) if needed. Then run the `sf-operator` command: diff --git a/main.go b/main.go index 9b439507..a23f901f 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,6 @@ import ( "os" "github.com/spf13/cobra" - "sigs.k8s.io/controller-runtime/pkg/log/zap" // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) // to ensure that exec-entrypoint and run can make use of them. @@ -58,11 +57,6 @@ func operatorCmd(kmd *cobra.Command, args []string) { } func main() { - opts := zap.Options{ - Development: true, - DestWriter: os.Stderr, - } - ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) var ( metricsAddr string @@ -93,6 +87,7 @@ func main() { rootCmd.PersistentFlags().StringVarP(&fqdn, "fqdn", "d", "", "The FQDN of the deployment (if no manifest is provided).") rootCmd.PersistentFlags().StringVarP(&configFile, "config", "C", "", "Path to the CLI configuration file.") rootCmd.PersistentFlags().StringVarP(&cliContext, "context", "c", "", "Context to use in the configuration file. Defaults to the \"default-context\" value in the config file if set, or the first available context in the config file.") + rootCmd.PersistentFlags().Bool("debug", false, "Enable DEBUG logs") // Flags for the operator command operatorCmd.Flags().StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.") diff --git a/playbooks/files/sf-operator-cli.yaml b/playbooks/files/sf-operator-cli.yaml index 6e6785da..30cb29bb 100644 --- a/playbooks/files/sf-operator-cli.yaml +++ b/playbooks/files/sf-operator-cli.yaml @@ -2,8 +2,6 @@ contexts: ci: namespace: sf fqdn: sfop.me - development: - sf-operator-repository-path: ./ ext-ze: namespace: ext-ze default-context: ci