HAOJX

取任意yaml资源转化为k8s资源的完全通用写法(kubectl源码的写法)

字数统计: 1.7k阅读时长: 10 min
2022/03/22 Share

k8spatcher.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
package utils

import (
"encoding/json"
"fmt"
"os"
"time"

"github.com/jonboulle/clockwork"
"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"
"k8s.io/apimachinery/pkg/util/jsonmergepatch"
"k8s.io/apimachinery/pkg/util/mergepatch"
"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/cli-runtime/pkg/resource"
oapi "k8s.io/kube-openapi/pkg/util/proto"
"k8s.io/kubectl/pkg/scheme"
"k8s.io/kubectl/pkg/util"
"k8s.io/kubectl/pkg/util/openapi"
)

const (
// maxPatchRetry is the maximum number of conflicts retry for during a patch operation before returning failure
maxPatchRetry = 5
// backOffPeriod is the period to back off when apply patch results in error.
backOffPeriod = 1 * time.Second
// how many times we can retry before back off
triesBeforeBackOff = 1
)

// All this code copied from
// <https://github.com/kubernetes/kubectl/blob/4ceef69fbc451d9bde6f4d5f92d55624b748141d/pkg/cmd/apply/patcher.go>
func NewPatcher(info *resource.Info, helper *resource.Helper) (*Patcher, error) {
var openapiSchema openapi.Resources

return &Patcher{
Mapping: info.Mapping,
Helper: helper,
Overwrite: true,
BackOff: clockwork.NewRealClock(),
Force: false,
Cascade: true,
Timeout: time.Duration(0),
GracePeriod: -1,
OpenapiSchema: openapiSchema,
Retries: 0,
}, nil
}

type Patcher struct {
Mapping *meta.RESTMapping
Helper *resource.Helper

Overwrite bool
BackOff clockwork.Clock

Force bool
Cascade bool
Timeout time.Duration
GracePeriod int

// If set, forces the patch against a specific resourceVersion
ResourceVersion *string

// Number of retries to make if the patch fails with conflict
Retries int

OpenapiSchema openapi.Resources
}

// Patch tries to patch an OpenAPI resource. On success, returns the merge patch as well
// the final patched object. On failure, returns an error.
func (p *Patcher) Patch(current runtime.Object, modified []byte,
namespace, name string) ([]byte, runtime.Object, error) {
var getErr error

patchBytes, patchObject, err := p.patchSimple(current, modified, namespace, name)

if p.Retries == 0 {
p.Retries = maxPatchRetry
}

for i := 1; i <= p.Retries && errors.IsConflict(err); i++ {
if i > triesBeforeBackOff {
p.BackOff.Sleep(backOffPeriod)
}

current, getErr = p.Helper.Get(namespace, name)
if getErr != nil {
return nil, nil, getErr
}

patchBytes, patchObject, err = p.patchSimple(current, modified, namespace, name)
}

if err != nil && (errors.IsConflict(err) || errors.IsInvalid(err)) && p.Force {
patchBytes, patchObject, err = p.deleteAndCreate(current, modified, namespace, name)
}

return patchBytes, patchObject, err
}

func (p *Patcher) patchSimple(obj runtime.Object, modified []byte, namespace, name string) ([]byte, runtime.Object, error) {
// Serialize the current configuration of the object from the server.
current, err := runtime.Encode(unstructured.UnstructuredJSONScheme, obj)
if err != nil {
return nil, nil, err
}

// Retrieve the original configuration of the object from the annotation.
original, err := util.GetOriginalConfiguration(obj)
if err != nil {
return nil, nil, err
}

var patchType types.PatchType
var patch []byte
var lookupPatchMeta strategicpatch.LookupPatchMeta
var schema oapi.Schema
// createPatchErrFormat := "creating patch with:\\noriginal:\\n%s\\nmodified:\\n%s\\ncurrent:\\n%s\\nfor:"

// Create the versioned struct from the type defined in the restmapping
// (which is the API version we'll be submitting the patch to)
versionedObject, err := scheme.Scheme.New(p.Mapping.GroupVersionKind)
switch {
case runtime.IsNotRegisteredError(err):
// fall back to generic JSON merge patch
patchType = types.MergePatchType
preconditions := []mergepatch.PreconditionFunc{mergepatch.RequireKeyUnchanged("apiversion"),
mergepatch.RequireKeyUnchanged("kind"), mergepatch.RequireMetadataKeyUnchanged("name")}
patch, err = jsonmergepatch.CreateThreeWayJSONMergePatch(original, modified, current, preconditions...)
if err != nil {
if mergepatch.IsPreconditionFailed(err) {
return nil, nil, fmt.Errorf("%s", "At least one of apiVersion, kind and name was changed")
}
return nil, nil, err
}
case err != nil:
return nil, nil, err
case err == nil:
// Compute a three way strategic merge patch to send to server.
patchType = types.StrategicMergePatchType

// Try to use openapi first if the openapi spec is available and can successfully calculate the patch.
// Otherwise, fall back to baked-in types.
if p.OpenapiSchema != nil {
if schema = p.OpenapiSchema.LookupResource(p.Mapping.GroupVersionKind); schema != nil {
lookupPatchMeta = strategicpatch.PatchMetaFromOpenAPI{Schema: schema}
if openapiPatch, err := strategicpatch.CreateThreeWayMergePatch(original, modified, current, lookupPatchMeta, p.Overwrite); err != nil {
fmt.Fprintf(os.Stderr, "warning: error calculating patch from openapi spec: %v\\n", err)
} else {
patchType = types.StrategicMergePatchType
patch = openapiPatch
}
}
}

if patch == nil {
lookupPatchMeta, err = strategicpatch.NewPatchMetaFromStruct(versionedObject)
if err != nil {
return nil, nil, err
}
patch, err = strategicpatch.CreateThreeWayMergePatch(original, modified, current, lookupPatchMeta, p.Overwrite)
if err != nil {
return nil, nil, err
}
}
}

if string(patch) == "{}" {
return patch, obj, nil
}

if p.ResourceVersion != nil {
patch, err = addResourceVersion(patch, *p.ResourceVersion)
if err != nil {
return nil, nil, err
}
}

patchedObj, err := p.Helper.Patch(namespace, name, patchType, patch, nil)
return patch, patchedObj, err
}

func (p *Patcher) deleteAndCreate(original runtime.Object, modified []byte, namespace, name string) ([]byte, runtime.Object, error) {
if err := p.delete(namespace, name); err != nil {
return modified, nil, err
}
// TODO: use wait
if err := wait.PollImmediate(1*time.Second, p.Timeout, func() (bool, error) {
if _, err := p.Helper.Get(namespace, name); !errors.IsNotFound(err) {
return false, err
}
return true, nil
}); err != nil {
return modified, nil, err
}
versionedObject, _, err := unstructured.UnstructuredJSONScheme.Decode(modified, nil, nil)
if err != nil {
return modified, nil, err
}
createdObject, err := p.Helper.Create(namespace, true, versionedObject)
if err != nil {
// restore the original object if we fail to create the new one
// but still propagate and advertise error to user
recreated, recreateErr := p.Helper.Create(namespace, true, original)
if recreateErr != nil {
err = fmt.Errorf("An error occurred force-replacing the existing object with the newly provided one:\\n\\n%v.\\n\\nAdditionally, an error occurred attempting to restore the original object:\\n\\n%v", err, recreateErr)
} else {
createdObject = recreated
}
}
return modified, createdObject, err
}

func (p *Patcher) delete(namespace, name string) error {
options := asDeleteOptions(p.Cascade, p.GracePeriod)
_, err := p.Helper.DeleteWithOptions(namespace, name, &options)
return err
}

func asDeleteOptions(cascade bool, gracePeriod int) metav1.DeleteOptions {
options := metav1.DeleteOptions{}
if gracePeriod >= 0 {
options = *metav1.NewDeleteOptions(int64(gracePeriod))
}
policy := metav1.DeletePropagationForeground
if !cascade {
policy = metav1.DeletePropagationOrphan
}
options.PropagationPolicy = &policy
return options
}

func addResourceVersion(patch []byte, rv string) ([]byte, error) {
var patchMap map[string]interface{}
err := json.Unmarshal(patch, &patchMap)
if err != nil {
return nil, err
}
u := unstructured.Unstructured{Object: patchMap}
a, err := meta.Accessor(&u)
if err != nil {
return nil, err
}
a.SetResourceVersion(rv)

return json.Marshal(patchMap)
}

k8shelper.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
package utils

import (
"bytes"
"fmt"
"io"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/rest"
"k8s.io/kubectl/pkg/util"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
syaml "k8s.io/apimachinery/pkg/runtime/serializer/yaml"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/cli-runtime/pkg/resource"
"log"
)

func setDefaultNamespaceIfScopedAndNoneSet(u *unstructured.Unstructured, helper *resource.Helper) {
namespace := u.GetNamespace()
if helper.NamespaceScoped && namespace == "" {
namespace = "default"
u.SetNamespace(namespace)
}
}
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)
}

//模拟kubectl apply 功能
func K8sApply(json []byte, restConfig *rest.Config, mapper meta.RESTMapper) error {
decoder := yaml.NewYAMLOrJSONDecoder(bytes.NewReader(json),
len(json))
for {
var rawObj runtime.RawExtension
err := decoder.Decode(&rawObj)
if err != nil {
if err == io.EOF {
break
} else {
return err
}
}
// 得到gvk
obj, gvk, err := syaml.NewDecodingSerializer(unstructured.
UnstructuredJSONScheme).Decode(rawObj.Raw, nil, nil)
if err != nil {
log.Fatal(err)
}
//把obj 变成map[string]interface{}
unstructuredMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
log.Fatal(err)
}
unstructuredObj := &unstructured.Unstructured{Object: unstructuredMap}

restMapping, err := mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
return err
}
//这里不能使用 传统的clientset 必须要使用这个函数
restClient, err := newRestClient(restConfig, gvk.GroupVersion())

helper := resource.NewHelper(restClient, restMapping)

setDefaultNamespaceIfScopedAndNoneSet(unstructuredObj, helper)

objInfo := &resource.Info{
Client: restClient,
Mapping: restMapping,
Namespace: unstructuredObj.GetNamespace(),
Name: unstructuredObj.GetName(),
Object: unstructuredObj,
ResourceVersion: restMapping.Resource.Version,
}

// kubectl 封装 的一个 patcher
patcher, err := NewPatcher(objInfo, helper)
if err != nil {
return err
}

//获取更改的 数据
modified, err := util.GetModifiedConfiguration(objInfo.Object, true, unstructured.UnstructuredJSONScheme)
if err != nil {
return err
}

if err := objInfo.Get(); err != nil {
if !errors.IsNotFound(err) { //资源不存在
return err
}

//这里是kubectl的一些注解增加, 不管了。 直接加进去
if err := util.CreateApplyAnnotation(objInfo.Object, unstructured.UnstructuredJSONScheme); err != nil {
return err
}

// 直接创建
obj, err := helper.Create(objInfo.Namespace, true, objInfo.Object)
if err != nil {

fmt.Println("有错")
return err
}
objInfo.Refresh(obj, true)
}

_, patchedObject, err := patcher.Patch(objInfo.Object, modified, objInfo.Namespace, objInfo.Name)
if err != nil {
return err
}

objInfo.Refresh(patchedObject, true)

}
return nil
}

helper.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
package utils

import (
"io/ioutil"
"os"
)

func checkErr(err error) {
if err != nil {
panic(err)
}
}
func MustLoadFile(path string) []byte {
f, err := os.Open(path)
checkErr(err)
defer f.Close()
b, err := ioutil.ReadAll(f)
checkErr(err)
return b
}
func LoadFile(path string) ([]byte, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
b, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
return b, nil
}
CATALOG