Skip to content

Commit

Permalink
chore: updated schema for integrations support (#616)
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

wip: enabling integration activation

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

wip: enabling integration activation

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>
  • Loading branch information
AlexsJones committed Aug 30, 2023
1 parent 5e17e66 commit 4d7a937
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
__debug*
.DS_Store
k8sgpt*
!charts/k8sgpt
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ require github.com/adrg/xdg v0.4.0

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.31.0-20230620082254-6f80f9533908.1
buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go v1.31.0-20230830164712-dc062a152c20.1
github.com/aws/aws-sdk-go v1.44.329
github.com/cohere-ai/cohere-go v0.2.0
)

require (
buf.build/gen/go/k8sgpt-ai/k8sgpt/connectrpc/go v1.11.1-20230830164712-dc062a152c20.1 // indirect
github.com/anchore/go-struct-converter v0.0.0-20221118182256-c68fdcfa2092 // indirect
github.com/cohere-ai/tokenizer v1.1.1 // indirect
github.com/dlclark/regexp2 v1.4.0 // indirect
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
buf.build/gen/go/k8sgpt-ai/k8sgpt/connectrpc/go v1.11.1-20230830164712-dc062a152c20.1 h1:Q936ek1+X3/Ud3o1ajG1eL52W5nsTZFYErLWiUt01A4=
buf.build/gen/go/k8sgpt-ai/k8sgpt/connectrpc/go v1.11.1-20230830164712-dc062a152c20.1/go.mod h1:hlKBEoYi1fvrRhi75lnznnXKH9VVBf9u6bY/lkj58oA=
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.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=
buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go v1.31.0-20230828112343-a9fd9ad20848.1 h1:fScSW5Gzu1OzUYylwpvQwgXX5J9YPKkOQpbkc5c8Q+8=
buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go v1.31.0-20230828112343-a9fd9ad20848.1/go.mod h1:gtnk2yAUexdY5nTuUg0SH5WCCGvpKzr7pd3Xbi7MWjE=
buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go v1.31.0-20230830164712-dc062a152c20.1 h1:oD5YCdsVnQgVLSHxium66FFfuxjjvn5u65mnQo6vAyc=
buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go v1.31.0-20230830164712-dc062a152c20.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
17 changes: 7 additions & 10 deletions pkg/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ package integration

import (
"errors"
"os"
"fmt"
"strings"

"github.com/fatih/color"
"github.com/k8sgpt-ai/k8sgpt/pkg/common"
"github.com/k8sgpt-ai/k8sgpt/pkg/integration/trivy"
"github.com/k8sgpt-ai/k8sgpt/pkg/util"
Expand Down Expand Up @@ -81,8 +80,7 @@ func (*Integration) Activate(name string, namespace string, activeFilters []stri

// Verify dupplicate
if len(dupplicatedFilters) != 0 {
color.Red("Integration already activated : %s", strings.Join(dupplicatedFilters, ", "))
os.Exit(1)
return fmt.Errorf("integration already activated : %s", strings.Join(dupplicatedFilters, ", "))
}

viper.Set("active_filters", uniqueFilters)
Expand All @@ -94,8 +92,8 @@ func (*Integration) Activate(name string, namespace string, activeFilters []stri
}

if err := viper.WriteConfig(); err != nil {
color.Red("Error writing config file: %s", err.Error())
os.Exit(1)
return fmt.Errorf("error writing config file: %s", err.Error())

}

return nil
Expand Down Expand Up @@ -123,8 +121,7 @@ func (*Integration) Deactivate(name string, namespace string) error {

}
if !foundFilter {
color.Red("Ingregation %s does not exist in configuration file. Please use k8sgpt integration add.", name)
os.Exit(1)
return fmt.Errorf("integration %s does not exist in configuration file. Please use k8sgpt integration add", name)
}

if err := integrations[name].UnDeploy(namespace); err != nil {
Expand All @@ -134,8 +131,8 @@ func (*Integration) Deactivate(name string, namespace string) error {
viper.Set("active_filters", activeFilters)

if err := viper.WriteConfig(); err != nil {
color.Red("Error writing config file: %s", err.Error())
os.Exit(1)
return fmt.Errorf("error writing config file: %s", err.Error())

}

return nil
Expand Down
17 changes: 17 additions & 0 deletions pkg/server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# serve

The serve commands allow you to run k8sgpt in a grpc server mode.
This would be enabled typically through `k8sgpt serve` and is how the in-cluster k8sgpt deployment functions when managed by the [k8sgpt-operator](https://github.com/k8sgpt-ai/k8sgpt-operator)

The grpc interface that is served is hosted on [buf](https://buf.build/k8sgpt-ai/schemas) and the repository for this is [here](https://github.com/k8sgpt-ai/schemas)

## grpcurl

A fantastic tool for local debugging and development is `grpcurl`
It allows you to form curl like requests that are http2
e.g.

```
grpcurl -plaintext -d '{"namespace": "k8sgpt", "explain" : "true"}' localhost:8080 schema.v1.ServerService/Analyze
```

31 changes: 31 additions & 0 deletions pkg/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,42 @@ import (
"errors"

schemav1 "buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go/schema/v1"
"github.com/k8sgpt-ai/k8sgpt/pkg/analyzer"
"github.com/k8sgpt-ai/k8sgpt/pkg/cache"
"github.com/k8sgpt-ai/k8sgpt/pkg/integration"
"github.com/spf13/viper"
)

func (h *handler) AddConfig(ctx context.Context, i *schemav1.AddConfigRequest) (*schemav1.AddConfigResponse, error,
) {

if i.Integrations != nil {
coreFilters, _, _ := analyzer.ListFilters()
// Update filters
activeFilters := viper.GetStringSlice("active_filters")
if len(activeFilters) == 0 {
activeFilters = coreFilters
}
integration := integration.NewIntegration()

if i.Integrations.Trivy != nil {
// Enable/Disable Trivy
var err error
var IntegrationResponse string = ""

err = integration.Activate("trivy", i.Integrations.Trivy.Namespace,
activeFilters, i.Integrations.Trivy.Enabled)
if err != nil {
IntegrationResponse = "Error enabling integration"
} else {
IntegrationResponse = "Integration enabled"
}
return &schemav1.AddConfigResponse{
Status: IntegrationResponse,
}, err
}
}
// Remote cache
if i.Cache.BucketName == "" || i.Cache.Region == "" {
return nil, errors.New("BucketName & Region are required")
}
Expand Down

0 comments on commit 4d7a937

Please sign in to comment.