forked from project-codeflare/codeflare-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocm.go
98 lines (76 loc) · 3.14 KB
/
ocm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
Copyright 2023.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package support
import (
"fmt"
"os"
"github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ocmsdk "github.com/openshift-online/ocm-sdk-go"
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
)
func CreateOCMConnection(test Test) *ocmsdk.Connection {
secretNamespace, secretName := GetInstascaleOcmSecret()
instascaleOCMSecret, err := test.Client().Core().CoreV1().Secrets(secretNamespace).Get(test.Ctx(), secretName, metav1.GetOptions{})
test.Expect(err).NotTo(gomega.HaveOccurred())
ocmToken := string(instascaleOCMSecret.Data["token"])
test.T().Logf("Retrieved Secret %s/%s successfully", instascaleOCMSecret.Namespace, instascaleOCMSecret.Name)
connection, err := buildOCMConnection(ocmToken)
test.Expect(err).NotTo(gomega.HaveOccurred())
return connection
}
func buildOCMConnection(secret string) (*ocmsdk.Connection, error) {
connection, err := ocmsdk.NewConnectionBuilder().
Tokens(secret).
Build()
if err != nil {
fmt.Fprintf(os.Stderr, "Can't build connection: %v\n", err)
return nil, err
}
return connection, nil
}
func MachinePools(t Test, connection *ocmsdk.Connection) func(g gomega.Gomega) []*cmv1.MachinePool {
osdClusterId, found := GetClusterId()
t.Expect(found).To(gomega.BeTrue(), "OSD cluster id not found, please configure environment properly")
return func(g gomega.Gomega) []*cmv1.MachinePool {
machinePoolsListResponse, err := connection.ClustersMgmt().V1().Clusters().Cluster(osdClusterId).MachinePools().List().Send()
g.Expect(err).NotTo(gomega.HaveOccurred())
return machinePoolsListResponse.Items().Slice()
}
}
func GetMachinePools(t Test, connection *ocmsdk.Connection) []*cmv1.MachinePool {
t.T().Helper()
return MachinePools(t, connection)(t)
}
func MachinePoolId(machinePool *cmv1.MachinePool) string {
return machinePool.ID()
}
func MachinePoolLabels(machinePool *cmv1.MachinePool) map[string]string {
return machinePool.Labels()
}
func NodePools(t Test, connection *ocmsdk.Connection) func(g gomega.Gomega) []*cmv1.NodePool {
clusterId, found := GetClusterId()
t.Expect(found).To(gomega.BeTrue(), "Cluster id not found, please configure environment properly")
return func(g gomega.Gomega) []*cmv1.NodePool {
nodePoolsListResponse, err := connection.ClustersMgmt().V1().Clusters().Cluster(clusterId).NodePools().List().Send()
g.Expect(err).NotTo(gomega.HaveOccurred())
return nodePoolsListResponse.Items().Slice()
}
}
func GetNodePools(t Test, connection *ocmsdk.Connection) []*cmv1.NodePool {
t.T().Helper()
return NodePools(t, connection)(t)
}
func NodePoolLabels(nodePool *cmv1.NodePool) map[string]string {
return nodePool.Labels()
}