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

fix: show trivy as active when activated with --no-install flag #675

Merged
merged 3 commits into from
Nov 13, 2023
Merged
Changes from all commits
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
51 changes: 47 additions & 4 deletions pkg/integration/trivy/trivy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ package trivy
import (
"context"
"fmt"
"os"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/fatih/color"
"github.com/k8sgpt-ai/k8sgpt/pkg/common"
"github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes"
helmclient "github.com/mittwald/go-helm-client"
"github.com/spf13/viper"
"helm.sh/helm/v3/pkg/repo"
)

Expand Down Expand Up @@ -126,13 +130,52 @@ func (t *Trivy) UnDeploy(namespace string) error {
return nil
}

func (t *Trivy) IsActivate() bool {
func (t *Trivy) isDeployed() bool {
// check if aquasec apigroup is available as a marker if trivy is installed on the cluster
kubecontext := viper.GetString("kubecontext")
kubeconfig := viper.GetString("kubeconfig")
client, err := kubernetes.NewClient(kubecontext, kubeconfig)
if err != nil {
// TODO: better error handling
color.Red("Error initialising kubernetes client: %v", err)
os.Exit(1)
}
groups, _, err := client.Client.Discovery().ServerGroupsAndResources()
if err != nil {
// TODO: better error handling
color.Red("Error initialising discovery client: %v", err)
os.Exit(1)
}

if _, err := t.helm.GetRelease(ReleaseName); err != nil {
return false
for _, group := range groups {
if group.Name == "aquasecurity.github.io" {
return true
}
}

return true
return false
}

func (t *Trivy) isFilterActive() bool {
activeFilters := viper.GetStringSlice("active_filters")

for _, filter := range t.GetAnalyzerName() {
for _, af := range activeFilters {
if af == filter {
return true
}
}
}

return false
}

func (t *Trivy) IsActivate() bool {
if t.isFilterActive() && t.isDeployed() {
return true
} else {
return false
}
}

func (t *Trivy) AddAnalyzer(mergedMap *map[string]common.IAnalyzer) {
Expand Down