Skip to content

Commit 933d29e

Browse files
author
Yuri Diakov
committed
from upstream
1 parent d6d69bc commit 933d29e

File tree

4 files changed

+294
-0
lines changed

4 files changed

+294
-0
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ In this example, we will login to a cluster with id `123456abcdef` in production
108108
```
109109
$ ocm backplane login <cluster> --service
110110
```
111+
### Get Cluster information after login
112+
113+
- Login to the target cluster via backplane and add --cluster-info flag
114+
```
115+
$ ocm backplane cluster login <cluster> --cluster-info
116+
```
111117

112118
### Login to multiple clusters
113119

cmd/ocm-backplane/login/login.go

+12
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ var (
4848
pd string
4949
defaultNamespace string
5050
ohss string
51+
clusterInfo bool
5152
}
5253

5354
// loginType derive the login type based on flags and args
@@ -125,6 +126,11 @@ func init() {
125126
"",
126127
"Login using JIRA Id",
127128
)
129+
flags.BoolVar(
130+
&args.clusterInfo,
131+
"cluster-info",
132+
false, "Print basic cluster information after login",
133+
)
128134

129135
}
130136

@@ -205,6 +211,12 @@ func runLogin(cmd *cobra.Command, argv []string) (err error) {
205211
"ID": clusterID,
206212
"Name": clusterName}).Infoln("Target cluster")
207213

214+
if args.clusterInfo {
215+
if err := login.PrintClusterInfo(clusterID); err != nil {
216+
return fmt.Errorf("failed to print cluster info: %v", err)
217+
}
218+
}
219+
208220
if globalOpts.Manager {
209221
logger.WithField("Cluster ID", clusterID).Debugln("Finding managing cluster")
210222
var isHostedControlPlane bool

pkg/login/printClusterInfo.go

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package login
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/openshift/backplane-cli/pkg/ocm"
7+
logger "github.com/sirupsen/logrus"
8+
)
9+
10+
//displayClusterInfo retrieves and displays basic information about the target cluster.
11+
12+
func PrintClusterInfo(clusterID string) error {
13+
logger := logger.WithField("clusterID", clusterID)
14+
15+
// Retrieve cluster information
16+
clusterInfo, err := ocm.DefaultOCMInterface.GetClusterInfoByID(clusterID)
17+
if err != nil {
18+
return fmt.Errorf("error retrieving cluster info: %w", err)
19+
}
20+
21+
// Display cluster information
22+
printClusterField("Cluster ID:", clusterInfo.ID())
23+
printClusterField("Cluster Name:", clusterInfo.Name())
24+
printClusterField("Cluster Status:", clusterInfo.State())
25+
printClusterField("Cluster Region:", clusterInfo.Region().ID())
26+
printClusterField("Cluster Provider:", clusterInfo.CloudProvider().ID())
27+
printClusterField("Hypershift Enabled:", clusterInfo.Hypershift().Enabled())
28+
printClusterField("Version:", clusterInfo.OpenshiftVersion())
29+
GetLimitedSupportStatus(clusterID)
30+
GetAccessProtectionStatus(clusterID)
31+
32+
logger.Info("Basic cluster information displayed.")
33+
return nil
34+
}
35+
36+
func GetAccessProtectionStatus(clusterID string) string {
37+
ocmConnection, err := ocm.DefaultOCMInterface.SetupOCMConnection()
38+
if err != nil {
39+
logger.Error("Error setting up OCM connection: ", err)
40+
return "Error setting up OCM connection: " + err.Error()
41+
}
42+
if ocmConnection != nil {
43+
defer ocmConnection.Close()
44+
}
45+
enabled, err := ocm.DefaultOCMInterface.IsClusterAccessProtectionEnabled(ocmConnection, clusterID)
46+
if err != nil {
47+
fmt.Println("Error retrieving access protection status: ", err)
48+
return "Error retrieving access protection status: " + err.Error()
49+
}
50+
51+
accessProtectionStatus := "Disabled"
52+
if enabled {
53+
accessProtectionStatus = "Enabled"
54+
}
55+
fmt.Printf("%-25s %s\n", "Access Protection:", accessProtectionStatus)
56+
57+
return accessProtectionStatus
58+
}
59+
60+
func GetLimitedSupportStatus(clusterID string) string {
61+
clusterInfo, err := ocm.DefaultOCMInterface.GetClusterInfoByID(clusterID)
62+
if err != nil {
63+
return "Error retrieving cluster info: " + err.Error()
64+
}
65+
if clusterInfo.Status().LimitedSupportReasonCount() != 0 {
66+
fmt.Printf("%-25s %s", "Limited Support Status: ", "Limited Support\n")
67+
} else {
68+
fmt.Printf("%-25s %s", "Limited Support Status: ", "Fully Supported\n")
69+
}
70+
return fmt.Sprintf("%d", clusterInfo.Status().LimitedSupportReasonCount())
71+
}
72+
73+
func printClusterField(fieldName string, value interface{}) {
74+
fmt.Printf("%-25s %v\n", fieldName, value)
75+
}

pkg/login/printClusterInfo_test.go

+201
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
package login
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"log"
7+
"os"
8+
9+
"github.com/golang/mock/gomock"
10+
. "github.com/onsi/ginkgo"
11+
. "github.com/onsi/gomega"
12+
"github.com/openshift/backplane-cli/pkg/ocm"
13+
ocmMock "github.com/openshift/backplane-cli/pkg/ocm/mocks"
14+
15+
ocmsdk "github.com/openshift-online/ocm-sdk-go"
16+
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
17+
)
18+
19+
var _ = Describe("PrintClusterInfo", func() {
20+
var (
21+
clusterID string
22+
buf *bytes.Buffer
23+
mockOcmInterface *ocmMock.MockOCMInterface
24+
mockCtrl *gomock.Controller
25+
oldStdout *os.File
26+
r, w *os.File
27+
ocmConnection *ocmsdk.Connection
28+
clusterInfo *cmv1.Cluster
29+
)
30+
31+
BeforeEach(func() {
32+
mockCtrl = gomock.NewController(GinkgoT())
33+
mockOcmInterface = ocmMock.NewMockOCMInterface(mockCtrl)
34+
ocmConnection = nil
35+
ocm.DefaultOCMInterface = mockOcmInterface
36+
37+
clusterID = "test-cluster-id"
38+
buf = new(bytes.Buffer)
39+
log.SetOutput(buf)
40+
41+
// Redirect standard output to the buffer
42+
oldStdout = os.Stdout
43+
r, w, _ = os.Pipe()
44+
os.Stdout = w
45+
})
46+
47+
AfterEach(func() {
48+
// Reset the ocm.DefaultOCMInterface to avoid side effects in other tests
49+
ocm.DefaultOCMInterface = nil
50+
})
51+
52+
Context("Cluster protection status", func() {
53+
BeforeEach(func() {
54+
55+
clusterInfo, _ = cmv1.NewCluster().
56+
ID(clusterID).
57+
Name("Test Cluster").
58+
CloudProvider(cmv1.NewCloudProvider().ID("aws")).
59+
State(cmv1.ClusterState("ready")).
60+
Region(cmv1.NewCloudRegion().ID("us-east-1")).
61+
Hypershift(cmv1.NewHypershift().Enabled(false)).
62+
OpenshiftVersion("4.14.8").
63+
Status(cmv1.NewClusterStatus().LimitedSupportReasonCount(0)).
64+
Build()
65+
66+
mockOcmInterface.EXPECT().GetClusterInfoByID(clusterID).Return(clusterInfo, nil).AnyTimes()
67+
mockOcmInterface.EXPECT().SetupOCMConnection().Return(ocmConnection, nil).AnyTimes()
68+
})
69+
70+
It("should print cluster information with access protection disabled", func() {
71+
mockOcmInterface.EXPECT().IsClusterAccessProtectionEnabled(ocmConnection, clusterID).Return(false, nil).AnyTimes()
72+
73+
err := PrintClusterInfo(clusterID)
74+
Expect(err).To(BeNil())
75+
76+
// Capture the output
77+
w.Close()
78+
os.Stdout = oldStdout
79+
_, _ = buf.ReadFrom(r)
80+
81+
output := buf.String()
82+
Expect(output).To(ContainSubstring(fmt.Sprintf("Cluster ID: %s\n", clusterID)))
83+
Expect(output).To(ContainSubstring("Cluster Name: Test Cluster\n"))
84+
Expect(output).To(ContainSubstring("Cluster Status: ready\n"))
85+
Expect(output).To(ContainSubstring("Cluster Region: us-east-1\n"))
86+
Expect(output).To(ContainSubstring("Cluster Provider: aws\n"))
87+
Expect(output).To(ContainSubstring("Hypershift Enabled: false\n"))
88+
Expect(output).To(ContainSubstring("Version: 4.14.8\n"))
89+
Expect(output).To(ContainSubstring("Limited Support Status: Fully Supported\n"))
90+
Expect(output).To(ContainSubstring("Access Protection: Disabled\n"))
91+
})
92+
93+
It("should print cluster information with access protection enabled", func() {
94+
mockOcmInterface.EXPECT().IsClusterAccessProtectionEnabled(ocmConnection, clusterID).Return(true, nil).AnyTimes()
95+
96+
err := PrintClusterInfo(clusterID)
97+
Expect(err).To(BeNil())
98+
99+
// Capture the output
100+
w.Close()
101+
os.Stdout = oldStdout
102+
_, _ = buf.ReadFrom(r)
103+
104+
output := buf.String()
105+
Expect(output).To(ContainSubstring(fmt.Sprintf("Cluster ID: %s\n", clusterID)))
106+
Expect(output).To(ContainSubstring("Cluster Name: Test Cluster\n"))
107+
Expect(output).To(ContainSubstring("Cluster Status: ready\n"))
108+
Expect(output).To(ContainSubstring("Cluster Region: us-east-1\n"))
109+
Expect(output).To(ContainSubstring("Cluster Provider: aws\n"))
110+
Expect(output).To(ContainSubstring("Hypershift Enabled: false\n"))
111+
Expect(output).To(ContainSubstring("Version: 4.14.8\n"))
112+
Expect(output).To(ContainSubstring("Limited Support Status: Fully Supported\n"))
113+
Expect(output).To(ContainSubstring("Access Protection: Enabled\n"))
114+
})
115+
})
116+
117+
Context("Limited Support set to 0", func() {
118+
BeforeEach(func() {
119+
120+
clusterInfo, _ = cmv1.NewCluster().
121+
ID(clusterID).
122+
Name("Test Cluster").
123+
CloudProvider(cmv1.NewCloudProvider().ID("aws")).
124+
State(cmv1.ClusterState("ready")).
125+
Region(cmv1.NewCloudRegion().ID("us-east-1")).
126+
Hypershift(cmv1.NewHypershift().Enabled(false)).
127+
OpenshiftVersion("4.14.8").
128+
Status(cmv1.NewClusterStatus().LimitedSupportReasonCount(0)).
129+
Build()
130+
131+
mockOcmInterface.EXPECT().GetClusterInfoByID(clusterID).Return(clusterInfo, nil).AnyTimes()
132+
mockOcmInterface.EXPECT().SetupOCMConnection().Return(ocmConnection, nil).AnyTimes()
133+
})
134+
135+
It("should print if cluster is Fully Supported", func() {
136+
137+
mockOcmInterface.EXPECT().IsClusterAccessProtectionEnabled(ocmConnection, clusterID).Return(true, nil).AnyTimes()
138+
139+
err := PrintClusterInfo(clusterID)
140+
Expect(err).To(BeNil())
141+
142+
// Capture the output
143+
w.Close()
144+
os.Stdout = oldStdout
145+
_, _ = buf.ReadFrom(r)
146+
147+
output := buf.String()
148+
Expect(output).To(ContainSubstring(fmt.Sprintf("Cluster ID: %s\n", clusterID)))
149+
Expect(output).To(ContainSubstring("Cluster Name: Test Cluster\n"))
150+
Expect(output).To(ContainSubstring("Cluster Status: ready\n"))
151+
Expect(output).To(ContainSubstring("Cluster Region: us-east-1\n"))
152+
Expect(output).To(ContainSubstring("Cluster Provider: aws\n"))
153+
Expect(output).To(ContainSubstring("Hypershift Enabled: false\n"))
154+
Expect(output).To(ContainSubstring("Version: 4.14.8\n"))
155+
Expect(output).To(ContainSubstring("Limited Support Status: Fully Supported\n"))
156+
Expect(output).To(ContainSubstring("Access Protection: Enabled\n"))
157+
})
158+
159+
})
160+
161+
Context("Limited Support set to 1", func() {
162+
BeforeEach(func() {
163+
164+
clusterInfo, _ = cmv1.NewCluster().
165+
ID(clusterID).
166+
Name("Test Cluster").
167+
CloudProvider(cmv1.NewCloudProvider().ID("aws")).
168+
State(cmv1.ClusterState("ready")).
169+
Region(cmv1.NewCloudRegion().ID("us-east-1")).
170+
Hypershift(cmv1.NewHypershift().Enabled(false)).
171+
OpenshiftVersion("4.14.8").
172+
Status(cmv1.NewClusterStatus().LimitedSupportReasonCount(1)).
173+
Build()
174+
175+
mockOcmInterface.EXPECT().GetClusterInfoByID(clusterID).Return(clusterInfo, nil).AnyTimes()
176+
mockOcmInterface.EXPECT().SetupOCMConnection().Return(ocmConnection, nil).AnyTimes()
177+
})
178+
179+
It("should print if cluster is Limited Support", func() {
180+
mockOcmInterface.EXPECT().IsClusterAccessProtectionEnabled(ocmConnection, clusterID).Return(true, nil).AnyTimes()
181+
err := PrintClusterInfo(clusterID)
182+
Expect(err).To(BeNil())
183+
184+
// Capture the output
185+
w.Close()
186+
os.Stdout = oldStdout
187+
_, _ = buf.ReadFrom(r)
188+
189+
output := buf.String()
190+
Expect(output).To(ContainSubstring(fmt.Sprintf("Cluster ID: %s\n", clusterID)))
191+
Expect(output).To(ContainSubstring("Cluster Name: Test Cluster\n"))
192+
Expect(output).To(ContainSubstring("Cluster Status: ready\n"))
193+
Expect(output).To(ContainSubstring("Cluster Region: us-east-1\n"))
194+
Expect(output).To(ContainSubstring("Cluster Provider: aws\n"))
195+
Expect(output).To(ContainSubstring("Hypershift Enabled: false\n"))
196+
Expect(output).To(ContainSubstring("Version: 4.14.8\n"))
197+
Expect(output).To(ContainSubstring("Limited Support Status: Limited Support\n"))
198+
Expect(output).To(ContainSubstring("Access Protection: Enabled\n"))
199+
})
200+
})
201+
})

0 commit comments

Comments
 (0)