Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add azure remote cache #246

Merged
merged 3 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,48 @@ you will be able to see the Results objects of the analysis after some minutes (

## Remote Cache

<details>

<summary>Azure Blob storage</summary>

1. Install the operator from the [Installation](#installation) section.

2. Create secret:
```sh
kubectl create secret generic k8sgpt-sample-cache-secret --from-literal=azure_client_id=<AZURE_CLIENT_ID> --from-literal=azure_tenant_id=<AZURE_TENANT_ID> --from-literal=azure_client_secret=<AZURE_CLIENT_SECRET> -n k8sgpt-
operator-system
```

3. Apply the K8sGPT configuration object:
```
kubectl apply -f - << EOF
apiVersion: core.k8sgpt.ai/v1alpha1
kind: K8sGPT
metadata:
name: k8sgpt-sample
namespace: k8sgpt-operator-system
spec:
model: gpt-3.5-turbo
backend: openai
noCache: false
version: v0.3.0
enableAI: true
secret:
name: k8sgpt-sample-secret
key: openai-api-key
remoteCache:
credentials:
name: k8sgpt-sample-cache-secret
azure:
# Storage account must already exist
storageAccount: "account_name"
containerName: "container_name"
EOF
```

</details>


<details>

<summary>S3</summary>
Expand Down Expand Up @@ -111,9 +153,10 @@ spec:
key: openai-api-key
remoteCache:
credentials:
name: k8sgpt-sample-cache-secret
bucketName: foo
region: us-west-1
name: k8sgpt-sample-cache-secret
s3:
bucketName: foo
region: us-west-1
EOF
```

Expand Down
14 changes: 12 additions & 2 deletions api/v1alpha1/k8sgpt_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,18 @@ type CredentialsRef struct {

type RemoteCacheRef struct {
Credentials *CredentialsRef `json:"credentials,omitempty"`
BucketName string `json:"bucketName,omitempty"`
Region string `json:"region,omitempty"`
S3 *S3Backend `json:"s3,omitempty"`
Azure *AzureBackend `json:"azure,omitempty"`
}

type S3Backend struct {
BucketName string `json:"bucketName,omitempty"`
Region string `json:"region,omitempty"`
}

type AzureBackend struct {
StorageAccount string `json:"storageAccount,omitempty"`
ContainerName string `json:"containerName,omitempty"`
}

type WebhookRef struct {
Expand Down
40 changes: 40 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.

22 changes: 22 additions & 0 deletions chart/operator/templates/k8sgpt-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,28 @@ spec:
type: array
noCache:
type: boolean
remoteCache:
properties:
azure:
properties:
containerName:
type: string
storageAccount:
type: string
type: object
credentials:
properties:
name:
type: string
type: object
s3:
properties:
bucketName:
type: string
region:
type: string
type: object
type: object
sink:
properties:
type:
Expand Down
18 changes: 14 additions & 4 deletions config/crd/bases/core.k8sgpt.ai_k8sgpts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,25 @@ spec:
type: boolean
remoteCache:
properties:
bucketName:
type: string
azure:
properties:
containerName:
type: string
storageAccount:
type: string
type: object
credentials:
properties:
name:
type: string
type: object
region:
type: string
s3:
properties:
bucketName:
type: string
region:
type: string
type: object
type: object
sink:
properties:
Expand Down
24 changes: 14 additions & 10 deletions pkg/client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@ import (

func (c *Client) AddConfig(config *v1alpha1.K8sGPT) error {
client := rpc.NewServerServiceClient(c.conn)

req := &schemav1.AddConfigRequest{
Cache: &schemav1.Cache{
BucketName: config.Spec.RemoteCache.BucketName,
Region: config.Spec.RemoteCache.Region,
},
req := &schemav1.AddConfigRequest{}
// If multiple caches are configured we pick S3
// which emulates the behaviour of K8sGPT cli
if config.Spec.RemoteCache.S3 != nil {
req.Cache = &schemav1.Cache{
BucketName: config.Spec.RemoteCache.S3.BucketName,
Region: config.Spec.RemoteCache.S3.Region,
}
} else if config.Spec.RemoteCache.Azure != nil {
req.Cache = &schemav1.Cache{
StorageAccount: config.Spec.RemoteCache.Azure.StorageAccount,
ContainerName: config.Spec.RemoteCache.Azure.ContainerName,
}
}

_, err := client.AddConfig(context.Background(), req)
Expand All @@ -31,10 +38,7 @@ func (c *Client) RemoveConfig(config *v1alpha1.K8sGPT) error {
client := rpc.NewServerServiceClient(c.conn)

req := &schemav1.RemoveConfigRequest{
Cache: &schemav1.Cache{
BucketName: config.Spec.RemoteCache.BucketName,
Region: config.Spec.RemoteCache.Region,
},
Cache: &schemav1.Cache{},
}

_, err := client.RemoveConfig(context.Background(), req)
Expand Down
12 changes: 9 additions & 3 deletions pkg/resources/k8sgpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,16 @@ func GetDeployment(config v1alpha1.K8sGPT) (*appsv1.Deployment, error) {
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.RemoteCache.Azure != nil {
addRemoteCacheEnvVar("AZURE_CLIENT_ID", "azure_client_id")
addRemoteCacheEnvVar("AZURE_TENANT_ID", "azure_tenant_id")
addRemoteCacheEnvVar("AZURE_CLIENT_SECRET", "azure_client_secret")
} else if config.Spec.RemoteCache.S3 != nil {
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",
Expand Down