generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathapply-manifest.go
206 lines (175 loc) · 5.73 KB
/
apply-manifest.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
package kubernetes
import (
"context"
"strings"
v1 "k8s.io/api/core/v1"
kubeerror "k8s.io/apimachinery/pkg/api/errors"
meta "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/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer/yaml"
"k8s.io/cli-runtime/pkg/resource"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/restmapper"
)
type ApplyOptions struct {
Namespace string
Update bool
Delete bool
IgnoreErrors bool
}
// ApplyManifest applies, updates or deletes resources as specified in ApplyOptions.
// The namespace specified in ApplyOptions is used, if no namespace is specified then
// the namespace from manifest is used.
// If the the namespace does not exists, it will be created.
func (client *Client) ApplyManifest(contents []byte, recvOptions ApplyOptions) error {
manifests := strings.Split(string(contents), "\n---\n")
if len(manifests) > 0 && manifests[len(manifests)-1] == "\n" {
manifests = manifests[:len(manifests)-1]
}
for _, manifest := range manifests {
// create a fresh options var at each run
options := recvOptions
// get the runtime.Object
object, obj, err := GetObjectFromManifest(manifest)
if err != nil {
if recvOptions.IgnoreErrors || len(obj.GetObjectKind().GroupVersionKind().Kind) < 1 {
continue
}
return err
}
helper, err := constructObject(client.KubeClient, client.RestConfig, object)
if err != nil {
if recvOptions.IgnoreErrors {
continue
}
return err
}
// Default to namespace from the UI. If no namespace is passed, use the namespace used in the manifest.
val, err := meta.NewAccessor().Namespace(object)
if err == nil && len(val) > 1 {
if len(options.Namespace) > 1 {
er := meta.NewAccessor().SetNamespace(object, options.Namespace)
if er != nil {
if recvOptions.IgnoreErrors {
continue
}
return er
}
} else {
options.Namespace = val
}
}
// Create namespace if it doesnt already exist
if err = createNamespaceIfNotExist(context.TODO(), client.KubeClient, options.Namespace); err != nil {
if recvOptions.IgnoreErrors {
continue
}
return err
}
if options.Delete {
_, err = deleteObject(helper, options.Namespace, object)
if err != nil && !kubeerror.IsNotFound(err) {
if recvOptions.IgnoreErrors {
continue
}
return err
}
} else {
_, err = createObject(helper, options.Namespace, object, options.Update)
if err != nil && !kubeerror.IsAlreadyExists(err) {
if recvOptions.IgnoreErrors {
continue
}
return err
}
}
}
return nil
}
func GetObjectFromManifest(manifest string) (runtime.Object, *unstructured.Unstructured, error) {
// decode YAML into unstructured.Unstructured
obj := &unstructured.Unstructured{}
dec := yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme)
object, _, err := dec.Decode([]byte(manifest), nil, obj)
if err != nil {
return nil, obj, ErrApplyManifest(err)
}
return object, obj, nil
}
func constructObject(kubeClientset kubernetes.Interface, restConfig rest.Config, obj runtime.Object) (*resource.Helper, error) {
// Create a REST mapper that tracks information about the available resources in the cluster.
groupResources, err := restmapper.GetAPIGroupResources(kubeClientset.Discovery())
if err != nil {
return nil, err
}
rm := restmapper.NewDiscoveryRESTMapper(groupResources)
// Get some metadata needed to make the REST request.
gvk := obj.GetObjectKind().GroupVersionKind()
gk := schema.GroupKind{Group: gvk.Group, Kind: gvk.Kind}
mapping, err := rm.RESTMapping(gk, gvk.Version)
if err != nil {
return nil, err
}
// Create a client specifically for creating the object.
restClient, err := newRestClient(restConfig, mapping.GroupVersionKind.GroupVersion())
if err != nil {
return nil, err
}
// Use the REST helper to create the object in the "default" namespace.
return resource.NewHelper(restClient, mapping), nil
}
func newRestClient(restConfig rest.Config, gv schema.GroupVersion) (rest.Interface, error) {
restConfig.ContentConfig = resource.UnstructuredPlusDefaultContentConfig()
restConfig.GroupVersion = &gv
if len(gv.Group) == 0 {
restConfig.APIPath = "/api"
} else {
restConfig.APIPath = "/apis"
}
return rest.RESTClientFor(&restConfig)
}
func createObject(restHelper *resource.Helper, namespace string, obj runtime.Object, update bool) (runtime.Object, error) {
name, err := meta.NewAccessor().Name(obj)
if err != nil {
return nil, err
}
var object runtime.Object
var er error
object, err = restHelper.Create(namespace, update, obj)
if err != nil {
if kubeerror.IsAlreadyExists(err) && update {
object, er = restHelper.Replace(namespace, name, update, obj)
if er != nil {
if (kubeerror.IsInvalid(er) && strings.Contains(er.Error(), "field is immutable")) || (kubeerror.IsInvalid(er) && strings.Contains(er.Error(), "primary clusterIP can not be unset")) {
return object, nil
}
return nil, er
}
return object, nil
}
return nil, err
}
return object, nil
}
func deleteObject(restHelper *resource.Helper, namespace string, obj runtime.Object) (runtime.Object, error) {
name, err := meta.NewAccessor().Name(obj)
if err != nil {
return nil, err
}
return restHelper.Delete(namespace, name)
}
func createNamespaceIfNotExist(ctx context.Context, kubeClientset kubernetes.Interface, namespace string) error {
if namespace == "" {
return nil
}
nsSpec := &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}}
_, err := kubeClientset.CoreV1().Namespaces().Create(ctx, nsSpec, metav1.CreateOptions{})
if !kubeerror.IsAlreadyExists(err) {
return err
}
return nil
}