Skip to content

Commit

Permalink
Handle duplicate archive and package specs; handle multifile archives…
Browse files Browse the repository at this point in the history
… better (fission#1018)

Archives with multiple files should have all files in the inputs.
Also clean up some inconsistent variable naming -- archive name,
archive file path, and archive input files were all stored in
incorrectly-named variables.
  • Loading branch information
soamvasani authored and life1347 committed Dec 11, 2018
1 parent f0c0936 commit 2699aa6
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 92 deletions.
23 changes: 15 additions & 8 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ import (
"strings"
"syscall"

log "github.com/sirupsen/logrus"

"github.com/gorilla/handlers"
"github.com/imdario/mergo"
"github.com/mholt/archiver"
uuid "github.com/satori/go.uuid"
"github.com/satori/go.uuid"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -144,18 +142,27 @@ func GetTempDir() (string, error) {
return dir, err
}

func MakeArchive(targetName string, globs ...string) (string, error) {
// FindAllGlobs returns a list of globs of input list.
func FindAllGlobs(inputList []string) ([]string, error) {
files := make([]string, 0)
for _, glob := range globs {
for _, glob := range inputList {
f, err := filepath.Glob(glob)
if err != nil {
log.Warn(fmt.Sprintf("Invalid glob %v: %v", glob, err))
return "", err
return nil, fmt.Errorf("Invalid glob %v: %v", glob, err)
}
files = append(files, f...)
}
return files, nil
}

func MakeArchive(targetName string, globs ...string) (string, error) {
files, err := FindAllGlobs(globs)
if err != nil {
return "", err
}

// zip up the file list
err := archiver.Zip.Make(targetName, files)
err = archiver.Zip.Make(targetName, files)
if err != nil {
return "", err
}
Expand Down
31 changes: 16 additions & 15 deletions fission/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ func fnCreate(c *cli.Context) error {
spec = true
specFile = fmt.Sprintf("function-%v.yaml", fnName)
}
specDir := getSpecDir(c)

// check for unique function names within a namespace
fnList, err := client.FunctionList(fnNamespace)
Expand Down Expand Up @@ -252,25 +253,25 @@ func fnCreate(c *cli.Context) error {
}
}

srcArchiveName := c.StringSlice("src")
var deployArchiveName []string
codeFlag := false
srcArchiveFiles := c.StringSlice("src")
var deployArchiveFiles []string
noZip := false
code := c.String("code")
if len(code) == 0 {
deployArchiveName = c.StringSlice("deploy")
deployArchiveFiles = c.StringSlice("deploy")
} else {
deployArchiveName = append(deployArchiveName, c.String("code"))
codeFlag = true
deployArchiveFiles = append(deployArchiveFiles, c.String("code"))
noZip = true
}
// fatal when both src & deploy archive are empty
if len(srcArchiveName) == 0 && len(deployArchiveName) == 0 {
if len(srcArchiveFiles) == 0 && len(deployArchiveFiles) == 0 {
log.Fatal("Need --deploy or --src argument.")
}

buildcmd := c.String("buildcmd")

// create new package in the same namespace as the function.
pkgMetadata = createPackage(client, fnNamespace, envName, envNamespace, srcArchiveName, deployArchiveName, buildcmd, specFile, codeFlag)
pkgMetadata = createPackage(client, fnNamespace, envName, envNamespace, srcArchiveFiles, deployArchiveFiles, buildcmd, specDir, specFile, noZip)

fmt.Printf("package '%v' created\n", pkgMetadata.Name)
}
Expand Down Expand Up @@ -471,17 +472,17 @@ func fnUpdate(c *cli.Context) error {
envNamespace = ""
}

var deployArchiveName []string
var deployArchiveFiles []string
codeFlag := false
code := c.String("code")
if len(code) == 0 {
deployArchiveName = c.StringSlice("deploy")
deployArchiveFiles = c.StringSlice("deploy")
} else {
deployArchiveName = append(deployArchiveName, c.String("code"))
deployArchiveFiles = append(deployArchiveFiles, c.String("code"))
codeFlag = true
}

srcArchiveName := c.StringSlice("src")
srcArchiveFiles := c.StringSlice("src")
pkgName := c.String("pkg")
entrypoint := c.String("entrypoint")
buildcmd := c.String("buildcmd")
Expand All @@ -490,7 +491,7 @@ func fnUpdate(c *cli.Context) error {
secretName := c.String("secret")
cfgMapName := c.String("configmap")

if len(srcArchiveName) > 0 && len(deployArchiveName) > 0 {
if len(srcArchiveFiles) > 0 && len(deployArchiveFiles) > 0 {
log.Fatal("Need either of --src or --deploy and not both arguments.")
}

Expand Down Expand Up @@ -566,15 +567,15 @@ func fnUpdate(c *cli.Context) error {

pkgMetadata := &pkg.Metadata

if len(deployArchiveName) != 0 || len(srcArchiveName) != 0 || len(buildcmd) != 0 || len(envName) != 0 || len(envNamespace) != 0 {
if len(deployArchiveFiles) != 0 || len(srcArchiveFiles) != 0 || len(buildcmd) != 0 || len(envName) != 0 || len(envNamespace) != 0 {
fnList, err := getFunctionsByPackage(client, pkg.Metadata.Name, pkg.Metadata.Namespace)
util.CheckErr(err, "get function list")

if !force && len(fnList) > 1 {
log.Fatal("Package is used by multiple functions, use --force to force update")
}

pkgMetadata, err = updatePackage(client, pkg, envName, envNamespace, srcArchiveName, deployArchiveName, buildcmd, false, codeFlag)
pkgMetadata, err = updatePackage(client, pkg, envName, envNamespace, srcArchiveFiles, deployArchiveFiles, buildcmd, false, codeFlag)
util.CheckErr(err, fmt.Sprintf("update package '%v'", pkgName))

fmt.Printf("package '%v' updated\n", pkgMetadata.GetName())
Expand Down
2 changes: 1 addition & 1 deletion fission/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func newCliApp() *cli.App {
fnEnvNameFlag := cli.StringFlag{Name: "env", Usage: "environment name for function"}
fnCodeFlag := cli.StringFlag{Name: "code", Usage: "local path or URL for source code"}
fnDeployArchiveFlag := cli.StringSliceFlag{Name: "deployarchive, deploy", Usage: "local path or URL for deployment archive"}
fnSrcArchiveFlag := cli.StringSliceFlag{Name: "sourcearchive, src", Usage: "local path or URL for source archive"}
fnSrcArchiveFlag := cli.StringSliceFlag{Name: "sourcearchive, src, source", Usage: "local path or URL for source archive"}
fnPkgNameFlag := cli.StringFlag{Name: "pkgname, pkg", Usage: "Name of the existing package (--deploy and --src and --env will be ignored), should be in the same namespace as the function"}
fnPodFlag := cli.StringFlag{Name: "pod", Usage: "function pod name, optional (use latest if unspecified)"}
fnFollowFlag := cli.BoolFlag{Name: "follow, f", Usage: "specify if the logs should be streamed"}
Expand Down
Loading

0 comments on commit 2699aa6

Please sign in to comment.