Skip to content

Commit

Permalink
fix: issue with image cleanup
Browse files Browse the repository at this point in the history
The API for Target has to be changed: the cleanup needs the image name (not the id).
  • Loading branch information
derkoe authored and ckotzbauer committed Mar 12, 2022
1 parent 2fedd5e commit 9fa7730
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
14 changes: 10 additions & 4 deletions internal/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ func (client *KubeClient) listPods(namespace, labelSelector string) []corev1.Pod
return list.Items
}

func (client *KubeClient) LoadImageInfos(namespaces []corev1.Namespace, podLabelSelector string) (map[string]ContainerImage, []string) {
func (client *KubeClient) LoadImageInfos(namespaces []corev1.Namespace, podLabelSelector string) (map[string]ContainerImage, []ContainerImage) {
images := map[string]ContainerImage{}
allImages := []string{}
allImages := []ContainerImage{}

for _, ns := range namespaces {
pods := client.listPods(ns.Name, podLabelSelector)
Expand Down Expand Up @@ -119,11 +119,17 @@ func (client *KubeClient) LoadImageInfos(namespaces []corev1.Namespace, podLabel

img.Pods = append(img.Pods, pod)
images[c.ImageID] = img
allImages = append(allImages, img)
} else {
logrus.Debugf("Skip image %s", c.ImageID)
allImages = append(allImages, ContainerImage{
Image: c.Image,
ImageID: c.ImageID,
Auth: pullSecrets,
LegacyAuth: legacy,
Pods: []corev1.Pod{},
})
}

allImages = append(allImages, c.ImageID)
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions internal/target/dtrack_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (g *DependencyTrackTarget) ProcessSbom(image kubernetes.ContainerImage, sbo
return nil
}

func (g *DependencyTrackTarget) Cleanup(allImages []string) {
func (g *DependencyTrackTarget) Cleanup(allImages []kubernetes.ContainerImage) {
client, _ := dtrack.NewClient(g.baseUrl, dtrack.WithAPIKey(g.apiKey))

var (
Expand All @@ -113,9 +113,9 @@ func (g *DependencyTrackTarget) Cleanup(allImages []string) {

allImageRefs := make([]parser.Reference, len(allImages))
for _, image := range allImages {
ref, err := parser.Parse(image)
ref, err := parser.Parse(image.Image)
if err != nil {
logrus.WithError(err).Errorf("Could not parse image %s", image)
logrus.WithError(err).Errorf("Could not parse image %s", image.Image)
continue
}
allImageRefs = append(allImageRefs, *ref)
Expand All @@ -136,7 +136,7 @@ func (g *DependencyTrackTarget) Cleanup(allImages []string) {

// Image used in current cluster
for _, image := range allImages {
if image == currentImageName {
if image.Image == currentImageName {
continue projectLoop
}
}
Expand Down
6 changes: 3 additions & 3 deletions internal/target/git_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (g *GitTarget) ProcessSbom(image kubernetes.ContainerImage, sbom string) er
return g.gitAccount.CommitAll(g.workingTree, fmt.Sprintf("Created new SBOM for image %s", imageID))
}

func (g *GitTarget) Cleanup(allImages []string) {
func (g *GitTarget) Cleanup(allImages []kubernetes.ContainerImage) {
logrus.Debug("Start to remove old SBOMs")
ignoreDirs := []string{".git"}

Expand All @@ -114,10 +114,10 @@ func (g *GitTarget) Cleanup(allImages []string) {
}
}

func (g *GitTarget) mapToFiles(allImages []string) []string {
func (g *GitTarget) mapToFiles(allImages []kubernetes.ContainerImage) []string {
paths := []string{}
for _, img := range allImages {
paths = append(paths, g.imageIDToFilePath(img))
paths = append(paths, g.imageIDToFilePath(img.ImageID))
}

return paths
Expand Down
2 changes: 1 addition & 1 deletion internal/target/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ type Target interface {
Initialize()
ValidateConfig() error
ProcessSbom(image kubernetes.ContainerImage, sbom string) error
Cleanup(allImages []string)
Cleanup(allImages []kubernetes.ContainerImage)
}

0 comments on commit 9fa7730

Please sign in to comment.