Skip to content

Commit 6038684

Browse files
davejrtbobheadxi
andauthored
check image names (sourcegraph#4014)
* check for images * remove yaml for use across different file types * remove commented code * change import package name * makes regex more precise, add docstring, exit on first failure * make variable names more obvious, declare vars in block * add tests, move tool * test uses assert.Contains * move bash script to buildkite dir * Update tools/check-image-names/check-image-names.go Co-authored-by: Robert Lin <[email protected]> * remove comment Co-authored-by: Robert Lin <[email protected]>
1 parent 51fc183 commit 6038684

File tree

7 files changed

+629
-405
lines changed

7 files changed

+629
-405
lines changed

.buildkite/check-image-names.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
3+
set -euxo pipefail
4+
5+
cd "$(dirname "${BASH_SOURCE[0]}")/.."
6+
ROOT="$(pwd)"
7+
8+
pushd tools/check-image-names
9+
10+
echo "--- Check to see if all manifests contain valid image names"
11+
go run check-image-names.go "${ROOT}"/base

.buildkite/pipeline.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ steps:
1111
command: .buildkite/shfmt.sh
1212
- label: ":git: :sleuth_or_spy:"
1313
command: .buildkite/verify-release/verify-release.sh
14+
- label: ":k8s: :sleuth_or_spy:"
15+
command: .buildkite/check-image-names.sh
1416

1517
- wait
1618

go.mod

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@ module github.com/sourcegraph/deploy-sourcegraph
33
go 1.16
44

55
require (
6+
cloud.google.com/go/logging v1.4.2 // indirect
67
github.com/docker/docker v1.13.1 // indirect
8+
github.com/fatih/color v1.10.0 // indirect
9+
github.com/pkg/errors v0.9.1
710
github.com/pulumi/pulumi v1.12.0
811
github.com/sethgrid/pester v1.1.0
9-
github.com/sourcegraph/sourcegraph/enterprise/dev/ci/images v0.0.0-20210928153802-14b7dd055792
12+
github.com/sourcegraph/sourcegraph/enterprise/dev/ci/images v0.0.0-20211005203732-b910fedad1f4
1013
github.com/sourcegraph/update-docker-tags v0.8.0
11-
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 // indirect
12-
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 // indirect
14+
github.com/spf13/cobra v1.1.3 // indirect
15+
github.com/stretchr/testify v1.7.0 // indirect
16+
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
17+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
18+
gotest.tools/v3 v3.0.3
1319
)
1420

1521
replace github.com/Azure/go-autorest => github.com/Azure/go-autorest v12.4.3+incompatible

go.sum

Lines changed: 499 additions & 402 deletions
Large diffs are not rendered by default.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"os"
7+
"path/filepath"
8+
"regexp"
9+
"sort"
10+
"strings"
11+
12+
"github.com/pkg/errors"
13+
"github.com/sourcegraph/sourcegraph/enterprise/dev/ci/images"
14+
)
15+
16+
var (
17+
i = regexp.MustCompile(`index\.docker\.io\/sourcegraph/(?P<image>[a-z0-9-_.]+):[a-z0-9-_]+@sha256:[[:alnum:]]+`)
18+
matches []string
19+
data []byte
20+
)
21+
22+
func main() {
23+
24+
path := os.Args[1]
25+
26+
fmt.Print(CheckImages(path))
27+
28+
}
29+
30+
func CheckImages(path string) error {
31+
32+
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
33+
if err != nil {
34+
fmt.Println(err)
35+
return err
36+
}
37+
if info.IsDir() {
38+
return nil
39+
}
40+
41+
if strings.HasPrefix(path, ".git") {
42+
return nil
43+
}
44+
45+
data, err = ioutil.ReadFile(path)
46+
if err != nil {
47+
return errors.Wrap(err, "when reading file contents")
48+
}
49+
50+
// matchedImages contains all lines matching our regex. FindAllSubmatch returns
51+
// a slice containing the full string, and the capture group `image` for each image in a file.
52+
// We then loop over each slice, pull out the capture group and append it to a list of images
53+
// to compare with upstream.
54+
matchedImages := i.FindAllSubmatch(data, -1)
55+
for _, match := range matchedImages {
56+
matchd := string(match[1])
57+
matches = append(matches, matchd)
58+
}
59+
60+
return nil
61+
})
62+
matches = Unique(matches)
63+
for i, image := range matches {
64+
if image != images.DeploySourcegraphDockerImages[i] {
65+
return fmt.Errorf("image: %s is not in the upstream list", image)
66+
}
67+
}
68+
69+
if err != nil {
70+
fmt.Println(err)
71+
}
72+
73+
return nil
74+
75+
}
76+
77+
func Unique(strSlice []string) []string {
78+
keys := make(map[string]bool)
79+
list := []string{}
80+
for _, entry := range strSlice {
81+
if _, found := keys[entry]; !found {
82+
keys[entry] = true
83+
list = append(list, entry)
84+
}
85+
}
86+
87+
sort.Strings(list)
88+
89+
return list
90+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestCheckImages(t *testing.T) {
10+
11+
errorString := "image: foo is not in the upstream list"
12+
err := CheckImages("tests")
13+
assert.NotNil(t, err)
14+
assert.Contains(t, err.Error(), errorString)
15+
16+
}

tools/check-image-names/tests

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
foo/test:asdfasdf
2+
index.docker.io/sourcegraph/foo:2323423@sha256:asdfasdfasdfasdf

0 commit comments

Comments
 (0)