Skip to content

Commit

Permalink
adding flag display-cluster-info": in bp config (#578)
Browse files Browse the repository at this point in the history
Co-authored-by: Yuri Diakov <[email protected]>
  • Loading branch information
diakovnec and Yuri Diakov authored Dec 16, 2024
1 parent 5695224 commit 47cfe05
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 3 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,21 @@ In this example, we will login to a cluster with id `123456abcdef` in production
```
$ ocm backplane login <cluster> --service
```
### Get Cluster information after login
### Get cluster information after login

- Login to the target cluster via backplane and add --cluster-info flag
- Login to the target cluster via backplane and add `--cluster-info` flag
```
$ ocm backplane cluster login <cluster> --cluster-info
```

- Set a `"display-cluster-info": true` flag in the backplane config for cluster info to be auto printed.
> Note: `"display-cluster-info": true` has to be set as a `boolean` value.
```
{
"proxy-url": "your-proxy-url",
"display-cluster-info": true
}
```
### Login to multiple clusters

Logging into multiple clusters via different terminal instances.
Expand Down
6 changes: 6 additions & 0 deletions cmd/ocm-backplane/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ func runLogin(cmd *cobra.Command, argv []string) (err error) {
}
}

if bpConfig.DisplayClusterInfo {
if err := login.PrintClusterInfo(clusterID); err != nil {
return fmt.Errorf("failed to print cluster info: %v", err)
}
}

if globalOpts.Manager {
logger.WithField("Cluster ID", clusterID).Debugln("Finding managing cluster")
var isHostedControlPlane bool
Expand Down
44 changes: 44 additions & 0 deletions cmd/ocm-backplane/login/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,51 @@ var _ = Describe("Login command", func() {
BeforeEach(func() {
globalOpts.Manager = false
globalOpts.Service = false
args.clusterInfo = false

})

It("when display cluster info is set to true", func() {
err := utils.CreateTempKubeConfig(nil)
args.defaultNamespace = "default"
args.clusterInfo = true
Expect(err).To(BeNil())
mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes()
mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil)
mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes()
mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil)
mockClientUtil.EXPECT().MakeRawBackplaneAPIClientWithAccessToken(backplaneAPIURI, testToken).Return(mockClient, nil)
mockClient.EXPECT().LoginCluster(gomock.Any(), gomock.Eq(trueClusterID)).Return(fakeResp, nil)
mockOcmInterface.EXPECT().GetClusterInfoByID(gomock.Any()).Return(mockCluster, nil).Times(2)
mockOcmInterface.EXPECT().SetupOCMConnection().Return(nil, nil)
mockOcmInterface.EXPECT().IsClusterAccessProtectionEnabled(gomock.Any(), trueClusterID).Return(false, nil)

err = runLogin(nil, []string{testClusterID})

Expect(err).To(BeNil())
})

It("should fail to print cluster info when is set to true and PrintClusterInfo returns an error", func() {
err := utils.CreateTempKubeConfig(nil)
Expect(err).To(BeNil())
args.defaultNamespace = "default"
args.clusterInfo = true

mockOcmInterface.EXPECT().GetOCMEnvironment().Return(ocmEnv, nil).AnyTimes()
mockOcmInterface.EXPECT().GetTargetCluster(testClusterID).Return(trueClusterID, testClusterID, nil)
mockOcmInterface.EXPECT().IsClusterHibernating(gomock.Eq(trueClusterID)).Return(false, nil).AnyTimes()
mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil).AnyTimes()
mockClientUtil.EXPECT().MakeRawBackplaneAPIClientWithAccessToken(backplaneAPIURI, testToken).Return(mockClient, nil).AnyTimes()
mockClient.EXPECT().LoginCluster(gomock.Any(), gomock.Eq(trueClusterID)).Return(fakeResp, nil).AnyTimes()

// Mock PrintClusterInfo to return an error
mockOcmInterface.EXPECT().GetClusterInfoByID(gomock.Any()).Return(nil, errors.New("mock error"))

err = runLogin(nil, []string{testClusterID})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("failed to print cluster info"))
})

It("when running with a simple case should work as expected", func() {
err := utils.CreateTempKubeConfig(nil)
Expect(err).To(BeNil())
Expand Down
2 changes: 2 additions & 0 deletions pkg/cli/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type BackplaneConfiguration struct {
JiraConfigForAccessRequests AccessRequestsJiraConfiguration `json:"jira-config-for-access-requests"`
VPNCheckEndpoint string `json:"vpn-check-endpoint"`
ProxyCheckEndpoint string `json:"proxy-check-endpoint"`
DisplayClusterInfo bool `json:"display-cluster-info"`
}

const (
Expand Down Expand Up @@ -150,6 +151,7 @@ func GetBackplaneConfiguration() (bpConfig BackplaneConfiguration, err error) {

bpConfig.SessionDirectory = viper.GetString("session-dir")
bpConfig.AssumeInitialArn = viper.GetString("assume-initial-arn")
bpConfig.DisplayClusterInfo = viper.GetBool("display-cluster-info")

// pagerDuty token is optional
pagerDutyAPIKey := viper.GetString("pd-key")
Expand Down
20 changes: 20 additions & 0 deletions pkg/cli/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ func TestGetBackplaneConfig(t *testing.T) {
t.Errorf("expected to return the explicitly defined proxy %v instead of the default one %v", userDefinedProxy, config.ProxyURL)
}
})

t.Run("display cluster info is true", func(t *testing.T) {
viper.Set("display-cluster-info", true)
svr := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("dummy data"))
}))

userDefinedProxy := "example-proxy"
t.Setenv("BACKPLANE_URL", svr.URL)
t.Setenv("HTTPS_PROXY", userDefinedProxy)
config, err := GetBackplaneConfiguration()
if err != nil {
t.Error(err)
}

if !config.DisplayClusterInfo {
t.Errorf("expected DisplayClusterInfo to be true, got %v", config.DisplayClusterInfo)
}
})

}

func TestGetBackplaneConnection(t *testing.T) {
Expand Down

0 comments on commit 47cfe05

Please sign in to comment.