-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.go
51 lines (40 loc) · 1.49 KB
/
common.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
package resrcinfo
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/json"
"github.com/werf/nelm/pkg/kubeclnt"
"github.com/werf/nelm/pkg/log"
"github.com/werf/nelm/pkg/resrc"
)
type UpToDateStatus string
const (
UpToDateStatusUnknown UpToDateStatus = "unknown"
UpToDateStatusYes UpToDateStatus = "yes"
UpToDateStatusNo UpToDateStatus = "no"
)
func fixManagedFields(ctx context.Context, namespace string, getObj *unstructured.Unstructured, getResource *resrc.RemoteResource, kubeClient kubeclnt.KubeClienter, mapper meta.ResettableRESTMapper) error {
if changed, err := getResource.FixManagedFields(); err != nil {
return fmt.Errorf("error fixing managed fields: %w", err)
} else if !changed {
return nil
}
unstruct := unstructured.Unstructured{Object: map[string]interface{}{}}
unstruct.SetManagedFields(getResource.Unstructured().GetManagedFields())
patch, err := json.Marshal(unstruct.UnstructuredContent())
if err != nil {
return fmt.Errorf("error marshaling fixed managed fields: %w", err)
}
log.Default.Info(ctx, "Fixing managed fields for resource %q", getResource.HumanID())
getObj, err = kubeClient.MergePatch(ctx, getResource.ResourceID, patch)
if err != nil {
return fmt.Errorf("error patching managed fields: %w", err)
}
getResource = resrc.NewRemoteResource(getObj, resrc.RemoteResourceOptions{
FallbackNamespace: namespace,
Mapper: mapper,
})
return nil
}