-
Notifications
You must be signed in to change notification settings - Fork 694
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: more significant refactor Signed-off-by: Alex Jones <[email protected]> * feat: more significant refactor Signed-off-by: Alex Jones <[email protected]> * feat: reworked the integration activate/deactivation Signed-off-by: Alex Jones <[email protected]> * chore: updated schema for list integrations Signed-off-by: Alex Jones <[email protected]> * fix: error with incorrect error being swallowed Signed-off-by: Alex Jones <[email protected]> * feat: added namespace check Signed-off-by: Alex Jones <[email protected]> * chore: fixed issue with namespace and skip install validation Signed-off-by: Alex Jones <[email protected]> --------- Signed-off-by: Alex Jones <[email protected]>
- Loading branch information
1 parent
ddeff9f
commit 69fe2db
Showing
9 changed files
with
176 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.idea | ||
__debug* | ||
.DS_Store | ||
k8sgpt* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,144 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
|
||
schemav1 "buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go/schema/v1" | ||
"context" | ||
"fmt" | ||
"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 ( | ||
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{} | ||
integrationProvider := integration.NewIntegration() | ||
if i.Integrations == nil { | ||
// If there are locally activate integrations, disable them | ||
err := h.deactivateAllIntegrations(integrationProvider) | ||
if err != nil { | ||
return response, status.Error(codes.NotFound, "deactivation error") | ||
} | ||
return response, nil | ||
} | ||
coreFilters, _, _ := analyzer.ListFilters() | ||
// Update filters | ||
activeFilters := viper.GetStringSlice("active_filters") | ||
if len(activeFilters) == 0 { | ||
activeFilters = coreFilters | ||
} | ||
var err error = status.Error(codes.OK, "") | ||
deactivateFunc := func(integrationRef integration.IIntegration) error { | ||
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") | ||
} | ||
return nil | ||
} | ||
integrationRef, err := integrationProvider.Get(trivyName) | ||
if err != nil { | ||
return response, status.Error(codes.NotFound, "provider get failure") | ||
} | ||
if i.Integrations.Trivy != nil { | ||
switch i.Integrations.Trivy.Enabled { | ||
case true: | ||
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) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} else { | ||
return response, status.Error(codes.AlreadyExists, "integration already active") | ||
} | ||
} | ||
case false: | ||
err = deactivateFunc(integrationRef) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// This break is included purely for static analysis to pass | ||
} | ||
} else { | ||
// If Trivy has been removed, disable it | ||
err = deactivateFunc(integrationRef) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return response, err | ||
} | ||
|
||
func (*handler) ListIntegrations(ctx context.Context, req *schemav1.ListIntegrationsRequest) (*schemav1.ListIntegrationsResponse, error) { | ||
|
||
integrationProvider := integration.NewIntegration() | ||
integrations := integrationProvider.List() | ||
// Update the requester with the status of Trivy | ||
trivy, err := integrationProvider.Get(trivyName) | ||
active := trivy.IsActivate() | ||
var skipInstall bool | ||
var namespace string = "" | ||
if active { | ||
namespace, err = trivy.GetNamespace() | ||
if err != nil { | ||
return nil, status.Error(codes.NotFound, "namespace not found") | ||
} | ||
if namespace == "" { | ||
skipInstall = true | ||
} | ||
} | ||
|
||
if err != nil { | ||
return nil, status.Error(codes.NotFound, "trivy integration") | ||
} | ||
resp := &schemav1.ListIntegrationsResponse{ | ||
Integrations: make([]string, 0), | ||
Trivy: &schemav1.Trivy{ | ||
Enabled: active, | ||
Namespace: namespace, | ||
SkipInstall: skipInstall, | ||
}, | ||
} | ||
|
||
return resp, nil | ||
} | ||
|
||
func (*handler) deactivateAllIntegrations(integrationProvider *integration.Integration) error { | ||
integrations := integrationProvider.List() | ||
for _, i := range integrations { | ||
b, _ := integrationProvider.IsActivate(i) | ||
if b { | ||
resp.Integrations = append(resp.Integrations, i) | ||
in, err := integrationProvider.Get(i) | ||
namespace, err := in.GetNamespace() | ||
if err != nil { | ||
return err | ||
} | ||
if err == nil { | ||
if namespace != "" { | ||
integrationProvider.Deactivate(i, namespace) | ||
} else { | ||
fmt.Printf("Skipping deactivation of %s, not installed\n", i) | ||
} | ||
} else { | ||
return err | ||
} | ||
} | ||
} | ||
return resp, nil | ||
return nil | ||
} |