Skip to content

Commit 1b5ccf2

Browse files
author
Zbynek Roubalik
committed
feat: add possibility to disable pushing of image in deploy command
Signed-off-by: Zbynek Roubalik <[email protected]>
1 parent 3177287 commit 1b5ccf2

File tree

4 files changed

+82
-25
lines changed

4 files changed

+82
-25
lines changed

client.go

Lines changed: 23 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,23 @@ 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+
go func() {
724+
<-ctx.Done()
725+
c.progressListener.Stopping()
726+
}()
727+
728+
f, err := NewFunction(path)
729+
if err != nil {
730+
return
731+
}
732+
733+
if !f.Built() {
734+
return ErrNotBuilt
735+
}
736+
737+
c.progressListener.Increment("Pushing function image to the registry")
738+
imageDigest, err := c.pusher.Push(ctx, f)
722739
if err != nil {
723740
return
724741
}

cmd/build.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ func newBuildClient(cfg buildConfig) (*fn.Client, error) {
5151

5252
return fn.New(
5353
fn.WithBuilder(builder),
54-
// fn.WithPusher(pusher),
5554
pusherOption,
5655
fn.WithProgressListener(listener),
5756
fn.WithRegistry(cfg.Registry), // for image name when --image not provided
@@ -201,7 +200,7 @@ func runBuild(cmd *cobra.Command, _ []string, clientFn buildClientFn) (err error
201200

202201
err = client.Build(cmd.Context(), config.Path)
203202
if err == nil && config.Push {
204-
err = client.Push(cmd.Context(), &function)
203+
err = client.Push(cmd.Context(), config.Path)
205204
}
206205
return
207206
}

cmd/build_test.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,31 +144,58 @@ 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.Log("tempDir created:", tempDir)
174+
t.Cleanup(func() {
175+
os.RemoveAll(tempDir)
176+
})
177+
178+
fullPath := tempDir + "/func.yaml"
179+
tempFile, err := os.Create(fullPath)
180+
if err != nil {
181+
t.Fatalf("temp file couldn't be created %v", err)
182+
}
183+
_, err = tempFile.WriteString(tt.fileContents)
184+
if err != nil {
185+
t.Fatalf("file content was not written %v", err)
186+
}
187+
162188
client, err := newBuildClient(tt.cfg)
163189
if err != nil {
164190
t.Error(err)
165191
}
192+
166193
defer func() {
167194
if r := recover(); r != nil && tt.succeed {
168195
t.Errorf("expected function call to succeed %v, got actually %v", tt.succeed, r)
169196
}
170197
}()
171-
err = client.Push(context.TODO(), &fn.Function{})
198+
err = client.Push(context.TODO(), tempDir)
172199
if err != nil {
173200
t.Error(err)
174201
}

cmd/deploy.go

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,23 @@ 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+
pusherOption := fn.WithPusher(nil)
33+
if cfg.Push {
34+
credentialsProvider := creds.NewCredentialsProvider(
35+
creds.WithPromptForCredentials(newPromptForCredentials()),
36+
creds.WithPromptForCredentialStore(newPromptForCredentialStore()),
37+
creds.WithTransport(cfg.Transport))
38+
pusher, err := docker.NewPusher(
39+
docker.WithCredentialsProvider(credentialsProvider),
40+
docker.WithProgressListener(listener),
41+
docker.WithTransport(cfg.Transport))
42+
if err != nil {
43+
return nil, err
44+
}
45+
pusher.Verbose = cfg.Verbose
46+
pusherOption = fn.WithPusher(pusher)
4347
}
4448

4549
deployer, err := knative.NewDeployer(cfg.Namespace)
@@ -49,13 +53,12 @@ func newDeployClient(cfg deployConfig) (*fn.Client, error) {
4953

5054
listener.Verbose = cfg.Verbose
5155
builder.Verbose = cfg.Verbose
52-
pusher.Verbose = cfg.Verbose
5356
deployer.Verbose = cfg.Verbose
5457

5558
return fn.New(
5659
fn.WithProgressListener(listener),
5760
fn.WithBuilder(builder),
58-
fn.WithPusher(pusher),
61+
pusherOption,
5962
fn.WithDeployer(deployer),
6063
fn.WithRegistry(cfg.Registry), // for deriving name when no --image value
6164
fn.WithVerbose(cfg.Verbose),
@@ -100,6 +103,7 @@ kn func deploy --image quay.io/myuser/myfunc -n myns
100103
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)")
101104
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)")
102105
cmd.Flags().BoolP("build", "b", true, "Build the image before deploying (Env: $FUNC_BUILD)")
106+
cmd.Flags().BoolP("push", "u", true, "Attempt to push the function image to registry before deploying.")
103107
setPathFlag(cmd)
104108
setNamespaceFlag(cmd)
105109

@@ -198,6 +202,12 @@ func runDeploy(cmd *cobra.Command, _ []string, clientFn deployClientFn) (err err
198202
}
199203
}
200204

205+
if config.Push {
206+
if err := client.Push(cmd.Context(), config.Path); err != nil {
207+
return err
208+
}
209+
}
210+
201211
return client.Deploy(cmd.Context(), config.Path)
202212

203213
// NOTE: Namespace is optional, default is that used by k8s client
@@ -289,6 +299,9 @@ type deployConfig struct {
289299
// Build the associated Function before deploying.
290300
Build bool
291301

302+
// Push function image to the registry before deploying.
303+
Push bool
304+
292305
// Envs passed via cmd to be added/updated
293306
EnvToUpdate *util.OrderedMap
294307

@@ -313,6 +326,7 @@ func newDeployConfig(cmd *cobra.Command) (deployConfig, error) {
313326
Verbose: viper.GetBool("verbose"), // defined on root
314327
Confirm: viper.GetBool("confirm"),
315328
Build: viper.GetBool("build"),
329+
Push: viper.GetBool("push"),
316330
EnvToUpdate: envToUpdate,
317331
EnvToRemove: envToRemove,
318332
}, nil

0 commit comments

Comments
 (0)