generated from kyma-project/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 11
/
kcpIpRange.go
129 lines (119 loc) · 3.34 KB
/
kcpIpRange.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
package dsl
import (
"context"
"errors"
"fmt"
cloudcontrolv1beta1 "github.com/kyma-project/cloud-manager/api/cloud-control/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func AssertKcpIpRangeScope(expectedScopeName string) ObjAssertion {
return func(obj client.Object) error {
x, ok := obj.(*cloudcontrolv1beta1.IpRange)
if !ok {
return fmt.Errorf("expected *cloudcontrolv1beta1.IpRange, but got %T", obj)
}
if x.Spec.Scope.Name != expectedScopeName {
return fmt.Errorf("the KCP IpRange %s/%s expected scope name is %s, but it has %s",
x.Namespace, x.Name, expectedScopeName, x.Spec.Scope.Name)
}
return nil
}
}
func AssertKcpIpRangeCidr(expectedCidr string) ObjAssertion {
return func(obj client.Object) error {
x, ok := obj.(*cloudcontrolv1beta1.IpRange)
if !ok {
return fmt.Errorf("expected *cloudcontrolv1beta1.IpRange, but got %T", obj)
}
if x.Spec.Cidr != expectedCidr {
return fmt.Errorf("the KCP IpRange %s/%s expected cidr is %s, but it has %s",
x.Namespace, x.Name, expectedCidr, x.Spec.Cidr)
}
return nil
}
}
func AssertKcpIpRangeRemoteRef(expectedNamespace, expectedName string) ObjAssertion {
return func(obj client.Object) error {
x, ok := obj.(*cloudcontrolv1beta1.IpRange)
if !ok {
return fmt.Errorf("expected *cloudcontrolv1beta1.IpRange, but got %T", obj)
}
if x.Spec.RemoteRef.Namespace != expectedNamespace {
return fmt.Errorf("the KCP IpRange %s/%s expected remoteRef namespace is %s, but it has %s",
x.Namespace, x.Name, expectedNamespace, x.Spec.Cidr)
}
if x.Spec.RemoteRef.Name != expectedName {
return fmt.Errorf("the KCP IpRange %s/%s expected remoteRef name is %s, but it has %s",
x.Namespace, x.Name, expectedNamespace, x.Spec.Cidr)
}
return nil
}
}
func CreateKcpIpRange(ctx context.Context, clnt client.Client, obj *cloudcontrolv1beta1.IpRange, opts ...ObjAction) error {
if obj == nil {
obj = &cloudcontrolv1beta1.IpRange{}
}
NewObjActions(opts...).
Append(
WithNamespace(DefaultKcpNamespace),
WithKcpIpRangeSpecCidr(DefaultIpRangeCidr),
).
ApplyOnObject(obj)
if obj.Name == "" {
return errors.New("the KCP IpRange must have name set")
}
err := clnt.Get(ctx, client.ObjectKeyFromObject(obj), obj)
if err == nil {
// already exists
return nil
}
if client.IgnoreNotFound(err) != nil {
// some error
return err
}
err = clnt.Create(ctx, obj)
return err
}
func WithKcpIpRangeSpecCidr(cidr string) ObjAction {
return &objAction{
f: func(obj client.Object) {
x := obj.(*cloudcontrolv1beta1.IpRange)
if x.Spec.Cidr == "" {
x.Spec.Cidr = cidr
}
},
}
}
func WithKcpIpRangeStatusCidr(cidr string) ObjStatusAction {
return &objStatusAction{
f: func(obj client.Object) {
x := obj.(*cloudcontrolv1beta1.IpRange)
if x.Status.Cidr == "" {
x.Status.Cidr = cidr
}
},
}
}
func WithKcpIpRangeSpecScope(scopeName string) ObjAction {
return &objAction{
f: func(obj client.Object) {
x := obj.(*cloudcontrolv1beta1.IpRange)
if x.Spec.Scope.Name == "" {
x.Spec.Scope.Name = scopeName
}
},
}
}
func WithKcpIpRangeRemoteRef(ns, name string) ObjAction {
return &objAction{
f: func(obj client.Object) {
x := obj.(*cloudcontrolv1beta1.IpRange)
if x.Spec.RemoteRef.Namespace == "" {
x.Spec.RemoteRef.Namespace = ns
}
if x.Spec.RemoteRef.Name == "" {
x.Spec.RemoteRef.Name = name
}
},
}
}