From 014522ff3947f6245a2803e8d2121e3ad1e47b71 Mon Sep 17 00:00:00 2001 From: Brady Pratt Date: Mon, 4 Apr 2022 04:39:55 -0500 Subject: [PATCH] fix json output for cluster creation When executing with `--output=json` and `--mode=auto`, plain info logs are printed from the create commands of `oidc-provider` and `operator-roles` causing the output to no longer parse as JSON. Bug was surfaced in #661 and mentioned in #662. Simply add the output flag/terminal check conditional to offending log statements. Test command: ``` rosa create cluster -c --mode=auto --output=json --sts --yes | jq . ``` Signed-off-by: Brady Pratt --- cmd/create/cluster/cmd.go | 8 ++++++-- cmd/create/oidcprovider/cmd.go | 9 +++++++-- cmd/create/operatorroles/cmd.go | 9 +++++++-- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/cmd/create/cluster/cmd.go b/cmd/create/cluster/cmd.go index 7a8ce90c7a..1046189c79 100644 --- a/cmd/create/cluster/cmd.go +++ b/cmd/create/cluster/cmd.go @@ -1787,9 +1787,13 @@ func run(cmd *cobra.Command, _ []string) { if isSTS { if mode != "" { - reporter.Infof("Preparing to create operator roles.") + if !output.HasFlag() || reporter.IsTerminal() { + reporter.Infof("Preparing to create operator roles.") + } operatorroles.Cmd.Run(operatorroles.Cmd, []string{clusterName, mode, permissionsBoundary}) - reporter.Infof("Preparing to create OIDC Provider.") + if !output.HasFlag() || reporter.IsTerminal() { + reporter.Infof("Preparing to create OIDC Provider.") + } oidcprovider.Cmd.Run(oidcprovider.Cmd, []string{clusterName, mode}) } else { rolesCMD := fmt.Sprintf("rosa create operator-roles --cluster %s", clusterName) diff --git a/cmd/create/oidcprovider/cmd.go b/cmd/create/oidcprovider/cmd.go index f6b645222a..ef9522b00b 100644 --- a/cmd/create/oidcprovider/cmd.go +++ b/cmd/create/oidcprovider/cmd.go @@ -35,6 +35,7 @@ import ( "github.com/openshift/rosa/pkg/interactive/confirm" "github.com/openshift/rosa/pkg/logging" "github.com/openshift/rosa/pkg/ocm" + "github.com/openshift/rosa/pkg/output" rprtr "github.com/openshift/rosa/pkg/reporter" ) @@ -169,7 +170,9 @@ func run(cmd *cobra.Command, argv []string) { "Verify that the cluster operator roles exist and are configured correctly.", clusterKey) os.Exit(1) } - reporter.Infof("Creating OIDC provider using '%s'", creator.ARN) + if !output.HasFlag() || reporter.IsTerminal() { + reporter.Infof("Creating OIDC provider using '%s'", creator.ARN) + } if !confirm.Prompt(true, "Create the OIDC provider for cluster '%s'?", clusterKey) { os.Exit(0) } @@ -223,7 +226,9 @@ func createProvider(reporter *rprtr.Object, awsClient aws.Client, cluster *cmv1. if err != nil { return err } - reporter.Infof("Created OIDC provider with ARN '%s'", oidcProviderARN) + if !output.HasFlag() || reporter.IsTerminal() { + reporter.Infof("Created OIDC provider with ARN '%s'", oidcProviderARN) + } return nil } diff --git a/cmd/create/operatorroles/cmd.go b/cmd/create/operatorroles/cmd.go index 3636c67d70..960bceb6f4 100644 --- a/cmd/create/operatorroles/cmd.go +++ b/cmd/create/operatorroles/cmd.go @@ -31,6 +31,7 @@ import ( "github.com/openshift/rosa/pkg/interactive/confirm" "github.com/openshift/rosa/pkg/logging" "github.com/openshift/rosa/pkg/ocm" + "github.com/openshift/rosa/pkg/output" rprtr "github.com/openshift/rosa/pkg/reporter" ) @@ -227,7 +228,9 @@ func run(cmd *cobra.Command, argv []string) { } switch mode { case aws.ModeAuto: - reporter.Infof("Creating roles using '%s'", creator.ARN) + if !output.HasFlag() || reporter.IsTerminal() { + reporter.Infof("Creating roles using '%s'", creator.ARN) + } err = createRoles(reporter, awsClient, prefix, permissionsBoundary, cluster, creator.AccountID, accountRoleVersion) if err != nil { @@ -325,7 +328,9 @@ func createRoles(reporter *rprtr.Object, awsClient aws.Client, if err != nil { return err } - reporter.Infof("Created role '%s' with ARN '%s'", roleName, roleARN) + if !output.HasFlag() || reporter.IsTerminal() { + reporter.Infof("Created role '%s' with ARN '%s'", roleName, roleARN) + } reporter.Debugf("Attaching permission policy '%s' to role '%s'", policyARN, roleName) err = awsClient.AttachRolePolicy(roleName, policyARN)