forked from kubevirt/kubevirt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock_config.go
118 lines (96 loc) · 3.86 KB
/
mock_config.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
package testutils
import (
"runtime"
v1 "k8s.io/api/core/v1"
extv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/client-go/tools/cache"
KVv1 "kubevirt.io/api/core/v1"
virtconfig "kubevirt.io/kubevirt/pkg/virt-config"
)
const (
configMapName = "kubevirt-config"
namespace = "kubevirt"
)
func NewFakeClusterConfig(cfgMap *v1.ConfigMap) (*virtconfig.ClusterConfig, cache.SharedIndexInformer, cache.SharedIndexInformer, cache.SharedIndexInformer) {
return NewFakeClusterConfigWithCPUArch(cfgMap, runtime.GOARCH)
}
func NewFakeClusterConfigWithCPUArch(cfgMap *v1.ConfigMap, CPUArch string) (*virtconfig.ClusterConfig, cache.SharedIndexInformer, cache.SharedIndexInformer, cache.SharedIndexInformer) {
configMapInformer, _ := NewFakeInformerFor(&v1.ConfigMap{})
crdInformer, _ := NewFakeInformerFor(&extv1.CustomResourceDefinition{})
kubeVirtInformer, _ := NewFakeInformerFor(&KVv1.KubeVirt{})
if cfgMap != nil {
copy := copy(cfgMap)
configMapInformer.GetStore().Add(copy)
}
AddDataVolumeAPI(crdInformer)
return virtconfig.NewClusterConfigWithCPUArch(configMapInformer, crdInformer, kubeVirtInformer, namespace, CPUArch), configMapInformer, crdInformer, kubeVirtInformer
}
func NewFakeClusterConfigUsingKV(kv *KVv1.KubeVirt) (*virtconfig.ClusterConfig, cache.SharedIndexInformer, cache.SharedIndexInformer, cache.SharedIndexInformer) {
return NewFakeClusterConfigUsingKVWithCPUArch(kv, runtime.GOARCH)
}
func NewFakeClusterConfigUsingKVWithCPUArch(kv *KVv1.KubeVirt, CPUArch string) (*virtconfig.ClusterConfig, cache.SharedIndexInformer, cache.SharedIndexInformer, cache.SharedIndexInformer) {
kv.ResourceVersion = rand.String(10)
configMapInformer, _ := NewFakeInformerFor(&v1.ConfigMap{})
crdInformer, _ := NewFakeInformerFor(&extv1.CustomResourceDefinition{})
kubeVirtInformer, _ := NewFakeInformerFor(&KVv1.KubeVirt{})
kubeVirtInformer.GetStore().Add(kv)
AddDataVolumeAPI(crdInformer)
return virtconfig.NewClusterConfigWithCPUArch(configMapInformer, crdInformer, kubeVirtInformer, namespace, CPUArch), configMapInformer, crdInformer, kubeVirtInformer
}
func NewFakeClusterConfigUsingKVConfig(config *KVv1.KubeVirtConfiguration) (*virtconfig.ClusterConfig, cache.SharedIndexInformer, cache.SharedIndexInformer, cache.SharedIndexInformer) {
kv := &KVv1.KubeVirt{
ObjectMeta: metav1.ObjectMeta{
Name: "kubevirt",
Namespace: "kubevirt",
},
Spec: KVv1.KubeVirtSpec{
Configuration: *config,
},
Status: KVv1.KubeVirtStatus{
Phase: "Deployed",
},
}
return NewFakeClusterConfigUsingKV(kv)
}
func NewFakeContainerDiskSource() *KVv1.ContainerDiskSource {
return &KVv1.ContainerDiskSource{
Image: "fake-image",
ImagePullSecret: "fake-pull-secret",
Path: "fake-path",
}
}
func RemoveDataVolumeAPI(crdInformer cache.SharedIndexInformer) {
crdInformer.GetStore().Replace(nil, "")
}
func AddDataVolumeAPI(crdInformer cache.SharedIndexInformer) {
crdInformer.GetStore().Add(&extv1.CustomResourceDefinition{
Spec: extv1.CustomResourceDefinitionSpec{
Names: extv1.CustomResourceDefinitionNames{
Kind: "DataVolume",
},
},
})
}
func UpdateFakeClusterConfig(configMapInformer cache.SharedIndexInformer, cfgMap *v1.ConfigMap) {
copy := copy(cfgMap)
configMapInformer.GetStore().Update(copy)
}
func UpdateFakeKubeVirtClusterConfig(kubeVirtInformer cache.SharedIndexInformer, kv *KVv1.KubeVirt) {
copy := kv.DeepCopy()
copy.ResourceVersion = rand.String(10)
copy.Name = "kubevirt"
copy.Namespace = "kubevirt"
kubeVirtInformer.GetStore().Update(copy)
}
func copy(cfgMap *v1.ConfigMap) *v1.ConfigMap {
copy := cfgMap.DeepCopy()
copy.ObjectMeta = metav1.ObjectMeta{
Namespace: namespace,
Name: configMapName,
// Change the resource version, or the config will not be updated
ResourceVersion: rand.String(10),
}
return copy
}