Skip to content

Commit 64ba17b

Browse files
authored
feat: add possibility to disable pushing of image in deploy command (#739)
* feat: add possibility to disable pushing of image in `deploy` command Signed-off-by: Zbynek Roubalik <[email protected]> * remove commented code Signed-off-by: Zbynek Roubalik <[email protected]> * incorporate feedback Signed-off-by: Zbynek Roubalik <[email protected]>
1 parent 2282cf8 commit 64ba17b

File tree

4 files changed

+84
-30
lines changed

4 files changed

+84
-30
lines changed

client.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,12 @@ func (c *Client) New(ctx context.Context, cfg Function) (err error) {
424424
return
425425
}
426426

427+
// Push the produced function image
428+
c.progressListener.Increment("Pushing container image to registry")
429+
if err = c.Push(ctx, f.Root); err != nil {
430+
return
431+
}
432+
427433
// Deploy the initialized Function, returning its publicly
428434
// addressible name for possible registration.
429435
c.progressListener.Increment("Deploying Function to cluster")
@@ -602,10 +608,6 @@ func (c *Client) Deploy(ctx context.Context, path string) (err error) {
602608
return ErrNotBuilt
603609
}
604610

605-
if err = c.Push(ctx, &f); err != nil {
606-
return
607-
}
608-
609611
// Deploy a new or Update the previously-deployed Function
610612
c.progressListener.Increment("Deploying function to the cluster")
611613
result, err := c.deployer.Deploy(ctx, f)
@@ -717,8 +719,17 @@ func (c *Client) Emit(ctx context.Context, endpoint string) error {
717719
}
718720

719721
// Push the image for the named service to the configured registry
720-
func (c *Client) Push(ctx context.Context, f *Function) (err error) {
721-
imageDigest, err := c.pusher.Push(ctx, *f)
722+
func (c *Client) Push(ctx context.Context, path string) (err error) {
723+
f, err := NewFunction(path)
724+
if err != nil {
725+
return
726+
}
727+
728+
if !f.Built() {
729+
return ErrNotBuilt
730+
}
731+
732+
imageDigest, err := c.pusher.Push(ctx, f)
722733
if err != nil {
723734
return
724735
}

cmd/build.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,31 @@ func newBuildClient(cfg buildConfig) (*fn.Client, error) {
2929
builder := buildpacks.NewBuilder()
3030
listener := progress.New()
3131

32-
pusherOption := fn.WithPusher(nil)
32+
var (
33+
pusher *docker.Pusher
34+
err error
35+
)
3336
if cfg.Push {
3437
credentialsProvider := creds.NewCredentialsProvider(
3538
creds.WithPromptForCredentials(newPromptForCredentials()),
3639
creds.WithPromptForCredentialStore(newPromptForCredentialStore()),
3740
creds.WithTransport(cfg.Transport))
38-
pusher, err := docker.NewPusher(
41+
pusher, err = docker.NewPusher(
3942
docker.WithCredentialsProvider(credentialsProvider),
4043
docker.WithProgressListener(listener),
4144
docker.WithTransport(cfg.Transport))
42-
4345
if err != nil {
4446
return nil, err
4547
}
4648
pusher.Verbose = cfg.Verbose
47-
pusherOption = fn.WithPusher(pusher)
4849
}
50+
4951
builder.Verbose = cfg.Verbose
5052
listener.Verbose = cfg.Verbose
5153

5254
return fn.New(
5355
fn.WithBuilder(builder),
54-
// fn.WithPusher(pusher),
55-
pusherOption,
56+
fn.WithPusher(pusher),
5657
fn.WithProgressListener(listener),
5758
fn.WithRegistry(cfg.Registry), // for image name when --image not provided
5859
fn.WithVerbose(cfg.Verbose)), nil
@@ -201,7 +202,7 @@ func runBuild(cmd *cobra.Command, _ []string, clientFn buildClientFn) (err error
201202

202203
err = client.Build(cmd.Context(), config.Path)
203204
if err == nil && config.Push {
204-
err = client.Push(cmd.Context(), &function)
205+
err = client.Push(cmd.Context(), config.Path)
205206
}
206207
return
207208
}

cmd/build_test.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,31 +144,57 @@ created: 2009-11-10 23:00:00`,
144144

145145
func Test_newBuildClient(t *testing.T) {
146146
tests := []struct {
147-
name string
148-
cfg buildConfig
149-
succeed bool
147+
name string
148+
cfg buildConfig
149+
fileContents string
150+
succeed bool
150151
}{
151152
{
152153
name: "push flag set to false avoids pusher instanciation",
153154
cfg: buildConfig{
154155
Push: false,
155156
},
157+
fileContents: `name: test-func
158+
runtime: go
159+
image: registry.io/foo/bar:latest
160+
imageDigest: sha256:1111111111111111111111
161+
created: 2009-11-10 23:00:00`,
156162
succeed: false,
157163
},
158164
}
159165

160166
for _, tt := range tests {
161167
t.Run(tt.name, func(t *testing.T) {
168+
169+
tempDir, err := os.MkdirTemp("", "func-tests")
170+
if err != nil {
171+
t.Fatalf("temp dir couldn't be created %v", err)
172+
}
173+
t.Cleanup(func() {
174+
os.RemoveAll(tempDir)
175+
})
176+
177+
fullPath := tempDir + "/func.yaml"
178+
tempFile, err := os.Create(fullPath)
179+
if err != nil {
180+
t.Fatalf("temp file couldn't be created %v", err)
181+
}
182+
_, err = tempFile.WriteString(tt.fileContents)
183+
if err != nil {
184+
t.Fatalf("file content was not written %v", err)
185+
}
186+
162187
client, err := newBuildClient(tt.cfg)
163188
if err != nil {
164189
t.Error(err)
165190
}
191+
166192
defer func() {
167193
if r := recover(); r != nil && tt.succeed {
168194
t.Errorf("expected function call to succeed %v, got actually %v", tt.succeed, r)
169195
}
170196
}()
171-
err = client.Push(context.TODO(), &fn.Function{})
197+
err = client.Push(context.TODO(), tempDir)
172198
if err != nil {
173199
t.Error(err)
174200
}

cmd/deploy.go

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,25 @@ func init() {
2727

2828
func newDeployClient(cfg deployConfig) (*fn.Client, error) {
2929
listener := progress.New()
30-
3130
builder := buildpacks.NewBuilder()
3231

33-
credentialsProvider := creds.NewCredentialsProvider(
34-
creds.WithPromptForCredentials(newPromptForCredentials()),
35-
creds.WithPromptForCredentialStore(newPromptForCredentialStore()),
36-
creds.WithTransport(cfg.Transport))
37-
pusher, err := docker.NewPusher(
38-
docker.WithCredentialsProvider(credentialsProvider),
39-
docker.WithProgressListener(listener),
40-
docker.WithTransport(cfg.Transport))
41-
if err != nil {
42-
return nil, err
32+
var (
33+
pusher *docker.Pusher
34+
err error
35+
)
36+
if cfg.Push {
37+
credentialsProvider := creds.NewCredentialsProvider(
38+
creds.WithPromptForCredentials(newPromptForCredentials()),
39+
creds.WithPromptForCredentialStore(newPromptForCredentialStore()),
40+
creds.WithTransport(cfg.Transport))
41+
pusher, err = docker.NewPusher(
42+
docker.WithCredentialsProvider(credentialsProvider),
43+
docker.WithProgressListener(listener),
44+
docker.WithTransport(cfg.Transport))
45+
if err != nil {
46+
return nil, err
47+
}
48+
pusher.Verbose = cfg.Verbose
4349
}
4450

4551
deployer, err := knative.NewDeployer(cfg.Namespace)
@@ -49,7 +55,6 @@ func newDeployClient(cfg deployConfig) (*fn.Client, error) {
4955

5056
listener.Verbose = cfg.Verbose
5157
builder.Verbose = cfg.Verbose
52-
pusher.Verbose = cfg.Verbose
5358
deployer.Verbose = cfg.Verbose
5459

5560
return fn.New(
@@ -90,7 +95,7 @@ kn func deploy --registry quay.io/myuser
9095
kn func deploy --image quay.io/myuser/myfunc -n myns
9196
`,
9297
SuggestFor: []string{"delpoy", "deplyo"},
93-
PreRunE: bindEnv("image", "namespace", "path", "registry", "confirm", "build"),
98+
PreRunE: bindEnv("image", "namespace", "path", "registry", "confirm", "build", "push"),
9499
}
95100

96101
cmd.Flags().BoolP("confirm", "c", false, "Prompt to confirm all configuration options (Env: $FUNC_CONFIRM)")
@@ -100,6 +105,7 @@ kn func deploy --image quay.io/myuser/myfunc -n myns
100105
cmd.Flags().StringP("image", "i", "", "Full image name in the form [registry]/[namespace]/[name]:[tag] (optional). This option takes precedence over --registry (Env: $FUNC_IMAGE)")
101106
cmd.Flags().StringP("registry", "r", "", "Registry + namespace part of the image to build, ex 'quay.io/myuser'. The full image name is automatically determined based on the local directory name. If not provided the registry will be taken from func.yaml (Env: $FUNC_REGISTRY)")
102107
cmd.Flags().BoolP("build", "b", true, "Build the image before deploying (Env: $FUNC_BUILD)")
108+
cmd.Flags().BoolP("push", "u", true, "Attempt to push the function image to registry before deploying (Env: $FUNC_PUSH)")
103109
setPathFlag(cmd)
104110
setNamespaceFlag(cmd)
105111

@@ -198,6 +204,12 @@ func runDeploy(cmd *cobra.Command, _ []string, clientFn deployClientFn) (err err
198204
}
199205
}
200206

207+
if config.Push {
208+
if err := client.Push(cmd.Context(), config.Path); err != nil {
209+
return err
210+
}
211+
}
212+
201213
return client.Deploy(cmd.Context(), config.Path)
202214

203215
// NOTE: Namespace is optional, default is that used by k8s client
@@ -289,6 +301,9 @@ type deployConfig struct {
289301
// Build the associated Function before deploying.
290302
Build bool
291303

304+
// Push function image to the registry before deploying.
305+
Push bool
306+
292307
// Envs passed via cmd to be added/updated
293308
EnvToUpdate *util.OrderedMap
294309

@@ -313,6 +328,7 @@ func newDeployConfig(cmd *cobra.Command) (deployConfig, error) {
313328
Verbose: viper.GetBool("verbose"), // defined on root
314329
Confirm: viper.GetBool("confirm"),
315330
Build: viper.GetBool("build"),
331+
Push: viper.GetBool("push"),
316332
EnvToUpdate: envToUpdate,
317333
EnvToRemove: envToRemove,
318334
}, nil

0 commit comments

Comments
 (0)