Skip to content

Commit 11440e2

Browse files
committed
FnCLI fn deploy
1 parent 06e5e0d commit 11440e2

File tree

4 files changed

+67
-13
lines changed

4 files changed

+67
-13
lines changed

commands/deploy.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,9 +390,12 @@ func (p *deploycmd) deployFuncV20180708(c *cli.Context, app *models.App, funcfil
390390
if !p.local {
391391
// fetch the architectures
392392
shape = app.Shape
393+
fmt.Printf("*****shape is %v *******", shape)
393394
if shape == "" {
395+
fmt.Printf("shape is default")
394396
shape = common.DefaultAppShape
395397
app.Shape = shape
398+
//shape = "GENERIC_ARM"
396399
}
397400

398401
if _, ok := common.ShapeMap[shape]; !ok {

common/common.go

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"os/exec"
3333
"os/signal"
3434
"path/filepath"
35+
"runtime"
3536
"strings"
3637
"time"
3738
"unicode"
@@ -55,6 +56,7 @@ const (
5556
MinRequiredDockerVersion = "17.5.0"
5657
BuildxBuilderInstance = "oci_fn_builder"
5758
DefaultAppShape = modelsv2.AppShapeGENERICX86
59+
containerEngineTypeDocker = "docker"
5860
)
5961

6062
var GlobalVerbose bool
@@ -66,6 +68,12 @@ var ShapeMap = map[string][]string{
6668
modelsv2.AppShapeGENERICX86ARM: {"linux/amd64", "linux/arm64"},
6769
}
6870

71+
var TargetPlatformMap = map[string][]string{
72+
modelsv2.AppShapeGENERICX86: {"amd64"},
73+
modelsv2.AppShapeGENERICARM: {"arm64"},
74+
modelsv2.AppShapeGENERICX86ARM: {"amd64_arm64"},
75+
}
76+
6977
func IsVerbose() bool {
7078
return GlobalVerbose || CommandVerbose
7179
}
@@ -408,15 +416,18 @@ func containerEngineBuildV20180708(verbose bool, fpath string, ff *FuncFileV2018
408416
return nil
409417
}
410418

411-
func buildXDockerCommand(imageName, dockerfile string, buildArgs []string, noCache bool, architectures []string) []string {
419+
func buildXDockerCommand(imageName, dockerfile string, buildArgs []string, noCache bool, architectures []string, containerEngineType string) []string {
412420
var buildCommand = "buildx"
413421
var name = imageName
414422

423+
plat:= strings.Join(architectures,"," )
424+
fmt.Println("buildX platform is %v", plat)
415425
args := []string{
416426
buildCommand,
417427
"build",
418428
"-t", name,
419429
"-f", dockerfile,
430+
//"--load",
420431
"--platform", strings.Join(architectures, ","),
421432
}
422433

@@ -435,7 +446,15 @@ func buildXDockerCommand(imageName, dockerfile string, buildArgs []string, noCac
435446
var label = "imageName=" + imageName
436447
args = append(args, "--build-arg", arg)
437448
args = append(args, "--label", label)
449+
}
450+
451+
if containerEngineType != containerEngineTypeDocker {
452+
fmt.Println("*** engine type not docker append load")
453+
args = append(args, "--load")
454+
} else {
455+
fmt.Println("*** engine type docker append push")
438456
args = append(args, "--push")
457+
439458
}
440459

441460
args = append(args,
@@ -512,20 +531,30 @@ func RunBuild(verbose bool, dir, imageName, dockerfile string, buildArgs []strin
512531
go func(done chan<- error) {
513532
var dockerBuildCmdArgs []string
514533
// Depending whether architecture list is passed or not trigger docker buildx or docker build accordingly
515-
var mappedArchitectures []string
534+
516535
if arch, ok := ShapeMap[shape]; ok {
536+
var mappedArchitectures []string
517537
mappedArchitectures = append(mappedArchitectures, arch...)
518-
err := initializeContainerBuilder(containerEngineType, mappedArchitectures)
519-
if err != nil {
520-
done <- err
521-
return
538+
var hostedPlatform = runtime.GOARCH
539+
if platform, ok := TargetPlatformMap[shape]; ok {
540+
// create target platform string to compare with hosted platform
541+
targetPlatform := strings.Join(platform," ")
542+
fmt.Println("hosted platform %v target platform %v", hostedPlatform, targetPlatform)
543+
if targetPlatform != hostedPlatform {
544+
fmt.Println("TargetedPlatform and hostPlatform are not same")
545+
err := initializeContainerBuilder(containerEngineType, mappedArchitectures)
546+
if err != nil {
547+
done <- err
548+
return
549+
}
550+
dockerBuildCmdArgs = buildXDockerCommand(imageName, dockerfile, buildArgs, noCache, mappedArchitectures, containerEngineType )
551+
// perform cleanup
552+
defer cleanupContainerBuilder(containerEngineType)
553+
} else {
554+
fmt.Println("TargetedPlatform and hostPlatform are same")
555+
dockerBuildCmdArgs = buildDockerCommand(imageName, dockerfile, buildArgs, noCache)
556+
}
522557
}
523-
524-
dockerBuildCmdArgs = buildXDockerCommand(imageName, dockerfile, buildArgs, noCache, mappedArchitectures)
525-
// perform cleanup
526-
defer cleanupContainerBuilder(containerEngineType)
527-
} else {
528-
dockerBuildCmdArgs = buildDockerCommand(imageName, dockerfile, buildArgs, noCache)
529558
}
530559

531560
cmd := exec.Command(containerEngineType, dockerBuildCmdArgs...)
@@ -551,6 +580,18 @@ func RunBuild(verbose bool, dir, imageName, dockerfile string, buildArgs []strin
551580
fmt.Fprintln(os.Stderr)
552581
return fmt.Errorf("build cancelled on signal %v", signal)
553582
}
583+
if containerEngineType != containerEngineTypeDocker {
584+
fmt.Println("Using Container engine", containerEngineType, "to push as --push id only for docker and --load is for podman")
585+
// Push to docker registry
586+
fmt.Println("Using Container engine", containerEngineType, "to push")
587+
fmt.Printf("Pushing %v to docker registry...", imageName)
588+
cmd := exec.Command(containerEngineType, "push", imageName)
589+
cmd.Stderr = os.Stderr
590+
cmd.Stdout = os.Stdout
591+
if err := cmd.Run(); err != nil {
592+
return fmt.Errorf("error running %v push, are you logged?: %v", containerEngineType, err)
593+
}
594+
}
554595
return nil
555596
}
556597

@@ -620,6 +661,10 @@ func isSupportedByDefaultBuildxPlatforms(containerEngineType string, platforms [
620661

621662
func initializeContainerBuilder(containerEngineType string, platforms []string) error {
622663

664+
if containerEngineType != containerEngineTypeDocker {
665+
fmt.Println("engine type not docker return nil")
666+
return nil
667+
}
623668
if isSupportedByDefaultBuildxPlatforms(containerEngineType, platforms) {
624669
return nil
625670
}

objects/app/apps.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,11 @@ func (a *appsCmd) create(c *cli.Context) error {
152152
}
153153

154154
appWithFlags(c, app)
155+
155156
// If architectures flag is not set then default it to nil
156157
if c.IsSet(SHAPE_PARAMETER) {
157158
shapeParam := c.String(SHAPE_PARAMETER)
158-
159+
fmt.Printf("in apps.go shape parameter is set and is %v", shapeParam)
159160
// Check for architectures parameter passed or set to default
160161
if len(shapeParam) == 0 {
161162
return errors.New("no shape specified for the application")
@@ -165,6 +166,8 @@ func (a *appsCmd) create(c *cli.Context) error {
165166
return errors.New("invalid shape specified for the application")
166167
}
167168
app.Shape = shapeParam
169+
} else {
170+
fmt.Printf("in apps.go shape parameter is not set")
168171
}
169172
_, err := CreateApp(a.client, app)
170173
return err
@@ -178,12 +181,14 @@ func CreateApp(a *fnclient.Fn, app *modelsv2.App) (*modelsv2.App, error) {
178181
})
179182

180183
if err != nil {
184+
181185
switch e := err.(type) {
182186
case *apiapps.CreateAppBadRequest:
183187
err = fmt.Errorf("%v", e.Payload.Message)
184188
case *apiapps.CreateAppConflict:
185189
err = fmt.Errorf("%v", e.Payload.Message)
186190
}
191+
//fmt.Println("create App error is %v", err)
187192
return nil, err
188193
}
189194
fmt.Println("Successfully created app: ", resp.Payload.Name)

objects/app/commands.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func Create() cli.Command {
5757
Usage: "Syslog URL to send application logs to",
5858
},
5959
cli.StringFlag{
60+
//cli.StringSliceFlag{
6061
Name: "shape",
6162
Usage: "Valid values are GENERIC_X86, GENERIC_ARM and GENERIC_X86_ARM. Default is GENERIC_X86. Setting this to GENERIC_X86, will run the functions in the application on X86 processor architecture.\n Setting this to GENERIC_ARM, will run the functions in the application on ARM processor architecture.\n When set to 'GENERIC_X86_ARM', functions in the application are run on either X86 or ARM processor architecture.\n Accepted values are:\n GENERIC_X86, GENERIC_ARM, GENERIC_X86_ARM",
6263
},

0 commit comments

Comments
 (0)