Skip to content

Commit f509dd3

Browse files
committed
Support --pull for build/publish commands
The --pull flag is supported by Docker and forces a pull attempt to be made for base images for the case that there is a newer version available. The main use-case for this is when teams are publishing production or internal images directly from their own machines which accumulate older images. This is not needed in CI pipelines which start with an empty library, so always pull the base images fresh from source. Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
1 parent 7264f71 commit f509dd3

File tree

5 files changed

+29
-7
lines changed

5 files changed

+29
-7
lines changed

builder/build.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const AdditionalPackageBuildArg = "ADDITIONAL_PACKAGE"
3030

3131
// BuildImage construct Docker image from function parameters
3232
// TODO: refactor signature to a struct to simplify the length of the method header
33-
func BuildImage(image string, handler string, functionName string, language string, nocache bool, squash bool, shrinkwrap bool, buildArgMap map[string]string, buildOptions []string, tagFormat schema.BuildFormat, buildLabelMap map[string]string, quietBuild bool, copyExtraPaths []string, remoteBuilder, payloadSecretPath string) error {
33+
func BuildImage(image string, handler string, functionName string, language string, nocache bool, squash bool, shrinkwrap bool, buildArgMap map[string]string, buildOptions []string, tagFormat schema.BuildFormat, buildLabelMap map[string]string, quietBuild bool, copyExtraPaths []string, remoteBuilder, payloadSecretPath string, forcePull bool) error {
3434

3535
if stack.IsValidTemplate(language) {
3636
pathToTemplateYAML := fmt.Sprintf("./template/%s/template.yml", language)
@@ -127,6 +127,7 @@ func BuildImage(image string, handler string, functionName string, language stri
127127
BuildArgMap: buildArgMap,
128128
BuildOptPackages: buildOptPackages,
129129
BuildLabelMap: buildLabelMap,
130+
ForcePull: forcePull,
130131
}
131132

132133
command, args := getDockerBuildCommand(dockerBuildVal)
@@ -135,6 +136,7 @@ func BuildImage(image string, handler string, functionName string, language stri
135136
if mountSSH {
136137
envs = append(envs, "DOCKER_BUILDKIT=1")
137138
}
139+
log.Printf("Build flags: %+v\n", args)
138140

139141
task := v2execute.ExecTask{
140142
Cwd: tempPath,
@@ -260,7 +262,7 @@ func hashFolder(contextPath string) (string, error) {
260262
}
261263

262264
func getDockerBuildCommand(build dockerBuild) (string, []string) {
263-
flagSlice := buildFlagSlice(build.NoCache, build.Squash, build.HTTPProxy, build.HTTPSProxy, build.BuildArgMap, build.BuildOptPackages, build.BuildLabelMap)
265+
flagSlice := buildFlagSlice(build.NoCache, build.Squash, build.HTTPProxy, build.HTTPSProxy, build.BuildArgMap, build.BuildOptPackages, build.BuildLabelMap, build.ForcePull)
264266
args := []string{"build"}
265267
args = append(args, flagSlice...)
266268

@@ -287,6 +289,8 @@ type dockerBuild struct {
287289

288290
// ExtraTags for published images like :latest
289291
ExtraTags []string
292+
293+
ForcePull bool
290294
}
291295

292296
var defaultDirPermissions os.FileMode = 0700
@@ -409,7 +413,7 @@ func pathInScope(path string, scope string) (string, error) {
409413
return "", fmt.Errorf("forbidden path appears to be outside of the build context: %s (%s)", path, abs)
410414
}
411415

412-
func buildFlagSlice(nocache bool, squash bool, httpProxy string, httpsProxy string, buildArgMap map[string]string, buildOptionPackages []string, buildLabelMap map[string]string) []string {
416+
func buildFlagSlice(nocache bool, squash bool, httpProxy string, httpsProxy string, buildArgMap map[string]string, buildOptionPackages []string, buildLabelMap map[string]string, forcePull bool) []string {
413417

414418
var spaceSafeBuildFlags []string
415419

@@ -445,6 +449,10 @@ func buildFlagSlice(nocache bool, squash bool, httpProxy string, httpsProxy stri
445449
spaceSafeBuildFlags = append(spaceSafeBuildFlags, "--label", fmt.Sprintf("%s=%s", k, v))
446450
}
447451

452+
if forcePull {
453+
spaceSafeBuildFlags = append(spaceSafeBuildFlags, "--pull")
454+
}
455+
448456
return spaceSafeBuildFlags
449457
}
450458

builder/build_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,8 @@ func Test_buildFlagSlice(t *testing.T) {
302302

303303
t.Run(test.title, func(t *testing.T) {
304304

305-
flagSlice := buildFlagSlice(test.nocache, test.squash, test.httpProxy, test.httpsProxy, test.buildArgMap, test.buildPackages, test.buildLabelMap)
305+
forcePull := false
306+
flagSlice := buildFlagSlice(test.nocache, test.squash, test.httpProxy, test.httpsProxy, test.buildArgMap, test.buildPackages, test.buildLabelMap, forcePull)
306307
fmt.Println(flagSlice)
307308
if len(flagSlice) != len(test.expectedSlice) {
308309
t.Errorf("Slices differ in size - wanted: %d, found %d", len(test.expectedSlice), len(flagSlice))

builder/publish.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const BuilderConfigFilename = "com.openfaas.docker.config"
4444
// PublishImage will publish images as multi-arch
4545
// TODO: refactor signature to a struct to simplify the length of the method header
4646
func PublishImage(image string, handler string, functionName string, language string, nocache bool, squash bool, shrinkwrap bool, buildArgMap map[string]string,
47-
buildOptions []string, tagMode schema.BuildFormat, buildLabelMap map[string]string, quietBuild bool, copyExtraPaths []string, platforms string, extraTags []string, remoteBuilder, payloadSecretPath string) error {
47+
buildOptions []string, tagMode schema.BuildFormat, buildLabelMap map[string]string, quietBuild bool, copyExtraPaths []string, platforms string, extraTags []string, remoteBuilder, payloadSecretPath string, forcePull bool) error {
4848

4949
if stack.IsValidTemplate(language) {
5050
pathToTemplateYAML := fmt.Sprintf("./template/%s/template.yml", language)
@@ -81,6 +81,11 @@ func PublishImage(image string, handler string, functionName string, language st
8181
fmt.Printf("Building: %s with %s template. Please wait..\n", imageName, language)
8282

8383
if remoteBuilder != "" {
84+
85+
if forcePull {
86+
return fmt.Errorf("--pull is not supported with --remote-builder")
87+
}
88+
8489
tempDir, err := os.MkdirTemp(os.TempDir(), "builder-*")
8590
if err != nil {
8691
return fmt.Errorf("failed to create temporary directory for %s, error: %w", functionName, err)
@@ -139,6 +144,7 @@ func PublishImage(image string, handler string, functionName string, language st
139144
BuildLabelMap: buildLabelMap,
140145
Platforms: platforms,
141146
ExtraTags: extraTags,
147+
ForcePull: forcePull,
142148
}
143149

144150
command, args := getDockerBuildxCommand(dockerBuildVal)
@@ -173,7 +179,7 @@ func PublishImage(image string, handler string, functionName string, language st
173179

174180
func getDockerBuildxCommand(build dockerBuild) (string, []string) {
175181
flagSlice := buildFlagSlice(build.NoCache, build.Squash, build.HTTPProxy, build.HTTPSProxy, build.BuildArgMap,
176-
build.BuildOptPackages, build.BuildLabelMap)
182+
build.BuildOptPackages, build.BuildLabelMap, build.ForcePull)
177183

178184
// pushOnly defined at https://github.com/docker/buildx
179185
const pushOnly = "--output=type=registry,push=true"

commands/build.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ var (
3737
envsubst bool
3838
quietBuild bool
3939
disableStackPull bool
40+
forcePull bool
4041
)
4142

4243
func init() {
@@ -59,6 +60,7 @@ func init() {
5960
buildCmd.Flags().BoolVar(&envsubst, "envsubst", true, "Substitute environment variables in stack.yml file")
6061
buildCmd.Flags().BoolVar(&quietBuild, "quiet", false, "Perform a quiet build, without showing output from Docker")
6162
buildCmd.Flags().BoolVar(&disableStackPull, "disable-stack-pull", false, "Disables the template configuration in the stack.yml")
63+
buildCmd.Flags().BoolVar(&forcePull, "pull", false, "Force a re-pull of base images in template during build, useful for publishing images")
6264

6365
// Set bash-completion.
6466
_ = buildCmd.Flags().SetAnnotation("handler", cobra.BashCompSubdirsInDir, []string{})
@@ -80,7 +82,8 @@ var buildCmd = &cobra.Command{
8082
[--build-arg KEY=VALUE]
8183
[--build-option VALUE]
8284
[--copy-extra PATH]
83-
[--tag <sha|branch|describe>]`,
85+
[--tag <sha|branch|describe>]
86+
[--forcePull]`,
8487
Short: "Builds OpenFaaS function containers",
8588
Long: `Builds OpenFaaS function containers either via the supplied YAML config using
8689
the "--yaml" flag (which may contain multiple function definitions), or directly
@@ -207,6 +210,7 @@ func runBuild(cmd *cobra.Command, args []string) error {
207210
copyExtra,
208211
remoteBuilder,
209212
payloadSecretPath,
213+
forcePull,
210214
); err != nil {
211215
return err
212216
}
@@ -263,6 +267,7 @@ func build(services *stack.Services, queueDepth int, shrinkwrap, quietBuild bool
263267
combinedExtraPaths,
264268
remoteBuilder,
265269
payloadSecretPath,
270+
forcePull,
266271
)
267272

268273
if err != nil {

commands/publish.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func init() {
5252
publishCmd.Flags().BoolVar(&resetQemu, "reset-qemu", false, "Runs \"docker run multiarch/qemu-user-static --reset -p yes\" to enable multi-arch builds. Compatible with AMD64 machines only.")
5353
publishCmd.Flags().StringVar(&remoteBuilder, "remote-builder", "", "URL to the builder")
5454
publishCmd.Flags().StringVar(&payloadSecretPath, "payload-secret", "", "Path to payload secret file")
55+
publishCmd.Flags().BoolVar(&forcePull, "pull", false, "Force a re-pull of base images in template during build, useful for publishing images")
5556

5657
// Set bash-completion.
5758
_ = publishCmd.Flags().SetAnnotation("handler", cobra.BashCompSubdirsInDir, []string{})
@@ -257,6 +258,7 @@ func publish(services *stack.Services, queueDepth int, shrinkwrap, quietBuild, m
257258
extraTags,
258259
remoteBuilder,
259260
payloadSecretPath,
261+
forcePull,
260262
)
261263

262264
if err != nil {

0 commit comments

Comments
 (0)