-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcreate.go
More file actions
executable file
·328 lines (278 loc) · 8.73 KB
/
create.go
File metadata and controls
executable file
·328 lines (278 loc) · 8.73 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
Copyright (C) 2022-2026 ApeCloud Co., Ltd
This file is part of KubeBlocks project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package action
import (
"context"
"embed"
"encoding/json"
"fmt"
"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/cuecontext"
cuejson "cuelang.org/go/encoding/json"
"github.com/leaanthony/debme"
"github.com/spf13/cobra"
apierrors "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/schema"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/genericiooptions"
"k8s.io/cli-runtime/pkg/printers"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/scheme"
"github.com/apecloud/kbcli/pkg/printer"
)
var (
//go:embed template/*
cueTemplate embed.FS
)
type CreateDependency func(dryRun []string) error
type DryRunStrategy int
const (
DryRunNone DryRunStrategy = iota
DryRunClient
DryRunServer
)
// CreateOptions the options of creation command should inherit baseOptions
type CreateOptions struct {
Factory cmdutil.Factory
Namespace string
// Name Resource name of the command line operation
Name string
Args []string
Dynamic dynamic.Interface
Client kubernetes.Interface
Format printer.Format
ToPrinter func(*meta.RESTMapping, bool) (printers.ResourcePrinterFunc, error)
DryRun string
EditBeforeCreate bool
// CueTemplateName cue template file name to render the resource
CueTemplateName string
// Options a command options object which extends CreateOptions that will be used
// to render the cue template
Options interface{}
// GVR is the GroupVersionResource of the resource to be created
GVR schema.GroupVersionResource
// CustomOutPut will be executed after creating successfully.
CustomOutPut func(options *CreateOptions)
// PreCreate optional, make changes on yaml before create
PreCreate func(*unstructured.Unstructured) error
// CleanUpFn will be executed after creating failed.
CleanUpFn func() error
// CreateDependencies will be executed before creating.
CreateDependencies CreateDependency
// Quiet minimize unnecessary output
Quiet bool
genericiooptions.IOStreams
}
func (o *CreateOptions) AddCommonFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&o.DryRun, "dry-run", "none", `Must be "client", or "server". If with client strategy, only print the object that would be sent, and no data is actually sent. If with server strategy, submit the server-side request, but no data is persistent.`)
cmd.Flags().Lookup("dry-run").NoOptDefVal = "unchanged"
cmd.Flags().BoolVar(&o.EditBeforeCreate, "edit", o.EditBeforeCreate, "Edit the API resource before creating")
// add print flags
printer.AddOutputFlagForCreate(cmd, &o.Format, false)
}
func (o *CreateOptions) Complete() error {
var err error
if o.Namespace == "" {
if o.Namespace, _, err = o.Factory.ToRawKubeConfigLoader().Namespace(); err != nil {
return err
}
}
// now we use the first argument as the resource name
if len(o.Args) > 0 {
o.Name = o.Args[0]
}
if o.Dynamic, err = o.Factory.DynamicClient(); err != nil {
return err
}
if o.Client, err = o.Factory.KubernetesClientSet(); err != nil {
return err
}
o.ToPrinter = func(mapping *meta.RESTMapping, withNamespace bool) (printers.ResourcePrinterFunc, error) {
var p printers.ResourcePrinter
switch o.Format {
case printer.JSON:
p = &printers.JSONPrinter{}
case printer.YAML:
p = &printers.YAMLPrinter{}
default:
return nil, genericclioptions.NoCompatiblePrinterError{AllowedFormats: []string{"JSON", "YAML"}}
}
p, err = printers.NewTypeSetter(scheme.Scheme).WrapToPrinter(p, nil)
if err != nil {
return nil, err
}
return p.PrintObj, nil
}
return nil
}
// Run execute command. the options of parameter contain the command flags and args.
func (o *CreateOptions) Run() error {
resObj, err := o.buildResourceObj()
if err != nil {
return err
}
if o.PreCreate != nil {
if err = o.PreCreate(resObj); err != nil {
return err
}
}
if o.EditBeforeCreate {
customEdit := NewCustomEditOptions(o.Factory, o.IOStreams, EditForCreate)
if err := customEdit.Run(resObj); err != nil {
return err
}
}
dryRunStrategy, err := o.GetDryRunStrategy()
if err != nil {
return err
}
if dryRunStrategy != DryRunClient {
createOptions := metav1.CreateOptions{}
if dryRunStrategy == DryRunServer {
createOptions.DryRun = []string{metav1.DryRunAll}
}
// create dependencies
if o.CreateDependencies != nil {
if err = o.CreateDependencies(createOptions.DryRun); err != nil {
return err
}
}
// create kubernetes resource
resObj, err = o.Dynamic.Resource(o.GVR).Namespace(o.Namespace).Create(context.TODO(), resObj, createOptions)
if err != nil {
if apierrors.IsAlreadyExists(err) {
return err
}
// for other errors, clean up dependencies
if cleanErr := o.CleanUp(); cleanErr != nil {
fmt.Fprintf(o.ErrOut, "Failed to clean up denpendencies: %v\n", cleanErr)
}
return err
}
if dryRunStrategy != DryRunServer {
o.Name = resObj.GetName()
if o.Quiet {
return nil
}
if o.CustomOutPut != nil {
o.CustomOutPut(o)
} else {
fmt.Fprintf(o.Out, "%s %s created\n", resObj.GetKind(), resObj.GetName())
}
return nil
}
}
p, err := o.ToPrinter(nil, false)
if err != nil {
return err
}
return p.PrintObj(resObj, o.Out)
}
func (o *CreateOptions) CleanUp() error {
if o.CreateDependencies == nil {
return nil
}
if o.CleanUpFn != nil {
return o.CleanUpFn()
}
return nil
}
func (o *CreateOptions) buildResourceObj() (*unstructured.Unstructured, error) {
var (
cueValue cue.Value
err error
optionsByte []byte
)
if optionsByte, err = json.Marshal(o.Options); err != nil {
return nil, err
}
// append namespace and name to options and marshal to json
m := make(map[string]interface{})
if err = json.Unmarshal(optionsByte, &m); err != nil {
return nil, err
}
m["namespace"] = o.Namespace
m["name"] = o.Name
if optionsByte, err = json.Marshal(m); err != nil {
return nil, err
}
if cueValue, err = newCueValue(o.CueTemplateName); err != nil {
return nil, err
}
if cueValue, err = fillOptions(cueValue, optionsByte); err != nil {
return nil, err
}
return convertContentToUnstructured(cueValue)
}
func (o *CreateOptions) GetDryRunStrategy() (DryRunStrategy, error) {
if o.DryRun == "" {
return DryRunNone, nil
}
switch o.DryRun {
case "client":
return DryRunClient, nil
case "server":
return DryRunServer, nil
case "unchanged":
return DryRunClient, nil
case "none":
return DryRunNone, nil
default:
return DryRunNone, fmt.Errorf(`invalid dry-run value (%v). Must be "none", "server", or "client"`, o.DryRun)
}
}
// NewCueValue converts cue template to cue Value which holds any value like Boolean,Struct,String and more cue type.
func newCueValue(cueTemplateName string) (cue.Value, error) {
tmplFs, _ := debme.FS(cueTemplate, "template")
if tmlBytes, err := tmplFs.ReadFile(cueTemplateName); err != nil {
return cue.Value{}, err
} else {
return cuecontext.New().CompileString(string(tmlBytes)), nil
}
}
// fillOptions fills options object in cue template file
func fillOptions(cueValue cue.Value, optionsByte []byte) (cue.Value, error) {
var (
expr ast.Expr
err error
)
if expr, err = cuejson.Extract("", optionsByte); err != nil {
return cue.Value{}, err
}
optionsValue := cueValue.Context().BuildExpr(expr)
cueValue = cueValue.FillPath(cue.ParsePath("options"), optionsValue)
return cueValue, nil
}
// convertContentToUnstructured gets content object in cue template file and convert it to Unstructured
func convertContentToUnstructured(cueValue cue.Value) (*unstructured.Unstructured, error) {
var (
contentByte []byte
err error
unstructuredObj = &unstructured.Unstructured{}
)
if contentByte, err = cueValue.LookupPath(cue.ParsePath("content")).MarshalJSON(); err != nil {
return nil, err
}
if err = json.Unmarshal(contentByte, &unstructuredObj); err != nil {
return nil, err
}
return unstructuredObj, nil
}