Skip to content

Commit

Permalink
[Hypershift] Filter regions where HostedCP is avalaible
Browse files Browse the repository at this point in the history
Add a check at cluster creation to ensure region can host an HostedCP
Add a filter in region list command for HostedCP enabled regions

Fixes SDA-6899
  • Loading branch information
andreadecorte committed Oct 19, 2022
1 parent 8b15d6f commit 8edd840
Show file tree
Hide file tree
Showing 313 changed files with 95,543 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ docs/
/*policy.json
.envrc
.env
cover.out
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ rosa:
test:
go test ./...

.PHONY: coverage
coverage:
go test -coverprofile=cover.out ./...

.PHONY: install
install:
go install ./cmd/rosa
Expand Down
17 changes: 16 additions & 1 deletion cmd/create/cluster/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,8 @@ func run(cmd *cobra.Command, _ []string) {
versionFilter = ""
}

regionList, regionAZ, err := r.OCMClient.GetRegionList(multiAZ, roleARN, externalID, versionFilter, awsClient)
regionList, regionAZ, err := r.OCMClient.GetRegionList(multiAZ, roleARN, externalID, versionFilter,
awsClient, isHostedCP)
if err != nil {
r.Reporter.Errorf(fmt.Sprintf("%s", err))
os.Exit(1)
Expand All @@ -1181,6 +1182,20 @@ func run(cmd *cobra.Command, _ []string) {
r.Reporter.Errorf("Expected a valid AWS region")
os.Exit(1)
} else {
if isHostedCP && !interactive.Enabled() {
found := false
for i := range regionList {
if regionList[i] == region {
found = true
break
}
}
if !found {
r.Reporter.Errorf("Region '%s' is not supported for Hosted Cluster", region)
os.Exit(1)
}
}

if supportsMultiAZ, found := regionAZ[region]; found {
if !supportsMultiAZ && multiAZ {
r.Reporter.Errorf("Region '%s' does not support multiple availability zones", region)
Expand Down
66 changes: 54 additions & 12 deletions cmd/list/region/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ import (
"text/tabwriter"

cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
"github.com/spf13/cobra"

"github.com/openshift/rosa/pkg/ocm"
"github.com/openshift/rosa/pkg/output"
"github.com/openshift/rosa/pkg/rosa"
"github.com/spf13/cobra"
)

var args struct {
multiAZ bool
roleARN string
externalID string
multiAZ bool
roleARN string
externalID string
hostedCluster bool
}

var Cmd = &cobra.Command{
Expand Down Expand Up @@ -64,11 +65,19 @@ func init() {
"",
"A unique identifier that might be required when you assume a role in another account",
)
flags.BoolVar(
&args.hostedCluster,
"hosted-cp",
false,
"List only regions with support for hosted control planes (HyperShift)",
)
flags.MarkHidden("hosted-cp")

output.AddFlag(Cmd)
}

func run(cmd *cobra.Command, _ []string) {
var regionsWithHostedCPSupport map[string]bool
r := rosa.NewRuntime().WithOCM()
defer r.Cleanup()

Expand All @@ -80,6 +89,20 @@ func run(cmd *cobra.Command, _ []string) {
os.Exit(1)
}

hypershiftEnabled, err := r.OCMClient.IsCapabilityEnabled(ocm.HypershiftCapability)
if err != nil {
r.Reporter.Errorf("%s", err)
os.Exit(1)
}

if hypershiftEnabled {
regionsWithHostedCPSupport, err = r.OCMClient.ListHostedCPSupportedRegion()
if err != nil {
r.Reporter.Errorf("%s", err)
os.Exit(1)
}
}

// Filter out unwanted regions
var availableRegions []*cmv1.CloudRegion
for _, region := range regions {
Expand All @@ -91,6 +114,11 @@ func run(cmd *cobra.Command, _ []string) {
continue
}
}
if hypershiftEnabled && cmd.Flags().Changed("hosted-cp") {
if args.hostedCluster != regionsWithHostedCPSupport[region.ID()] {
continue
}
}
availableRegions = append(availableRegions, region)
}

Expand All @@ -110,15 +138,29 @@ func run(cmd *cobra.Command, _ []string) {

// Create the writer that will be used to print the tabulated results:
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
fmt.Fprintf(writer, "ID\t\tNAME\t\tMULTI-AZ SUPPORT\n")
headerFormat := "ID\t\tNAME\t\tMULTI-AZ SUPPORT\n"
if hypershiftEnabled {
headerFormat = "ID\t\tNAME\t\tMULTI-AZ SUPPORT\t\tHOSTED-CP SUPPORT\n"
}
fmt.Fprint(writer, headerFormat)

for _, region := range availableRegions {
fmt.Fprintf(writer,
"%s\t\t%s\t\t%t\n",
region.ID(),
region.DisplayName(),
region.SupportsMultiAZ(),
)
if hypershiftEnabled {
fmt.Fprintf(writer,
"%s\t\t%s\t\t%t\t\t%t\n",
region.ID(),
region.DisplayName(),
region.SupportsMultiAZ(),
regionsWithHostedCPSupport[region.ID()],
)
} else {
fmt.Fprintf(writer,
"%s\t\t%s\t\t%t\n",
region.ID(),
region.DisplayName(),
region.SupportsMultiAZ(),
)
}
}
writer.Flush()
}
12 changes: 12 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,21 @@ require (
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/itchyny/gojq v0.12.7 // indirect
github.com/itchyny/timefmt-go v0.1.3 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.12.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.11.0 // indirect
github.com/jackc/pgx/v4 v4.16.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
Expand All @@ -53,6 +64,7 @@ require (
github.com/prometheus/procfs v0.7.3 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f // indirect
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 // indirect
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ github.com/AlecAivazis/survey/v2 v2.2.15 h1:6UNMnk+YGegYFiPfdTOyZDIN+m08x2nGnqOn
github.com/AlecAivazis/survey/v2 v2.2.15/go.mod h1:TH2kPCDU3Kqq7pLbnCWwZXDBjnhZtmsCle5EiYDJ2fg=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8 h1:xzYJEypr/85nBpB11F9br+3HUrpgb+fcm5iADzXXYEw=
github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8/go.mod h1:oX5x61PbNXchhh0oikYAH+4Pcfw5LKv21+Jnpr6r6Pc=
Expand Down Expand Up @@ -72,6 +73,7 @@ github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5P
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I=
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
Expand Down Expand Up @@ -115,6 +117,7 @@ github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw=
github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
Expand Down Expand Up @@ -218,7 +221,6 @@ github.com/itchyny/gojq v0.12.7 h1:hYPTpeWfrJ1OT+2j6cvBScbhl0TkdwGM4bc66onUSOQ=
github.com/itchyny/gojq v0.12.7/go.mod h1:ZdvNHVlzPgUf8pgjnuDTmGfHA/21KoutQUJ3An/xNuw=
github.com/itchyny/timefmt-go v0.1.3 h1:7M3LGVDsqcd0VZH2U+x393obrzZisp7C0uEe921iRkU=
github.com/itchyny/timefmt-go v0.1.3/go.mod h1:0osSSCQSASBJMsIZnhAaF1C2fCBTJZXrnj37mG8/c+A=
github.com/jackc/chunkreader v1.0.0 h1:4s39bBR8ByfqH+DKm8rQA3E1LHZWB9XWcrz8fqaZbe0=
github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo=
github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk=
github.com/jackc/chunkreader/v2 v2.0.1 h1:i+RDz65UE+mmpjTfyz0MoVTnzeYxroil2G82ki7MGG8=
Expand All @@ -235,10 +237,10 @@ github.com/jackc/pgio v1.0.0 h1:g12B9UwVnzGhueNavwioyEEpAmqMe1E/BN9ES+8ovkE=
github.com/jackc/pgio v1.0.0/go.mod h1:oP+2QK2wFfUWgr+gxjoBH9KGBb31Eio69xUb0w5bYf8=
github.com/jackc/pgmock v0.0.0-20190831213851-13a1b77aafa2/go.mod h1:fGZlG77KXmcq05nJLRkk0+p82V8B8Dw8KN2/V9c/OAE=
github.com/jackc/pgmock v0.0.0-20201204152224-4fe30f7445fd/go.mod h1:hrBW0Enj2AZTNpt/7Y5rr2xe/9Mn757Wtb2xeBzPv2c=
github.com/jackc/pgmock v0.0.0-20210724152146-4ad1a8207f65 h1:DadwsjnMwFjfWc9y5Wi/+Zz7xoE5ALHsRQlOctkOiHc=
github.com/jackc/pgmock v0.0.0-20210724152146-4ad1a8207f65/go.mod h1:5R2h2EEX+qri8jOWMbJCtaPWkrrNc7OHwsp2TCqp7ak=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgproto3 v1.1.0 h1:FYYE4yRw+AgI8wXIinMlNjBbp/UitDJwfj5LqqewP1A=
github.com/jackc/pgproto3 v1.1.0/go.mod h1:eR5FA3leWg7p9aeAqi37XOTgTIbkABlvcPB3E5rlc78=
github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190420180111-c116219b62db/go.mod h1:bhq50y+xrl9n5mRYyCBFKkpRVTLYJVWeCc+mEAI3yXA=
github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190609003834-432c2951c711/go.mod h1:uH0AWtUmuShn0bcesswc4aBTWGvw0cAxIJp+6OB//Wg=
Expand Down Expand Up @@ -306,6 +308,7 @@ github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/lib/pq v1.10.5 h1:J+gdV2cUmX7ZqL2B0lFcW0m+egaHC2V3lpO8nWxyYiQ=
github.com/lib/pq v1.10.5/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
Expand Down Expand Up @@ -412,6 +415,7 @@ github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4=
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
Expand Down
10 changes: 8 additions & 2 deletions pkg/ocm/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -820,10 +820,13 @@ func (c *Client) createClusterSpec(config Spec, awsClient aws.Client) (*cmv1.Clu
}

func (c *Client) HibernateCluster(clusterID string) error {
err := c.IsHibernateCapabilityEnabled()
enabled, err := c.IsCapabilityEnabled(HibernateCapability)
if err != nil {
return err
}
if !enabled {
return fmt.Errorf("The '%s' capability is not set for current org", HibernateCapability)
}
_, err = c.ocm.ClustersMgmt().V1().Clusters().Cluster(clusterID).Hibernate().Send()
if err != nil {
return fmt.Errorf("Failed to hibernate the cluster: %v", err)
Expand All @@ -833,10 +836,13 @@ func (c *Client) HibernateCluster(clusterID string) error {
}

func (c *Client) ResumeCluster(clusterID string) error {
err := c.IsHibernateCapabilityEnabled()
enabled, err := c.IsCapabilityEnabled(HibernateCapability)
if err != nil {
return err
}
if !enabled {
return fmt.Errorf("The '%s' capability is not set for current org", HibernateCapability)
}
_, err = c.ocm.ClustersMgmt().V1().Clusters().Cluster(clusterID).Resume().Send()
if err != nil {
return fmt.Errorf("Failed to resume the cluster: %v", err)
Expand Down
19 changes: 10 additions & 9 deletions pkg/ocm/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ func init() {
}

const (
ANY = "any"
HibernateCapability = "capability.organization.hibernate_cluster"
ANY = "any"
HibernateCapability = "capability.organization.hibernate_cluster"
HypershiftCapability = "capability.organization.hypershift"
//Pendo Events
Success = "Success"
Failure = "Failure"
Expand Down Expand Up @@ -236,23 +237,23 @@ func (c *Client) GetCurrentOrganization() (id string, externalID string, err err
return
}

func (c *Client) IsHibernateCapabilityEnabled() error {
func (c *Client) IsCapabilityEnabled(capability string) (enabled bool, err error) {
organizationID, _, err := c.GetCurrentOrganization()
if err != nil {
return err
return
}
isCapabilityEnable, err := c.IsCapabilityEnabled(HibernateCapability, organizationID)
isCapabilityEnable, err := c.isCapabilityEnabled(capability, organizationID)

if err != nil {
return err
return
}
if !isCapabilityEnable {
return fmt.Errorf("The '%s' capability is not set for org '%s'", HibernateCapability, organizationID)
return false, nil
}
return nil
return true, nil
}

func (c *Client) IsCapabilityEnabled(capabilityName string, orgID string) (bool, error) {
func (c *Client) isCapabilityEnabled(capabilityName string, orgID string) (bool, error) {
capabilityResponse, err := c.ocm.AccountsMgmt().V1().Organizations().
Organization(orgID).Get().Parameter("fetchCapabilities", true).Send()

Expand Down
Loading

0 comments on commit 8edd840

Please sign in to comment.