/
utils.go
271 lines (220 loc) · 8.5 KB
/
utils.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
package tests
import (
"context"
"flag"
"fmt"
"os"
"sync"
"time"
"github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega" //nolint dot-imports
openshiftconfigv1 "github.com/openshift/api/config/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/utils/net"
"kubevirt.io/client-go/kubecli"
"kubevirt.io/kubevirt/tests/flags"
kvtutil "kubevirt.io/kubevirt/tests/util"
"github.com/kubevirt/hyperconverged-cluster-operator/api/v1beta1"
hcoutil "github.com/kubevirt/hyperconverged-cluster-operator/pkg/util"
)
var KubeVirtStorageClassLocal string
const resource = "hyperconvergeds"
func init() {
flag.StringVar(&KubeVirtStorageClassLocal, "storage-class-local", "local", "Storage provider to use for tests which want local storage")
}
// GetJobTypeEnvVar returns "JOB_TYPE" environment variable
func GetJobTypeEnvVar() string {
return (os.Getenv("JOB_TYPE"))
}
func FlagParse() {
flag.Parse()
}
func BeforeEach() {
virtClient, err := kubecli.GetKubevirtClient()
Expect(err).ToNot(HaveOccurred())
deleteAllResources(virtClient.RestClient(), "virtualmachines")
deleteAllResources(virtClient.RestClient(), "virtualmachineinstances")
deleteAllResources(virtClient.CoreV1().RESTClient(), "persistentvolumeclaims")
}
func SkipIfNotOpenShift(cli kubecli.KubevirtClient, testName string) {
isOpenShift := false
Eventually(func() error {
var err error
isOpenShift, err = IsOpenShift(cli)
return err
}).WithTimeout(10*time.Second).WithPolling(time.Second).Should(Succeed(), "failed to check if running on an openshift cluster")
if !isOpenShift {
ginkgo.Skip(fmt.Sprintf("Skipping %s tests when the cluster is not OpenShift", testName))
}
}
func SkipIfNotSingleStackIPv6OpenShift(cli kubecli.KubevirtClient, testName string) {
isSingleStackIPv6, err := IsOpenShiftSingleStackIPv6(cli)
Expect(err).ToNot(HaveOccurred())
if !isSingleStackIPv6 {
ginkgo.Skip(fmt.Sprintf("Skipping %s tests since the OpenShift cluster is not single stack IPv6", testName))
}
}
type cacheIsOpenShift struct {
isOpenShift bool
hasSet bool
lock sync.Mutex
}
func (c *cacheIsOpenShift) IsOpenShift(cli kubecli.KubevirtClient) (bool, error) {
c.lock.Lock()
defer c.lock.Unlock()
if c.hasSet {
return c.isOpenShift, nil
}
s := scheme.Scheme
_ = openshiftconfigv1.Install(s)
s.AddKnownTypes(openshiftconfigv1.GroupVersion)
clusterVersion := &openshiftconfigv1.ClusterVersion{
ObjectMeta: metav1.ObjectMeta{
Name: "version",
},
}
err := cli.RestClient().Get().
Resource("clusterversions").
Name("version").
AbsPath("/apis", openshiftconfigv1.GroupVersion.Group, openshiftconfigv1.GroupVersion.Version).
Timeout(10 * time.Second).
Do(context.TODO()).Into(clusterVersion)
if err == nil {
c.isOpenShift = true
c.hasSet = true
return c.isOpenShift, nil
}
if meta.IsNoMatchError(err) || apierrors.IsNotFound(err) {
c.isOpenShift = false
c.hasSet = true
return c.isOpenShift, nil
}
return false, err
}
func (c *cacheIsOpenShift) IsOpenShiftSingleStackIPv6(cli kubecli.KubevirtClient) (bool, error) {
// confirm we are on OpenShift
isOpenShift, err := c.IsOpenShift(cli)
if err != nil || !isOpenShift {
return false, err
}
s := scheme.Scheme
_ = openshiftconfigv1.Install(s)
s.AddKnownTypes(openshiftconfigv1.GroupVersion)
clusterNetwork := &openshiftconfigv1.Network{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster",
},
}
gvr := schema.GroupVersionResource{
Group: openshiftconfigv1.GroupVersion.Group,
Version: openshiftconfigv1.GroupVersion.Version,
Resource: "networks",
}
clustnet, err := cli.DynamicClient().
Resource(gvr).
Get(context.TODO(), "cluster", metav1.GetOptions{})
if err != nil {
return false, err
}
err = runtime.DefaultUnstructuredConverter.FromUnstructured(clustnet.Object, clusterNetwork)
if err != nil {
return false, err
}
cn := clusterNetwork.Status.ClusterNetwork
isSingleStackIPv6 := len(cn) == 1 && net.IsIPv6CIDRString(cn[0].CIDR)
return isSingleStackIPv6, nil
}
var isOpenShiftCache cacheIsOpenShift
func IsOpenShift(cli kubecli.KubevirtClient) (bool, error) {
return isOpenShiftCache.IsOpenShift(cli)
}
func IsOpenShiftSingleStackIPv6(cli kubecli.KubevirtClient) (bool, error) {
return isOpenShiftCache.IsOpenShiftSingleStackIPv6(cli)
}
// GetHCO reads the HCO CR from the APIServer with a DynamicClient
func GetHCO(ctx context.Context, client kubecli.KubevirtClient) *v1beta1.HyperConverged {
hco := &v1beta1.HyperConverged{}
hcoGVR := schema.GroupVersionResource{Group: v1beta1.SchemeGroupVersion.Group, Version: v1beta1.SchemeGroupVersion.Version, Resource: resource}
unstHco, err := client.DynamicClient().Resource(hcoGVR).Namespace(flags.KubeVirtInstallNamespace).Get(ctx, hcoutil.HyperConvergedName, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
err = runtime.DefaultUnstructuredConverter.FromUnstructured(unstHco.Object, hco)
Expect(err).ToNot(HaveOccurred())
return hco
}
// UpdateHCORetry updates the HCO CR in a safe way internally calling UpdateHCO
// UpdateHCORetry internally uses an async Eventually block refreshing the in-memory
// object if needed and setting there Spec, Annotations, Finalizers and Labels from the
// input object.
// UpdateHCORetry should be preferred over UpdateHCO to reduce test flakiness due to
// inevitable concurrency conflicts
func UpdateHCORetry(ctx context.Context, client kubecli.KubevirtClient, input *v1beta1.HyperConverged) *v1beta1.HyperConverged {
var output *v1beta1.HyperConverged
var err error
Eventually(func() error {
hco := GetHCO(ctx, client)
input.Spec.DeepCopyInto(&hco.Spec)
hco.ObjectMeta.Annotations = input.ObjectMeta.Annotations
hco.ObjectMeta.Finalizers = input.ObjectMeta.Finalizers
hco.ObjectMeta.Labels = input.ObjectMeta.Labels
output, err = UpdateHCO(ctx, client, hco)
return err
}, 10*time.Second, time.Second).Should(Succeed())
return output
}
// UpdateHCO updates the HCO CR using a DynamicClient, it can return errors on failures
func UpdateHCO(ctx context.Context, client kubecli.KubevirtClient, input *v1beta1.HyperConverged) (*v1beta1.HyperConverged, error) {
hcoGVR := schema.GroupVersionResource{Group: input.GroupVersionKind().Group, Version: input.GroupVersionKind().Version, Resource: resource}
hcoNamespace := input.Namespace
unstructuredHco := &unstructured.Unstructured{}
hco := GetHCO(ctx, client)
input.Spec.DeepCopyInto(&hco.Spec)
hco.ObjectMeta.Annotations = input.ObjectMeta.Annotations
hco.ObjectMeta.Finalizers = input.ObjectMeta.Finalizers
hco.ObjectMeta.Labels = input.ObjectMeta.Labels
hco.Status = v1beta1.HyperConvergedStatus{} // to silence warning about unknown fields.
object, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&hco)
if err != nil {
return nil, err
}
unstructuredHco = &unstructured.Unstructured{Object: object}
unstructuredHco, err = client.DynamicClient().Resource(hcoGVR).Namespace(hcoNamespace).Update(ctx, unstructuredHco, metav1.UpdateOptions{})
if err != nil {
return nil, err
}
output := &v1beta1.HyperConverged{}
err = runtime.DefaultUnstructuredConverter.FromUnstructured(unstructuredHco.Object, output)
if err != nil {
return nil, err
}
return output, nil
}
// PatchHCO updates the HCO CR using a DynamicClient, it can return errors on failures
func PatchHCO(ctx context.Context, cl kubecli.KubevirtClient, patch []byte) error {
hcoGVR := schema.GroupVersionResource{Group: v1beta1.SchemeGroupVersion.Group, Version: v1beta1.SchemeGroupVersion.Version, Resource: resource}
_, err := cl.DynamicClient().Resource(hcoGVR).Namespace(flags.KubeVirtInstallNamespace).Patch(ctx, hcoutil.HyperConvergedName, types.JSONPatchType, patch, metav1.PatchOptions{})
return err
}
func RestoreDefaults(ctx context.Context, cli kubecli.KubevirtClient) {
Eventually(PatchHCO).
WithArguments(ctx, cli, []byte(`[{"op": "replace", "path": "/spec", "value": {}}]`)).
WithOffset(1).
WithTimeout(time.Second * 5).
WithPolling(time.Millisecond * 100).
Should(Succeed())
}
func deleteAllResources(restClient rest.Interface, resourceName string) {
Eventually(func() bool {
err := restClient.Delete().Namespace(kvtutil.NamespaceTestDefault).Resource(resourceName).Do(context.TODO()).Error()
return err == nil || apierrors.IsNotFound(err)
}).WithTimeout(time.Minute).
WithPolling(time.Second).
Should(BeTrue())
}