aboutsummaryrefslogtreecommitdiffstats
path: root/src/rsync/pkg/client/client.go
blob: 0eaded2289479bf2554210b2b770f73f52c2ff60 (plain)
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
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Based on Code: https://github.com/johandry/klient
package client

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, ns string) (*Client, error) {
	var namespace string
	var enforceNamespace bool
	var err 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)

	if ns == "" {
		namespace, enforceNamespace, err = factory.ToRawKubeConfigLoader().Namespace()
		if err != nil {
			namespace = v1.NamespaceDefault
			enforceNamespace = true
		}
	} else {
		namespace = ns
		enforceNamespace = false
	}
	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, namespace string) *Client {
	client, _ := NewE(context, kubeconfig, namespace)
	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)
}

// IsReachable tests connectivity to the cluster
func (c *Client) IsReachable() error {
	client, _ := c.factory.KubernetesClientSet()
	_, err := client.ServerVersion()
	if err != nil {
		return fmt.Errorf("Kubernetes cluster unreachable")
	}
	return nil
}