Skip to content

Commit

Permalink
feat: add additional printer columns to Result CR (#114)
Browse files Browse the repository at this point in the history
* add additional printer columns

Signed-off-by: Tyler Gillson <tyler.gillson@gmail.com>

* add localai k8sgpt sample

Signed-off-by: Tyler Gillson <tyler.gillson@gmail.com>

* always inherit backend from k8sgpt config

Signed-off-by: Tyler Gillson <tyler.gillson@gmail.com>

* remove 'secret: null' from localai example

Signed-off-by: Tyler Gillson <tyler.gillson@gmail.com>

---------

Signed-off-by: Tyler Gillson <tyler.gillson@gmail.com>
  • Loading branch information
TylerGillson committed May 23, 2023
1 parent b6d5cf8 commit 778357d
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 26 deletions.
10 changes: 9 additions & 1 deletion api/v1alpha1/k8sgpt_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type SecretRef struct {
type K8sGPTSpec struct {
// +kubebuilder:default:=openai
// +kubebuilder:validation:Enum=openai;localai;azureopenai
Backend string `json:"backend,omitempty"`
Backend `json:"backend"`
BaseUrl string `json:"baseUrl,omitempty"`
// +kubebuilder:default:=gpt-3.5-turbo
Model string `json:"model,omitempty"`
Expand All @@ -44,6 +44,14 @@ type K8sGPTSpec struct {
Filters []string `json:"filters,omitempty"`
}

type Backend string

const (
OpenAI Backend = "openai"
AzureOpenAI Backend = "azureopenai"
LocalAI Backend = "localai"
)

// K8sGPTStatus defines the observed state of K8sGPT
type K8sGPTStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
Expand Down
30 changes: 14 additions & 16 deletions api/v1alpha1/k8sgpt_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,48 +34,46 @@ var _ = Describe("The test cases for the K8sGPT CRDs", func() {
Key: "k8s-gpt",
}

Kind = "K8sGPT"
Backend = "gpt"
Backend2 = "gpt-2"
BaseUrl = "https://api.k8s-gpt.localhost"
Model = "345M"
Version = "v1alpha1"
kind = "K8sGPT"
baseUrl = "https://api.k8s-gpt.localhost"
model = "345M"
version = "v1alpha1"

Namespace = "k8sGPT"

k8sGPT = K8sGPT{
TypeMeta: metav1.TypeMeta{
Kind: Kind,
Kind: kind,
},
ObjectMeta: metav1.ObjectMeta{
Name: "k8s-gpt",
Namespace: Namespace,
},
Spec: K8sGPTSpec{
Backend: Backend,
BaseUrl: BaseUrl,
Model: Model,
Backend: OpenAI,
BaseUrl: baseUrl,
Model: model,
Secret: &secretRef,
Version: Version,
Version: version,
EnableAI: true,
NoCache: true,
},
}

k8sGPT2 = K8sGPT{
TypeMeta: metav1.TypeMeta{
Kind: Kind,
Kind: kind,
},
ObjectMeta: metav1.ObjectMeta{
Name: "k8s-gpt-2",
Namespace: Namespace,
},
Spec: K8sGPTSpec{
Backend: Backend2,
BaseUrl: BaseUrl,
Model: Model,
Backend: AzureOpenAI,
BaseUrl: baseUrl,
Model: model,
Secret: &secretRef,
Version: Version,
Version: version,
EnableAI: false,
NoCache: false,
},
Expand Down
5 changes: 3 additions & 2 deletions api/v1alpha1/result_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ type Sensitive struct {

// ResultSpec defines the desired state of Result
type ResultSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file
Backend `json:"backend"`
Kind string `json:"kind"`
Name string `json:"name"`
Error []Failure `json:"error"`
Expand All @@ -49,6 +48,8 @@ type ResultStatus struct {

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:printcolumn:name="Kind",type="string",JSONPath=".spec.kind",description="Kind"
//+kubebuilder:printcolumn:name="Backend",type="string",JSONPath=".spec.backend",description="Backend"

// Result is the Schema for the results API
type Result struct {
Expand Down
17 changes: 17 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions config/crd/bases/core.k8sgpt.ai_k8sgpts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ spec:
type: object
version:
type: string
required:
- backend
type: object
status:
description: K8sGPTStatus defines the observed state of K8sGPT
Expand Down
16 changes: 13 additions & 3 deletions config/crd/bases/core.k8sgpt.ai_results.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@ spec:
singular: result
scope: Namespaced
versions:
- name: v1alpha1
- additionalPrinterColumns:
- description: Kind
jsonPath: .spec.kind
name: Kind
type: string
- description: Backend
jsonPath: .spec.backend
name: Backend
type: string
name: v1alpha1
schema:
openAPIV3Schema:
description: Result is the Schema for the results API
Expand All @@ -35,6 +44,8 @@ spec:
spec:
description: ResultSpec defines the desired state of Result
properties:
backend:
type: string
details:
type: string
error:
Expand All @@ -54,14 +65,13 @@ spec:
type: object
type: array
kind:
description: 'INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
Important: Run "make" to regenerate code after modifying this file'
type: string
name:
type: string
parentObject:
type: string
required:
- backend
- details
- error
- kind
Expand Down
12 changes: 12 additions & 0 deletions config/samples/k8sgpt_localai.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: core.k8sgpt.ai/v1alpha1
kind: K8sGPT
metadata:
name: k8sgpt-sample-localai
namespace: k8sgpt-operator-system
spec:
model: ggml-gpt4all-j-v1.3-groovy.bin
backend: localai
baseUrl: http://local-ai.local-ai.svc.cluster.local:8080/v1
noCache: false
enableAI: true
version: v0.3.0
1 change: 1 addition & 0 deletions controllers/k8sgpt_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
k8sgptNumberOfResults.Set(float64(len(response.Results)))
rawResults := make(map[string]corev1alpha1.Result)
for _, resultSpec := range response.Results {
resultSpec.Backend = k8sgptConfig.Spec.Backend
name := strings.ReplaceAll(resultSpec.Name, "-", "")
name = strings.ReplaceAll(name, "/", "")
result := corev1alpha1.Result{
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (c *Client) ProcessAnalysis(deployment v1.Deployment, config *v1alpha1.K8sG
req := &schemav1.AnalyzeRequest{
Explain: config.Spec.EnableAI,
Nocache: config.Spec.NoCache,
Backend: config.Spec.Backend,
Backend: string(config.Spec.Backend),
Filters: config.Spec.Filters,
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/resources/k8sgpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func GetDeployment(config v1alpha1.K8sGPT) (*appsv1.Deployment, error) {
},
{
Name: "K8SGPT_BACKEND",
Value: config.Spec.Backend,
Value: string(config.Spec.Backend),
},
},
Ports: []v1.ContainerPort{
Expand Down Expand Up @@ -235,15 +235,15 @@ func GetDeployment(config v1alpha1.K8sGPT) (*appsv1.Deployment, error) {
)
}
// Engine is required only when azureopenai is the ai backend
if config.Spec.Engine != "" && config.Spec.Backend == "azureopenai" {
if config.Spec.Engine != "" && config.Spec.Backend == v1alpha1.AzureOpenAI {
engine := v1.EnvVar{
Name: "K8SGPT_ENGINE",
Value: config.Spec.Engine,
}
deployment.Spec.Template.Spec.Containers[0].Env = append(
deployment.Spec.Template.Spec.Containers[0].Env, engine,
)
} else if config.Spec.Engine != "" && config.Spec.Backend != "azureopenai" {
} else if config.Spec.Engine != "" && config.Spec.Backend != v1alpha1.AzureOpenAI {
return &appsv1.Deployment{}, err.New("Engine is supported only by azureopenai provider.")
}
return &deployment, nil
Expand Down

0 comments on commit 778357d

Please sign in to comment.