/
common.go
320 lines (268 loc) · 9.36 KB
/
common.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Copyright Contributors to the Open Cluster Management project
package common
import (
"context"
"errors"
"flag"
"fmt"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"golang.org/x/mod/semver"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog"
"open-cluster-management.io/governance-policy-propagator/test/utils"
)
var (
KubeconfigHub string
KubeconfigManaged string
UserNamespace string
ClusterNamespace string
ClusterNamespaceOnHub string
PolicyCollectionBranch string
OCMNamespace string
OCMAddOnNamespace string
DefaultTimeoutSeconds int
ManuallyPatchDecisions bool
K8sClient string
IsHosted bool
ClientHub kubernetes.Interface
ClientHubDynamic dynamic.Interface
ClientManaged kubernetes.Interface
ClientManagedDynamic dynamic.Interface
ClientHosting kubernetes.Interface
ClientHostingDynamic dynamic.Interface
)
const MaxTimeoutSeconds = 900 // 15 minutes
func InitFlags(flagset *flag.FlagSet) {
if flagset == nil {
flagset = flag.CommandLine
}
flagset.StringVar(
&KubeconfigHub, "kubeconfig_hub", "../../kubeconfig_hub",
"Location of the kubeconfig to use; defaults to KUBECONFIG if not set",
)
flagset.StringVar(
&KubeconfigManaged, "kubeconfig_managed", "../../kubeconfig_managed",
"Location of the kubeconfig to use; defaults to KUBECONFIG if not set",
)
flagset.BoolVar(
&IsHosted, "is_hosted", false,
"Whether is hosted mode or not",
)
flagset.StringVar(&UserNamespace, "user_namespace", "policy-test", "ns on hub to create root policy")
flagset.StringVar(&ClusterNamespace, "cluster_namespace", "local-cluster", "cluster ns name")
flagset.StringVar(&ClusterNamespaceOnHub, "cluster_namespace_on_hub", "", "cluster ns name on hub")
flagset.StringVar(&OCMNamespace, "ocm_namespace", "open-cluster-management", "ns of ocm installation")
flagset.StringVar(
&OCMAddOnNamespace,
"ocm_addon_namespace",
"open-cluster-management-agent-addon",
"ns of ocm addon installations",
)
flagset.StringVar(
&PolicyCollectionBranch, "policy_collection_branch", "main", "the branch of the policy-collection repo",
)
flagset.IntVar(&DefaultTimeoutSeconds, "timeout_seconds", 30, "Timeout seconds for assertion")
flagset.BoolVar(
&ManuallyPatchDecisions, "patch_decisions", true,
"Whether to 'manually' patch PlacementRules with PlacementDecisions "+
"(set to false if the PlacementRule controller is running)",
)
flagset.StringVar(
&K8sClient, "k8s_client", "oc",
"Which k8s client to use for some tests - `oc`, `kubectl`, "+
"or something else entirely",
)
}
// Initializes the Hub and Managed Clients. Should be called after InitFlags,
// and before any tests using common functions are run.
func InitInterfaces(hubConfig, managedConfig string, isHosted bool) {
if isHosted {
ClientHosting = NewKubeClient("", hubConfig, "")
ClientHostingDynamic = NewKubeClientDynamic("", hubConfig, "")
} else {
ClientHosting = NewKubeClient("", managedConfig, "")
ClientHostingDynamic = NewKubeClientDynamic("", managedConfig, "")
}
if ClusterNamespaceOnHub == "" {
ClusterNamespaceOnHub = ClusterNamespace
}
ClientHub = NewKubeClient("", hubConfig, "")
ClientHubDynamic = NewKubeClientDynamic("", hubConfig, "")
ClientManaged = NewKubeClient("", managedConfig, "")
ClientManagedDynamic = NewKubeClientDynamic("", managedConfig, "")
}
func NewKubeClient(url, kubeconfig, context string) kubernetes.Interface {
klog.V(5).Infof("Create kubeclient for url %s using kubeconfig path %s\n", url, kubeconfig)
config, err := LoadConfig(url, kubeconfig, context)
if err != nil {
panic(err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err)
}
return clientset
}
func NewKubeClientDynamic(url, kubeconfig, context string) dynamic.Interface {
klog.V(5).Infof("Create kubeclient dynamic for url %s using kubeconfig path %s\n", url, kubeconfig)
config, err := LoadConfig(url, kubeconfig, context)
if err != nil {
panic(err)
}
clientset, err := dynamic.NewForConfig(config)
if err != nil {
panic(err)
}
return clientset
}
func LoadConfig(url, kubeconfig, context string) (*rest.Config, error) {
if kubeconfig == "" {
kubeconfig = os.Getenv("KUBECONFIG")
}
klog.V(5).Infof("Kubeconfig path %s\n", kubeconfig)
// If we have an explicit indication of where the kubernetes config lives, read that.
if kubeconfig != "" {
if context == "" {
return clientcmd.BuildConfigFromFlags(url, kubeconfig)
}
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfig},
&clientcmd.ConfigOverrides{
CurrentContext: context,
}).ClientConfig()
}
// If not, try the in-cluster config.
if c, err := rest.InClusterConfig(); err == nil {
return c, nil
}
// If no in-cluster config, try the default location in the user's home directory.
if usr, err := user.Current(); err == nil {
klog.V(5).Infof(
"clientcmd.BuildConfigFromFlags for url %s using %s\n",
url,
filepath.Join(usr.HomeDir, ".kube", "config"),
)
if c, err := clientcmd.BuildConfigFromFlags("", filepath.Join(usr.HomeDir, ".kube", "config")); err == nil {
return c, nil
}
}
return nil, fmt.Errorf("could not create a valid kubeconfig")
}
func oc(args ...string) (string, error) {
// Determine whether output should be logged
printOutput := true
for _, a := range args {
if a == "whoami" || strings.HasPrefix(a, "secret") {
printOutput = false
break
}
}
output, err := exec.Command(K8sClient, args...).Output()
if len(args) > 0 && printOutput {
klog.V(2).Infof("OC command output %s\n", output)
}
var exitError *exec.ExitError
ok := errors.As(err, &exitError)
if ok {
if exitError.Stderr == nil {
return string(output), nil
}
return string(output), fmt.Errorf(string(exitError.Stderr))
}
return string(output), err
}
// Runs the given oc/kubectl command against the configured hub cluster.
// Prints and returns the stdout from the command.
// If the command fails (non-zero exit code) and stderr was populated, that
// content will be returned in the error.
func OcHub(args ...string) (string, error) {
args = append([]string{"--kubeconfig=" + KubeconfigHub}, args...)
return oc(args...)
}
// Runs the given oc/kubectl command against the configured managed cluster.
// Prints and returns the stdout from the command.
// If the command fails (non-zero exit code) and stderr was populated, that
// content will be returned in the error.
func OcManaged(args ...string) (string, error) {
args = append([]string{"--kubeconfig=" + KubeconfigManaged}, args...)
return oc(args...)
}
func OcHosting(args ...string) (string, error) {
if IsHosted {
args = append([]string{"--kubeconfig=" + KubeconfigHub}, args...)
} else {
args = append([]string{"--kubeconfig=" + KubeconfigManaged}, args...)
}
return oc(args...)
}
func OutputDebugInfo(testName string, kubeconfig string, additionalResources ...string) {
GinkgoWriter.Printf("%s test Kubernetes info:\n", testName)
resources := []string{
"policies.policy.open-cluster-management.io",
"placementrules.apps.open-cluster-management.io",
"placements.cluster.open-cluster-management.io",
"placementbindings.policy.open-cluster-management.io",
}
resources = append(resources, additionalResources...)
for _, resource := range resources {
_, _ = utils.KubectlWithOutput("get", resource, "--all-namespaces", "-o", "yaml", "--kubeconfig="+kubeconfig)
}
}
// IsAtLeastVersion detects OCP versions given an x.y version lower bound
func IsAtLeastVersion(minVersion string) bool {
clusterVersion, err := ClientManagedDynamic.Resource(GvrClusterVersion).Get(
context.TODO(),
"version",
metav1.GetOptions{},
)
if err != nil {
if k8serrors.IsNotFound(err) {
// no version CR, not ocp
klog.V(5).Info("This is not an OCP cluster")
} else {
klog.Infof("Encountered an error fetching the OCP version: %v", err)
}
return false
}
version, _, _ := unstructured.NestedString(clusterVersion.Object, "status", "desired", "version")
klog.V(5).Info("OCP Version " + version)
// Convert to valid semantic versions by adding the "v" prefix
minSemVer := fmt.Sprintf("v%s", minVersion)
ocpSemVer := semver.MajorMinor(fmt.Sprintf("v%s", version))
// Compare returns: 0 if ver1 == ver2, -1 if ver1 < ver2, or +1 if ver1 > ver2
return semver.Compare(ocpSemVer, minSemVer) >= 0
}
func CleanupHubNamespace(namespace string) {
By("Deleting namespace " + namespace)
err := ClientHub.CoreV1().Namespaces().Delete(context.TODO(), namespace, metav1.DeleteOptions{})
if !k8serrors.IsNotFound(err) {
ExpectWithOffset(1, err).ShouldNot(HaveOccurred())
}
// Wait for the namespace to be fully deleted before proceeding.
EventuallyWithOffset(1,
func() bool {
_, err := ClientHub.CoreV1().Namespaces().Get(
context.TODO(), namespace, metav1.GetOptions{},
)
isNotFound := k8serrors.IsNotFound(err)
if !isNotFound && err != nil {
GinkgoWriter.Printf("'%s' namespace 'get' error: %w", err)
}
return isNotFound
},
DefaultTimeoutSeconds*6,
1,
).Should(BeTrue(), fmt.Sprintf("Namespace %s should be deleted.", namespace))
}