Skip to content

Commit

Permalink
Merge pull request #273 from intelops/minorchanges
Browse files Browse the repository at this point in the history
minor cache clean up and small lint changes
  • Loading branch information
vijeyash1 authored Nov 10, 2023
2 parents 62ee201 + b2d2c21 commit e20421a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 23 deletions.
18 changes: 13 additions & 5 deletions agent/kubviz/kubePreUpgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ var result *model.Result
func publishK8sDepricated_Deleted_Api(result *model.Result, js nats.JetStreamContext) error {
for _, deprecatedAPI := range result.DeprecatedAPIs {
deprecatedAPI.ClusterName = ClusterName
fmt.Println("deprecatedAPI", deprecatedAPI)
deprecatedAPIJson, _ := json.Marshal(deprecatedAPI)
_, err := js.Publish(constants.EventSubject_depricated, deprecatedAPIJson)
if err != nil {
Expand Down Expand Up @@ -90,7 +89,7 @@ func KubePreUpgradeDetector(config *rest.Config, js nats.JetStreamContext) error
if err != nil {
return err
}
defer os.RemoveAll(filename)
defer os.RemoveAll(swaggerdir)
swaggerfile := filename
kubernetesAPIs, err := PopulateKubeAPIMap(swaggerfile)
if err != nil {
Expand Down Expand Up @@ -178,23 +177,32 @@ func getKubeAPIValues(value map[string]interface{}) (model.KubeAPI, bool) {
return model.KubeAPI{}, false
}
func downloadFile(filename, url string) error {
log.Debugf("Downloading file from %s", url)
resp, err := http.Get(url)
if err != nil {
log.Error(err)
return err
}
if resp.StatusCode > 305 {
log.Errorf("could not download the swagger file %s", url)
return fmt.Errorf("failed to download file, status code: %d", resp.StatusCode)
}
contentLength := resp.ContentLength
log.Infof("The size of the file to be downloaded for kubepreupgrade plugin is %d bytes", contentLength)

defer resp.Body.Close()
out, err := os.Create(filename)
if err != nil {
log.Error(err)
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
bytesCopied, err := io.Copy(out, resp.Body)
if err != nil {
log.WithError(err).Error("Failed to copy the file contents")
return err
}
log.Infof("Downloaded %d bytes for file %s", bytesCopied, filename)

return err
return nil
}

func getGroupVersionKind(value map[string]interface{}) (group, version, kind string) {
Expand Down
34 changes: 16 additions & 18 deletions agent/kubviz/outdated.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/intelops/kubviz/constants"
"log"
"os"
"regexp"
Expand All @@ -15,10 +14,12 @@ import (
"sync"
"time"

"github.com/intelops/kubviz/constants"

"github.com/intelops/kubviz/model"
"github.com/nats-io/nats.go"

"github.com/docker/docker/api/types"
types "github.com/docker/docker/api/types/registry"
"github.com/genuinetools/reg/registry"
semver "github.com/hashicorp/go-version"
"github.com/pkg/errors"
Expand Down Expand Up @@ -137,7 +138,7 @@ func ParseImageName(imageName string) (string, string, string, error) {
matches := dockerImageNameRegex.FindStringSubmatch(imageName)

if len(matches) != 5 {
return "", "", "", fmt.Errorf("Expected 5 matches in regex, but found %d", len(matches))
return "", "", "", fmt.Errorf("expected 5 matches in regex, but found %d", len(matches))
}

hostname := matches[1]
Expand Down Expand Up @@ -184,9 +185,7 @@ func ListImages(config *rest.Config) ([]model.RunningImage, error) {
for _, pod := range pods.Items {
for _, initContainerStatus := range pod.Status.InitContainerStatuses {
pullable := initContainerStatus.ImageID
if strings.HasPrefix(pullable, "docker-pullable://") {
pullable = strings.TrimPrefix(pullable, "docker-pullable://")
}
pullable = strings.TrimPrefix(pullable, "docker-pullable://")
runningImage := model.RunningImage{
Pod: pod.Name,
Namespace: pod.Namespace,
Expand All @@ -199,9 +198,8 @@ func ListImages(config *rest.Config) ([]model.RunningImage, error) {

for _, containerStatus := range pod.Status.ContainerStatuses {
pullable := containerStatus.ImageID
if strings.HasPrefix(pullable, "docker-pullable://") {
pullable = strings.TrimPrefix(pullable, "docker-pullable://")
}
pullable = strings.TrimPrefix(pullable, "docker-pullable://")

runningImage := model.RunningImage{
Pod: pod.Name,
Namespace: pod.Namespace,
Expand Down Expand Up @@ -393,8 +391,8 @@ func fetchTags(reg *registry.Registry, imageName string) ([]string, error) {
}

func parseTags(tags []string) ([]*semver.Version, []string, error) {
semverTags := make([]*semver.Version, 0, 0)
nonSemverTags := make([]string, 0, 0)
semverTags := make([]*semver.Version, 0)
nonSemverTags := make([]string, 0)

for _, tag := range tags {
v, err := semver.NewVersion(tag)
Expand Down Expand Up @@ -449,12 +447,12 @@ func splitOutlierSemvers(allSemverTags []*semver.Version) ([]*semver.Version, []
return outliers, remaining, nil
}

func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE")
}
// func homeDir() string {
// if h := os.Getenv("HOME"); h != "" {
// return h
// }
// return os.Getenv("USERPROFILE")
// }

type VersionTag struct {
Sort int `json:"sort"`
Expand Down Expand Up @@ -547,7 +545,7 @@ func (c SemverTagCollection) Unique() ([]*semver.Version, error) {
}
}

result := make([]*semver.Version, 0, 0)
result := make([]*semver.Version, 0)
for _, u := range unique {
result = append(result, u)
}
Expand Down
1 change: 1 addition & 0 deletions agent/kubviz/trivy.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func RunTrivyK8sClusterScan(js nats.JetStreamContext) error {
if err != nil {
return err
}
cleanupCache("/tmp/.cache")
return nil
}

Expand Down
11 changes: 11 additions & 0 deletions agent/kubviz/trivy_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"log"
"os"
"strings"

"github.com/aquasecurity/trivy/pkg/types"
Expand Down Expand Up @@ -47,6 +48,7 @@ func RunTrivyImageScans(config *rest.Config, js nats.JetStreamContext) error {
if err != nil {
return err
}
cleanupCache("/tmp/.cache")
}
return nil
}
Expand All @@ -65,3 +67,12 @@ func publishImageScanReports(report types.Report, js nats.JetStreamContext) erro
log.Printf("Trivy image report with ID:%s has been published\n", metrics.ID)
return nil
}

func cleanupCache(cacheDir string) {
err := os.RemoveAll(cacheDir)
if err != nil {
log.Printf("Failed to clean up cache directory %s: %v", cacheDir, err)
} else {
log.Printf("Cache directory %s cleaned up successfully", cacheDir)
}
}
1 change: 1 addition & 0 deletions agent/kubviz/trivy_sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func RunTrivySbomScan(config *rest.Config, js nats.JetStreamContext) error {

// Publish the report using the given function
publishTrivySbomReport(report, js)
cleanupCache("/tmp/.cache")
}
return nil
}

0 comments on commit e20421a

Please sign in to comment.