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: integration refactor #684

Merged
merged 8 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat: more significant refactor
Signed-off-by: Alex Jones <[email protected]>
  • Loading branch information
AlexsJones committed Sep 26, 2023
commit 33b3ab1e33f5992f9df0a86b97bec829b1bee130
8 changes: 8 additions & 0 deletions .idea/.gitignore

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

8 changes: 8 additions & 0 deletions .idea/modules.xml

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

6 changes: 6 additions & 0 deletions .idea/vcs.xml

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

2 changes: 2 additions & 0 deletions pkg/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type IIntegration interface {
AddAnalyzer(*map[string]common.IAnalyzer)

GetAnalyzerName() []string
// An integration must keep record of its deployed namespace (if not using --no-install)
GetNamespace() string

OwnsAnalyzer(string) bool

Expand Down
9 changes: 8 additions & 1 deletion pkg/integration/trivy/trivy.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ const (
)

type Trivy struct {
helm helmclient.Client
namespace string
helm helmclient.Client
}

func NewTrivy() *Trivy {
Expand All @@ -51,6 +52,10 @@ func (t *Trivy) GetAnalyzerName() []string {
}
}

func (t *Trivy) GetNamespace() string {
return t.namespace
}

func (t *Trivy) OwnsAnalyzer(analyzer string) bool {

for _, a := range t.GetAnalyzerName() {
Expand All @@ -62,6 +67,8 @@ func (t *Trivy) OwnsAnalyzer(analyzer string) bool {
}
func (t *Trivy) Deploy(namespace string) error {

// Store the namespace
t.namespace = namespace
// Add the repository
chartRepo := repo.Entry{
Name: RepoShortName,
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ grpcurl -plaintext -d '{"namespace": "k8sgpt", "explain" : "true"}' localhost:80
```

```
grpcurl -plaintext localhost:8080 schema.v1.ServerService/ListIntegrations
grpcurl -plaintext localhost:8080 schema.v1.ServerService/ListIntegrations
{
"integrations": [
"trivy"
Expand Down
28 changes: 8 additions & 20 deletions pkg/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,17 @@ 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 = integration.Activate("trivy", i.Integrations.Trivy.Namespace,
activeFilters, i.Integrations.Trivy.SkipInstall)
return &schemav1.AddConfigResponse{
Status: "",
}, err
}
resp, err := h.syncIntegration(ctx, i)
if err != nil {
return resp, err
}

if i.Cache != nil {
// Remote cache
if i.Cache.BucketName == "" || i.Cache.Region == "" {
Expand All @@ -52,13 +36,17 @@ func (h *handler) AddConfig(ctx context.Context, i *schemav1.AddConfigRequest) (

func (h *handler) RemoveConfig(ctx context.Context, i *schemav1.RemoveConfigRequest) (*schemav1.RemoveConfigResponse, error,
) {

err := cache.RemoveRemoteCache(i.Cache.BucketName)
if err != nil {
return &schemav1.RemoveConfigResponse{
Status: err.Error(),
}, err
}

// Remove any integrations is a TBD as it would be nice to make this more granular
// Currently integrations can be removed in the AddConfig sync

return &schemav1.RemoveConfigResponse{
Status: "Successfully removed the remote cache",
}, nil
Expand Down
80 changes: 80 additions & 0 deletions pkg/server/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,71 @@ package server

import (
"context"
"fmt"

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/integration"
"github.com/spf13/viper"
)

const (
trivyName = "trivy"
)

// syncIntegration is aware of the following events
// A new integration added
// An integration removed from the Integration block
func (h *handler) syncIntegration(ctx context.Context,
i *schemav1.AddConfigRequest) (*schemav1.AddConfigResponse, error,
) {
response := &schemav1.AddConfigResponse{
Status: "",
}
integrationProvider := integration.NewIntegration()
if i.Integrations == nil {
// If there are locally activate integrations, disable them
err := h.deactivateAllIntegrations(integrationProvider)
if err != nil {
response.Status = "Deactivated all integrations"
return response, err
}
return response, nil
}
coreFilters, _, _ := analyzer.ListFilters()
// Update filters
activeFilters := viper.GetStringSlice("active_filters")
if len(activeFilters) == 0 {
activeFilters = coreFilters
}
var err error
deactivateFunc := func(integrationRef integration.IIntegration) error {
return integrationProvider.Deactivate(trivyName, integrationRef.GetNamespace())
}
integrationRef, err := integrationProvider.Get(trivyName)
if err != nil {
return response, err
}
if i.Integrations.Trivy != nil {
switch i.Integrations.Trivy.Enabled {
case true:
err = integrationProvider.Activate(trivyName, i.Integrations.Trivy.Namespace,
activeFilters, i.Integrations.Trivy.SkipInstall)
case false:
err = deactivateFunc(integrationRef)
// This break is included purely for static analysis to pass
if err != nil {
break
}
}
} else {
// If Trivy has been removed, disable it
err = deactivateFunc(integrationRef)
}

return response, err
}

func (*handler) ListIntegrations(ctx context.Context, req *schemav1.ListIntegrationsRequest) (*schemav1.ListIntegrationsResponse, error) {

integrationProvider := integration.NewIntegration()
Expand All @@ -22,3 +82,23 @@ func (*handler) ListIntegrations(ctx context.Context, req *schemav1.ListIntegrat
}
return resp, nil
}

func (*handler) deactivateAllIntegrations(integrationProvider *integration.Integration) error {
integrations := integrationProvider.List()
for _, i := range integrations {
b, _ := integrationProvider.IsActivate(i)
if b {
in, err := integrationProvider.Get(i)
if err == nil {
if in.GetNamespace() != "" {
integrationProvider.Deactivate(i, in.GetNamespace())
} else {
fmt.Printf("Skipping deactivation of %s, not installed\n", i)
}
} else {
return err
}
}
}
return nil
}