/
interface.go
329 lines (278 loc) · 7.29 KB
/
interface.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
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
329
/*
* CloudLinux Software Inc 2019-2021 All Rights Reserved
*/
package commons
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"fmt"
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
v12 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"log"
mathRand "math/rand"
"sigs.k8s.io/controller-runtime/pkg/client"
"text/template"
)
type protocol string
const (
TCPproto protocol = "tcp"
HTTPproto protocol = "http"
)
var (
errPersistentSecretWrongArgs = errors.New("PersistentSecret function needs `secretId <optional,string>, secretData <string> arguments`")
)
// PluginService is the interface that we're exposing as a plugin.
type PluginService interface {
Convert(req PluginRequest) *PluginResponse
Status(req PluginRequest) *PluginResponseStatus
Types() *PluginResponse
Default() *PluginResponseDefault
ValidateCreate(req PluginRequest) *PluginResponseValidation
ValidateUpdate(req PluginRequest) *PluginResponseValidation
ValidateDelete(req PluginRequest) *PluginResponseValidation
GetCredentialsMethod(req PluginRequestCredentialsMethod) *PluginResponseCredentialsMethod
}
type PluginRequestEmpty struct{}
type PluginRequest struct {
// Requested service Name
Name string
// Namespace where the service object must be located
Namespace string
// Optional. Host is address by which service should be available.
Host string
// Service Replicas
Replicas int32
// Requested service Version
Version string
// If a service should be exposed via TLS
Insecure bool
// TLSSecretName is a Kubernetes secret that contains tls.key / tls.crt fields. Must reside in the same namespace
TLSSecretName string
// Service resource Limits. Manipulated via SetLimits / GetLimits methods.
Limits []byte
// Service will use StorageClass / IngressClass for volume / ingress or default if empty
StorageClass string
IngressClass string
// Additional Parameters
Parameters map[string]interface{}
// Credentials
Credentials map[string]string
// Objects contains a list of service related objects
Objects []*unstructured.Unstructured
}
type TemplatedValue struct {
Raw string
Secret bool
SecretID string
}
func (pl *PluginRequest) SetObjects(objs []*unstructured.Unstructured) {
pl.Objects = objs
}
func (pl *PluginRequest) AddObject(o *unstructured.Unstructured) {
pl.Objects = append(pl.Objects, o)
}
func (pl *PluginRequest) GetObjects() []*unstructured.Unstructured {
return pl.Objects
}
func (pl *PluginRequest) SetLimits(limits *v1.ResourceList) error {
bytes, _ := json.Marshal(limits)
pl.Limits = bytes
return nil
}
func (pl *PluginRequest) GetLimits() (*v1.ResourceList, error) {
limits := &v1.ResourceList{}
if pl.Limits != nil && len(pl.Limits) > 0 {
err := json.Unmarshal(pl.Limits, limits)
if err != nil {
return nil, err
}
}
return limits, nil
}
func (pl *PluginRequest) RenderTemplate(tpl string) (*TemplatedValue, error) {
v := &TemplatedValue{}
tmpl, err := template.New("value").Funcs(template.FuncMap{
// PersistentSecret func accepts two args (one is optional):
// * keyId (this will be used to identify a secret key), empty if not passed
// * data (data that needs to be stored in secret)
"PersistentSecret": func(args ...string) (string, error) {
v.Secret = true
switch len(args) {
case 1:
return args[0], nil
case 2:
v.SecretID = args[0]
return args[1], nil
default:
return "", errPersistentSecretWrongArgs
}
},
"Base64": func(arg string) string {
return base64.StdEncoding.EncodeToString([]byte(arg))
},
"GenerateRSA": func(bits int) (string, error) {
pk, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return "", err
}
privateKeyBlock := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(pk),
}
buff := new(bytes.Buffer)
err = pem.Encode(buff, privateKeyBlock)
return buff.String(), err
},
"GenerateKey": func(length int) string {
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
b := make([]byte, length)
for i := range b {
b[i] = letterBytes[mathRand.Intn(len(letterBytes))]
}
return string(b)
},
"Endpoint": func(defaultValue string) string {
proto := "https"
if pl.Insecure {
proto = "http"
}
host := defaultValue
if pl.Host != "" {
host = pl.Host
}
return fmt.Sprintf("%s://%s", proto, host)
},
}).Parse(tpl)
if err != nil {
return v, errors.Wrap(err, "failed to parse template data")
}
data := &bytes.Buffer{}
err = tmpl.Execute(data, struct {
Name string
Namespace string
Host string
Replicas int32
Version string
Insecure bool
Parameters map[string]interface{}
}{
Name: pl.Name,
Namespace: pl.Namespace,
Host: pl.Host,
Replicas: pl.Replicas,
Version: pl.Version,
Insecure: pl.Insecure,
Parameters: pl.Parameters,
})
v.Raw = data.String()
return v, err
}
type PluginResponseValidation struct {
Err string
}
func (pl *PluginResponseValidation) Error() error {
if pl.Err != "" {
return errors.New(pl.Err)
}
return nil
}
type PluginResponse struct {
Objects []*unstructured.Unstructured
Protocol protocol
Service string
Err string
}
func (pl *PluginResponse) Error() error {
if pl.Err != "" {
return errors.New(pl.Err)
}
return nil
}
func (pl *PluginResponse) AddUnstructuredObject(object client.Object, gvk schema.GroupVersionKind) error {
o, err := ToUnstructured(object, gvk)
if err != nil {
pl.Err = err.Error()
return err
}
pl.Objects = append(pl.Objects, o)
return nil
}
type PluginResponseStatus struct {
IsReady bool
Err string
}
func (pl *PluginResponseStatus) Error() error {
if pl.Err != "" {
return errors.New(pl.Err)
}
return nil
}
type PluginResponseDefault struct {
Replicas int32
Version string
Host string
// *v1.ResourceList
Limits []byte
Parameters map[string]interface{}
Err string
}
func (pl *PluginResponseDefault) SetLimits(limits *v1.ResourceList) error {
bytes, err := json.Marshal(limits)
if err != nil {
log.Fatalf("error when marshaling limits: %v", err)
}
pl.Limits = bytes
return nil
}
func (pl *PluginResponseDefault) GetLimits() *v1.ResourceList {
if pl.Limits == nil && len(pl.Limits) == 0 {
return nil
}
limits := &v1.ResourceList{}
err := json.Unmarshal(pl.Limits, limits)
if err != nil {
log.Fatalf("error when unmarshaling limits: %v", err)
}
return limits
}
func (pl *PluginResponseDefault) Error() error {
if pl.Err != "" {
return errors.New(pl.Err)
}
return nil
}
type PluginRequestCredentialsMethod struct {
// service Name
Name string
// Credentials Data
Data map[string]string
}
func (m *PluginRequestCredentialsMethod) RenderTemplate(tpl string) (*TemplatedValue, error) {
v := &TemplatedValue{}
tmpl, err := template.New("value").Parse(tpl)
if err != nil || tmpl == nil {
return nil, errors.Wrap(err, "failed to parse template")
}
data := &bytes.Buffer{}
err = tmpl.Execute(data, m.Data)
v.Raw = data.String()
return v, err
}
type PluginResponseCredentialsMethod struct {
Method string // exec, etc
Exec CredentialsMethodExec
Err string
}
type CredentialsMethodExec struct {
PodSelector v12.LabelSelector
Container string
Command []string
}