Skip to content

Commit

Permalink
fix, simplify
Browse files Browse the repository at this point in the history
Signed-off-by: David Fridrich <dfridric@dfridric-thinkpadp16vgen1.tpb.csb>
  • Loading branch information
David Fridrich committed Aug 26, 2024
1 parent 9bc91a2 commit 1ceeadc
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 33 deletions.
50 changes: 31 additions & 19 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,16 +298,24 @@ func runDeploy(cmd *cobra.Command, newClient ClientFactory) (err error) {
return
}

// Preprocess image name. Validate the image and check whether its digested
// This might alter f.Deploy.Image.
var digested bool
f, digested, err = processImageName(f, cfg.Image)
if err != nil {
return
var (
digested bool
justBuilt bool
justPushed bool
)

// Validate the image and check whether its digested or not
if cfg.Image != "" {
digested, err = isDigested(cfg.Image)
if err != nil {
return
}
// image is valid and undigested
if !digested {
f.Deploy.Image = cfg.Image
}
}

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 @@ -323,9 +331,9 @@ func runDeploy(cmd *cobra.Command, newClient ClientFactory) (err error) {
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?
// TODO: gauron99 - temporary fix for undigested image direct deploy
// (w/out build) This might be more complex to do than leaving like this
// image digests are created via the registry on push.
fmt.Printf("justBuilt '%v'; justPushed '%v'\n", justBuilt, justPushed)
fmt.Printf("builtImage '%v'; deployimage '%v'\n", f.Build.Image, f.Deploy.Image)
if (justBuilt || justPushed) && f.Build.Image != "" {
Expand Down Expand Up @@ -822,10 +830,10 @@ func isDigested(v string) (validDigest bool, err error) {
vv := strings.Split(v, "@")
if len(vv) < 2 {
// image does NOT have a digest, validate further
// if v == "" {
// err = fmt.Errorf("provided image is empty, cannot validate")
// return
// }
if v == "" {
err = fmt.Errorf("provided image is empty, cannot validate")
return
}
vvv := strings.Split(v, ":")
if len(vvv) < 2 {
// assume user knows what hes doing
Expand Down Expand Up @@ -866,19 +874,23 @@ func isDigested(v string) (validDigest bool, err error) {
// fields of Function structure are populated if needed.
// Returns a Function structure(1), bool indicating if image was given with
// digest(2) and error(3)
func processImageName(fin fn.Function, configImage string) (f fn.Function, digested bool, err error) {
f = fin
func processImageName(f fn.Function, configImage string) (fn.Function, bool, error) {

Check failure on line 877 in cmd/deploy.go

View workflow job for this annotation

GitHub Actions / style / Golang / Lint

func `processImageName` is unused (unused)
var (
digested bool
err error
)

// check if --image was provided with a digest. 'digested' bool indicates if
// image contains a digest or not (image is "digested").
digested, err = isDigested(configImage)
// image is digested, no need to process further || error occurred
if digested || err != nil {
return
return f, digested, err
}

// assign valid, undigested image as deployed image before any other changes.
// This can be overridden when build&push=enabled with freshly built image
// OR directly deployed when build&push=disabled as is.
f.Deploy.Image = configImage
return
return f, digested, err
}
34 changes: 20 additions & 14 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,28 +172,28 @@ func runRun(cmd *cobra.Command, newClient ClientFactory) (err error) {
client, done := newClient(ClientConfig{Verbose: cfg.Verbose}, clientOptions...)
defer done()

var (
digested bool
justBuilt bool
)

// if image was specified, check if its digested and do basic validation
if cfg.Image != "" {
digested, err = isDigested(cfg.Image)
if err != nil {
return err
}
}
// Build
//
// If requesting to run via the container, build the container if it is
// either out-of-date or a build was explicitly requested.
if cfg.Container {
var (
digested bool
justBuilt bool
)

buildOptions, err := cfg.buildOptions()
if err != nil {
return err
}

// if image was specified, check if its digested and do basic validation
if cfg.Image != "" {
digested, err = isDigested(cfg.Image)
if err != nil {
return err
}
}
if digested {
// run cmd takes f.Build.Image - see newContainerConfig in docker/runner.go
// it doesnt get saved, just runtime image
Expand All @@ -207,8 +207,14 @@ func runRun(cmd *cobra.Command, newClient ClientFactory) (err error) {
}
}
} else { // dont run digested image without a container
if digested {
return fmt.Errorf("cannot use digested image with --container=false")
if cfg.Image != "" {
digested, err := isDigested(cfg.Image)
if err != nil {
return err
}
if digested {
return fmt.Errorf("cannot use digested image with --container=false")
}
}
}

Expand Down

0 comments on commit 1ceeadc

Please sign in to comment.