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
Prev Previous commit
Next Next commit
feat: reworked the integration activate/deactivation
Signed-off-by: Alex Jones <[email protected]>
  • Loading branch information
AlexsJones committed Sep 26, 2023
commit fab234a5e437b729356092b5e953775ed5ba33e1
9 changes: 5 additions & 4 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package cache

import (
"errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/spf13/viper"
)
Expand Down Expand Up @@ -63,17 +64,17 @@ func RemoveRemoteCache(bucketName string) error {
var cacheInfo CacheProvider
err := viper.UnmarshalKey("cache", &cacheInfo)
if err != nil {
return err
return status.Error(codes.Internal, "cache unmarshal")
}
if cacheInfo.BucketName == "" {
return errors.New("Error: no cache is configured")
return status.Error(codes.Internal, "no cache configured")
}

cacheInfo = CacheProvider{}
viper.Set("cache", cacheInfo)
err = viper.WriteConfig()
if err != nil {
return err
return status.Error(codes.Internal, "unable to write config")
}

return nil
Expand Down
4 changes: 4 additions & 0 deletions pkg/server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ grpcurl -plaintext localhost:8080 schema.v1.ServerService/ListIntegrations
}

```

```
grpcurl -plaintext -d '{"integrations":{"trivy":{"enabled":"true","namespace":"default","skipInstall":"false"}}}' localhost:8080 schema.v1.ServerService/AddConfig
```
21 changes: 7 additions & 14 deletions pkg/server/config.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package server

import (
"context"
"errors"

schemav1 "buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go/schema/v1"
"context"
"github.com/k8sgpt-ai/k8sgpt/pkg/cache"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func (h *handler) AddConfig(ctx context.Context, i *schemav1.AddConfigRequest) (*schemav1.AddConfigResponse, error,
Expand All @@ -19,29 +19,22 @@ func (h *handler) AddConfig(ctx context.Context, i *schemav1.AddConfigRequest) (
if i.Cache != nil {
// Remote cache
if i.Cache.BucketName == "" || i.Cache.Region == "" {
return &schemav1.AddConfigResponse{}, errors.New("BucketName & Region are required")
return resp, status.Error(codes.InvalidArgument, "cache arguments")
}

err := cache.AddRemoteCache(i.Cache.BucketName, i.Cache.Region)
if err != nil {
return &schemav1.AddConfigResponse{
Status: err.Error(),
}, err
return resp, err
}
}
return &schemav1.AddConfigResponse{
Status: "Configuration updated.",
}, nil
return resp, nil
}

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
return &schemav1.RemoveConfigResponse{}, err
}

// Remove any integrations is a TBD as it would be nice to make this more granular
Expand Down
37 changes: 22 additions & 15 deletions pkg/server/integration.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package server

import (
schemav1 "buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go/schema/v1"
"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"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

const (
Expand All @@ -20,16 +21,13 @@ const (
func (h *handler) syncIntegration(ctx context.Context,
i *schemav1.AddConfigRequest) (*schemav1.AddConfigResponse, error,
) {
response := &schemav1.AddConfigResponse{
Status: "",
}
response := &schemav1.AddConfigResponse{}
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, status.Error(codes.NotFound, "deactivation error")
}
return response, nil
}
Expand All @@ -39,25 +37,34 @@ func (h *handler) syncIntegration(ctx context.Context,
if len(activeFilters) == 0 {
activeFilters = coreFilters
}
var err error
var err error = status.Error(codes.OK, "")
deactivateFunc := func(integrationRef integration.IIntegration) error {
return integrationProvider.Deactivate(trivyName, integrationRef.GetNamespace())
err := integrationProvider.Deactivate(trivyName, integrationRef.GetNamespace())
if err != nil {
return status.Error(codes.NotFound, "integration already deactivated")
}
return nil
}
integrationRef, err := integrationProvider.Get(trivyName)
if err != nil {
return response, err
return response, status.Error(codes.NotFound, "provider get failure")
}
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)
if b, err := integrationProvider.IsActivate(trivyName); err != nil {
return response, status.Error(codes.Internal, "integration activation error")
} else {
if !b {
err = integrationProvider.Activate(trivyName, i.Integrations.Trivy.Namespace,
activeFilters, i.Integrations.Trivy.SkipInstall)
} else {
return response, status.Error(codes.AlreadyExists, "integration already active")
}
}
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
Expand Down