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: remote config rework #201

Merged
merged 5 commits into from
Aug 28, 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
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,47 @@ you will be able to see the Results objects of the analysis after some minutes (
"details": "The error message means that the service in Kubernetes doesn't have any associated endpoints, which should have been labeled with \"control-plane=controller-manager\". \n\nTo solve this issue, you need to add the \"control-plane=controller-manager\" label to the endpoint that matches the service. Once the endpoint is labeled correctly, Kubernetes can associate it with the service, and the error should be resolved.",
```

## Remote Cache

<details>

<summary>S3</summary>

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

2. Create secret:
```sh
kubectl create secret generic k8sgpt-sample-cache-secret --from-literal=aws_access_key_id=<AWS_ACCESS_KEY_ID> --from-literal=aws_secret_access_key=<AWS_SECRET_ACCESS_KEY> -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
bucketName: foo
region: us-west-1
EOF
```

</details>

## Other AI Backend Examples

<details>
Expand Down
11 changes: 11 additions & 0 deletions api/v1alpha1/k8sgpt_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ type ExtraOptionsRef struct {
Backstage *Backstage `json:"backstage,omitempty"`
}

type CredentialsRef struct {
Name string `json:"name,omitempty"`
}

type RemoteCacheRef struct {
Credentials *CredentialsRef `json:"credentials,omitempty"`
BucketName string `json:"bucketName,omitempty"`
Region string `json:"region,omitempty"`
}

type WebhookRef struct {
// +kubebuilder:validation:Enum=slack
Type string `json:"type,omitempty"`
Expand Down Expand Up @@ -66,6 +76,7 @@ type K8sGPTSpec struct {
ExtraOptions *ExtraOptionsRef `json:"extraOptions,omitempty"`
Sink *WebhookRef `json:"sink,omitempty"`
AI *AISpec `json:"ai,omitempty"`
RemoteCache *RemoteCacheRef `json:"remoteCache,omitempty"`
}

const (
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.

12 changes: 12 additions & 0 deletions config/crd/bases/core.k8sgpt.ai_k8sgpts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ spec:
type: array
noCache:
type: boolean
remoteCache:
properties:
bucketName:
type: string
credentials:
properties:
name:
type: string
type: object
region:
type: string
type: object
sink:
properties:
type:
Expand Down
7 changes: 6 additions & 1 deletion config/samples/core_v1alpha1_k8sgpt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ spec:
name: k8sgpt-sample-secret
key: openai-api-key
noCache: false
version: v0.3.0
version: v0.3.5
remoteCache:
credentials:
name: k8sgpt-sample-cache-secret
bucketName: k8sgpt-debug-test
region: eu-west-2
9 changes: 9 additions & 0 deletions controllers/k8sgpt_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr

defer k8sgptClient.Close()

// Configure the k8sgpt deployment if required
if k8sgptConfig.Spec.RemoteCache != nil {
err = k8sgptClient.AddConfig(k8sgptConfig)
if err != nil {
k8sgptReconcileErrorCount.Inc()
return r.finishReconcile(err, false)
}
}

response, err := k8sgptClient.ProcessAnalysis(deployment, k8sgptConfig)
if err != nil {
k8sgptReconcileErrorCount.Inc()
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.19

require (
buf.build/gen/go/k8sgpt-ai/k8sgpt/grpc/go v1.3.0-20230620082254-6f80f9533908.1
buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go v1.30.0-20230620082254-6f80f9533908.1
buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go v1.31.0-20230620082254-6f80f9533908.1
github.com/onsi/ginkgo/v2 v2.12.0
github.com/onsi/gomega v1.27.10
github.com/prometheus/client_golang v1.16.0
Expand Down Expand Up @@ -91,7 +91,7 @@ require (
gomodules.xyz/jsonpatch/v2 v2.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
google.golang.org/protobuf v1.30.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
buf.build/gen/go/k8sgpt-ai/k8sgpt/grpc/go v1.3.0-20230620082254-6f80f9533908.1 h1:Z0zeGzAumjLyAb/24aiBNyAheT+SDBhlxPfcUy12nPI=
buf.build/gen/go/k8sgpt-ai/k8sgpt/grpc/go v1.3.0-20230620082254-6f80f9533908.1/go.mod h1:ydXSuYyk0CN76EA+cjFemhpz87XtuU310GdmkmXUUY8=
buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go v1.28.1-20230620082254-6f80f9533908.4/go.mod h1:i/s4ALHwKvjA1oGNKpoHg0FpEOTbufoOm/NdTE6YQAE=
buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go v1.30.0-20230620082254-6f80f9533908.1 h1:FNJYUdFjROTTKhIQ+VtJCzuWywQU430leJfnkStRRic=
buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go v1.30.0-20230620082254-6f80f9533908.1/go.mod h1:karV92RruD5davytOQq20lDyAqBnai8ajNolo98nu94=
buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go v1.31.0-20230620082254-6f80f9533908.1 h1:FCNAoc1SnZQ0nUgWgV75izPiLGl3Q1X6pxWjL/wB6cI=
buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go v1.31.0-20230620082254-6f80f9533908.1/go.mod h1:gtnk2yAUexdY5nTuUg0SH5WCCGvpKzr7pd3Xbi7MWjE=
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU=
Expand Down Expand Up @@ -1259,8 +1259,8 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
Expand Down
12 changes: 11 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package main
import (
"context"
"flag"
"fmt"
"math/rand"
"os"
"time"

Expand Down Expand Up @@ -64,7 +66,15 @@ func main() {
flag.Parse()

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))

if os.Getenv("LOCAL_MODE") != "" {
setupLog.Info("Running in local mode")
min := 7000
max := 8000
metricsAddr = fmt.Sprintf(":%d", rand.Intn(max-min+1)+min)
probeAddr = fmt.Sprintf(":%d", rand.Intn(max-min+1)+min)
setupLog.Info(fmt.Sprintf("Metrics address: %s", metricsAddr))
setupLog.Info(fmt.Sprintf("Probe address: %s", probeAddr))
}
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Expand Down
50 changes: 50 additions & 0 deletions pkg/client/analysis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package client

import (
"context"
"encoding/json"
"fmt"

rpc "buf.build/gen/go/k8sgpt-ai/k8sgpt/grpc/go/schema/v1/schemav1grpc"
schemav1 "buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go/schema/v1"
"github.com/k8sgpt-ai/k8sgpt-operator/api/v1alpha1"
"github.com/k8sgpt-ai/k8sgpt-operator/pkg/common"
v1 "k8s.io/api/apps/v1"
)

func (c *Client) ProcessAnalysis(deployment v1.Deployment, config *v1alpha1.K8sGPT) (*common.K8sGPTReponse, error) {

client := rpc.NewServerServiceClient(c.conn)
req := &schemav1.AnalyzeRequest{
Explain: config.Spec.AI.Enabled,
Nocache: config.Spec.NoCache,
Backend: config.Spec.AI.Backend,
Filters: config.Spec.Filters,
Anonymize: config.Spec.AI.Anonymize,
Language: config.Spec.AI.Language,
}

res, err := client.Analyze(context.Background(), req)
if err != nil {
return nil, fmt.Errorf("failed to call Analyze RPC: %v", err)
}

var target []v1alpha1.ResultSpec

jsonBytes, err := json.Marshal(res.Results)
if err != nil {
return nil, err
}

err = json.Unmarshal(jsonBytes, &target)
if err != nil {
return nil, err
}

response := &common.K8sGPTReponse{
Status: res.Status,
Results: target,
Problems: int(res.Problems),
}
return response, nil
}
42 changes: 0 additions & 42 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,13 @@ package client

import (
"context"
"encoding/json"
"fmt"
"net"
"os"
"time"

rpc "buf.build/gen/go/k8sgpt-ai/k8sgpt/grpc/go/schema/v1/schemav1grpc"
schemav1 "buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go/schema/v1"
"github.com/k8sgpt-ai/k8sgpt-operator/api/v1alpha1"
"github.com/k8sgpt-ai/k8sgpt-operator/pkg/common"
"google.golang.org/grpc"
v1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand Down Expand Up @@ -79,40 +74,3 @@ func GenerateAddress(ctx context.Context, cli client.Client, k8sgptConfig *v1alp

return address, nil
}

func (c *Client) ProcessAnalysis(deployment v1.Deployment, config *v1alpha1.K8sGPT) (*common.K8sGPTReponse, error) {

client := rpc.NewServerServiceClient(c.conn)
req := &schemav1.AnalyzeRequest{
Explain: config.Spec.AI.Enabled,
Nocache: config.Spec.NoCache,
Backend: config.Spec.AI.Backend,
Filters: config.Spec.Filters,
Anonymize: config.Spec.AI.Anonymize,
Language: config.Spec.AI.Language,
}

res, err := client.Analyze(context.Background(), req)
if err != nil {
return nil, fmt.Errorf("failed to call Analyze RPC: %v", err)
}

var target []v1alpha1.ResultSpec

jsonBytes, err := json.Marshal(res.Results)
if err != nil {
return nil, err
}

err = json.Unmarshal(jsonBytes, &target)
if err != nil {
return nil, err
}

response := &common.K8sGPTReponse{
Status: res.Status,
Results: target,
Problems: int(res.Problems),
}
return response, nil
}
Loading