-
Notifications
You must be signed in to change notification settings - Fork 1
/
manifest_op.go
178 lines (152 loc) · 4.32 KB
/
manifest_op.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
package kubernetes
import (
"encoding/json"
"io"
"strings"
k8s "github.com/rishi-anand/terraform-provider-kubernetes/client"
apierrs "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/types"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/restmapper"
)
type ManifestAction string
const (
ApplyAction ManifestAction = "apply"
DeleteAction ManifestAction = "delete"
)
func doManifestAction(action ManifestAction, content, nsoverride string, skipResources []string) error {
//Get Kube client
kubeClient, err := k8s.GetClient()
if err != nil {
return err
}
_, apiCollections, err := kubeClient.Discovery().ServerGroupsAndResources()
if err != nil {
return err
}
clusterScopedResourceKindMap := make(map[string]bool)
for _, apiCollection := range apiCollections {
for _, apiResource := range apiCollection.APIResources {
if !apiResource.Namespaced {
clusterScopedResourceKindMap[apiResource.Kind] = true
}
}
}
//Discover Server Resource Types
dd := kubeClient.Discovery()
apigroups, err := restmapper.GetAPIGroupResources(dd)
if err != nil {
return err
}
//Create Map of resoruces
rm := restmapper.NewDiscoveryRESTMapper(apigroups)
//Decoder for the Yaml/Json file
d := yaml.NewYAMLOrJSONDecoder(strings.NewReader(content), 4096)
//Read & Process Manifest Blocls from Manifest file one by one till EOF
for {
ext := runtime.RawExtension{}
if err := d.Decode(&ext); err != nil {
if err == io.EOF {
break
}
return err
}
//Gt Group Version Kid for the Manifest Block
_, gvk, _ := unstructured.UnstructuredJSONScheme.Decode(ext.Raw, nil, nil)
mapping, err := rm.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
return err
}
restconfig, err := k8s.GetConfig()
if err != nil {
return err
}
//Get Dynamic Client for Specific Group, Version
restconfig.GroupVersion = &schema.GroupVersion{
Group: mapping.GroupVersionKind.Group,
Version: mapping.GroupVersionKind.Version,
}
dclient, err := k8s.GetDynamicClientForConfig(restconfig)
if err != nil {
return err
}
//Construct Unstructured Object to apply
var unstruct unstructured.Unstructured
unstruct.Object = make(map[string]interface{})
var blob interface{}
if err := json.Unmarshal(ext.Raw, &blob); err != nil {
return err
}
//Handle Namespace and Object Name
unstruct.Object = blob.(map[string]interface{})
ns := "default"
if nsoverride != "" {
ns = nsoverride
}
//If Object had specified namespace, the override if override specified
if md, ok := unstruct.Object["metadata"]; ok {
metadata := md.(map[string]interface{})
if internalns, ok := metadata["namespace"]; ok {
objns := internalns.(string)
if objns != "" && nsoverride != "" {
//Override the namespace provided in the object
metadata["namespace"] = ns
}
}
}
var res dynamic.ResourceInterface
var skipResource = false
for _, v := range skipResources {
if v == mapping.GroupVersionKind.Kind {
skipResource = true
break
}
}
if skipResource {
continue
}
if clusterScopedResourceKindMap[mapping.GroupVersionKind.Kind] {
res = dclient.Resource(mapping.Resource)
} else {
res = dclient.Resource(mapping.Resource).Namespace(ns)
}
getObj := metav1.GetOptions{
TypeMeta: metav1.TypeMeta{
Kind: unstruct.GetKind(),
APIVersion: unstruct.GetAPIVersion(),
},
}
if action == ApplyAction {
if unstructGet, err := res.Get(unstruct.GetName(), getObj); err != nil {
if apierrs.IsNotFound(err) {
if _, err = res.Create(&unstruct, metav1.CreateOptions{}); err != nil {
return err
}
} else {
return err
}
} else {
unstruct.SetResourceVersion(unstructGet.GetResourceVersion())
out, err := unstruct.MarshalJSON()
if err != nil {
return err
}
if _, err = res.Patch(unstruct.GetName(), types.MergePatchType, out, metav1.PatchOptions{}); err != nil {
return err
}
}
} else if action == DeleteAction {
if err := res.Delete(unstruct.GetName(), &metav1.DeleteOptions{}); err != nil {
if !apierrs.IsNotFound(err) {
return err
}
}
}
}
return nil
}