Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.

Commit d2b317e

Browse files
authored
Merge pull request #202 from nkubala/latest_tag
only append latest tag to image once
2 parents bc5c88f + 1e0e5ff commit d2b317e

File tree

6 files changed

+80
-17
lines changed

6 files changed

+80
-17
lines changed

boilerplate/boilerplate.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def file_passes(filename, refs, regexs):
8989
if p.search(d):
9090
return False
9191

92-
# Replace all occurrences of the regex "2017|2016|2015|2014" with "YEAR"
92+
# Replace all occurrences of the regex "2018|2017|2016|2015|2014" with "YEAR"
9393
p = regexs["date"]
9494
for i, d in enumerate(data):
9595
(data[i], found) = p.subn('YEAR', d)
@@ -149,8 +149,8 @@ def get_regexs():
149149
regexs = {}
150150
# Search for "YEAR" which exists in the boilerplate, but shouldn't in the real thing
151151
regexs["year"] = re.compile( 'YEAR' )
152-
# dates can be 2014, 2015, 2016 or 2017, company holder names can be anything
153-
regexs["date"] = re.compile( '(2014|2015|2016|2017)' )
152+
# dates can be 2014, 2015, 2016, 2017, or 2018, company holder names can be anything
153+
regexs["date"] = re.compile( '(2014|2015|2016|2017|2018)' )
154154
# strip // +build \n\n build constraints
155155
regexs["go_build_constraints"] = re.compile(r"^(// \+build.*\n)+\n", re.MULTILINE)
156156
# strip #!.* from shell scripts

cmd/root.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -135,28 +135,21 @@ func getPrepperForImage(image string) (pkgutil.Prepper, error) {
135135
}
136136

137137
// see if the image name has tag provided, if not add latest as tag
138-
if !strings.Contains(image, ":") {
139-
image = image + ":latest"
138+
if !pkgutil.HasTag(image) {
139+
image = image + pkgutil.LatestTag
140140
}
141141

142142
if strings.HasPrefix(image, DaemonPrefix) {
143143
// remove the DaemonPrefix
144-
image := strings.Replace(image, DaemonPrefix, "", -1)
145-
if !strings.Contains(image, ":") {
146-
image = image + ":latest"
147-
}
144+
image = strings.Replace(image, DaemonPrefix, "", -1)
148145

149146
return pkgutil.DaemonPrepper{
150147
Source: image,
151148
Client: cli,
152149
}, nil
153150
}
154-
// either has remote prefix or has no prefix, in which case we force remote
155151

156-
// see if the image name has tag provided, if not add latest as tag
157-
if !strings.Contains(image, ":") {
158-
image = image + ":latest"
159-
}
152+
// either has remote prefix or has no prefix, in which case we force remote
160153
ref, err := docker.ParseReference("//" + image)
161154
if err != nil {
162155
return nil, err

pkg/util/image_prep_utils.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,14 @@ type ConfigSchema struct {
9999
}
100100

101101
func getImage(p Prepper) (Image, error) {
102-
output.PrintToStdErr("Retrieving image %s from source %s\n", p.GetSource(), p.Name())
102+
var source string
103+
// see if the image name has tag provided, if not add latest as tag
104+
if !HasTag(p.GetSource()) {
105+
source = p.GetSource() + LatestTag
106+
} else {
107+
source = p.GetSource()
108+
}
109+
output.PrintToStdErr("Retrieving image %s from source %s\n", source, p.Name())
103110
imgPath, err := p.GetFileSystem()
104111
if err != nil {
105112
return Image{}, err
@@ -110,9 +117,9 @@ func getImage(p Prepper) (Image, error) {
110117
logrus.Error("Error retrieving History: ", err)
111118
}
112119

113-
logrus.Infof("Finished prepping image %s", p.GetSource())
120+
logrus.Infof("Finished prepping image %s", source)
114121
return Image{
115-
Source: p.GetSource(),
122+
Source: source,
116123
FSPath: imgPath,
117124
Config: config,
118125
}, nil

pkg/util/image_utils.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ import (
2121
"io/ioutil"
2222
"os"
2323
"path/filepath"
24+
"regexp"
2425

2526
"github.com/docker/docker/pkg/system"
2627
"github.com/sirupsen/logrus"
2728
)
2829

30+
const LatestTag string = ":latest"
31+
2932
func GetImageLayers(pathToImage string) []string {
3033
layers := []string{}
3134
contents, err := ioutil.ReadDir(pathToImage)
@@ -67,3 +70,9 @@ func copyToFile(outfile string, r io.Reader) error {
6770

6871
return nil
6972
}
73+
74+
// checks to see if an image string contains a tag.
75+
func HasTag(image string) bool {
76+
tagRegex := regexp.MustCompile(".*:[^/]+$")
77+
return tagRegex.MatchString(image)
78+
}

util/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ go_test(
2727
srcs = [
2828
"file_cache_test.go",
2929
"fs_utils_test.go",
30+
"image_utils_test.go",
3031
"output_sort_utils_test.go",
3132
"package_diff_utils_test.go",
3233
"tar_utils_test.go",

util/image_utils_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Copyright 2018 Google, Inc. All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package util
18+
19+
import (
20+
"testing"
21+
22+
pkgutil "github.com/GoogleCloudPlatform/container-diff/pkg/util"
23+
)
24+
25+
func TestImageTags(t *testing.T) {
26+
tests := []struct {
27+
image string
28+
hasTag bool
29+
}{
30+
{
31+
image: "gcr.io/test_image/foo:latest",
32+
hasTag: true,
33+
},
34+
{
35+
image: "gcr.io/test_image/foo:",
36+
hasTag: false,
37+
},
38+
{
39+
image: "daemon://gcr.io/test_image/foo:test",
40+
hasTag: true,
41+
},
42+
{
43+
image: "remote://gcr.io/test_image_foo",
44+
hasTag: false,
45+
},
46+
}
47+
48+
for _, test := range tests {
49+
if pkgutil.HasTag(test.image) != test.hasTag {
50+
t.Errorf("Error checking tag on image %s", test.image)
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)