@@ -121,57 +121,42 @@ const ( // They should complete the sentence "Deployment default/foo has been ..
121
121
//
122
122
// It returns the executed operation and an error.
123
123
func CreateOrUpdate (ctx context.Context , c client.Client , obj runtime.Object , f MutateFn ) (OperationResult , error ) {
124
- // op is the operation we are going to attempt
125
- op := OperationResultNone
126
-
127
- // get the existing object meta
128
- metaObj , ok := obj .(v1.Object )
129
- if ! ok {
130
- return OperationResultNone , fmt .Errorf ("%T does not implement metav1.Object interface" , obj )
124
+ key , err := client .ObjectKeyFromObject (obj )
125
+ if err != nil {
126
+ return OperationResultNone , err
131
127
}
132
128
133
- // retrieve the existing object
134
- key := client.ObjectKey {
135
- Name : metaObj .GetName (),
136
- Namespace : metaObj .GetNamespace (),
129
+ if err := c .Get (ctx , key , obj ); err != nil {
130
+ if errors .IsNotFound (err ) {
131
+ if err := c .Create (ctx , obj ); err != nil {
132
+ return OperationResultNone , err
133
+ }
134
+ return OperationResultCreated , nil
135
+ }
136
+ return OperationResultNone , err
137
137
}
138
- err := c .Get (ctx , key , obj )
139
138
140
- // reconcile the existing object
141
139
existing := obj .DeepCopyObject ()
142
- existingObjMeta := existing .(v1.Object )
143
- existingObjMeta .SetName (metaObj .GetName ())
144
- existingObjMeta .SetNamespace (metaObj .GetNamespace ())
145
-
146
- if e := f (obj ); e != nil {
147
- return OperationResultNone , e
148
- }
149
-
150
- if metaObj .GetName () != existingObjMeta .GetName () {
151
- return OperationResultNone , fmt .Errorf ("ReconcileFn cannot mutate objects name" )
140
+ if err := f (obj ); err != nil {
141
+ return OperationResultNone , err
152
142
}
153
143
154
- if metaObj . GetNamespace () != existingObjMeta . GetNamespace ( ) {
155
- return OperationResultNone , fmt . Errorf ( "ReconcileFn cannot mutate objects namespace" )
144
+ if reflect . DeepEqual ( existing , obj ) {
145
+ return OperationResultNone , nil
156
146
}
157
147
158
- if errors .IsNotFound (err ) {
159
- err = c .Create (ctx , obj )
160
- op = OperationResultCreated
161
- } else if err == nil {
162
- if reflect .DeepEqual (existing , obj ) {
163
- return OperationResultNone , nil
164
- }
165
- err = c .Update (ctx , obj )
166
- op = OperationResultUpdated
167
- } else {
148
+ newKey , err := client .ObjectKeyFromObject (obj )
149
+ if err != nil {
168
150
return OperationResultNone , err
169
151
}
152
+ if key != newKey {
153
+ return OperationResultNone , fmt .Errorf ("MutateFn cannot mutate object namespace and/or object name" )
154
+ }
170
155
171
- if err != nil {
172
- op = OperationResultNone
156
+ if err := c . Update ( ctx , obj ); err != nil {
157
+ return OperationResultNone , err
173
158
}
174
- return op , err
159
+ return OperationResultUpdated , nil
175
160
}
176
161
177
162
// MutateFn is a function which mutates the existing object into it's desired state.
0 commit comments