forked from argoproj/argo-cd
/
kube.go
650 lines (607 loc) · 20.5 KB
/
kube.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
// Package kube provides helper utilities common for kubernetes
package kube
import (
"context"
"encoding/json"
"fmt"
"os"
"reflect"
"regexp"
"strings"
"sync"
"time"
"github.com/ghodss/yaml"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/equality"
apierr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"k8s.io/kubernetes/pkg/kubectl/scheme"
"github.com/argoproj/argo-cd/util/cache"
jsonutil "github.com/argoproj/argo-cd/util/json"
)
const (
listVerb = "list"
deleteVerb = "delete"
deleteCollectionVerb = "deletecollection"
watchVerb = "watch"
)
const (
SecretKind = "Secret"
ServiceKind = "Service"
EndpointsKind = "Endpoints"
DeploymentKind = "Deployment"
ReplicaSetKind = "ReplicaSet"
StatefulSetKind = "StatefulSet"
DaemonSetKind = "DaemonSet"
IngressKind = "Ingress"
PersistentVolumeClaimKind = "PersistentVolumeClaim"
CustomResourceDefinitionKind = "CustomResourceDefinition"
)
const (
apiResourceCacheDuration = 10 * time.Minute
)
var (
// location to use for generating temporary files, such as the kubeconfig needed by kubectl
kubectlTempDir string
// apiResourceCache is a in-memory cache of api resources supported by a k8s server
apiResourceCache = cache.NewInMemoryCache(apiResourceCacheDuration)
)
func init() {
fileInfo, err := os.Stat("/dev/shm")
if err == nil && fileInfo.IsDir() {
kubectlTempDir = "/dev/shm"
}
}
// TestConfig tests to make sure the REST config is usable
func TestConfig(config *rest.Config) error {
kubeclientset, err := kubernetes.NewForConfig(config)
if err != nil {
return fmt.Errorf("REST config invalid: %s", err)
}
_, err = kubeclientset.ServerVersion()
if err != nil {
return fmt.Errorf("REST config invalid: %s", err)
}
return nil
}
// ToUnstructured converts a concrete K8s API type to a un unstructured object
func ToUnstructured(obj interface{}) (*unstructured.Unstructured, error) {
uObj, err := runtime.NewTestUnstructuredConverter(equality.Semantic).ToUnstructured(obj)
if err != nil {
return nil, err
}
return &unstructured.Unstructured{Object: uObj}, nil
}
// MustToUnstructured converts a concrete K8s API type to a un unstructured object and panics if not successful
func MustToUnstructured(obj interface{}) *unstructured.Unstructured {
uObj, err := ToUnstructured(obj)
if err != nil {
panic(err)
}
return uObj
}
// UnsetLabel removes our app labels from an unstructured object
func UnsetLabel(target *unstructured.Unstructured, key string) {
if labels := target.GetLabels(); labels != nil {
if _, ok := labels[key]; ok {
delete(labels, key)
if len(labels) == 0 {
unstructured.RemoveNestedField(target.Object, "metadata", "labels")
} else {
target.SetLabels(labels)
}
}
}
}
// SetLabel sets our app labels against an unstructured object
func SetLabel(target *unstructured.Unstructured, key, val string) error {
labels := target.GetLabels()
if labels == nil {
labels = make(map[string]string)
}
labels[key] = val
target.SetLabels(labels)
// special case for deployment types: make sure that derived replicaset and pod has application label
switch target.GetKind() {
case DeploymentKind, ReplicaSetKind, StatefulSetKind, DaemonSetKind:
templateLabels, ok, err := unstructured.NestedMap(target.UnstructuredContent(), "spec", "template", "metadata", "labels")
if err != nil {
return err
}
if !ok || templateLabels == nil {
templateLabels = make(map[string]interface{})
}
templateLabels[key] = val
err = unstructured.SetNestedMap(target.UnstructuredContent(), templateLabels, "spec", "template", "metadata", "labels")
if err != nil {
return err
}
// The following is a workaround for issue #335. In API version extensions/v1beta1 or
// apps/v1beta1, if a spec omits spec.selector then k8s will default the
// spec.selector.matchLabels to match spec.template.metadata.labels. This means ArgoCD
// labels can potentially make their way into spec.selector.matchLabels, which is a bad
// thing. The following logic prevents this behavior.
switch target.GetAPIVersion() {
case "apps/v1beta1", "extensions/v1beta1":
selector, _, err := unstructured.NestedMap(target.UnstructuredContent(), "spec", "selector")
if err != nil {
return err
}
if len(selector) == 0 {
// If we get here, user did not set spec.selector in their manifest. We do not want
// our ArgoCD labels to get defaulted by kubernetes, so we explicitly set the labels
// for them (minus the ArgoCD labels).
delete(templateLabels, key)
err = unstructured.SetNestedMap(target.UnstructuredContent(), templateLabels, "spec", "selector", "matchLabels")
if err != nil {
return err
}
}
}
}
return nil
}
// GetCachedServerResources discovers API resources supported by a Kube API server.
// Caches the results for apiResourceCacheDuration (per host)
func GetCachedServerResources(host string, disco discovery.DiscoveryInterface) ([]*metav1.APIResourceList, error) {
var resList []*metav1.APIResourceList
cacheKey := fmt.Sprintf("apires|%s", host)
err := apiResourceCache.Get(cacheKey, &resList)
if err == nil {
log.Debugf("cache hit: %s", cacheKey)
return resList, nil
}
if err == cache.ErrCacheMiss {
log.Infof("cache miss: %s", cacheKey)
} else {
log.Warnf("cache error %s: %v", cacheKey, err)
}
resList, err = disco.ServerResources()
if err != nil {
if len(resList) == 0 {
return nil, errors.WithStack(err)
}
// It's possible for ServerResources to return error as well as a resource list
log.Warnf("Resource discovery partially successful. Encountered error: %v", err)
}
err = apiResourceCache.Set(&cache.Item{
Key: cacheKey,
Object: resList,
})
if err != nil {
log.Warnf("Failed to cache %s: %v", cacheKey, err)
}
return resList, nil
}
func FlushServerResourcesCache() {
apiResourceCache.Flush()
}
// GetLiveResource returns the corresponding live resource from a unstructured object
func GetLiveResource(dclient dynamic.Interface, obj *unstructured.Unstructured, apiResource *metav1.APIResource, namespace string) (*unstructured.Unstructured, error) {
resourceName := obj.GetName()
if resourceName == "" {
return nil, fmt.Errorf("resource was supplied without a name")
}
reIf := dclient.Resource(apiResource, namespace)
liveObj, err := reIf.Get(resourceName, metav1.GetOptions{})
if err != nil {
if apierr.IsNotFound(err) {
log.Infof("No live counterpart to %s/%s/%s/%s in namespace: '%s'", apiResource.Group, apiResource.Version, apiResource.Name, resourceName, namespace)
return nil, nil
}
return nil, errors.WithStack(err)
}
return liveObj, nil
}
func IsCRDGroupVersionKind(gvk schema.GroupVersionKind) bool {
return gvk.Kind == CustomResourceDefinitionKind && gvk.Group == "apiextensions.k8s.io"
}
func IsCRD(obj *unstructured.Unstructured) bool {
return IsCRDGroupVersionKind(obj.GroupVersionKind())
}
// temporal solution for https://github.com/argoproj/argo-cd/issues/650.
func isExcludedResourceGroup(resource metav1.APIResource) bool {
return resource.Group == "servicecatalog.k8s.io"
}
func WatchResourcesWithLabel(ctx context.Context, config *rest.Config, namespace string, labelName string) (chan watch.Event, error) {
log.Infof("Start watching for resources changes with label %s in cluster %s", labelName, config.Host)
dynClientPool := dynamic.NewDynamicClientPool(config)
disco, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
return nil, err
}
serverResources, err := GetCachedServerResources(config.Host, disco)
if err != nil {
return nil, err
}
resources := make([]dynamic.ResourceInterface, 0)
for _, apiResourcesList := range serverResources {
for i := range apiResourcesList.APIResources {
apiResource := apiResourcesList.APIResources[i]
watchSupported := false
for _, verb := range apiResource.Verbs {
if verb == watchVerb {
watchSupported = true
break
}
}
if watchSupported && !isExcludedResourceGroup(apiResource) {
dclient, err := dynClientPool.ClientForGroupVersionKind(schema.FromAPIVersionAndKind(apiResourcesList.GroupVersion, apiResource.Kind))
if err != nil {
return nil, err
}
resources = append(resources, dclient.Resource(&apiResource, namespace))
}
}
}
ch := make(chan watch.Event)
go func() {
var wg sync.WaitGroup
wg.Add(len(resources))
for i := 0; i < len(resources); i++ {
resource := resources[i]
go func() {
defer wg.Done()
w, err := resource.Watch(metav1.ListOptions{LabelSelector: labelName})
if err == nil {
defer w.Stop()
copyEventsChannel(ctx, w.ResultChan(), ch)
}
}()
}
wg.Wait()
close(ch)
log.Infof("Stop watching for resources changes with label %s in cluster %s", labelName, config.ServerName)
}()
return ch, nil
}
func copyEventsChannel(ctx context.Context, src <-chan watch.Event, dst chan watch.Event) {
stopped := false
done := make(chan bool)
go func() {
for event := range src {
if stopped {
break
}
dst <- event
}
done <- true
}()
select {
case <-done:
case <-ctx.Done():
stopped = true
}
}
// GetResourcesWithLabel returns all kubernetes resources with specified label
func GetResourcesWithLabel(config *rest.Config, namespace string, labelName string, labelValue string) ([]*unstructured.Unstructured, error) {
dynClientPool := dynamic.NewDynamicClientPool(config)
disco, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
return nil, err
}
resources, err := GetCachedServerResources(config.Host, disco)
if err != nil {
return nil, err
}
var resourceInterfaces []dynamic.ResourceInterface
for _, apiResourcesList := range resources {
for i := range apiResourcesList.APIResources {
apiResource := apiResourcesList.APIResources[i]
listSupported := false
for _, verb := range apiResource.Verbs {
if verb == listVerb {
listSupported = true
break
}
}
if listSupported && !isExcludedResourceGroup(apiResource) {
dclient, err := dynClientPool.ClientForGroupVersionKind(schema.FromAPIVersionAndKind(apiResourcesList.GroupVersion, apiResource.Kind))
if err != nil {
return nil, err
}
resourceInterfaces = append(resourceInterfaces, dclient.Resource(&apiResource, namespace))
}
}
}
var asyncErr error
var result []*unstructured.Unstructured
var wg sync.WaitGroup
var lock sync.Mutex
wg.Add(len(resourceInterfaces))
for _, c := range resourceInterfaces {
go func(client dynamic.ResourceInterface) {
defer wg.Done()
list, err := client.List(metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", labelName, labelValue),
})
if err != nil {
if !apierr.IsNotFound(err) {
asyncErr = err
}
return
}
// apply client side filtering since not every kubernetes API supports label filtering
for i := range list.(*unstructured.UnstructuredList).Items {
item := list.(*unstructured.UnstructuredList).Items[i]
labels := item.GetLabels()
if labels != nil {
if value, ok := labels[labelName]; ok && value == labelValue {
lock.Lock()
result = append(result, &item)
lock.Unlock()
}
}
}
}(c)
}
wg.Wait()
return result, asyncErr
}
// DeleteResourceWithLabel delete all resources which match to specified label selector
func DeleteResourceWithLabel(config *rest.Config, namespace string, labelName string, labelValue string) error {
dynClientPool := dynamic.NewDynamicClientPool(config)
disco, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
return err
}
resources, err := GetCachedServerResources(config.Host, disco)
if err != nil {
return err
}
type resClient struct {
dynamic.ResourceInterface
deleteCollectionSupported bool
}
var resourceInterfaces []resClient
for _, apiResourcesList := range resources {
for i := range apiResourcesList.APIResources {
apiResource := apiResourcesList.APIResources[i]
deleteCollectionSupported := false
deleteSupported := false
for _, verb := range apiResource.Verbs {
if verb == deleteCollectionVerb {
deleteCollectionSupported = true
} else if verb == deleteVerb {
deleteSupported = true
}
}
dclient, err := dynClientPool.ClientForGroupVersionKind(schema.FromAPIVersionAndKind(apiResourcesList.GroupVersion, apiResource.Kind))
if err != nil {
return err
}
if (deleteCollectionSupported || deleteSupported) &&
!IsCRDGroupVersionKind(schema.FromAPIVersionAndKind(apiResourcesList.GroupVersion, apiResource.Kind)) &&
!isExcludedResourceGroup(apiResource) {
resourceInterfaces = append(resourceInterfaces, resClient{
dclient.Resource(&apiResource, namespace),
deleteCollectionSupported,
})
}
}
}
var asyncErr error
propagationPolicy := metav1.DeletePropagationForeground
var wg sync.WaitGroup
wg.Add(len(resourceInterfaces))
for _, c := range resourceInterfaces {
go func(client resClient) {
defer wg.Done()
if client.deleteCollectionSupported {
err = client.DeleteCollection(&metav1.DeleteOptions{
PropagationPolicy: &propagationPolicy,
}, metav1.ListOptions{LabelSelector: fmt.Sprintf("%s=%s", labelName, labelValue)})
if err != nil && !apierr.IsNotFound(err) {
asyncErr = err
}
} else {
items, err := client.List(metav1.ListOptions{LabelSelector: fmt.Sprintf("%s=%s", labelName, labelValue)})
if err != nil {
asyncErr = err
return
}
for _, item := range items.(*unstructured.UnstructuredList).Items {
// apply client side filtering since not every kubernetes API supports label filtering
labels := item.GetLabels()
if labels != nil {
if value, ok := labels[labelName]; ok && value == labelValue {
err = client.Delete(item.GetName(), &metav1.DeleteOptions{
PropagationPolicy: &propagationPolicy,
})
if err != nil && !apierr.IsNotFound(err) {
asyncErr = err
return
}
}
}
}
}
}(c)
}
wg.Wait()
return asyncErr
}
// See: https://github.com/ksonnet/ksonnet/blob/master/utils/client.go
func ServerResourceForGroupVersionKind(disco discovery.DiscoveryInterface, gvk schema.GroupVersionKind) (*metav1.APIResource, error) {
resources, err := disco.ServerResourcesForGroupVersion(gvk.GroupVersion().String())
if err != nil {
return nil, err
}
for _, r := range resources.APIResources {
if r.Kind == gvk.Kind {
log.Debugf("Chose API '%s' for %s", r.Name, gvk)
return &r, nil
}
}
return nil, apierr.NewNotFound(schema.GroupResource{Group: gvk.Group, Resource: gvk.Kind}, "")
}
type listResult struct {
Items []*unstructured.Unstructured `json:"items"`
}
// ListResources returns a list of resources of a particular API type using the dynamic client
func ListResources(dclient dynamic.Interface, apiResource metav1.APIResource, namespace string, listOpts metav1.ListOptions) ([]*unstructured.Unstructured, error) {
reIf := dclient.Resource(&apiResource, namespace)
liveObjs, err := reIf.List(listOpts)
if err != nil {
return nil, errors.WithStack(err)
}
liveObjsBytes, err := json.Marshal(liveObjs)
if err != nil {
return nil, errors.WithStack(err)
}
var objList listResult
err = json.Unmarshal(liveObjsBytes, &objList)
if err != nil {
return nil, errors.WithStack(err)
}
return objList.Items, nil
}
// deleteFile is best effort deletion of a file
func deleteFile(path string) {
if _, err := os.Stat(path); os.IsNotExist(err) {
return
}
_ = os.Remove(path)
}
// cleanKubectlOutput makes the error output of kubectl a little better to read
func cleanKubectlOutput(s string) string {
s = strings.TrimSpace(s)
s = strings.Replace(s, ": error validating \"STDIN\"", "", -1)
s = strings.Replace(s, ": unable to recognize \"STDIN\"", "", -1)
s = strings.Replace(s, ": error when creating \"STDIN\"", "", -1)
s = strings.Replace(s, "; if you choose to ignore these errors, turn validation off with --validate=false", "", -1)
s = strings.Replace(s, "error: error", "error", -1)
return s
}
// WriteKubeConfig takes a rest.Config and writes it as a kubeconfig at the specified path
func WriteKubeConfig(restConfig *rest.Config, namespace, filename string) error {
var kubeConfig = clientcmdapi.Config{
CurrentContext: restConfig.Host,
Contexts: map[string]*clientcmdapi.Context{
restConfig.Host: {
Cluster: restConfig.Host,
AuthInfo: restConfig.Host,
Namespace: namespace,
},
},
Clusters: map[string]*clientcmdapi.Cluster{
restConfig.Host: {
Server: restConfig.Host,
},
},
AuthInfos: map[string]*clientcmdapi.AuthInfo{
restConfig.Host: {},
},
}
// Set Cluster info
if restConfig.TLSClientConfig.Insecure {
kubeConfig.Clusters[restConfig.Host].InsecureSkipTLSVerify = true
}
if restConfig.TLSClientConfig.CAFile != "" {
kubeConfig.Clusters[restConfig.Host].CertificateAuthority = restConfig.TLSClientConfig.CAFile
}
// Set AuthInfo
if len(restConfig.TLSClientConfig.CAData) > 0 {
kubeConfig.Clusters[restConfig.Host].CertificateAuthorityData = restConfig.TLSClientConfig.CAData
}
if restConfig.TLSClientConfig.CertFile != "" {
kubeConfig.AuthInfos[restConfig.Host].ClientCertificate = restConfig.TLSClientConfig.CertFile
}
if len(restConfig.TLSClientConfig.CertData) > 0 {
kubeConfig.AuthInfos[restConfig.Host].ClientCertificateData = restConfig.TLSClientConfig.CertData
}
if restConfig.TLSClientConfig.KeyFile != "" {
kubeConfig.AuthInfos[restConfig.Host].ClientKey = restConfig.TLSClientConfig.KeyFile
}
if len(restConfig.TLSClientConfig.KeyData) > 0 {
kubeConfig.AuthInfos[restConfig.Host].ClientKeyData = restConfig.TLSClientConfig.KeyData
}
if restConfig.Username != "" {
kubeConfig.AuthInfos[restConfig.Host].Username = restConfig.Username
}
if restConfig.Password != "" {
kubeConfig.AuthInfos[restConfig.Host].Password = restConfig.Password
}
if restConfig.BearerToken != "" {
kubeConfig.AuthInfos[restConfig.Host].Token = restConfig.BearerToken
}
if restConfig.ExecProvider != nil {
kubeConfig.AuthInfos[restConfig.Host].Exec = restConfig.ExecProvider
}
return clientcmd.WriteToFile(kubeConfig, filename)
}
var diffSeparator = regexp.MustCompile(`\n---`)
// SplitYAML splits a YAML file into unstructured objects. Returns list of all unstructured objects
// found in the yaml. If any errors occurred, returns the first one
func SplitYAML(out string) ([]*unstructured.Unstructured, error) {
parts := diffSeparator.Split(out, -1)
var objs []*unstructured.Unstructured
var firstErr error
for _, part := range parts {
var objMap map[string]interface{}
err := yaml.Unmarshal([]byte(part), &objMap)
if err != nil {
if firstErr == nil {
firstErr = fmt.Errorf("Failed to unmarshal manifest: %v", err)
}
continue
}
if len(objMap) == 0 {
// handles case where theres no content between `---`
continue
}
var obj unstructured.Unstructured
err = yaml.Unmarshal([]byte(part), &obj)
if err != nil {
if firstErr == nil {
firstErr = fmt.Errorf("Failed to unmarshal manifest: %v", err)
}
continue
}
remObj, err := Remarshal(&obj)
if err != nil {
log.Warnf("Failed to remarshal oject: %v", err)
} else {
obj = *remObj
}
objs = append(objs, &obj)
}
return objs, firstErr
}
// Remarshal checks resource kind and version and re-marshal using corresponding struct custom marshaller.
// This ensures that expected resource state is formatter same as actual resource state in kubernetes
// and allows to find differences between actual and target states more accurately.
func Remarshal(obj *unstructured.Unstructured) (*unstructured.Unstructured, error) {
data, err := json.Marshal(obj)
if err != nil {
return nil, err
}
item, err := scheme.Scheme.New(obj.GroupVersionKind())
if err != nil {
return nil, err
}
// This will drop any omitempty fields, perform resource conversion etc...
unmarshalledObj := reflect.New(reflect.TypeOf(item).Elem()).Interface()
err = json.Unmarshal(data, &unmarshalledObj)
if err != nil {
return nil, err
}
unstrBody, err := runtime.DefaultUnstructuredConverter.ToUnstructured(unmarshalledObj)
if err != nil {
return nil, err
}
// remove all default values specified by custom formatter (e.g. creationTimestamp)
unstrBody = jsonutil.RemoveMapFields(obj.Object, unstrBody)
return &unstructured.Unstructured{Object: unstrBody}, nil
}