Skip to content

Commit

Permalink
OCM-11565 | fix: include zero egress status in cluster describe command
Browse files Browse the repository at this point in the history
  • Loading branch information
philipwu08 committed Oct 9, 2024
1 parent 55057af commit 9090c05
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
27 changes: 27 additions & 0 deletions cmd/describe/cluster/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
122 changes: 122 additions & 0 deletions cmd/describe/cluster/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 9090c05

Please sign in to comment.