Skip to content

Commit

Permalink
Add 'version' flag to wif-config create and update commands
Browse files Browse the repository at this point in the history
Version will set the wif_templates field of the wif-config.
  • Loading branch information
JakobGray committed Dec 18, 2024
1 parent 3a8ef75 commit 53caa6b
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 10 deletions.
43 changes: 38 additions & 5 deletions cmd/ocm/gcp/create-wif-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ import (
var (
// CreateWifConfigOpts captures the options that affect creation of the workload identity configuration
CreateWifConfigOpts = options{
Mode: ModeAuto,
Name: "",
Project: "",
RolePrefix: "",
TargetDir: "",
Mode: ModeAuto,
Name: "",
Project: "",
RolePrefix: "",
TargetDir: "",
OpenshiftVersion: "",
}
)

Expand Down Expand Up @@ -75,6 +76,12 @@ wif-config resource within OCM to represent those resources.`,
"",
targetDirFlagDescription,
)
createWifConfigCmd.PersistentFlags().StringVar(
&CreateWifConfigOpts.OpenshiftVersion,
"version",
"",
versionFlagDescription,
)

return createWifConfigCmd
}
Expand All @@ -86,6 +93,9 @@ func validationForCreateWorkloadIdentityConfigurationCmd(cmd *cobra.Command, arg
if err := promptProjectId(); err != nil {
return err
}
if err := promptVersion(); err != nil {
return err
}

if CreateWifConfigOpts.Mode != ModeAuto && CreateWifConfigOpts.Mode != ModeManual {
return fmt.Errorf("Invalid mode. Allowed values are %s", Modes)
Expand Down Expand Up @@ -137,6 +147,24 @@ func promptProjectId() error {
return nil
}

func promptVersion() error {
const versionHelp = "The OCP version to configure the wif-config for. " +
"Will default to the latest supported version if left unset."
if CreateWifConfigOpts.OpenshiftVersion == "" {
if CreateWifConfigOpts.Interactive {
prompt := &survey.Input{
Message: "Openshift version:",
Help: versionHelp,
}
return survey.AskOne(
prompt,
&CreateWifConfigOpts.OpenshiftVersion,
)
}
}
return nil
}

func createWorkloadIdentityConfigurationCmd(cmd *cobra.Command, argv []string) error {
ctx := context.Background()
log := log.Default()
Expand Down Expand Up @@ -225,6 +253,11 @@ func createWorkloadIdentityConfiguration(
}
wifBuilder.Gcp(gcpBuilder)

if CreateWifConfigOpts.OpenshiftVersion != "" {
wifTemplate := versionToTemplateID(CreateWifConfigOpts.OpenshiftVersion)
wifBuilder.WifTemplates(wifTemplate)
}

wifBuilder.DisplayName(displayName)
wifConfigInput, err := wifBuilder.Build()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/ocm/gcp/flag_descriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ manual: Commands necessary to modify GCP resources will be output
`

targetDirFlagDescription = `Directory to place generated files (defaults to current directory)`
versionFlagDescription = `Version of OpenShift to configure the WIF resources for`
)
1 change: 1 addition & 0 deletions cmd/ocm/gcp/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type options struct {
Interactive bool
Mode string
Name string
OpenshiftVersion string
Project string
Region string
RolePrefix string
Expand Down
13 changes: 13 additions & 0 deletions cmd/ocm/gcp/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"

cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
"github.com/pkg/errors"
Expand Down Expand Up @@ -82,3 +83,15 @@ func getPathFromFlag(targetDir string) (string, error) {

return targetDir, nil
}

// converts openshift version of form X.Y to template ID of form vX.Y
func versionToTemplateID(version string) string {
// Check if version is a semver in the form X.Y
re := regexp.MustCompile(`^\d+\.\d+$`)
if re.MatchString(version) {
return "v" + version
}

// Otherwise, return the version as is
return version
}
33 changes: 31 additions & 2 deletions cmd/ocm/gcp/update-wif-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import (

"github.com/openshift-online/ocm-cli/pkg/gcp"
"github.com/openshift-online/ocm-cli/pkg/ocm"
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

var (
UpdateWifConfigOpts = options{
Mode: ModeAuto,
TargetDir: "",
Mode: ModeAuto,
TargetDir: "",
OpenshiftVersion: "",
}
)

Expand Down Expand Up @@ -46,6 +48,12 @@ the wif-config metadata and the GCP resources it represents.`,
"",
targetDirFlagDescription,
)
updateWifConfigCmd.PersistentFlags().StringVar(
&UpdateWifConfigOpts.OpenshiftVersion,
"version",
"",
versionFlagDescription,
)

return updateWifConfigCmd
}
Expand Down Expand Up @@ -85,6 +93,27 @@ func updateWorkloadIdentityConfigurationCmd(cmd *cobra.Command, argv []string) e
return errors.Wrapf(err, "failed to get wif-config")
}

// Update the WIF configuration
if UpdateWifConfigOpts.OpenshiftVersion != "" {
wifTemplate := versionToTemplateID(UpdateWifConfigOpts.OpenshiftVersion)

wifBuilder := cmv1.NewWifConfig()
existingTemplates, _ := wifConfig.GetWifTemplates()
wifBuilder.WifTemplates(append(existingTemplates, wifTemplate)...)

updatedWifConfig, err := wifBuilder.Build()
if err != nil {
return errors.Wrapf(err, "failed to create wif-config body")
}

resp, err := connection.ClustersMgmt().V1().GCP().WifConfigs().
WifConfig(wifConfig.ID()).Update().Body(updatedWifConfig).Send()
if err != nil {
return errors.Wrapf(err, "failed to update wif-config")
}
wifConfig = resp.Body()
}

gcpClient, err := gcp.NewGcpClient(ctx)
if err != nil {
return errors.Wrapf(err, "failed to initiate GCP client")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/nwidger/jsoncolor v0.3.2
github.com/onsi/ginkgo/v2 v2.11.0
github.com/onsi/gomega v1.27.8
github.com/openshift-online/ocm-sdk-go v0.1.449
github.com/openshift-online/ocm-sdk-go v0.1.451
github.com/openshift/rosa v1.2.24
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
github.com/pkg/errors v0.9.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,8 @@ github.com/onsi/ginkgo/v2 v2.11.0 h1:WgqUCUt/lT6yXoQ8Wef0fsNn5cAuMK7+KT9UFRz2tcU
github.com/onsi/ginkgo/v2 v2.11.0/go.mod h1:ZhrRA5XmEE3x3rhlzamx/JJvujdZoJ2uvgI7kR0iZvM=
github.com/onsi/gomega v1.27.8 h1:gegWiwZjBsf2DgiSbf5hpokZ98JVDMcWkUiigk6/KXc=
github.com/onsi/gomega v1.27.8/go.mod h1:2J8vzI/s+2shY9XHRApDkdgPo1TKT7P2u6fXeJKFnNQ=
github.com/openshift-online/ocm-sdk-go v0.1.449 h1:hgegxZuVl8bvR8uA4hCj0/GTfyNLHeQTzBlWXACQX7k=
github.com/openshift-online/ocm-sdk-go v0.1.449/go.mod h1:CiAu2jwl3ITKOxkeV0Qnhzv4gs35AmpIzVABQLtcI2Y=
github.com/openshift-online/ocm-sdk-go v0.1.451 h1:lkMUmReNb7fOLZagnCp5Hu4yY7UyJH47nPEKSUEUVFw=
github.com/openshift-online/ocm-sdk-go v0.1.451/go.mod h1:CiAu2jwl3ITKOxkeV0Qnhzv4gs35AmpIzVABQLtcI2Y=
github.com/openshift/rosa v1.2.24 h1:vv0yYnWHx6CCPEAau/0rS54P2ksaf+uWXb1TQPWxiYE=
github.com/openshift/rosa v1.2.24/go.mod h1:MVXB27O3PF8WoOic23I03mmq6/9kVxpFx6FKyLMCyrQ=
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU=
Expand Down

0 comments on commit 53caa6b

Please sign in to comment.