@@ -53,6 +53,7 @@ import (
53
53
opv1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
54
54
sfv1 "github.com/softwarefactory-project/sf-operator/api/v1"
55
55
controllers "github.com/softwarefactory-project/sf-operator/controllers"
56
+ ctrlutils "github.com/softwarefactory-project/sf-operator/controllers/libs/utils"
56
57
57
58
"k8s.io/client-go/kubernetes"
58
59
)
@@ -137,21 +138,6 @@ func SetLogger(command *cobra.Command) {
137
138
ctrl .SetLogger (zap .New (zap .UseFlagOptions (& opts )))
138
139
}
139
140
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
-
155
141
func GetCLIContext (command * cobra.Command ) (SoftwareFactoryConfigContext , error ) {
156
142
157
143
// This is usually called for every CLI command so here let's set the Logger settings
@@ -164,10 +150,10 @@ func GetCLIContext(command *cobra.Command) (SoftwareFactoryConfigContext, error)
164
150
if configPath != "" {
165
151
ctxName , cliContext , err = getContextFromFile (command )
166
152
if err != nil {
167
- logE (err , "Could not load config file" )
153
+ ctrlutils . LogE (err , "Could not load config file" )
168
154
os .Exit (1 )
169
155
} else {
170
- logD ("Using configuration context " + ctxName )
156
+ ctrlutils . LogD ("Using configuration context " + ctxName )
171
157
}
172
158
}
173
159
// Override with defaults
@@ -190,12 +176,12 @@ func GetCLIContext(command *cobra.Command) (SoftwareFactoryConfigContext, error)
190
176
if cliContext .Dev .SFOperatorRepositoryPath == "" {
191
177
defaultSFOperatorRepositoryPath , getwdErr := os .Getwd ()
192
178
if getwdErr != nil {
193
- logE (getwdErr ,
179
+ ctrlutils . LogE (getwdErr ,
194
180
"sf-operator-repository-path is not set in `dev` section of the configuration file and unable to determine the current working directory" )
195
181
os .Exit (1 )
196
182
}
197
183
cliContext .Dev .SFOperatorRepositoryPath = defaultSFOperatorRepositoryPath
198
- logD ("Using current working directory for sf-operator-repository-path: " + cliContext .Dev .SFOperatorRepositoryPath )
184
+ ctrlutils . LogD ("Using current working directory for sf-operator-repository-path: " + cliContext .Dev .SFOperatorRepositoryPath )
199
185
}
200
186
return cliContext , nil
201
187
}
@@ -239,7 +225,7 @@ func CreateKubernetesClient(contextName string) (client.Client, error) {
239
225
func CreateKubernetesClientOrDie (contextName string ) client.Client {
240
226
cli , err := CreateKubernetesClient (contextName )
241
227
if err != nil {
242
- ctrl . Log . Error (err , "Error creating Kubernetes client" )
228
+ ctrlutils . LogE (err , "Error creating Kubernetes client" )
243
229
os .Exit (1 )
244
230
}
245
231
return cli
@@ -249,7 +235,7 @@ func GetCLIENV(kmd *cobra.Command) (string, ENV) {
249
235
250
236
cliCtx , err := GetCLIContext (kmd )
251
237
if err != nil {
252
- ctrl . Log . Error (err , "Error initializing CLI:" )
238
+ ctrlutils . LogE (err , "Error initializing CLI:" )
253
239
os .Exit (1 )
254
240
}
255
241
@@ -283,7 +269,7 @@ func DeleteOrDie(env *ENV, obj client.Object, opts ...client.DeleteOption) bool
283
269
return false
284
270
} else if err != nil {
285
271
msg := fmt .Sprintf ("Error while deleting %s \" %s\" " , reflect .TypeOf (obj ).Name (), obj .GetName ())
286
- ctrl . Log . Error (err , msg )
272
+ ctrlutils . LogE (err , msg )
287
273
os .Exit (1 )
288
274
}
289
275
return true
@@ -295,49 +281,49 @@ func GetMOrDie(env *ENV, name string, obj client.Object) bool {
295
281
return false
296
282
} else if err != nil {
297
283
msg := fmt .Sprintf ("Error while fetching %s \" %s\" " , reflect .TypeOf (obj ).Name (), name )
298
- ctrl . Log . Error (err , msg )
284
+ ctrlutils . LogE (err , msg )
299
285
os .Exit (1 )
300
286
}
301
287
return true
302
288
}
303
289
304
290
func UpdateROrDie (env * ENV , obj client.Object ) {
305
291
var msg = fmt .Sprintf ("Updating %s \" %s\" in %s" , reflect .TypeOf (obj ).Name (), obj .GetName (), env .Ns )
306
- ctrl . Log . Info (msg )
292
+ ctrlutils . LogI (msg )
307
293
if err := env .Cli .Update (env .Ctx , obj ); err != nil {
308
294
msg = fmt .Sprintf ("Error while updating %s \" %s\" " , reflect .TypeOf (obj ).Name (), obj .GetName ())
309
- ctrl . Log . Error (err , msg )
295
+ ctrlutils . LogE (err , msg )
310
296
os .Exit (1 )
311
297
}
312
298
msg = fmt .Sprintf ("%s \" %s\" updated" , reflect .TypeOf (obj ).Name (), obj .GetName ())
313
- ctrl . Log . Info (msg )
299
+ ctrlutils . LogI (msg )
314
300
}
315
301
316
302
func CreateROrDie (env * ENV , obj client.Object ) {
317
303
var msg = fmt .Sprintf ("Creating %s \" %s\" in %s" , reflect .TypeOf (obj ).Name (), obj .GetName (), env .Ns )
318
- ctrl . Log . Info (msg )
304
+ ctrlutils . LogI (msg )
319
305
obj .SetNamespace (env .Ns )
320
306
if err := env .Cli .Create (env .Ctx , obj ); err != nil {
321
307
msg = fmt .Sprintf ("Error while creating %s \" %s\" " , reflect .TypeOf (obj ).Name (), obj .GetName ())
322
- ctrl . Log . Error (err , msg )
308
+ ctrlutils . LogE (err , msg )
323
309
os .Exit (1 )
324
310
}
325
311
msg = fmt .Sprintf ("%s \" %s\" created" , reflect .TypeOf (obj ).Name (), obj .GetName ())
326
- ctrl . Log . Info (msg )
312
+ ctrlutils . LogI (msg )
327
313
}
328
314
329
315
func DeleteAllOfOrDie (env * ENV , obj client.Object , opts ... client.DeleteAllOfOption ) {
330
316
if err := env .Cli .DeleteAllOf (env .Ctx , obj , opts ... ); err != nil {
331
317
var msg = "Error while deleting"
332
- ctrl . Log . Error (err , msg )
318
+ ctrlutils . LogE (err , msg )
333
319
os .Exit (1 )
334
320
}
335
321
}
336
322
337
323
func GetCLIctxOrDie (kmd * cobra.Command , args []string , allowedArgs []string ) SoftwareFactoryConfigContext {
338
324
cliCtx , err := GetCLIContext (kmd )
339
325
if err != nil {
340
- ctrl . Log . Error (err , "Error initializing:" )
326
+ ctrlutils . LogE (err , "Error initializing:" )
341
327
os .Exit (1 )
342
328
}
343
329
if len (allowedArgs ) == 0 {
@@ -346,15 +332,15 @@ func GetCLIctxOrDie(kmd *cobra.Command, args []string, allowedArgs []string) Sof
346
332
} else {
347
333
argumentError := errors .New ("argument must be in: " + strings .Join (allowedArgs , ", " ))
348
334
if len (args ) != 1 {
349
- ctrl . Log . Error (argumentError , "Need one argument" )
335
+ ctrlutils . LogE (argumentError , "Need one argument" )
350
336
os .Exit (1 )
351
337
}
352
338
for _ , a := range allowedArgs {
353
339
if args [0 ] == a {
354
340
return cliCtx
355
341
}
356
342
}
357
- ctrl . Log . Error (argumentError , "Unknown argument " + args [0 ])
343
+ ctrlutils . LogE (argumentError , "Unknown argument " + args [0 ])
358
344
os .Exit (1 )
359
345
}
360
346
return SoftwareFactoryConfigContext {}
@@ -380,8 +366,8 @@ func RunCmdWithEnvOrDie(environ []string, cmd string, args ...string) string {
380
366
kmd .Env = append (os .Environ (), environ ... )
381
367
out , err := kmd .CombinedOutput ()
382
368
if err != nil {
383
- logE (err , "Could not run command '" + cmd + "'" )
384
- logI ("Captured output:\n " + string (out ))
369
+ ctrlutils . LogE (err , "Could not run command '" + cmd + "'" )
370
+ ctrlutils . LogI ("Captured output:\n " + string (out ))
385
371
os .Exit (1 )
386
372
}
387
373
return string (out )
@@ -397,7 +383,7 @@ func EnsureNamespaceOrDie(env *ENV, name string) {
397
383
ns .Name = name
398
384
CreateROrDie (env , & ns )
399
385
} else if err != nil {
400
- ctrl . Log . Error (err , "Error checking namespace " + name )
386
+ ctrlutils . LogE (err , "Error checking namespace " + name )
401
387
os .Exit (1 )
402
388
}
403
389
}
@@ -412,7 +398,7 @@ func EnsureServiceAccountOrDie(env *ENV, name string) {
412
398
func WriteContentToFile (filePath string , content []byte , mode fs.FileMode ) {
413
399
err := os .WriteFile (filePath , content , mode )
414
400
if err != nil {
415
- ctrl . Log . Error (err , "Can not write a file " + filePath )
401
+ ctrlutils . LogE (err , "Can not write a file " + filePath )
416
402
os .Exit (1 )
417
403
}
418
404
}
@@ -425,7 +411,7 @@ func VarListToMap(varsList []string) map[string]string {
425
411
tokens := strings .Split (v , "=" )
426
412
427
413
if len (tokens ) != 2 {
428
- ctrl . Log . Error (errors .New ("parse error" ), "parsed value `" + v + "` needs to be defined as 'foo=bar'" )
414
+ ctrlutils . LogE (errors .New ("parse error" ), "parsed value `" + v + "` needs to be defined as 'foo=bar'" )
429
415
os .Exit (1 )
430
416
}
431
417
vars [tokens [0 ]] = tokens [1 ]
@@ -436,7 +422,7 @@ func VarListToMap(varsList []string) map[string]string {
436
422
func CreateDirectory (dirPath string , mode fs.FileMode ) {
437
423
err := os .MkdirAll (dirPath , mode )
438
424
if err != nil {
439
- ctrl . Log . Error (err , "Can not create directory " + dirPath )
425
+ ctrlutils . LogE (err , "Can not create directory " + dirPath )
440
426
os .Exit (1 )
441
427
}
442
428
}
@@ -454,7 +440,7 @@ func GetClientset(kubeContext string) (*rest.Config, *kubernetes.Clientset) {
454
440
restConfig := controllers .GetConfigContextOrDie (kubeContext )
455
441
kubeClientset , err := kubernetes .NewForConfig (restConfig )
456
442
if err != nil {
457
- ctrl . Log . Error (err , "Could not instantiate Clientset" )
443
+ ctrlutils . LogE (err , "Could not instantiate Clientset" )
458
444
os .Exit (1 )
459
445
}
460
446
return restConfig , kubeClientset
@@ -482,7 +468,7 @@ func RunRemoteCmd(kubeContext string, namespace string, podName string, containe
482
468
if err != nil {
483
469
errMsg := fmt .Sprintf ("Command \" %s\" [Pod: %s - Container: %s] failed with the following stderr: %s" ,
484
470
strings .Join (cmdArgs , " " ), podName , containerName , errorBuffer .String ())
485
- ctrl . Log . Error (err , errMsg )
471
+ ctrlutils . LogE (err , errMsg )
486
472
os .Exit (1 )
487
473
}
488
474
return buffer
@@ -493,10 +479,10 @@ func ReadYAMLToMapOrDie(filePath string) map[string]interface{} {
493
479
secretContent := make (map [string ]interface {})
494
480
err := yaml .Unmarshal (readFile , & secretContent )
495
481
if err != nil {
496
- ctrl . Log . Error (err , "Problem on reading the file content" )
482
+ ctrlutils . LogE (err , "Problem on reading the file content" )
497
483
}
498
484
if len (secretContent ) == 0 {
499
- ctrl . Log . Error (errors .New ("file is empty" ), "The file is empty or it does not exist!" )
485
+ ctrlutils . LogE (errors .New ("file is empty" ), "The file is empty or it does not exist!" )
500
486
os .Exit (1 )
501
487
}
502
488
return secretContent
@@ -505,7 +491,7 @@ func ReadYAMLToMapOrDie(filePath string) map[string]interface{} {
505
491
func GetKubectlPath () string {
506
492
kubectlPath , err := exec .LookPath ("kubectl" )
507
493
if err != nil {
508
- ctrl . Log . Error (errors .New ("no kubectl binary" ),
494
+ ctrlutils . LogE (errors .New ("no kubectl binary" ),
509
495
"No 'kubectl' binary found. Please install the 'kubectl' binary before attempting a restore" )
510
496
os .Exit (1 )
511
497
}
@@ -519,7 +505,7 @@ func ExecuteKubectlClient(ns string, podName string, containerName string, execu
519
505
520
506
err := cmd .Run ()
521
507
if err != nil {
522
- ctrl . Log . Error (err , "There is an issue on executing command: " + executeCommand )
508
+ ctrlutils . LogE (err , "There is an issue on executing command: " + executeCommand )
523
509
os .Exit (1 )
524
510
}
525
511
0 commit comments