forked from totvs-cloud/go-manifest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apply.go
130 lines (100 loc) · 3.22 KB
/
apply.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
package manifest
import (
"context"
"fmt"
"strings"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
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/types"
)
func (l *list) Apply(ctx context.Context) error {
for _, v := range l.Resources() {
if err := l.apply(ctx, v); err != nil {
return err
}
}
return nil
}
func (l *list) apply(ctx context.Context, obj *unstructured.Unstructured) error {
log := logr.FromContextOrDiscard(ctx)
current, err := l.find(ctx, obj)
if err != nil {
return err
}
gvk := obj.GroupVersionKind()
kind := fmt.Sprintf("%s.%s", strings.ToLower(gvk.Kind), gvk.Group)
if len(gvk.Group) == 0 {
kind = strings.ToLower(gvk.Kind)
}
if current == nil { // create
if _, err = l.patch(ctx, obj); err != nil {
return err
}
log.Info(fmt.Sprintf("%s %q created", kind, obj.GetName()))
return nil
}
// update
updated, err := l.patch(ctx, obj)
if err != nil {
return err
}
if current.GetResourceVersion() == updated.GetResourceVersion() {
log.Info(fmt.Sprintf("%s %q unchanged", kind, obj.GetName()))
return nil
}
log.Info(fmt.Sprintf("%s %q configured", kind, obj.GetName()))
return nil
}
func (l *list) find(ctx context.Context, obj *unstructured.Unstructured) (*unstructured.Unstructured, error) {
gvk := obj.GroupVersionKind()
kind := fmt.Sprintf("%s.%s", strings.ToLower(gvk.Kind), gvk.Group)
if len(gvk.Group) == 0 {
kind = strings.ToLower(gvk.Kind)
}
mapper, err := l.mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
return nil, fmt.Errorf("failed to retrieve REST mapping for %s: %w", kind, err)
}
resource := l.client.Resource(mapper.Resource).Namespace(obj.GetNamespace())
if mapper.Scope.Name() == meta.RESTScopeNameRoot {
resource = l.client.Resource(mapper.Resource)
}
result, err := resource.Get(ctx, obj.GetName(), metav1.GetOptions{})
if errors.IsNotFound(err) {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("failed to get %s %q: %w", kind, obj.GetName(), err)
}
return result, nil
}
func (l *list) patch(ctx context.Context, obj *unstructured.Unstructured) (*unstructured.Unstructured, error) {
gvk := obj.GroupVersionKind()
kind := fmt.Sprintf("%s.%s", strings.ToLower(gvk.Kind), gvk.Group)
if len(gvk.Group) == 0 {
kind = strings.ToLower(gvk.Kind)
}
mapper, err := l.mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
return nil, fmt.Errorf("failed to retrieve REST mapping for %s: %w", kind, err)
}
data, err := runtime.Encode(unstructured.UnstructuredJSONScheme, obj)
if err != nil {
return nil, fmt.Errorf("failed to encode JSON for %s: %w", kind, err)
}
resource := l.client.Resource(mapper.Resource).Namespace(obj.GetNamespace())
if mapper.Scope.Name() == meta.RESTScopeNameRoot {
resource = l.client.Resource(mapper.Resource)
}
force := true
options := metav1.PatchOptions{Force: &force, FieldManager: l.fieldManager}
patch, err := resource.Patch(ctx, obj.GetName(), types.ApplyPatchType, data, options)
if err != nil {
return nil, fmt.Errorf("failed to patch %s %q: %w", kind, obj.GetName(), err)
}
return patch, nil
}