Skip to content

Commit 3840f7d

Browse files
Microzuul CIGerrit Code Review
Microzuul CI
authored and
Gerrit Code Review
committed
Merge "doc/cli - improve handling of 'sf-operator-repository-path' setting"
2 parents e118982 + 3f6a0da commit 3840f7d

File tree

5 files changed

+72
-54
lines changed

5 files changed

+72
-54
lines changed

cli/cmd/dev/dev.go

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -105,46 +105,34 @@ func createMicroshift(kmd *cobra.Command, cliCtx cliutils.SoftwareFactoryConfigC
105105
msAnsibleMicroshiftRolePath = rootDir + "/ansible-microshift-role"
106106
ctrl.Log.Info("No path to ansible-microshift-role provided, the role will be cloned into " + msAnsibleMicroshiftRolePath)
107107
}
108-
defaultSFOperatorRepositoryPath, _ := os.Getwd()
109-
msSFOperatorRepositoryPath := cliCtx.Dev.SFOperatorRepositoryPath
110-
if msSFOperatorRepositoryPath == "" {
111-
if defaultSFOperatorRepositoryPath != "" {
112-
msSFOperatorRepositoryPath = defaultSFOperatorRepositoryPath
113-
ctrl.Log.Info("Using current working directory for sf-operator-repository-path: " + msSFOperatorRepositoryPath)
114-
} else {
115-
ctrl.Log.Error(errMissingArg, "The path to the sf-operator repository must be set in `dev` section of the configuration file")
116-
os.Exit(1)
117-
}
118-
}
119-
120108
options := ms.MkAnsiblePlaybookOptions(msHost, msUser, msOpenshiftPullSecret, rootDir)
121109
varsFile := ms.MkTemporaryVarsFile(
122110
cliCtx.FQDN, msDiskFileSize, msAnsibleMicroshiftRolePath, rootDir, msEtcdOnRamdisk)
123111
options.ExtraVarsFile = []string{"@" + varsFile}
124112
// Ensure ansible-microshift-role is available
125113
ms.MkMicroshiftRoleSetupPlaybook(rootDir)
126114
if !dryRun {
127-
ms.RunMicroshiftRoleSetup(rootDir, msSFOperatorRepositoryPath, msAnsibleMicroshiftRolePath, options)
115+
ms.RunMicroshiftRoleSetup(rootDir, cliCtx.Dev.SFOperatorRepositoryPath, msAnsibleMicroshiftRolePath, options)
128116
}
129117
// Ensure tooling and prerequisites are installed
130118
if !skipLocalSetup {
131119
ms.MkLocalSetupPlaybook(rootDir)
132120
if !dryRun {
133-
ms.RunLocalSetup(rootDir, msSFOperatorRepositoryPath, msAnsibleMicroshiftRolePath, options)
121+
ms.RunLocalSetup(rootDir, cliCtx.Dev.SFOperatorRepositoryPath, msAnsibleMicroshiftRolePath, options)
134122
}
135123
}
136124
// Deploy MicroShift
137125
if !skipDeploy {
138126
ms.MkDeployMicroshiftPlaybook(rootDir)
139127
if !dryRun {
140-
ms.RunDeploy(rootDir, msSFOperatorRepositoryPath, msAnsibleMicroshiftRolePath, options)
128+
ms.RunDeploy(rootDir, cliCtx.Dev.SFOperatorRepositoryPath, msAnsibleMicroshiftRolePath, options)
141129
}
142130
}
143131
// Configure cluster for development and testing
144132
if !skipPostInstall {
145133
ms.MkPostInstallPlaybook(rootDir)
146134
if !dryRun {
147-
ms.RunPostInstall(rootDir, msSFOperatorRepositoryPath, msAnsibleMicroshiftRolePath, options)
135+
ms.RunPostInstall(rootDir, cliCtx.Dev.SFOperatorRepositoryPath, msAnsibleMicroshiftRolePath, options)
148136
}
149137
}
150138
if !dryRun {
@@ -157,7 +145,6 @@ func createMicroshift(kmd *cobra.Command, cliCtx cliutils.SoftwareFactoryConfigC
157145
func devRunTests(kmd *cobra.Command, args []string) {
158146
cliCtx := cliutils.GetCLIctxOrDie(kmd, args, runTestsAllowedArgs)
159147
target := args[0]
160-
sfOperatorRepositoryPath := cliCtx.Dev.SFOperatorRepositoryPath
161148
vars, _ := kmd.Flags().GetStringSlice("extra-var")
162149
extraVars := cliutils.VarListToMap(vars)
163150
if len(extraVars) == 0 {
@@ -166,10 +153,6 @@ func devRunTests(kmd *cobra.Command, args []string) {
166153
if extraVars == nil {
167154
extraVars = make(map[string]string)
168155
}
169-
if sfOperatorRepositoryPath == "" {
170-
ctrl.Log.Error(errMissingArg, "The path to the sf-operator repository must be set in `dev` section of the configuration")
171-
os.Exit(1)
172-
}
173156
var verbosity string
174157
verbose, _ := kmd.Flags().GetBool("v")
175158
debug, _ := kmd.Flags().GetBool("vvv")
@@ -196,7 +179,7 @@ func devRunTests(kmd *cobra.Command, args []string) {
196179
reposPath = "deploy"
197180
}
198181
extraVars["demo_repos_path"] = reposPath
199-
createDemoEnv(env, restConfig, fqdn, reposPath, sfOperatorRepositoryPath, false)
182+
createDemoEnv(env, restConfig, fqdn, reposPath, cliCtx.Dev.SFOperatorRepositoryPath, false)
200183
}
201184
// use config file and context for CLI calls in the tests
202185
var cliGlobalFlags string
@@ -212,11 +195,11 @@ func devRunTests(kmd *cobra.Command, args []string) {
212195
}
213196
extraVars["cli_global_flags"] = cliGlobalFlags
214197
if target == "olm" {
215-
runTestOLM(extraVars, sfOperatorRepositoryPath, verbosity)
198+
runTestOLM(extraVars, cliCtx.Dev.SFOperatorRepositoryPath, verbosity)
216199
} else if target == "standalone" {
217-
runTestStandalone(extraVars, sfOperatorRepositoryPath, verbosity)
200+
runTestStandalone(extraVars, cliCtx.Dev.SFOperatorRepositoryPath, verbosity)
218201
} else if target == "upgrade" {
219-
runTestUpgrade(extraVars, sfOperatorRepositoryPath, verbosity)
202+
runTestUpgrade(extraVars, cliCtx.Dev.SFOperatorRepositoryPath, verbosity)
220203
}
221204
}
222205

@@ -260,13 +243,8 @@ func devCreate(kmd *cobra.Command, args []string) {
260243
ctrl.Log.Info("Demo repos path unset; repos will be cloned into ./deploy")
261244
reposPath = "deploy"
262245
}
263-
sfOperatorRepositoryPath := cliCtx.Dev.SFOperatorRepositoryPath
264-
if sfOperatorRepositoryPath == "" {
265-
ctrl.Log.Error(errMissingArg, "The path to the sf-operator repository must be set in `dev` section of the configuration")
266-
os.Exit(1)
267-
}
268246
keepDemoTenantDefinition, _ := kmd.Flags().GetBool("keep-demo-tenant")
269-
createDemoEnv(env, restConfig, fqdn, reposPath, sfOperatorRepositoryPath, keepDemoTenantDefinition)
247+
createDemoEnv(env, restConfig, fqdn, reposPath, cliCtx.Dev.SFOperatorRepositoryPath, keepDemoTenantDefinition)
270248

271249
} else {
272250
ctrl.Log.Error(errors.New("unsupported target"), "Invalid argument '"+target+"'")

cli/cmd/utils/utils.go

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"reflect"
2929
"strings"
3030

31+
"go.uber.org/zap/zapcore"
3132
"gopkg.in/yaml.v3"
3233

3334
apiv1 "k8s.io/api/core/v1"
@@ -45,6 +46,7 @@ import (
4546
ctrl "sigs.k8s.io/controller-runtime"
4647

4748
"sigs.k8s.io/controller-runtime/pkg/client"
49+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
4850

4951
monitoring "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
5052

@@ -119,17 +121,53 @@ func getContextFromFile(command *cobra.Command) (ctxName string, cliContext Soft
119121
return ctxName, cliContext, errors.New("context not found")
120122
}
121123

124+
// SetLogger enables the DEBUG LogLevel in the Logger when the debug flag is set
125+
func SetLogger(command *cobra.Command) {
126+
debug, _ := command.Flags().GetBool("debug")
127+
logLevel := zapcore.InfoLevel
128+
if debug {
129+
logLevel = zapcore.DebugLevel
130+
}
131+
132+
opts := zap.Options{
133+
Development: true,
134+
Level: logLevel,
135+
DestWriter: os.Stderr,
136+
}
137+
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
138+
}
139+
140+
// logI logs a message with the INFO log Level
141+
func logI(msg string) {
142+
ctrl.Log.Info(msg)
143+
}
144+
145+
// logI logs a message with the DEBUG log Level
146+
func logD(msg string) {
147+
ctrl.Log.V(1).Info(msg)
148+
}
149+
150+
// logI logs a message with the Error log Level
151+
func logE(err error, msg string) {
152+
ctrl.Log.Error(err, msg)
153+
}
154+
122155
func GetCLIContext(command *cobra.Command) (SoftwareFactoryConfigContext, error) {
156+
157+
// This is usually called for every CLI command so here let's set the Logger settings
158+
SetLogger(command)
159+
123160
var cliContext SoftwareFactoryConfigContext
124161
var ctxName string
125162
var err error
126163
configPath, _ := command.Flags().GetString("config")
127164
if configPath != "" {
128165
ctxName, cliContext, err = getContextFromFile(command)
129166
if err != nil {
130-
ctrl.Log.Error(err, "Could not load config file")
167+
logE(err, "Could not load config file")
168+
os.Exit(1)
131169
} else {
132-
ctrl.Log.V(5).Info("Using configuration context " + ctxName)
170+
logD("Using configuration context " + ctxName)
133171
}
134172
}
135173
// Override with defaults
@@ -149,6 +187,16 @@ func GetCLIContext(command *cobra.Command) (SoftwareFactoryConfigContext, error)
149187
if cliContext.FQDN == "" {
150188
cliContext.FQDN = fqdn
151189
}
190+
if cliContext.Dev.SFOperatorRepositoryPath == "" {
191+
defaultSFOperatorRepositoryPath, getwdErr := os.Getwd()
192+
if getwdErr != nil {
193+
logE(getwdErr,
194+
"sf-operator-repository-path is not set in `dev` section of the configuration file and unable to determine the current working directory")
195+
os.Exit(1)
196+
}
197+
cliContext.Dev.SFOperatorRepositoryPath = defaultSFOperatorRepositoryPath
198+
logD("Using current working directory for sf-operator-repository-path: " + cliContext.Dev.SFOperatorRepositoryPath)
199+
}
152200
return cliContext, nil
153201
}
154202

@@ -332,8 +380,8 @@ func RunCmdWithEnvOrDie(environ []string, cmd string, args ...string) string {
332380
kmd.Env = append(os.Environ(), environ...)
333381
out, err := kmd.CombinedOutput()
334382
if err != nil {
335-
ctrl.Log.Error(err, "Could not run command '"+cmd+"'")
336-
ctrl.Log.Info("Captured output:\n" + string(out))
383+
logE(err, "Could not run command '"+cmd+"'")
384+
logI("Captured output:\n" + string(out))
337385
os.Exit(1)
338386
}
339387
return string(out)

doc/developer/getting_started.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,18 @@ kubectl create namespace sf
4848
kubectl config set-context microshift --namespace=sf
4949
```
5050

51-
[Create a sf-operator configuration file](../reference/cli/index.md#config) to your liking:
52-
53-
```sh
54-
go run ./main.go init config --dev > /path/to/sfcli.yaml
51+
Ensure the following minimal settings in your sf-operator CLI file:
52+
53+
```yaml
54+
contexts:
55+
my-context:
56+
namespace: sf
57+
fqdn: sfop.me
58+
default-context: my-context
5559
```
5660
57-
Then edit it as necessary, for example by setting up a custom FQDN. Below is an example you can copy and edit.
58-
59-
??? abstract "CLI configuration example"
60-
61-
```yaml title="sfcli.yaml"
62-
--8<-- "sfcli.yaml"
63-
```
61+
Consult [Create a sf-operator configuration file](../reference/cli/index.md#config) and
62+
[The configuration file schema](../reference/cli/index.md#configuration-file) if needed.
6463
6564
Then run the `sf-operator` command:
6665

main.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"os"
88

99
"github.com/spf13/cobra"
10-
"sigs.k8s.io/controller-runtime/pkg/log/zap"
1110

1211
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
1312
// to ensure that exec-entrypoint and run can make use of them.
@@ -58,11 +57,6 @@ func operatorCmd(kmd *cobra.Command, args []string) {
5857
}
5958

6059
func main() {
61-
opts := zap.Options{
62-
Development: true,
63-
DestWriter: os.Stderr,
64-
}
65-
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
6660

6761
var (
6862
metricsAddr string
@@ -93,6 +87,7 @@ func main() {
9387
rootCmd.PersistentFlags().StringVarP(&fqdn, "fqdn", "d", "", "The FQDN of the deployment (if no manifest is provided).")
9488
rootCmd.PersistentFlags().StringVarP(&configFile, "config", "C", "", "Path to the CLI configuration file.")
9589
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.")
90+
rootCmd.PersistentFlags().Bool("debug", false, "Enable DEBUG logs")
9691

9792
// Flags for the operator command
9893
operatorCmd.Flags().StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")

playbooks/files/sf-operator-cli.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ contexts:
22
ci:
33
namespace: sf
44
fqdn: sfop.me
5-
development:
6-
sf-operator-repository-path: ./
75
ext-ze:
86
namespace: ext-ze
97
default-context: ci

0 commit comments

Comments
 (0)