Skip to content

Commit

Permalink
Deploy handles given functionConfig file (#1682)
Browse files Browse the repository at this point in the history
  • Loading branch information
quaark committed May 31, 2020
1 parent 1d0406c commit 05d582b
Show file tree
Hide file tree
Showing 6 changed files with 449 additions and 142 deletions.
36 changes: 21 additions & 15 deletions pkg/nuctl/command/build.go
Expand Up @@ -32,6 +32,9 @@ type buildCommandeer struct {
rootCommandeer *RootCommandeer
commands stringSliceFlag
functionConfig functionconfig.Config
functionConfigPath string
runtime string
handler string
encodedRuntimeAttributes string
encodedCodeEntryAttributes string
outputImageFile string
Expand Down Expand Up @@ -61,6 +64,9 @@ func newBuildCommandeer(rootCommandeer *RootCommandeer) *buildCommandeer {

commandeer.functionConfig.Meta.Namespace = rootCommandeer.namespace
commandeer.functionConfig.Spec.Build.Commands = commandeer.commands
commandeer.functionConfig.Spec.Build.FunctionConfigPath = commandeer.functionConfigPath
commandeer.functionConfig.Spec.Runtime = commandeer.runtime
commandeer.functionConfig.Spec.Handler = commandeer.handler

// decode the JSON build runtime attributes
if err := json.Unmarshal([]byte(commandeer.encodedRuntimeAttributes),
Expand Down Expand Up @@ -89,29 +95,29 @@ func newBuildCommandeer(rootCommandeer *RootCommandeer) *buildCommandeer {
},
}

addBuildFlags(cmd, &commandeer.functionConfig, &commandeer.commands, &commandeer.encodedRuntimeAttributes, &commandeer.encodedCodeEntryAttributes)
addBuildFlags(cmd, &commandeer.functionConfig.Spec.Build, &commandeer.functionConfigPath, &commandeer.runtime, &commandeer.handler, &commandeer.commands, &commandeer.encodedRuntimeAttributes, &commandeer.encodedCodeEntryAttributes)
cmd.Flags().StringVarP(&commandeer.outputImageFile, "output-image-file", "", "", "Path to output container image of the build")

commandeer.cmd = cmd

return commandeer
}

func addBuildFlags(cmd *cobra.Command, config *functionconfig.Config, commands *stringSliceFlag, encodedRuntimeAttributes *string, encodedCodeEntryAttributes *string) { // nolint
cmd.Flags().StringVarP(&config.Spec.Build.Path, "path", "p", "", "Path to the function's source code")
cmd.Flags().StringVarP(&config.Spec.Build.FunctionSourceCode, "source", "", "", "The function's source code (overrides \"path\")")
cmd.Flags().StringVarP(&config.Spec.Build.FunctionConfigPath, "file", "f", "", "Path to a function-configuration file")
cmd.Flags().StringVarP(&config.Spec.Build.Image, "image", "i", "", "Name of a container image (default - the function name)")
cmd.Flags().StringVarP(&config.Spec.Build.Registry, "registry", "r", os.Getenv("NUCTL_REGISTRY"), "URL of a container registry (env: NUCTL_REGISTRY)")
cmd.Flags().StringVarP(&config.Spec.Runtime, "runtime", "", "", "Runtime (for example, \"golang\", \"python:3.6\")")
cmd.Flags().StringVarP(&config.Spec.Handler, "handler", "", "", "Name of a function handler")
cmd.Flags().BoolVarP(&config.Spec.Build.NoBaseImagesPull, "no-pull", "", false, "Don't pull base images - use local versions")
cmd.Flags().BoolVarP(&config.Spec.Build.NoCleanup, "no-cleanup", "", false, "Don't clean up temporary directories")
cmd.Flags().StringVarP(&config.Spec.Build.BaseImage, "base-image", "", "", "Name of the base image (default - per-runtime default)")
func addBuildFlags(cmd *cobra.Command, functionBuild *functionconfig.Build, functionConfigPath *string, runtime *string, handler *string, commands *stringSliceFlag, encodedRuntimeAttributes *string, encodedCodeEntryAttributes *string) { // nolint
cmd.Flags().StringVarP(&functionBuild.Path, "path", "p", "", "Path to the function's source code")
cmd.Flags().StringVarP(&functionBuild.FunctionSourceCode, "source", "", "", "The function's source code (overrides \"path\")")
cmd.Flags().StringVarP(functionConfigPath, "file", "f", "", "Path to a function-configuration file")
cmd.Flags().StringVarP(&functionBuild.Image, "image", "i", "", "Name of a container image (default - the function name)")
cmd.Flags().StringVarP(&functionBuild.Registry, "registry", "r", os.Getenv("NUCTL_REGISTRY"), "URL of a container registry (env: NUCTL_REGISTRY)")
cmd.Flags().StringVarP(runtime, "runtime", "", "", "Runtime (for example, \"golang\", \"python:3.6\")")
cmd.Flags().StringVarP(handler, "handler", "", "", "Name of a function handler")
cmd.Flags().BoolVarP(&functionBuild.NoBaseImagesPull, "no-pull", "", false, "Don't pull base images - use local versions")
cmd.Flags().BoolVarP(&functionBuild.NoCleanup, "no-cleanup", "", false, "Don't clean up temporary directories")
cmd.Flags().StringVarP(&functionBuild.BaseImage, "base-image", "", "", "Name of the base image (default - per-runtime default)")
cmd.Flags().Var(commands, "build-command", "Commands to run when building the processor image")
cmd.Flags().StringVarP(&config.Spec.Build.OnbuildImage, "onbuild-image", "", "", "The runtime onbuild image used to build the processor image")
cmd.Flags().BoolVarP(&config.Spec.Build.Offline, "offline", "", false, "Don't assume internet connectivity exists")
cmd.Flags().StringVarP(&functionBuild.OnbuildImage, "onbuild-image", "", "", "The runtime onbuild image used to build the processor image")
cmd.Flags().BoolVarP(&functionBuild.Offline, "offline", "", false, "Don't assume internet connectivity exists")
cmd.Flags().StringVar(encodedRuntimeAttributes, "build-runtime-attrs", "{}", "JSON-encoded build runtime attributes for the function")
cmd.Flags().StringVar(encodedCodeEntryAttributes, "build-code-entry-attrs", "{}", "JSON-encoded build code entry attributes for the function")
cmd.Flags().StringVar(&config.Spec.Build.CodeEntryType, "code-entry-type", "", "Type of code entry (for example, \"url\", \"github\", \"image\")")
cmd.Flags().StringVar(&functionBuild.CodeEntryType, "code-entry-type", "", "Type of code entry (for example, \"url\", \"github\", \"image\")")
}
17 changes: 17 additions & 0 deletions pkg/nuctl/command/common/helpers.go
@@ -1,13 +1,15 @@
package common

import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"

"github.com/nuclio/nuclio/pkg/platform"

"github.com/ghodss/yaml"
"github.com/nuclio/errors"
)

Expand Down Expand Up @@ -71,3 +73,18 @@ func OpenFile(filepath string) (*os.File, error) {
}
return file, err
}

func GetUnmarshalFunc(bytes []byte) (func(data []byte, v interface{}) error, error) {
var err error
var obj map[string]interface{}

if err = json.Unmarshal(bytes, &obj); err == nil {
return json.Unmarshal, nil
}

if err = yaml.Unmarshal(bytes, &obj); err == nil {
return yaml.Unmarshal, nil
}

return nil, errors.New("Input is neither json nor yaml")
}

0 comments on commit 05d582b

Please sign in to comment.