8
8
k8serrors "k8s.io/apimachinery/pkg/api/errors"
9
9
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10
10
"k8s.io/apimachinery/pkg/runtime/schema"
11
+ runtimeClient "sigs.k8s.io/controller-runtime/pkg/client"
11
12
12
13
"context"
13
14
"encoding/json"
@@ -19,7 +20,7 @@ type Builder struct {
19
20
Definition * nadV1.NetworkAttachmentDefinition
20
21
Object * nadV1.NetworkAttachmentDefinition
21
22
metaPluginConfigs []Plugin
22
- apiClient * clients. Settings
23
+ apiClient runtimeClient. Client
23
24
errorMsg string
24
25
}
25
26
@@ -36,6 +37,19 @@ func NewBuilder(apiClient *clients.Settings, name, nsname string) *Builder {
36
37
"name: %s, namespace: %s" ,
37
38
name , nsname )
38
39
40
+ if apiClient == nil {
41
+ glog .V (100 ).Infof ("The apiClient cannot be nil" )
42
+
43
+ return nil
44
+ }
45
+
46
+ err := apiClient .AttachScheme (nadV1 .AddToScheme )
47
+ if err != nil {
48
+ glog .V (100 ).Infof ("Failed to add nad v1 scheme to client schemes" )
49
+
50
+ return nil
51
+ }
52
+
39
53
builder := Builder {
40
54
apiClient : apiClient ,
41
55
Definition : & nadV1.NetworkAttachmentDefinition {
@@ -63,7 +77,21 @@ func NewBuilder(apiClient *clients.Settings, name, nsname string) *Builder {
63
77
64
78
// Pull pulls existing networkattachmentdefinition from cluster.
65
79
func Pull (apiClient * clients.Settings , name , nsname string ) (* Builder , error ) {
66
- glog .V (100 ).Infof ("Pulling existing networkattachmentdefinition name %s under namespace %s from cluster" , name , nsname )
80
+ glog .V (100 ).Infof (
81
+ "Pulling existing networkattachmentdefinition name %s under namespace %s from cluster" , name , nsname )
82
+
83
+ if apiClient == nil {
84
+ glog .V (100 ).Infof ("The apiClient cannot be nil" )
85
+
86
+ return nil , fmt .Errorf ("the apiClient cannot be nil" )
87
+ }
88
+
89
+ err := apiClient .AttachScheme (nadV1 .AddToScheme )
90
+ if err != nil {
91
+ glog .V (100 ).Infof ("Failed to add nad v1 scheme to client schemes" )
92
+
93
+ return nil , fmt .Errorf ("failed to add nad v1 scheme to client schemes" )
94
+ }
67
95
68
96
builder := Builder {
69
97
apiClient : apiClient ,
@@ -78,13 +106,13 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) {
78
106
if name == "" {
79
107
glog .V (100 ).Infof ("The name of the networkattachmentdefinition is empty" )
80
108
81
- builder . errorMsg = "networkattachmentdefinition 'name' cannot be empty"
109
+ return nil , fmt . Errorf ( "networkattachmentdefinition 'name' cannot be empty" )
82
110
}
83
111
84
112
if nsname == "" {
85
113
glog .V (100 ).Infof ("The namespace of the networkattachmentdefinition is empty" )
86
114
87
- builder . errorMsg = "networkattachmentdefinition 'namespace' cannot be empty"
115
+ return nil , fmt . Errorf ( "networkattachmentdefinition 'namespace' cannot be empty" )
88
116
}
89
117
90
118
if ! builder .Exists () {
@@ -96,6 +124,32 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) {
96
124
return & builder , nil
97
125
}
98
126
127
+ // Get returns CatalogSource object if found.
128
+ func (builder * Builder ) Get () (* nadV1.NetworkAttachmentDefinition , error ) {
129
+ if valid , err := builder .validate (); ! valid {
130
+ return nil , err
131
+ }
132
+
133
+ glog .V (100 ).Infof (
134
+ "Collecting NetworkAttachmentDefinition object %s in namespace %s" ,
135
+ builder .Definition .Name , builder .Definition .Namespace )
136
+
137
+ network := & nadV1.NetworkAttachmentDefinition {}
138
+ err := builder .apiClient .Get (context .TODO (),
139
+ runtimeClient.ObjectKey {Name : builder .Definition .Name , Namespace : builder .Definition .Namespace },
140
+ network )
141
+
142
+ if err != nil {
143
+ glog .V (100 ).Infof (
144
+ "NetworkAttachmentDefinition object %s does not exist in namespace %s" ,
145
+ builder .Definition .Name , builder .Definition .Namespace )
146
+
147
+ return nil , err
148
+ }
149
+
150
+ return network , nil
151
+ }
152
+
99
153
// Create builds a NetworkAttachmentDefinition resource with the builder configuration.
100
154
//
101
155
// if the creation failed, the builder errorMsg will be updated.
@@ -118,13 +172,17 @@ func (builder *Builder) Create() (*Builder, error) {
118
172
}
119
173
120
174
if ! builder .Exists () {
121
- builder . Object , err = builder .apiClient .NetworkAttachmentDefinitions ( builder .Definition . Namespace ).
122
- Create ( context . TODO (), builder . Definition , metav1. CreateOptions {})
175
+ err : = builder .apiClient .Create ( context . TODO (), builder .Definition )
176
+
123
177
if err != nil {
124
- return builder , fmt .Errorf ("fail to create NAD object due to: " + err .Error ())
178
+ glog .V (100 ).Infof ("Failed to create NAD object" )
179
+
180
+ return nil , err
125
181
}
126
182
}
127
183
184
+ builder .Object = builder .Definition
185
+
128
186
return builder , nil
129
187
}
130
188
@@ -140,11 +198,12 @@ func (builder *Builder) Delete() error {
140
198
builder .Definition .Name , builder .Definition .Namespace )
141
199
142
200
if ! builder .Exists () {
201
+ glog .V (100 ).Infof ("NetworkAttachmentDefinition cannot be deleted because it does not exist" )
202
+
143
203
return nil
144
204
}
145
205
146
- err := builder .apiClient .NetworkAttachmentDefinitions (builder .Definition .Namespace ).Delete (
147
- context .TODO (), builder .Definition .Namespace , metav1.DeleteOptions {})
206
+ err := builder .apiClient .Delete (context .TODO (), builder .Definition )
148
207
149
208
if err != nil {
150
209
return fmt .Errorf ("fail to delete NAD object due to: %w" , err )
@@ -164,13 +223,17 @@ func (builder *Builder) Update() (*Builder, error) {
164
223
glog .V (100 ).Infof ("Updating NetworkAttachmentDefinition %s in namespace %s" ,
165
224
builder .Definition .Name , builder .Definition .Namespace )
166
225
167
- var err error
226
+ if ! builder .Exists () {
227
+ return nil , fmt .Errorf ("failed to update NetworkAttachmentDefinition, object does not exist on cluster" )
228
+ }
168
229
169
230
builder .Definition .CreationTimestamp = metav1.Time {}
170
231
builder .Definition .ResourceVersion = builder .Object .ResourceVersion
232
+ err := builder .apiClient .Update (context .TODO (), builder .Definition )
171
233
172
- builder .Object , err = builder .apiClient .NetworkAttachmentDefinitions (builder .Definition .Namespace ).Update (
173
- context .TODO (), builder .Definition , metav1.UpdateOptions {})
234
+ if err == nil {
235
+ builder .Object = builder .Definition
236
+ }
174
237
175
238
return builder , err
176
239
}
@@ -187,8 +250,8 @@ func (builder *Builder) Exists() bool {
187
250
glog .V (100 ).Infof ("Checking if NetworkAttachmentDefinition %s exists in namespace %s" ,
188
251
builder .Definition .Name , builder .Definition .Namespace )
189
252
190
- _ , err := builder . apiClient . NetworkAttachmentDefinitions ( builder . Definition . Namespace ). Get ( context . TODO (),
191
- builder .Definition . Name , metav1. GetOptions {} )
253
+ var err error
254
+ builder .Object , err = builder . Get ( )
192
255
193
256
return nil == err || ! k8serrors .IsNotFound (err )
194
257
}
@@ -249,6 +312,10 @@ func (builder *Builder) WithMasterPlugin(masterPlugin *MasterPlugin) *Builder {
249
312
return builder
250
313
}
251
314
315
+ if masterPlugin == nil {
316
+ builder .errorMsg = "error 'masterPlugin' is empty"
317
+ }
318
+
252
319
glog .V (100 ).Infof ("Adding masterPlugin %v to NAD %s" , masterPlugin , builder .Definition .Name )
253
320
254
321
emptyNadConfig := nadV1.NetworkAttachmentDefinitionSpec {}
0 commit comments