Skip to content

Commit

Permalink
fea(score) image tag scoring
Browse files Browse the repository at this point in the history
  • Loading branch information
zegl committed Sep 17, 2018
1 parent 9ccee7d commit bc3e749
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
29 changes: 29 additions & 0 deletions score/score.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"io/ioutil"
"log"
"strings"

//"errors"
//"fmt"
Expand Down Expand Up @@ -94,6 +95,7 @@ func Score(file io.Reader) (*Scorecard, error) {

podTests := []func(corev1.PodSpec) TestScore{
scoreContainerLimits,
scoreContainerImageTag,
}

scoreCard := Scorecard{}
Expand Down Expand Up @@ -160,3 +162,30 @@ func scoreContainerLimits(pod corev1.PodSpec) (score TestScore) {

return
}

func scoreContainerImageTag(pod corev1.PodSpec) (score TestScore) {
score.Name = "Container Image Tag"

allContainers := pod.InitContainers
allContainers = append(allContainers, pod.Containers...)

hasTagLatest := false

for _, container := range allContainers{
imageParts := strings.Split(container.Image, ":")
imageVersion := imageParts[len(imageParts)-1]

if imageVersion == "latest" {
score.Comments = append(score.Comments, "Image with latest tag")
hasTagLatest = true
}
}

if hasTagLatest {
score.Grade = 0
} else {
score.Grade = 10
}

return
}
8 changes: 8 additions & 0 deletions score/score_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,11 @@ func TestDeploymentResources(t *testing.T) {
func TestStatefulSetResources(t *testing.T) {
testExpectedScore(t, "statefulset-test-resources.yaml", "Container Resources", 5)
}

func TestPodContainerTagLatest(t *testing.T) {
testExpectedScore(t, "pod-image-tag-latest.yaml", "Container Image Tag", 0)
}

func TestPodContainerTagFixed(t *testing.T) {
testExpectedScore(t, "pod-image-tag-fixed.yaml", "Container Image Tag", 10)
}
9 changes: 9 additions & 0 deletions score/testdata/pod-image-tag-fixed.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: v1
kind: Pod
metadata:
name: pod-test-1
spec:
containers:
- name: foobar
resources:
image: foo/bar:123
9 changes: 9 additions & 0 deletions score/testdata/pod-image-tag-latest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: v1
kind: Pod
metadata:
name: pod-test-1
spec:
containers:
- name: foobar
resources:
image: foo/bar:latest

0 comments on commit bc3e749

Please sign in to comment.