-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathclient.go
More file actions
160 lines (137 loc) · 4.13 KB
/
client.go
File metadata and controls
160 lines (137 loc) · 4.13 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
package klient
import (
"bytes"
"fmt"
"io"
v1 "k8s.io/api/core/v1"
"k8s.io/cli-runtime/pkg/resource"
"k8s.io/client-go/kubernetes"
"k8s.io/kubectl/pkg/validation"
)
// DefaultValidation default action to validate. If `true` all resources by
// default will be validated.
const DefaultValidation = true
// Client is a kubernetes client, like `kubectl`
type Client struct {
Clientset *kubernetes.Clientset
factory *factory
validator validation.Schema
namespace string
enforceNamespace bool
forceConflicts bool
ServerSideApply bool
}
// Result is an alias for the Kubernetes CLI runtime resource.Result
type Result = resource.Result
// BuilderOptions parameters to create a Resource Builder
type BuilderOptions struct {
Unstructured bool
Validate bool
Namespace string
LabelSelector string
FieldSelector string
All bool
AllNamespaces bool
}
// NewBuilderOptions creates a BuilderOptions with the default values for
// the parameters to create a Resource Builder
func NewBuilderOptions() *BuilderOptions {
return &BuilderOptions{
Unstructured: true,
Validate: true,
}
}
// NewE creates a kubernetes client, returns an error if fail
func NewE(context, kubeconfig string) (*Client, error) {
factory := newFactory(context, kubeconfig)
// If `true` it will always validate the given objects/resources
// Unless something different is specified in the NewBuilderOptions
validator, _ := factory.Validator(DefaultValidation)
namespace, enforceNamespace, err := factory.ToRawKubeConfigLoader().Namespace()
if err != nil {
namespace = v1.NamespaceDefault
enforceNamespace = true
}
clientset, err := factory.KubernetesClientSet()
if err != nil {
return nil, err
}
if clientset == nil {
return nil, fmt.Errorf("cannot create a clientset from given context and kubeconfig")
}
return &Client{
factory: factory,
Clientset: clientset,
validator: validator,
namespace: namespace,
enforceNamespace: enforceNamespace,
}, nil
}
// New creates a kubernetes client
func New(context, kubeconfig string) *Client {
client, _ := NewE(context, kubeconfig)
return client
}
// Builder creates a resource builder
func (c *Client) builder(opt *BuilderOptions) *resource.Builder {
validator := c.validator
namespace := c.namespace
if opt == nil {
opt = NewBuilderOptions()
} else {
if opt.Validate != DefaultValidation {
validator, _ = c.factory.Validator(opt.Validate)
}
if opt.Namespace != "" {
namespace = opt.Namespace
}
}
b := c.factory.NewBuilder()
if opt.Unstructured {
b = b.Unstructured()
}
return b.
Schema(validator).
ContinueOnError().
NamespaceParam(namespace).DefaultNamespace()
}
// ResultForFilenameParam returns the builder results for the given list of files or URLs
func (c *Client) ResultForFilenameParam(filenames []string, opt *BuilderOptions) *Result {
filenameOptions := &resource.FilenameOptions{
Recursive: false,
Filenames: filenames,
}
return c.builder(opt).
FilenameParam(c.enforceNamespace, filenameOptions).
Flatten().
Do()
}
// ResultForReader returns the builder results for the given reader
func (c *Client) ResultForReader(r io.Reader, opt *BuilderOptions) *Result {
return c.builder(opt).
Stream(r, "").
Flatten().
Do()
}
// func (c *Client) ResultForName(opt *BuilderOptions, names ...string) *Result {
// return c.builder(opt).
// LabelSelectorParam(opt.LabelSelector).
// FieldSelectorParam(opt.FieldSelector).
// SelectAllParam(opt.All).
// AllNamespaces(opt.AllNamespaces).
// ResourceTypeOrNameArgs(false, names...).RequireObject(false).
// Flatten().
// Do()
// }
// ResultForContent returns the builder results for the given content
func (c *Client) ResultForContent(content []byte, opt *BuilderOptions) *Result {
b := bytes.NewBuffer(content)
return c.ResultForReader(b, opt)
}
func failedTo(action string, info *resource.Info, err error) error {
var resKind string
if info.Mapping != nil {
resKind = info.Mapping.GroupVersionKind.Kind + " "
}
return fmt.Errorf("cannot %s object Kind: %q, Name: %q, Namespace: %q. %s", action, resKind, info.Name, info.Namespace, err)
}