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
Prev Previous commit
Next Next commit
feat: added namespace check
Signed-off-by: Alex Jones <[email protected]>
  • Loading branch information
AlexsJones committed Sep 27, 2023
commit ba608c29017bec01dcdba782b18a54f50494d4f6
3 changes: 1 addition & 2 deletions pkg/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type IIntegration interface {

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

OwnsAnalyzer(string) bool

Expand Down Expand Up @@ -88,7 +88,6 @@ func (*Integration) Activate(name string, namespace string, activeFilters []stri
return err
}
}

mergedFilters := activeFilters
mergedFilters = append(mergedFilters, integrations[name].GetAnalyzerName()...)
uniqueFilters, _ := util.RemoveDuplicates(mergedFilters)
Expand Down
22 changes: 15 additions & 7 deletions pkg/integration/trivy/trivy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package trivy
import (
"context"
"fmt"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/k8sgpt-ai/k8sgpt/pkg/common"
helmclient "github.com/mittwald/go-helm-client"
Expand All @@ -31,8 +33,7 @@ const (
)

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

func NewTrivy() *Trivy {
Expand All @@ -52,8 +53,18 @@ func (t *Trivy) GetAnalyzerName() []string {
}
}

func (t *Trivy) GetNamespace() string {
return t.namespace
// This doesnt work
func (t *Trivy) GetNamespace() (string, error) {
releases, err := t.helm.ListDeployedReleases()
if err != nil {
return "", err
}
for _, rel := range releases {
if rel.Name == ReleaseName {
return rel.Namespace, nil
}
}
return "", status.Error(codes.NotFound, "trivy release not found")
}

func (t *Trivy) OwnsAnalyzer(analyzer string) bool {
Expand All @@ -67,14 +78,11 @@ 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,
URL: Repo,
}

// Add a chart-repository to the client.
if err := t.helm.AddOrUpdateChartRepo(chartRepo); err != nil {
panic(err)
Expand Down
28 changes: 22 additions & 6 deletions pkg/server/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ func (h *handler) syncIntegration(ctx context.Context,
}
var err error = status.Error(codes.OK, "")
deactivateFunc := func(integrationRef integration.IIntegration) error {
err := integrationProvider.Deactivate(trivyName, integrationRef.GetNamespace())
namespace, err := integrationRef.GetNamespace()
if err != nil {
return err
}
err = integrationProvider.Deactivate(trivyName, namespace)
if err != nil {
return status.Error(codes.NotFound, "integration already deactivated")
}
Expand Down Expand Up @@ -86,16 +90,24 @@ func (h *handler) syncIntegration(ctx context.Context,
func (*handler) ListIntegrations(ctx context.Context, req *schemav1.ListIntegrationsRequest) (*schemav1.ListIntegrationsResponse, error) {

integrationProvider := integration.NewIntegration()

// Update the requester with the status of Trivy
trivy, err := integrationProvider.Get(trivyName)
active := trivy.IsActivate()
var namespace string = ""
if active {
namespace, err = trivy.GetNamespace()
if err != nil {
return nil, status.Error(codes.NotFound, "namespace not found")
}
}

if err != nil {
return nil, status.Error(codes.NotFound, "trivy integration")
}
resp := &schemav1.ListIntegrationsResponse{
Trivy: &schemav1.Trivy{
Enabled: trivy.IsActivate(),
Namespace: trivy.GetNamespace(),
Enabled: active,
Namespace: namespace,
},
}

Expand All @@ -108,9 +120,13 @@ func (*handler) deactivateAllIntegrations(integrationProvider *integration.Integ
b, _ := integrationProvider.IsActivate(i)
if b {
in, err := integrationProvider.Get(i)
namespace, err := in.GetNamespace()
if err != nil {
return err
}
if err == nil {
if in.GetNamespace() != "" {
integrationProvider.Deactivate(i, in.GetNamespace())
if namespace != "" {
integrationProvider.Deactivate(i, namespace)
} else {
fmt.Printf("Skipping deactivation of %s, not installed\n", i)
}
Expand Down