Skip to content

Commit

Permalink
Merge "doc/cli - improve handling of 'sf-operator-repository-path' se…
Browse files Browse the repository at this point in the history
…tting"
  • Loading branch information
Microzuul CI authored and Gerrit Code Review committed Jun 11, 2024
2 parents e118982 + 3f6a0da commit 3840f7d
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 54 deletions.
40 changes: 9 additions & 31 deletions cli/cmd/dev/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,46 +105,34 @@ 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)
options.ExtraVarsFile = []string{"@" + varsFile}
// 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 {
Expand All @@ -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 {
Expand All @@ -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")
Expand All @@ -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
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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+"'")
Expand Down
56 changes: 52 additions & 4 deletions cli/cmd/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"reflect"
"strings"

"go.uber.org/zap/zapcore"
"gopkg.in/yaml.v3"

apiv1 "k8s.io/api/core/v1"
Expand All @@ -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"

Expand Down Expand Up @@ -119,17 +121,53 @@ 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
configPath, _ := command.Flags().GetString("config")
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
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down
21 changes: 10 additions & 11 deletions doc/developer/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
7 changes: 1 addition & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.")
Expand Down
2 changes: 0 additions & 2 deletions playbooks/files/sf-operator-cli.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ contexts:
ci:
namespace: sf
fqdn: sfop.me
development:
sf-operator-repository-path: ./
ext-ze:
namespace: ext-ze
default-context: ci

0 comments on commit 3840f7d

Please sign in to comment.