-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathget.go
204 lines (148 loc) · 6.16 KB
/
get.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package get
import (
"encoding/base64"
"fmt"
"github.com/golang/glog"
"github.com/hashicorp/go-version"
"github.com/openshift-kni/eco-goinfra/pkg/clients"
"github.com/openshift-kni/eco-goinfra/pkg/kmm"
"github.com/openshift-kni/eco-goinfra/pkg/nodes"
"github.com/openshift-kni/eco-goinfra/pkg/olm"
"github.com/openshift-kni/eco-gotests/tests/hw-accel/kmm/internal/kmmparams"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
// NumberOfNodesForSelector returns the number or worker nodes.
func NumberOfNodesForSelector(apiClient *clients.Settings, selector map[string]string) (int, error) {
nodeBuilder, err := nodes.List(apiClient, metav1.ListOptions{LabelSelector: labels.Set(selector).String()})
if err != nil {
fmt.Println("could not discover number of nodes")
return 0, err
}
glog.V(kmmparams.KmmLogLevel).Infof("NumberOfNodesForSelector return %v nodes", len(nodeBuilder))
return len(nodeBuilder), nil
}
// ClusterArchitecture returns first node architecture of the nodes that match nodeSelector (e.g. worker nodes).
func ClusterArchitecture(apiClient *clients.Settings, nodeSelector map[string]string) (string, error) {
nodeLabel := "kubernetes.io/arch"
return getLabelFromNodeSelector(apiClient, nodeLabel, nodeSelector)
}
// KernelFullVersion returns first node architecture of the nodes that match nodeSelector (e.g. worker nodes).
func KernelFullVersion(apiClient *clients.Settings, nodeSelector map[string]string) (string, error) {
nodeBuilder, err := nodes.List(apiClient, metav1.ListOptions{LabelSelector: labels.Set(nodeSelector).String()})
if err != nil {
glog.V(kmmparams.KmmLogLevel).Infof("could not discover %v nodes", nodeSelector)
return "", err
}
for _, node := range nodeBuilder {
kernelVersion := node.Object.Status.NodeInfo.KernelVersion
glog.V(kmmparams.KmmLogLevel).Infof("Found kernelVersion '%v' on node '%v'",
kernelVersion, node.Object.Name)
return kernelVersion, nil
}
err = fmt.Errorf("could not find kernelVersion on node")
return "", err
}
func getLabelFromNodeSelector(
apiClient *clients.Settings,
nodeLabel string,
nodeSelector map[string]string) (string, error) {
nodeBuilder, err := nodes.List(apiClient, metav1.ListOptions{LabelSelector: labels.Set(nodeSelector).String()})
// Check if at least one node matching the nodeSelector has the specific nodeLabel label set to true
// For example, look in all the worker nodes for specific label
if err != nil {
glog.V(kmmparams.KmmLogLevel).Infof("could not discover %v nodes", nodeSelector)
return "", err
}
for _, node := range nodeBuilder {
labelValue, ok := node.Object.Labels[nodeLabel]
if ok {
glog.V(kmmparams.KmmLogLevel).Infof("Found label '%v' with label value '%v' on node '%v'",
nodeLabel, labelValue, node.Object.Name)
return labelValue, nil
}
}
err = fmt.Errorf("could not find one node with label '%s'", nodeLabel)
return "", err
}
// MachineConfigPoolName returns machineconfigpool's name for a specified label.
func MachineConfigPoolName(apiClient *clients.Settings) string {
nodeBuilder, err := nodes.List(
apiClient,
metav1.ListOptions{LabelSelector: labels.Set(map[string]string{"kubernetes.io": ""}).String()},
)
if err != nil {
glog.V(kmmparams.KmmLogLevel).Infof("could not discover nodes")
return ""
}
if len(nodeBuilder) == 1 {
glog.V(kmmparams.KmmLogLevel).Infof("Using 'master' as mcp")
return "master"
}
glog.V(kmmparams.KmmLogLevel).Infof("Using 'worker' as mcp")
return "worker"
}
// SigningData returns struct used for creating secrets for module signing.
func SigningData(key string, value string) map[string][]byte {
val, err := base64.StdEncoding.DecodeString(value)
if err != nil {
glog.V(kmmparams.KmmLogLevel).Infof("Error decoding signing key")
}
secretContents := map[string][]byte{key: val}
return secretContents
}
// PreflightImage returns preflightvalidationocp image to be used based on architecture.
func PreflightImage(arch string) string {
if arch == "arm64" {
arch = "aarch64"
}
if arch == "amd64" {
arch = "x86_64"
}
return fmt.Sprintf(kmmparams.PreflightTemplateImage, arch)
}
// PreflightReason returns the reason of a preflightvalidationocp check.
func PreflightReason(apiClient *clients.Settings, preflight, module, nsname string) (string, error) {
pre, _ := kmm.PullPreflightValidationOCP(apiClient, preflight, nsname)
preflightValidationOCP, err := pre.Get()
if err == nil {
reason := preflightValidationOCP.Status.CRStatuses[module].StatusReason
glog.V(kmmparams.KmmLogLevel).Infof("VerificationStatus: %s", reason)
return reason, nil
}
return "", err
}
// ModuleLoadedMessage returns message for a module loaded event.
func ModuleLoadedMessage(module, nsname string) string {
message := fmt.Sprintf("Module %s/%s loaded into the kernel", nsname, module)
glog.V(kmmparams.KmmLogLevel).Infof("Return: '%s'", message)
return message
}
// ModuleUnloadedMessage returns message for a module unloaded event.
func ModuleUnloadedMessage(module, nsname string) string {
message := fmt.Sprintf("Module %s/%s unloaded from the kernel", nsname, module)
glog.V(kmmparams.KmmLogLevel).Infof("Return: '%s'", message)
return message
}
// KmmOperatorVersion returns CSV version of the installed KMM operator.
func KmmOperatorVersion(apiClient *clients.Settings) (ver *version.Version, err error) {
return operatorVersion(apiClient, "kernel", kmmparams.KmmOperatorNamespace)
}
// KmmHubOperatorVersion returns CSV version of the installed KMM-HUB operator.
func KmmHubOperatorVersion(apiClient *clients.Settings) (ver *version.Version, err error) {
return operatorVersion(apiClient, "hub", kmmparams.KmmHubOperatorNamespace)
}
func operatorVersion(apiClient *clients.Settings, namePattern, namespace string) (ver *version.Version, err error) {
csv, err := olm.ListClusterServiceVersionWithNamePattern(apiClient, namePattern,
namespace)
if err != nil {
return nil, err
}
for _, c := range csv {
glog.V(kmmparams.KmmLogLevel).Infof("CSV: %s, Version: %s, Status: %s",
c.Object.Spec.DisplayName, c.Object.Spec.Version, c.Object.Status.Phase)
csvVersion, _ := version.NewVersion(c.Object.Spec.Version.String())
return csvVersion, nil
}
return nil, err
}