Skip to content

feat: add possibility to disable pushing of image in deploy command #736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,12 @@ func (c *Client) New(ctx context.Context, cfg Function) (err error) {
return
}

// Push the produced function image
c.progressListener.Increment("Pushing container image to registry")
if err = c.Push(ctx, f.Root); err != nil {
return
}

// Deploy the initialized Function, returning its publicly
// addressible name for possible registration.
c.progressListener.Increment("Deploying Function to cluster")
Expand Down Expand Up @@ -602,10 +608,6 @@ func (c *Client) Deploy(ctx context.Context, path string) (err error) {
return ErrNotBuilt
}

if err = c.Push(ctx, &f); err != nil {
return
}

// Deploy a new or Update the previously-deployed Function
c.progressListener.Increment("Deploying function to the cluster")
result, err := c.deployer.Deploy(ctx, f)
Expand Down Expand Up @@ -717,8 +719,23 @@ func (c *Client) Emit(ctx context.Context, endpoint string) error {
}

// Push the image for the named service to the configured registry
func (c *Client) Push(ctx context.Context, f *Function) (err error) {
imageDigest, err := c.pusher.Push(ctx, *f)
func (c *Client) Push(ctx context.Context, path string) (err error) {
go func() {
<-ctx.Done()
c.progressListener.Stopping()
}()

f, err := NewFunction(path)
if err != nil {
return
}

if !f.Built() {
return ErrNotBuilt
}

c.progressListener.Increment("Pushing function image to the registry")
imageDigest, err := c.pusher.Push(ctx, f)
if err != nil {
return
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func newBuildClient(cfg buildConfig) (*fn.Client, error) {

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

err = client.Build(cmd.Context(), config.Path)
if err == nil && config.Push {
err = client.Push(cmd.Context(), &function)
err = client.Push(cmd.Context(), config.Path)
}
return
}
Expand Down
35 changes: 31 additions & 4 deletions cmd/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,31 +144,58 @@ created: 2009-11-10 23:00:00`,

func Test_newBuildClient(t *testing.T) {
tests := []struct {
name string
cfg buildConfig
succeed bool
name string
cfg buildConfig
fileContents string
succeed bool
}{
{
name: "push flag set to false avoids pusher instanciation",
cfg: buildConfig{
Push: false,
},
fileContents: `name: test-func
runtime: go
image: registry.io/foo/bar:latest
imageDigest: sha256:1111111111111111111111
created: 2009-11-10 23:00:00`,
succeed: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

tempDir, err := os.MkdirTemp("", "func-tests")
if err != nil {
t.Fatalf("temp dir couldn't be created %v", err)
}
// t.Log("tempDir created:", tempDir)
t.Cleanup(func() {
os.RemoveAll(tempDir)
})

fullPath := tempDir + "/func.yaml"
tempFile, err := os.Create(fullPath)
if err != nil {
t.Fatalf("temp file couldn't be created %v", err)
}
_, err = tempFile.WriteString(tt.fileContents)
if err != nil {
t.Fatalf("file content was not written %v", err)
}

client, err := newBuildClient(tt.cfg)
if err != nil {
t.Error(err)
}

defer func() {
if r := recover(); r != nil && tt.succeed {
t.Errorf("expected function call to succeed %v, got actually %v", tt.succeed, r)
}
}()
err = client.Push(context.TODO(), &fn.Function{})
err = client.Push(context.TODO(), tempDir)
if err != nil {
t.Error(err)
}
Expand Down
40 changes: 27 additions & 13 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,23 @@ func init() {

func newDeployClient(cfg deployConfig) (*fn.Client, error) {
listener := progress.New()

builder := buildpacks.NewBuilder()

credentialsProvider := creds.NewCredentialsProvider(
creds.WithPromptForCredentials(newPromptForCredentials()),
creds.WithPromptForCredentialStore(newPromptForCredentialStore()),
creds.WithTransport(cfg.Transport))
pusher, err := docker.NewPusher(
docker.WithCredentialsProvider(credentialsProvider),
docker.WithProgressListener(listener),
docker.WithTransport(cfg.Transport))
if err != nil {
return nil, err
pusherOption := fn.WithPusher(nil)
if cfg.Push {
credentialsProvider := creds.NewCredentialsProvider(
creds.WithPromptForCredentials(newPromptForCredentials()),
creds.WithPromptForCredentialStore(newPromptForCredentialStore()),
creds.WithTransport(cfg.Transport))
pusher, err := docker.NewPusher(
docker.WithCredentialsProvider(credentialsProvider),
docker.WithProgressListener(listener),
docker.WithTransport(cfg.Transport))
if err != nil {
return nil, err
}
pusher.Verbose = cfg.Verbose
pusherOption = fn.WithPusher(pusher)
}

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

listener.Verbose = cfg.Verbose
builder.Verbose = cfg.Verbose
pusher.Verbose = cfg.Verbose
deployer.Verbose = cfg.Verbose

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

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

if config.Push {
if err := client.Push(cmd.Context(), config.Path); err != nil {
return err
}
}

return client.Deploy(cmd.Context(), config.Path)

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

// Push function image to the registry before deploying.
Push bool

// Envs passed via cmd to be added/updated
EnvToUpdate *util.OrderedMap

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