From 9090c0570e6f62ea9a2c84e1165b9da8c2fa148d Mon Sep 17 00:00:00 2001 From: philipwu08 Date: Wed, 2 Oct 2024 15:15:52 -0400 Subject: [PATCH] OCM-11565 | fix: include zero egress status in cluster describe command --- cmd/describe/cluster/cmd.go | 27 +++++++ cmd/describe/cluster/cmd_test.go | 122 +++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+) diff --git a/cmd/describe/cluster/cmd.go b/cmd/describe/cluster/cmd.go index 3ef2d10f1..99ac03daf 100644 --- a/cmd/describe/cluster/cmd.go +++ b/cmd/describe/cluster/cmd.go @@ -23,6 +23,7 @@ import ( "os" "strconv" "strings" + "time" "github.com/aws/aws-sdk-go-v2/aws/arn" ocmConsts "github.com/openshift-online/ocm-common/pkg/ocm/consts" @@ -511,6 +512,15 @@ func run(cmd *cobra.Command, argv []string) { "Registry Configuration:\n"+ "%s\n", str, registryConfigOutput) } + + zeroEgressStatus, err := getZeroEgressStatus(r, cluster) + if err != nil { + r.Reporter.Errorf("Failed to get zero egress info: %v", err) + os.Exit(1) + } + if zeroEgressStatus != "" { + str = fmt.Sprintf("%s"+"%s\n", str, zeroEgressStatus) + } } if cluster.Status().State() == cmv1.ClusterStateError { @@ -935,3 +945,20 @@ func getRolePolicyBindings(roleARN string, rolePolicyDetails map[string][]aws.Po } return str, nil } + +func getZeroEgressStatus(r *rosa.Runtime, cluster *cmv1.Cluster) (string, error) { + techPreviewMsg, err := r.OCMClient.GetTechnologyPreviewMessage("hcp-zero-egress", time.Now()) + if err != nil { + return "", fmt.Errorf("Failed to get technology preview message for zero egress: %v", err) + } + if techPreviewMsg != "" { + zeroEgressOutput := DisabledOutput + zeroEgressPropVal, ok := cluster.Properties()["zero_egress"] + if ok && zeroEgressPropVal == "true" { + zeroEgressOutput = EnabledOutput + } + str := fmt.Sprintf("%s: %s\n", techPreviewMsg, zeroEgressOutput) + return str, nil + } + return "", nil +} diff --git a/cmd/describe/cluster/cmd_test.go b/cmd/describe/cluster/cmd_test.go index ce298c979..71eaf0f4e 100644 --- a/cmd/describe/cluster/cmd_test.go +++ b/cmd/describe/cluster/cmd_test.go @@ -2,13 +2,22 @@ package cluster import ( "encoding/json" + "net/http" "time" . "github.com/onsi/ginkgo/v2/dsl/core" . "github.com/onsi/ginkgo/v2/dsl/decorators" . "github.com/onsi/ginkgo/v2/dsl/table" . "github.com/onsi/gomega" + "github.com/onsi/gomega/ghttp" + sdk "github.com/openshift-online/ocm-sdk-go" cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" + "github.com/openshift-online/ocm-sdk-go/logging" + . "github.com/openshift-online/ocm-sdk-go/testing" + + "github.com/openshift/rosa/pkg/aws" + "github.com/openshift/rosa/pkg/ocm" + "github.com/openshift/rosa/pkg/rosa" ) const ( @@ -168,6 +177,119 @@ var _ = Describe("getClusterRegistryConfig", func() { }) }) +var _ = Describe("getZeroEgressStatus", func() { + var ( + ssoServer, apiServer *ghttp.Server + r *rosa.Runtime + + // GET /api/clusters_mgmt/v1/products/rosa/technology_previews/hcp-zero-egress + zeroEgressResp = ` + { + "kind":"ProductTechnologyPreview", + "id":"hcp-zero-egress", + "href":"/api/clusters_mgmt/v1/products/rosa/technology_previews/hcp-zero-egress", + "end_date":"2024-11-16T00:00:00Z", + "additional_text":"Zero Egress" + }` + ) + + BeforeEach(func() { + ssoServer = MakeTCPServer() + apiServer = MakeTCPServer() + apiServer.SetAllowUnhandledRequests(true) + apiServer.SetUnhandledRequestStatusCode(http.StatusInternalServerError) + + // Create the token: + accessToken := MakeTokenString("Bearer", 15*time.Minute) + + // Prepare the server: + ssoServer.AppendHandlers( + RespondWithAccessToken(accessToken), + ) + logger, err := logging.NewGoLoggerBuilder(). + Debug(false). + Build() + Expect(err).ToNot(HaveOccurred()) + // Set up the connection with the fake config + connection, err := sdk.NewConnectionBuilder(). + Logger(logger). + Tokens(accessToken). + URL(apiServer.URL()). + Build() + // Initialize client object + Expect(err).To(BeNil()) + ocmClient := ocm.NewClientWithConnection(connection) + + r = rosa.NewRuntime() + r.OCMClient = ocmClient + r.Creator = &aws.Creator{ + ARN: "fake", + AccountID: "123", + IsSTS: false, + } + }) + + It("Should include zero egress enabled in the output", func() { + // GET /api/clusters_mgmt/v1/products/rosa/technology_previews/hcp-zero-egress + apiServer.AppendHandlers( + RespondWithJSON( + http.StatusOK, + zeroEgressResp, + ), + ) + + mockCluster, err := cmv1.NewCluster().Properties(map[string]string{"zero_egress": "true"}).Build() + Expect(err).NotTo(HaveOccurred()) + output, err := getZeroEgressStatus(r, mockCluster) + Expect(err).NotTo(HaveOccurred()) + expectedOutput := "Zero Egress: Enabled\n" + Expect(output).To(Equal(expectedOutput)) + }) + It("Should include zero egress disabled in the output", func() { + // GET /api/clusters_mgmt/v1/products/rosa/technology_previews/hcp-zero-egress + apiServer.AppendHandlers( + RespondWithJSON( + http.StatusOK, + zeroEgressResp, + ), + ) + + mockCluster, err := cmv1.NewCluster().Properties(map[string]string{"zero_egress": "false"}).Build() + Expect(err).NotTo(HaveOccurred()) + output, err := getZeroEgressStatus(r, mockCluster) + Expect(err).NotTo(HaveOccurred()) + expectedOutput := "Zero Egress: Disabled\n" + Expect(output).To(Equal(expectedOutput)) + }) + It("Should not include zero egress in the output", func() { + // GET /api/clusters_mgmt/v1/products/rosa/technology_previews/hcp-zero-egress + apiServer.AppendHandlers( + RespondWithJSON( + http.StatusNotFound, "", + ), + ) + + mockCluster, err := cmv1.NewCluster().Properties(map[string]string{"zero_egress": "true"}).Build() + Expect(err).NotTo(HaveOccurred()) + output, err := getZeroEgressStatus(r, mockCluster) + Expect(err).ToNot(HaveOccurred()) + Expect(output).To(Equal("")) + }) + It("Should not include zero egress in the output", func() { + // GET /api/clusters_mgmt/v1/products/rosa/technology_previews/hcp-zero-egress + apiServer.AppendHandlers( + RespondWithJSON( + http.StatusInternalServerError, "", + ), + ) + + mockCluster, err := cmv1.NewCluster().Properties(map[string]string{"zero_egress": "true"}).Build() + Expect(err).NotTo(HaveOccurred()) + _, err = getZeroEgressStatus(r, mockCluster) + Expect(err).To(HaveOccurred()) + }) +}) + func printJson(cluster func() *cmv1.Cluster, upgrade func() *cmv1.UpgradePolicy, state func() *cmv1.UpgradePolicyState,