Skip to content

Commit

Permalink
Merge pull request #49 from dewe/pr-get-images-from-cronjob
Browse files Browse the repository at this point in the history
Add CronJob support
  • Loading branch information
acastle authored Oct 13, 2021
2 parents 745e50d + 1153971 commit 6d9ec8a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
9 changes: 9 additions & 0 deletions .images.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
target:
host: myregistry.nu
sources:
- repository: some/image
host: otherhost.com
tag: v1.0.0
- repository: other/jobimage
host: yetanotherhost.com
tag: "234"
23 changes: 23 additions & 0 deletions internal/manifest/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

promv1 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1"
kubeyaml "github.com/ghodss/yaml"
batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -149,6 +150,15 @@ func getImagesFromResource(resource string) ([]string, error) {
return podImages, nil
}

if typeMeta.Kind == "CronJob" {
cronJobImages, err := getCronJobImages(byteResource)
if err != nil {
return nil, fmt.Errorf("get cronjob images: %w", err)
}

return cronJobImages, nil
}

type BaseSpec struct {
Template corev1.PodTemplateSpec `json:"template" protobuf:"bytes,3,opt,name=template"`
}
Expand Down Expand Up @@ -224,6 +234,19 @@ func getPodImages(resource []byte) ([]string, error) {
return images, nil
}

func getCronJobImages(resource []byte) ([]string, error) {
var cj batchv1beta1.CronJob
if err := kubeyaml.Unmarshal(resource, &cj); err != nil {
return nil, fmt.Errorf("unmarshal cronjob: %w", err)
}

var images []string
images = append(images, getImagesFromContainers(cj.Spec.JobTemplate.Spec.Template.Spec.Containers)...)
images = append(images, getImagesFromContainers(cj.Spec.JobTemplate.Spec.Template.Spec.InitContainers)...)

return images, nil
}

func getImagesFromContainers(containers []corev1.Container) []string {
var images []string

Expand Down
19 changes: 18 additions & 1 deletion test/create/bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,21 @@ metadata:
spec:
containers:
- image: otherhost.com/some/image:v1.0.0
name: test-create
name: test-create
---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: test-cronjob
spec:
schedule: '*/1 * * * *'
jobTemplate:
metadata:
name: test-job
spec:
template:
spec:
containers:
- image: yetanotherhost.com/other/jobimage:234
name: test-job
restartPolicy: OnFailure
3 changes: 3 additions & 0 deletions test/create/expected-images.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ sources:
- repository: some/image
host: otherhost.com
tag: v1.0.0
- repository: other/jobimage
host: yetanotherhost.com
tag: "234"

0 comments on commit 6d9ec8a

Please sign in to comment.