generated from kyma-project/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 13
/
awsNfsVolume.go
142 lines (132 loc) · 3.83 KB
/
awsNfsVolume.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
package dsl
import (
"context"
"errors"
"fmt"
cloudresourcesv1beta1 "github.com/kyma-project/cloud-manager/api/cloud-resources/v1beta1"
"k8s.io/apimachinery/pkg/api/resource"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func WithNfsVolumeIpRange(ipRangeName string) ObjAction {
return &objAction{
f: func(obj client.Object) {
if x, ok := obj.(*cloudresourcesv1beta1.AwsNfsVolume); ok {
if x.Spec.IpRange.Name == "" {
x.Spec.IpRange.Name = ipRangeName
}
if x.Spec.IpRange.Namespace == "" {
x.Spec.IpRange.Namespace = DefaultSkrNamespace
}
return
}
if x, ok := obj.(*cloudresourcesv1beta1.GcpNfsVolume); ok {
if x.Spec.IpRange.Name == "" {
x.Spec.IpRange.Name = ipRangeName
}
if x.Spec.IpRange.Namespace == "" {
x.Spec.IpRange.Namespace = DefaultSkrNamespace
}
return
}
panic(fmt.Errorf("unhandled type %T in WithNfsVolumeIpRange", obj))
},
}
}
func WithAwsNfsVolumePvName(name string) ObjAction {
return &objAction{
f: func(obj client.Object) {
if x, ok := obj.(*cloudresourcesv1beta1.AwsNfsVolume); ok {
if x.Spec.PersistentVolume == nil {
x.Spec.PersistentVolume = &cloudresourcesv1beta1.AwsNfsVolumePvSpec{}
}
x.Spec.PersistentVolume.Name = name
return
}
panic(fmt.Errorf("unhandled type %T in WithAwsNfsVolumePvName", obj))
},
}
}
func WithAwsNfsVolumePvLabels(labels map[string]string) ObjAction {
return &objAction{
f: func(obj client.Object) {
if x, ok := obj.(*cloudresourcesv1beta1.AwsNfsVolume); ok {
if x.Spec.PersistentVolume == nil {
x.Spec.PersistentVolume = &cloudresourcesv1beta1.AwsNfsVolumePvSpec{}
}
if x.Spec.PersistentVolume.Labels == nil {
x.Spec.PersistentVolume.Labels = map[string]string{}
}
for k, v := range labels {
x.Spec.PersistentVolume.Labels[k] = v
}
return
}
panic(fmt.Errorf("unhandled type %T in WithAwsNfsVolumePvLabels", obj))
},
}
}
func WithAwsNfsVolumePvAnnotations(annotations map[string]string) ObjAction {
return &objAction{
f: func(obj client.Object) {
if x, ok := obj.(*cloudresourcesv1beta1.AwsNfsVolume); ok {
if x.Spec.PersistentVolume == nil {
x.Spec.PersistentVolume = &cloudresourcesv1beta1.AwsNfsVolumePvSpec{}
}
if x.Spec.PersistentVolume.Annotations == nil {
x.Spec.PersistentVolume.Annotations = map[string]string{}
}
for k, v := range annotations {
x.Spec.PersistentVolume.Annotations[k] = v
}
return
}
panic(fmt.Errorf("unhandled type %T in WithAwsNfsVolumePvAnnotations", obj))
},
}
}
func WithAwsNfsVolumeCapacity(capacity string) ObjAction {
return &objAction{
f: func(obj client.Object) {
if x, ok := obj.(*cloudresourcesv1beta1.AwsNfsVolume); ok {
if x.Spec.Capacity.IsZero() {
x.Spec.Capacity = resource.MustParse(capacity)
}
return
}
panic(fmt.Errorf("unhandled type %T in WithNfsVolumeCapacity", obj))
},
}
}
func CreateAwsNfsVolume(ctx context.Context, clnt client.Client, obj *cloudresourcesv1beta1.AwsNfsVolume, opts ...ObjAction) error {
if obj == nil {
obj = &cloudresourcesv1beta1.AwsNfsVolume{}
}
NewObjActions(opts...).
Append(
WithNamespace(DefaultSkrNamespace),
).
ApplyOnObject(obj)
if obj.Name == "" {
return errors.New("the SKR AwsNfsVolume must have name set")
}
if obj.Spec.IpRange.Name == "" {
return errors.New("the SKR AwsNfsVolume must have spec.iprange.name set")
}
if obj.Spec.IpRange.Namespace == "" {
obj.Spec.IpRange.Namespace = DefaultSkrNamespace
}
err := clnt.Create(ctx, obj)
return err
}
func HavingAwsNfsVolumeStatusId() ObjAssertion {
return func(obj client.Object) error {
x, ok := obj.(*cloudresourcesv1beta1.AwsNfsVolume)
if !ok {
return fmt.Errorf("the object %T is not SKR AwsNfsVolume", obj)
}
if x.Status.Id == "" {
return errors.New("the SKR AwsNfsVolume ID not set")
}
return nil
}
}