-
Notifications
You must be signed in to change notification settings - Fork 80
/
k8sgpt.go
432 lines (394 loc) · 11 KB
/
k8sgpt.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
Copyright 2023 The K8sGPT Authors.
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.
*/
package resources
import (
"context"
err "errors"
"github.com/k8sgpt-ai/k8sgpt-operator/api/v1alpha1"
"github.com/k8sgpt-ai/k8sgpt-operator/pkg/utils"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
r1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/retry"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
// SyncOrDestroy enum create or destroy
type SyncOrDestroy int
const (
SyncOp SyncOrDestroy = iota
DestroyOp
DeploymentName = "k8sgpt-deployment"
)
// GetService Create service for K8sGPT
func GetService(config v1alpha1.K8sGPT) (*corev1.Service, error) {
// Create service
service := corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "k8sgpt",
Namespace: config.Namespace,
OwnerReferences: []metav1.OwnerReference{
{
Kind: config.Kind,
Name: config.Name,
UID: config.UID,
APIVersion: config.APIVersion,
BlockOwnerDeletion: utils.PtrBool(true),
Controller: utils.PtrBool(true),
},
},
},
Spec: corev1.ServiceSpec{
Selector: map[string]string{
"app": DeploymentName,
},
Ports: []corev1.ServicePort{
{
Port: 8080,
},
},
},
}
return &service, nil
}
// GetServiceAccount Create Service Account for K8sGPT and bind it to K8sGPT role
func GetServiceAccount(config v1alpha1.K8sGPT) (*corev1.ServiceAccount, error) {
// Create service account
serviceAccount := corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: "k8sgpt",
Namespace: config.Namespace,
OwnerReferences: []metav1.OwnerReference{
{
Kind: config.Kind,
Name: config.Name,
UID: config.UID,
APIVersion: config.APIVersion,
BlockOwnerDeletion: utils.PtrBool(true),
Controller: utils.PtrBool(true),
},
},
},
}
return &serviceAccount, nil
}
// GetClusterRoleBinding Create cluster role binding for K8sGPT
func GetClusterRoleBinding(config v1alpha1.K8sGPT) (*r1.ClusterRoleBinding, error) {
// Create cluster role binding
clusterRoleBinding := r1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "k8sgpt",
OwnerReferences: []metav1.OwnerReference{
{
Kind: config.Kind,
Name: config.Name,
UID: config.UID,
APIVersion: config.APIVersion,
BlockOwnerDeletion: utils.PtrBool(true),
Controller: utils.PtrBool(true),
},
},
},
Subjects: []r1.Subject{
{
Kind: "ServiceAccount",
Name: "k8sgpt",
Namespace: config.Namespace,
},
},
RoleRef: r1.RoleRef{
Kind: "ClusterRole",
Name: "k8sgpt",
APIGroup: "rbac.authorization.k8s.io",
},
}
return &clusterRoleBinding, nil
}
// GetClusterRole Create ClusterRole for K8sGPT with cluster read all
func GetClusterRole(config v1alpha1.K8sGPT) (*r1.ClusterRole, error) {
// Create cluster role
clusterRole := r1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: "k8sgpt",
OwnerReferences: []metav1.OwnerReference{
{
Kind: config.Kind,
Name: config.Name,
UID: config.UID,
APIVersion: config.APIVersion,
BlockOwnerDeletion: utils.PtrBool(true),
Controller: utils.PtrBool(true),
},
},
},
Rules: []r1.PolicyRule{
{
APIGroups: []string{"*"},
Resources: []string{"*"},
Verbs: []string{"list", "get", "watch"},
},
},
}
return &clusterRole, nil
}
// GetDeployment Create deployment with the latest K8sGPT image
func GetDeployment(config v1alpha1.K8sGPT) (*appsv1.Deployment, error) {
// Create deployment
replicas := int32(1)
deployment := appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: DeploymentName,
Namespace: config.Namespace,
OwnerReferences: []metav1.OwnerReference{
{
Kind: config.Kind,
Name: config.Name,
UID: config.UID,
APIVersion: config.APIVersion,
BlockOwnerDeletion: utils.PtrBool(true),
Controller: utils.PtrBool(true),
},
},
},
Spec: appsv1.DeploymentSpec{
Replicas: &replicas,
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app": DeploymentName,
},
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"app": DeploymentName,
},
},
Spec: corev1.PodSpec{
ServiceAccountName: "k8sgpt",
Containers: []corev1.Container{
{
Name: "k8sgpt",
ImagePullPolicy: corev1.PullAlways,
Image: "ghcr.io/k8sgpt-ai/k8sgpt:" + config.Spec.Version,
Args: []string{
"serve",
},
Env: []corev1.EnvVar{
{
Name: "K8SGPT_MODEL",
Value: config.Spec.AI.Model,
},
{
Name: "K8SGPT_BACKEND",
Value: config.Spec.AI.Backend,
},
{
Name: "XDG_CONFIG_HOME",
Value: "/k8sgpt-data/.config",
},
{
Name: "XDG_CACHE_HOME",
Value: "/k8sgpt-data/.cache",
},
},
Ports: []corev1.ContainerPort{
{
ContainerPort: 8080,
},
},
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("1"),
corev1.ResourceMemory: resource.MustParse("512Mi"),
},
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("0.2"),
corev1.ResourceMemory: resource.MustParse("156Mi"),
},
},
VolumeMounts: []corev1.VolumeMount{
{
MountPath: "/k8sgpt-data",
Name: "k8sgpt-vol",
},
},
},
},
Volumes: []corev1.Volume{
{
VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}},
Name: "k8sgpt-vol",
},
},
},
},
},
}
if config.Spec.AI.Secret != nil {
password := corev1.EnvVar{
Name: "K8SGPT_PASSWORD",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: config.Spec.AI.Secret.Name,
},
Key: config.Spec.AI.Secret.Key,
},
},
}
deployment.Spec.Template.Spec.Containers[0].Env = append(
deployment.Spec.Template.Spec.Containers[0].Env, password,
)
}
if config.Spec.RemoteCache != nil {
// check to see if key/value exists
addRemoteCacheEnvVar := func(name, key string) {
envVar := v1.EnvVar{
Name: name,
ValueFrom: &v1.EnvVarSource{
SecretKeyRef: &v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{
Name: config.Spec.RemoteCache.Credentials.Name,
},
Key: key,
},
},
}
deployment.Spec.Template.Spec.Containers[0].Env = append(
deployment.Spec.Template.Spec.Containers[0].Env, envVar,
)
}
addRemoteCacheEnvVar("AWS_ACCESS_KEY_ID", "aws_access_key_id")
addRemoteCacheEnvVar("AWS_SECRET_ACCESS_KEY", "aws_secret_access_key")
}
if config.Spec.AI.BaseUrl != "" {
baseUrl := corev1.EnvVar{
Name: "K8SGPT_BASEURL",
Value: config.Spec.AI.BaseUrl,
}
deployment.Spec.Template.Spec.Containers[0].Env = append(
deployment.Spec.Template.Spec.Containers[0].Env, baseUrl,
)
}
// Engine is required only when azureopenai is the ai backend
if config.Spec.AI.Engine != "" && config.Spec.AI.Backend == v1alpha1.AzureOpenAI {
engine := corev1.EnvVar{
Name: "K8SGPT_ENGINE",
Value: config.Spec.AI.Engine,
}
deployment.Spec.Template.Spec.Containers[0].Env = append(
deployment.Spec.Template.Spec.Containers[0].Env, engine,
)
} else if config.Spec.AI.Engine != "" && config.Spec.AI.Backend != v1alpha1.AzureOpenAI {
return &appsv1.Deployment{}, err.New("Engine is supported only by azureopenai provider.")
}
return &deployment, nil
}
func Sync(ctx context.Context, c client.Client,
config v1alpha1.K8sGPT, i SyncOrDestroy) error {
var objs []client.Object
svc, er := GetService(config)
if er != nil {
return er
}
objs = append(objs, svc)
svcAcc, er := GetServiceAccount(config)
if er != nil {
return er
}
objs = append(objs, svcAcc)
clusterRole, er := GetClusterRole(config)
if er != nil {
return er
}
objs = append(objs, clusterRole)
clusterRoleBinding, er := GetClusterRoleBinding(config)
if er != nil {
return er
}
objs = append(objs, clusterRoleBinding)
deployment, er := GetDeployment(config)
if er != nil {
return er
}
objs = append(objs, deployment)
// for each object, create or destroy
for _, obj := range objs {
switch i {
case SyncOp:
// before creation, we will check to see if the secret exists if used as a ref
if config.Spec.AI.Secret != nil {
secret := &corev1.Secret{}
er := c.Get(ctx, types.NamespacedName{Name: config.Spec.AI.Secret.Name,
Namespace: config.Namespace}, secret)
if er != nil {
return err.New("references secret does not exist, cannot create deployment")
}
}
err := doSync(ctx, c, obj)
if err != nil {
// If the object already exists, ignore the error
if !errors.IsAlreadyExists(err) {
return err
}
}
case DestroyOp:
err := c.Delete(ctx, obj)
if err != nil {
// if the object is not found, ignore the error
if !errors.IsNotFound(err) {
return err
}
}
}
}
return nil
}
func doSync(ctx context.Context, clt client.Client, obj client.Object) error {
var mutateFn controllerutil.MutateFn
switch expect := obj.(type) {
case *appsv1.Deployment:
exist := &appsv1.Deployment{}
err := clt.Get(context.Background(), client.ObjectKeyFromObject(obj), exist)
if err != nil && !errors.IsNotFound(err) {
return err
} else if err == nil {
mutateFn = func() error {
exist.Spec = expect.Spec
return nil
}
obj = exist
}
case *corev1.Service:
exist := &corev1.Service{}
err := clt.Get(context.Background(), client.ObjectKeyFromObject(obj), exist)
if err != nil && !errors.IsNotFound(err) {
return err
} else if err == nil {
mutateFn = func() error {
exist.Spec = expect.Spec
return nil
}
obj = exist
}
}
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
_, err := controllerutil.CreateOrPatch(ctx, clt, obj, mutateFn)
return err
})
}