From 7e7769c4cc7ecdcbd74c5f2f58b6b85fe00b04b0 Mon Sep 17 00:00:00 2001 From: Tanuj Dwivedi Date: Sat, 7 Sep 2024 23:15:18 +0530 Subject: [PATCH] feat: add proxysettings for azureopenai and openai (#415) * feat: add proxysettings for azureopenai and openai Signed-off-by: tanujd11 * update readme Signed-off-by: tanujd11 --------- Signed-off-by: tanujd11 --- README.md | 1 + api/v1alpha1/k8sgpt_types.go | 3 +- chart/operator/templates/k8sgpt-crd.yaml | 2 ++ config/crd/bases/core.k8sgpt.ai_k8sgpts.yaml | 2 ++ pkg/resources/k8sgpt.go | 34 ++++++++++++++------ 5 files changed, 31 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index db968586..25ef8ea7 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ spec: key: openai-api-key # anonymized: false # language: english + # proxyEndpoint: https://10.255.30.150 # use proxyEndpoint to setup backend through an HTTP/HTTPS proxy noCache: false repository: ghcr.io/k8sgpt-ai/k8sgpt version: v0.3.8 diff --git a/api/v1alpha1/k8sgpt_types.go b/api/v1alpha1/k8sgpt_types.go index 287fd8ed..a7d09412 100644 --- a/api/v1alpha1/k8sgpt_types.go +++ b/api/v1alpha1/k8sgpt_types.go @@ -105,7 +105,8 @@ type AISpec struct { // +kubebuilder:default:=true Anonymize *bool `json:"anonymized,omitempty"` // +kubebuilder:default:=english - Language string `json:"language,omitempty"` + Language string `json:"language,omitempty"` + ProxyEndpoint string `json:"proxyEndpoint,omitempty"` } type Trivy struct { diff --git a/chart/operator/templates/k8sgpt-crd.yaml b/chart/operator/templates/k8sgpt-crd.yaml index de2e36c0..60c0fd39 100644 --- a/chart/operator/templates/k8sgpt-crd.yaml +++ b/chart/operator/templates/k8sgpt-crd.yaml @@ -77,6 +77,8 @@ spec: model: default: gpt-3.5-turbo type: string + proxyEndpoint: + type: string region: type: string secret: diff --git a/config/crd/bases/core.k8sgpt.ai_k8sgpts.yaml b/config/crd/bases/core.k8sgpt.ai_k8sgpts.yaml index f8244b19..ff438024 100644 --- a/config/crd/bases/core.k8sgpt.ai_k8sgpts.yaml +++ b/config/crd/bases/core.k8sgpt.ai_k8sgpts.yaml @@ -80,6 +80,8 @@ spec: model: default: gpt-3.5-turbo type: string + proxyEndpoint: + type: string region: type: string secret: diff --git a/pkg/resources/k8sgpt.go b/pkg/resources/k8sgpt.go index 89cabf2c..1c02d003 100644 --- a/pkg/resources/k8sgpt.go +++ b/pkg/resources/k8sgpt.go @@ -23,7 +23,6 @@ import ( "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" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -51,11 +50,11 @@ func addSecretAsEnvToDeployment(secretName string, secretKey string, if er != nil { return err.New("secret does not exist, cannot add to env of deployment") } - envVar := v1.EnvVar{ + envVar := corev1.EnvVar{ Name: secretKey, - ValueFrom: &v1.EnvVarSource{ - SecretKeyRef: &v1.SecretKeySelector{ - LocalObjectReference: v1.LocalObjectReference{ + ValueFrom: &corev1.EnvVarSource{ + SecretKeyRef: &corev1.SecretKeySelector{ + LocalObjectReference: corev1.LocalObjectReference{ Name: secretName, }, Key: secretKey, @@ -213,7 +212,7 @@ func GetDeployment(config v1alpha1.K8sGPT, outOfClusterMode bool, c client.Clien }) deployment.Spec.Template.Spec.Volumes = append(deployment.Spec.Template.Spec.Volumes, corev1.Volume{ Name: "kubeconfig", - VolumeSource: v1.VolumeSource{ + VolumeSource: corev1.VolumeSource{ Secret: &corev1.SecretVolumeSource{ SecretName: config.Spec.Kubeconfig.Name, Items: []corev1.KeyToPath{ @@ -247,11 +246,11 @@ func GetDeployment(config v1alpha1.K8sGPT, outOfClusterMode bool, c client.Clien // check to see if key/value exists addRemoteCacheEnvVar := func(name, key string) { - envVar := v1.EnvVar{ + envVar := corev1.EnvVar{ Name: name, - ValueFrom: &v1.EnvVarSource{ - SecretKeyRef: &v1.SecretKeySelector{ - LocalObjectReference: v1.LocalObjectReference{ + ValueFrom: &corev1.EnvVarSource{ + SecretKeyRef: &corev1.SecretKeySelector{ + LocalObjectReference: corev1.LocalObjectReference{ Name: config.Spec.RemoteCache.Credentials.Name, }, Key: key, @@ -293,6 +292,21 @@ func GetDeployment(config v1alpha1.K8sGPT, outOfClusterMode bool, c client.Clien } else if config.Spec.AI.Engine != "" && config.Spec.AI.Backend != v1alpha1.AzureOpenAI { return &appsv1.Deployment{}, err.New("engine is supported only by azureopenai provider") } + + // ProxyEndpoint is required only when azureopenai or openai is the ai backend + if config.Spec.AI.ProxyEndpoint != "" && (config.Spec.AI.Backend == v1alpha1.AzureOpenAI || config.Spec.AI.Backend == v1alpha1.OpenAI) { + proxyEndpoint := corev1.EnvVar{ + Name: "K8SGPT_PROXY_ENDPOINT", + Value: config.Spec.AI.ProxyEndpoint, + } + deployment.Spec.Template.Spec.Containers[0].Env = append( + deployment.Spec.Template.Spec.Containers[0].Env, proxyEndpoint, + ) + } else if config.Spec.AI.ProxyEndpoint != "" && config.Spec.AI.Backend != v1alpha1.AzureOpenAI && config.Spec.AI.Backend != v1alpha1.OpenAI { + return &appsv1.Deployment{}, err.New("proxyEndpoint is supported only by azureopenai and openai provider") + + } + // Add checks for amazonbedrock if config.Spec.AI.Backend == v1alpha1.AmazonBedrock { if config.Spec.AI.Secret != nil {