forked from ebauman/crder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crd_test.go
132 lines (110 loc) · 2.5 KB
/
crd_test.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
package crder
import (
"context"
v13 "k8s.io/api/admissionregistration/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
"testing"
)
type Foo struct {
v1.TypeMeta
Metadata v1.ObjectMeta
Spec FooSpec
}
type FooSpec struct {
Bar bool
}
type Foo2 struct {
v1.TypeMeta
Metadata v1.ObjectMeta
Spec Foo2Spec
}
type Foo2Spec struct {
Bar bool
Baz bool
}
func (f *Foo) GroupVersionKind() schema.GroupVersionKind {
return schema.GroupVersionKind{
Group: "example.org",
Version: "v1",
Kind: "Foo",
}
}
func (f *Foo2) GroupVersionKind() schema.GroupVersionKind {
return schema.GroupVersionKind{
Group: "example.org",
Version: "v2",
Kind: "Foo",
}
}
func Test_CRD(t *testing.T) {
c := NewCRD(Foo{}, "example.org", nil)
c.AddVersion("v1", Foo{}, func(cv *Version) {
cv.
IsServed(true).
IsStored(true)
})
_, err := c.ToV1CustomResourceDefinition()
if err != nil {
t.Error(err)
}
}
func Test_Install(t *testing.T) {
c := NewCRD(Foo{}, "example.org", nil)
c.AddVersion("v1", Foo{}, func(cv *Version) {
cv.
IsServed(true).
IsStored(false)
})
c.AddVersion("v2", Foo2{}, func(cv *Version) {
cv.
IsServed(true).
IsStored(true)
cv.
WithColumn("baz", ".spec.baz")
})
c.AddValidation("foo.example.org", func(vv *Validation) {
vv.
MatchPolicyExact().
WithVersions("v1").
WithService(v13.ServiceReference{
Namespace: "default",
Name: "service",
Path: pointer.String("/foo.example.org"),
Port: pointer.Int32(6443),
}).
AddRules(v13.RuleWithOperations{
Operations: []v13.OperationType{v13.Create},
Rule: v13.Rule{
APIGroups: []string{"foo.example.org"},
APIVersions: []string{"v1"},
Resources: []string{"foo"},
},
})
})
cfg, err := clientcmd.BuildConfigFromFlags("", "/tmp/k3s-default")
if err != nil {
t.Error(err)
}
objs, err := InstallUpdateCRDsWithRecordedObjects(cfg, *c)
if err != nil {
t.Error(err)
}
defer cleanup(t, cfg, objs)
}
func cleanup(t *testing.T, cfg *rest.Config, objs []client.Object) {
cli, err := client.New(cfg, client.Options{Scheme: scheme}) // scheme defined in install.go
if err != nil {
t.Errorf("error during cleanup: %s", err.Error())
}
for _, o := range objs {
err = cli.Delete(context.Background(), o)
if err != nil {
t.Errorf("error during cleanup: %s", err.Error())
}
}
}