Skip to content

Commit b2d2c21

Browse files
committed
minor cache clean up and small lint changes
1 parent 62ee201 commit b2d2c21

File tree

5 files changed

+42
-23
lines changed

5 files changed

+42
-23
lines changed

agent/kubviz/kubePreUpgrade.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ var result *model.Result
5757
func publishK8sDepricated_Deleted_Api(result *model.Result, js nats.JetStreamContext) error {
5858
for _, deprecatedAPI := range result.DeprecatedAPIs {
5959
deprecatedAPI.ClusterName = ClusterName
60-
fmt.Println("deprecatedAPI", deprecatedAPI)
6160
deprecatedAPIJson, _ := json.Marshal(deprecatedAPI)
6261
_, err := js.Publish(constants.EventSubject_depricated, deprecatedAPIJson)
6362
if err != nil {
@@ -90,7 +89,7 @@ func KubePreUpgradeDetector(config *rest.Config, js nats.JetStreamContext) error
9089
if err != nil {
9190
return err
9291
}
93-
defer os.RemoveAll(filename)
92+
defer os.RemoveAll(swaggerdir)
9493
swaggerfile := filename
9594
kubernetesAPIs, err := PopulateKubeAPIMap(swaggerfile)
9695
if err != nil {
@@ -178,23 +177,32 @@ func getKubeAPIValues(value map[string]interface{}) (model.KubeAPI, bool) {
178177
return model.KubeAPI{}, false
179178
}
180179
func downloadFile(filename, url string) error {
181-
log.Debugf("Downloading file from %s", url)
182180
resp, err := http.Get(url)
183181
if err != nil {
184182
log.Error(err)
183+
return err
185184
}
186185
if resp.StatusCode > 305 {
187186
log.Errorf("could not download the swagger file %s", url)
187+
return fmt.Errorf("failed to download file, status code: %d", resp.StatusCode)
188188
}
189+
contentLength := resp.ContentLength
190+
log.Infof("The size of the file to be downloaded for kubepreupgrade plugin is %d bytes", contentLength)
191+
189192
defer resp.Body.Close()
190193
out, err := os.Create(filename)
191194
if err != nil {
192195
log.Error(err)
193196
}
194197
defer out.Close()
195-
_, err = io.Copy(out, resp.Body)
198+
bytesCopied, err := io.Copy(out, resp.Body)
199+
if err != nil {
200+
log.WithError(err).Error("Failed to copy the file contents")
201+
return err
202+
}
203+
log.Infof("Downloaded %d bytes for file %s", bytesCopied, filename)
196204

197-
return err
205+
return nil
198206
}
199207

200208
func getGroupVersionKind(value map[string]interface{}) (group, version, kind string) {

agent/kubviz/outdated.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/base64"
66
"encoding/json"
77
"fmt"
8-
"github.com/intelops/kubviz/constants"
98
"log"
109
"os"
1110
"regexp"
@@ -15,10 +14,12 @@ import (
1514
"sync"
1615
"time"
1716

17+
"github.com/intelops/kubviz/constants"
18+
1819
"github.com/intelops/kubviz/model"
1920
"github.com/nats-io/nats.go"
2021

21-
"github.com/docker/docker/api/types"
22+
types "github.com/docker/docker/api/types/registry"
2223
"github.com/genuinetools/reg/registry"
2324
semver "github.com/hashicorp/go-version"
2425
"github.com/pkg/errors"
@@ -137,7 +138,7 @@ func ParseImageName(imageName string) (string, string, string, error) {
137138
matches := dockerImageNameRegex.FindStringSubmatch(imageName)
138139

139140
if len(matches) != 5 {
140-
return "", "", "", fmt.Errorf("Expected 5 matches in regex, but found %d", len(matches))
141+
return "", "", "", fmt.Errorf("expected 5 matches in regex, but found %d", len(matches))
141142
}
142143

143144
hostname := matches[1]
@@ -184,9 +185,7 @@ func ListImages(config *rest.Config) ([]model.RunningImage, error) {
184185
for _, pod := range pods.Items {
185186
for _, initContainerStatus := range pod.Status.InitContainerStatuses {
186187
pullable := initContainerStatus.ImageID
187-
if strings.HasPrefix(pullable, "docker-pullable://") {
188-
pullable = strings.TrimPrefix(pullable, "docker-pullable://")
189-
}
188+
pullable = strings.TrimPrefix(pullable, "docker-pullable://")
190189
runningImage := model.RunningImage{
191190
Pod: pod.Name,
192191
Namespace: pod.Namespace,
@@ -199,9 +198,8 @@ func ListImages(config *rest.Config) ([]model.RunningImage, error) {
199198

200199
for _, containerStatus := range pod.Status.ContainerStatuses {
201200
pullable := containerStatus.ImageID
202-
if strings.HasPrefix(pullable, "docker-pullable://") {
203-
pullable = strings.TrimPrefix(pullable, "docker-pullable://")
204-
}
201+
pullable = strings.TrimPrefix(pullable, "docker-pullable://")
202+
205203
runningImage := model.RunningImage{
206204
Pod: pod.Name,
207205
Namespace: pod.Namespace,
@@ -393,8 +391,8 @@ func fetchTags(reg *registry.Registry, imageName string) ([]string, error) {
393391
}
394392

395393
func parseTags(tags []string) ([]*semver.Version, []string, error) {
396-
semverTags := make([]*semver.Version, 0, 0)
397-
nonSemverTags := make([]string, 0, 0)
394+
semverTags := make([]*semver.Version, 0)
395+
nonSemverTags := make([]string, 0)
398396

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

452-
func homeDir() string {
453-
if h := os.Getenv("HOME"); h != "" {
454-
return h
455-
}
456-
return os.Getenv("USERPROFILE")
457-
}
450+
// func homeDir() string {
451+
// if h := os.Getenv("HOME"); h != "" {
452+
// return h
453+
// }
454+
// return os.Getenv("USERPROFILE")
455+
// }
458456

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

550-
result := make([]*semver.Version, 0, 0)
548+
result := make([]*semver.Version, 0)
551549
for _, u := range unique {
552550
result = append(result, u)
553551
}

agent/kubviz/trivy.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func RunTrivyK8sClusterScan(js nats.JetStreamContext) error {
6363
if err != nil {
6464
return err
6565
}
66+
cleanupCache("/tmp/.cache")
6667
return nil
6768
}
6869

agent/kubviz/trivy_image.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"encoding/json"
55
"log"
6+
"os"
67
"strings"
78

89
"github.com/aquasecurity/trivy/pkg/types"
@@ -47,6 +48,7 @@ func RunTrivyImageScans(config *rest.Config, js nats.JetStreamContext) error {
4748
if err != nil {
4849
return err
4950
}
51+
cleanupCache("/tmp/.cache")
5052
}
5153
return nil
5254
}
@@ -65,3 +67,12 @@ func publishImageScanReports(report types.Report, js nats.JetStreamContext) erro
6567
log.Printf("Trivy image report with ID:%s has been published\n", metrics.ID)
6668
return nil
6769
}
70+
71+
func cleanupCache(cacheDir string) {
72+
err := os.RemoveAll(cacheDir)
73+
if err != nil {
74+
log.Printf("Failed to clean up cache directory %s: %v", cacheDir, err)
75+
} else {
76+
log.Printf("Cache directory %s cleaned up successfully", cacheDir)
77+
}
78+
}

agent/kubviz/trivy_sbom.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func RunTrivySbomScan(config *rest.Config, js nats.JetStreamContext) error {
7878

7979
// Publish the report using the given function
8080
publishTrivySbomReport(report, js)
81+
cleanupCache("/tmp/.cache")
8182
}
8283
return nil
8384
}

0 commit comments

Comments
 (0)