Skip to content

Commit

Permalink
fix build/deploy image passing bug add test
Browse files Browse the repository at this point in the history
Signed-off-by: gauron99 <fridrich.david19@gmail.com>
  • Loading branch information
gauron99 committed Aug 6, 2024
1 parent 66729d4 commit 8ee05f7
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 16 deletions.
2 changes: 1 addition & 1 deletion cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func runBuild(cmd *cobra.Command, _ []string, newClient ClientFactory) (err erro
return
}
if cfg.Push {
if f, err = client.Push(cmd.Context(), f); err != nil {
if f, _, err = client.Push(cmd.Context(), f); err != nil {
return
}
}
Expand Down
13 changes: 6 additions & 7 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func runDeploy(cmd *cobra.Command, newClient ClientFactory) (err error) {
}

var justBuilt bool

var justPushed bool
// If user provided --image with digest, they are requesting that specific
// image to be used which means building phase should be skipped and image
// should be deployed as is
Expand All @@ -319,19 +319,18 @@ func runDeploy(cmd *cobra.Command, newClient ClientFactory) (err error) {
return
}
if cfg.Push {
if f, err = client.Push(cmd.Context(), f); err != nil {
if f, justPushed, err = client.Push(cmd.Context(), f); err != nil {
return
}
}
// TODO: gauron99 - temporary fix for undigested image direct deploy (w/out
// build) I think we will be able to remove this after we clean up the
// building process - move the setting of built image in building phase?
if justBuilt && f.Build.Image != "" {
if (justBuilt || justPushed) && f.Build.Image != "" {
// f.Build.Image is set in Push for now, just set it as a deployed image
f.Deploy.Image = f.Build.Image
}
}

if f, err = client.Deploy(cmd.Context(), f, fn.WithDeploySkipBuildCheck(cfg.Build == "false")); err != nil {
return
}
Expand Down Expand Up @@ -370,11 +369,11 @@ func build(cmd *cobra.Command, flag string, f fn.Function, client *fn.Client, bu
if f, err = client.Build(cmd.Context(), f, buildOptions...); err != nil {
return f, false, err
}
} else if !build {
return f, false, nil
} else if _, err = strconv.ParseBool(flag); err != nil {
return f, false, fmt.Errorf("--build ($FUNC_BUILD) %q not recognized. Should be 'auto' or a truthy value such as 'true', 'false', '0', or '1'.", flag)

} else if !build {
fmt.Printf("didnt currently build anything but no errors either\n")
return f, false, nil
}
return f, true, nil
}
Expand Down
49 changes: 49 additions & 0 deletions cmd/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2017,3 +2017,52 @@ func TestDeploy_WithoutHome(t *testing.T) {
t.Fatal(err)
}
}

func TestDeploy_AfterBuild(t *testing.T) {
var (
root = FromTempDirectory(t)
ns = "myns"
)
f := fn.Function{
Runtime: "go",
Root: root,
}
_, err := fn.New().Init(f)
if err != nil {
t.Fatal(err)
}
// prebuild function
cmd := NewBuildCmd(NewTestClient(
fn.WithBuilder(mock.NewBuilder()),
fn.WithRegistry(TestRegistry)))

if err = cmd.Execute(); err != nil {
t.Fatal(err)
}

deployer := mock.NewDeployer()
deployer.DeployFn = func(_ context.Context, f fn.Function) (result fn.DeploymentResult, err error) {
fmt.Printf("IMAGE IN DEPLOYER IS %v\n", f.Deploy.Image)
if f.Deploy.Image == "" {
return fn.DeploymentResult{}, fmt.Errorf("image was not set for deployer")
}
if f.Namespace != "" {
result.Namespace = f.Namespace // deployed to that requested
} else if f.Deploy.Namespace != "" {
result.Namespace = f.Deploy.Namespace // redeploy to current
} else {
err = errors.New("namespace required for initial deployment")
}
return
}

// Deploy the function
cmd = NewDeployCmd(NewTestClient(
fn.WithDeployer(deployer),
fn.WithRegistry(TestRegistry)))
cmd.SetArgs([]string{fmt.Sprintf("--namespace=%s", ns), "--build=false"})

if err := cmd.Execute(); err != nil {
t.Fatal(err)
}
}
16 changes: 10 additions & 6 deletions pkg/functions/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ func (c *Client) Update(ctx context.Context, f Function) (string, Function, erro
if f, err = c.Build(ctx, f); err != nil {
return "", f, err
}
if f, err = c.Push(ctx, f); err != nil {
if f, _, err = c.Push(ctx, f); err != nil {
return "", f, err
}

Expand Down Expand Up @@ -505,7 +505,7 @@ func (c *Client) New(ctx context.Context, cfg Function) (string, Function, error
// Push the produced function image
fmt.Fprintf(os.Stderr, "Pushing container image to registry\n")

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

Expand Down Expand Up @@ -739,6 +739,8 @@ func WithDeploySkipBuildCheck(skipBuiltCheck bool) DeployOption {
// Errors if the function has not been built unless explicitly instructed
// to ignore this build check.
func (c *Client) Deploy(ctx context.Context, f Function, oo ...DeployOption) (Function, error) {
fmt.Fprintf(os.Stderr, ">> deploying image: %s\n", f.Deploy.Image)

options := &DeployOptions{}
for _, o := range oo {
o(options)
Expand Down Expand Up @@ -1071,15 +1073,17 @@ func (c *Client) Invoke(ctx context.Context, root string, target string, m Invok
}

// Push the image for the named service to the configured registry
func (c *Client) Push(ctx context.Context, f Function) (Function, error) {
// returns in this order: 1)Function structure 2)bool indicating if push succeeded
// 3) error
func (c *Client) Push(ctx context.Context, f Function) (Function, bool, error) {
if !f.Built() {
return f, ErrNotBuilt
return f, false, ErrNotBuilt
}
var err error

imageDigest, err := c.pusher.Push(ctx, f)
if err != nil {
return f, err
return f, false, err
}

// TODO: gauron99 - this is here because of a temporary workaround.
Expand All @@ -1089,7 +1093,7 @@ func (c *Client) Push(ctx context.Context, f Function) (Function, error) {
// the full image name and its digest right after building
f.Build.Image = f.ImageNameWithDigest(imageDigest)

return f, err
return f, true, err
}

// ensureRunDataDir creates a .func directory at the given path, and
Expand Down
4 changes: 2 additions & 2 deletions pkg/oci/pusher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestPusher_Push(t *testing.T) {
t.Fatal(err)
}

if _, err = client.Push(context.Background(), f); err != nil {
if _, _, err = client.Push(context.Background(), f); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -152,7 +152,7 @@ func TestPusher_BasicAuth(t *testing.T) {
ctx = context.WithValue(ctx, fn.PushUsernameKey{}, username)
ctx = context.WithValue(ctx, fn.PushPasswordKey{}, password)

if _, err = client.Push(ctx, f); err != nil {
if _, _, err = client.Push(ctx, f); err != nil {
t.Fatal(err)
}

Expand Down

0 comments on commit 8ee05f7

Please sign in to comment.