containers as functions (#1681)

PR adds a new executortype which supports running containers as functions. New CLI under functions is added to create container as functions.

Co-authored-by: Harsh Thakur <harshthakur9030@gmail.com>
Co-authored-by: Sanket Sudake <sanketsudake@gmail.com>
This commit is contained in:
Sahil Lakhwani
2021-07-16 18:08:41 +05:30
committed by GitHub
co-authored by Harsh Thakur Sanket Sudake
parent f3e1f9df90
commit a82281ad1c
33 changed files with 6053 additions and 198 deletions
+49 -2
View File
@@ -154,13 +154,60 @@ func Commands() *cobra.Command {
},
})
runContainerCmd := &cobra.Command{
Use: "run-container",
Aliases: []string{"runc"},
Short: "Alpha: Run a container image as a function",
RunE: wrapper.Wrapper(RunContainer),
}
wrapper.SetFlags(runContainerCmd, flag.FlagSet{
Required: []flag.Flag{flag.FnName, flag.FnImageName},
Optional: []flag.Flag{
flag.FnPort, flag.FnCommand, flag.FnArgs,
flag.FnCfgMap, flag.FnSecret,
flag.FnExecutionTimeout,
flag.FnIdleTimeout,
flag.Labels, flag.Annotation,
// flag for newdeploy to use.
flag.RunTimeMinCPU, flag.RunTimeMaxCPU, flag.RunTimeMinMemory,
flag.RunTimeMaxMemory, flag.ReplicasMin,
flag.ReplicasMax, flag.RunTimeTargetCPU,
flag.NamespaceFunction, flag.SpecSave, flag.SpecDry,
},
})
updateContainerCmd := &cobra.Command{
Use: "update-container",
Aliases: []string{"updatec"},
Short: "Alpha: Update a function running a container",
RunE: wrapper.Wrapper(UpdateContainer),
}
wrapper.SetFlags(updateContainerCmd, flag.FlagSet{
Required: []flag.Flag{flag.FnName},
Optional: []flag.Flag{
flag.FnImageName, flag.FnPort,
flag.FnCommand, flag.FnArgs,
flag.FnSecret, flag.FnCfgMap,
flag.FnExecutionTimeout, flag.FnIdleTimeout,
flag.Labels, flag.Annotation,
flag.RunTimeMinCPU, flag.RunTimeMaxCPU, flag.RunTimeMinMemory,
flag.RunTimeMaxMemory, flag.ReplicasMin, flag.ReplicasMax,
flag.RunTimeTargetCPU,
flag.NamespaceFunction, flag.SpecSave,
},
})
command := &cobra.Command{
Use: "function",
Aliases: []string{"fn"},
Short: "Create, update and manage functions",
}
command.AddCommand(createCmd, getCmd, getmetaCmd, updateCmd, deleteCmd, listCmd, logsCmd, testCmd)
command.AddCommand(createCmd, getCmd, getmetaCmd, updateCmd, deleteCmd, listCmd, logsCmd, testCmd,
runContainerCmd, updateContainerCmd)
return command
}
+35 -24
View File
@@ -285,18 +285,6 @@ func (opts *CreateSubCommand) complete(input cli.Input) error {
Namespace: fnNamespace,
},
Spec: fv1.FunctionSpec{
Environment: fv1.EnvironmentReference{
Name: envName,
Namespace: envNamespace,
},
Package: fv1.FunctionPackageRef{
FunctionName: entrypoint,
PackageRef: fv1.PackageRef{
Namespace: pkgMetadata.Namespace,
Name: pkgMetadata.Name,
ResourceVersion: pkgMetadata.ResourceVersion,
},
},
Secrets: secrets,
ConfigMaps: cfgmaps,
Resources: *resourceReq,
@@ -313,6 +301,18 @@ func (opts *CreateSubCommand) complete(input cli.Input) error {
if err != nil {
return err
}
opts.function.Spec.Environment = fv1.EnvironmentReference{
Name: envName,
Namespace: envNamespace,
}
opts.function.Spec.Package = fv1.FunctionPackageRef{
FunctionName: entrypoint,
PackageRef: fv1.PackageRef{
Namespace: pkgMetadata.Namespace,
Name: pkgMetadata.Name,
ResourceVersion: pkgMetadata.ResourceVersion,
},
}
return nil
}
@@ -392,13 +392,19 @@ func getInvokeStrategy(input cli.Input, existingInvokeStrategy *fv1.InvokeStrate
var es *fv1.ExecutionStrategy
if existingInvokeStrategy == nil {
es, err = getExecutionStrategy(input)
executorType, err := getExecutorType(input)
if err != nil {
return nil, err
}
es, err = getExecutionStrategy(executorType, input)
if err != nil {
return nil, err
}
} else {
es, err = updateExecutionStrategy(input, &existingInvokeStrategy.ExecutionStrategy)
}
if err != nil {
return nil, err
if err != nil {
return nil, err
}
}
return &fv1.InvokeStrategy{
@@ -407,20 +413,23 @@ func getInvokeStrategy(input cli.Input, existingInvokeStrategy *fv1.InvokeStrate
}, nil
}
func getExecutionStrategy(input cli.Input) (strategy *fv1.ExecutionStrategy, err error) {
var fnExecutor fv1.ExecutorType
func getExecutorType(input cli.Input) (executorType fv1.ExecutorType, err error) {
switch input.String(flagkey.FnExecutorType) {
case "":
fallthrough
case string(fv1.ExecutorTypePoolmgr):
fnExecutor = fv1.ExecutorTypePoolmgr
executorType = fv1.ExecutorTypePoolmgr
case string(fv1.ExecutorTypeNewdeploy):
fnExecutor = fv1.ExecutorTypeNewdeploy
executorType = fv1.ExecutorTypeNewdeploy
case string(fv1.ExecutorTypeContainer):
executorType = fv1.ExecutorTypeContainer
default:
return nil, errors.Errorf("executor type must be one of '%v' or '%v'", fv1.ExecutorTypePoolmgr, fv1.ExecutorTypeNewdeploy)
err = errors.Errorf("executor type must be one of '%v', '%v' or '%v'", fv1.ExecutorTypePoolmgr, fv1.ExecutorTypeNewdeploy, fv1.ExecutorTypeContainer)
}
return executorType, err
}
func getExecutionStrategy(fnExecutor fv1.ExecutorType, input cli.Input) (strategy *fv1.ExecutionStrategy, err error) {
specializationTimeout := fv1.DefaultSpecializationTimeOut
if input.IsSet(flagkey.FnSpecializationTimeout) {
@@ -495,8 +504,10 @@ func updateExecutionStrategy(input cli.Input, existingExecutionStrategy *fv1.Exe
fnExecutor = fv1.ExecutorTypePoolmgr
case string(fv1.ExecutorTypeNewdeploy):
fnExecutor = fv1.ExecutorTypeNewdeploy
case string(fv1.ExecutorTypeContainer):
fnExecutor = fv1.ExecutorTypeContainer
default:
return nil, errors.Errorf("executor type must be one of '%v' or '%v'", fv1.ExecutorTypePoolmgr, fv1.ExecutorTypeNewdeploy)
return nil, errors.Errorf("executor type must be one of '%v', %v or '%v'", fv1.ExecutorTypePoolmgr, fv1.ExecutorTypeNewdeploy, fv1.ExecutorTypeContainer)
}
}
@@ -0,0 +1,238 @@
/*
Copyright 2019 The Fission Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package function
import (
"fmt"
"strings"
"github.com/pkg/errors"
apiv1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
fv1 "github.com/fission/fission/pkg/apis/core/v1"
ferror "github.com/fission/fission/pkg/error"
"github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
"github.com/fission/fission/pkg/fission-cli/cmd"
"github.com/fission/fission/pkg/fission-cli/cmd/spec"
"github.com/fission/fission/pkg/fission-cli/console"
flagkey "github.com/fission/fission/pkg/fission-cli/flag/key"
"github.com/fission/fission/pkg/fission-cli/util"
)
type RunContainerSubCommand struct {
cmd.CommandActioner
function *fv1.Function
specFile string
}
func RunContainer(input cli.Input) error {
return (&RunContainerSubCommand{}).do(input)
}
func (opts *RunContainerSubCommand) do(input cli.Input) error {
err := opts.complete(input)
if err != nil {
return err
}
return opts.run(input)
}
func (opts *RunContainerSubCommand) complete(input cli.Input) error {
fnName := input.String(flagkey.FnName)
fnNamespace := input.String(flagkey.NamespaceFunction)
// user wants a spec, create a yaml file with package and function
toSpec := false
if input.Bool(flagkey.SpecSave) {
toSpec = true
opts.specFile = fmt.Sprintf("function-%v.yaml", fnName)
}
if !toSpec {
// check for unique function names within a namespace
fn, err := opts.Client().V1().Function().Get(&metav1.ObjectMeta{
Name: input.String(flagkey.FnName),
Namespace: input.String(flagkey.NamespaceFunction),
})
if err != nil && !ferror.IsNotFound(err) {
return err
} else if fn != nil {
return errors.New("a function with the same name already exists")
}
}
fnTimeout := input.Int(flagkey.FnExecutionTimeout)
if fnTimeout <= 0 {
return errors.Errorf("--%v must be greater than 0", flagkey.FnExecutionTimeout)
}
fnIdleTimeout := input.Int(flagkey.FnIdleTimeout)
secretNames := input.StringSlice(flagkey.FnSecret)
cfgMapNames := input.StringSlice(flagkey.FnCfgMap)
es, err := getExecutionStrategy(fv1.ExecutorTypeContainer, input)
if err != nil {
return err
}
invokeStrategy := &fv1.InvokeStrategy{
ExecutionStrategy: *es,
StrategyType: fv1.StrategyTypeExecution,
}
resourceReq, err := util.GetResourceReqs(input, &apiv1.ResourceRequirements{})
if err != nil {
return err
}
var imageName string
var port int
var command, args string
imageName = input.String(flagkey.FnImageName)
if imageName == "" {
return errors.New("need --image argument")
}
port = input.Int(flagkey.FnPort)
command = input.String(flagkey.FnCommand)
args = input.String(flagkey.FnArgs)
var secrets []fv1.SecretReference
var cfgmaps []fv1.ConfigMapReference
if len(secretNames) > 0 {
// check the referenced secret is in the same ns as the function, if not give a warning.
if !toSpec { // TODO: workaround in order not to block users from creating function spec, remove it.
for _, secretName := range secretNames {
err := opts.Client().V1().Misc().SecretExists(&metav1.ObjectMeta{
Namespace: fnNamespace,
Name: secretName,
})
if err != nil {
if k8serrors.IsNotFound(err) {
console.Warn(fmt.Sprintf("Secret %s not found in Namespace: %s. Secret needs to be present in the same namespace as function", secretName, fnNamespace))
} else {
return errors.Wrapf(err, "error checking secret %s", secretName)
}
}
}
}
for _, secretName := range secretNames {
newSecret := fv1.SecretReference{
Name: secretName,
Namespace: fnNamespace,
}
secrets = append(secrets, newSecret)
}
}
if len(cfgMapNames) > 0 {
// check the referenced cfgmap is in the same ns as the function, if not give a warning.
if !toSpec {
for _, cfgMapName := range cfgMapNames {
err := opts.Client().V1().Misc().ConfigMapExists(&metav1.ObjectMeta{
Namespace: fnNamespace,
Name: cfgMapName,
})
if err != nil {
if k8serrors.IsNotFound(err) {
console.Warn(fmt.Sprintf("ConfigMap %s not found in Namespace: %s. ConfigMap needs to be present in the same namespace as function", cfgMapName, fnNamespace))
} else {
return errors.Wrapf(err, "error checking configmap %s", cfgMapName)
}
}
}
}
for _, cfgMapName := range cfgMapNames {
newCfgMap := fv1.ConfigMapReference{
Name: cfgMapName,
Namespace: fnNamespace,
}
cfgmaps = append(cfgmaps, newCfgMap)
}
}
opts.function = &fv1.Function{
ObjectMeta: metav1.ObjectMeta{
Name: fnName,
Namespace: fnNamespace,
},
Spec: fv1.FunctionSpec{
Secrets: secrets,
ConfigMaps: cfgmaps,
Resources: *resourceReq,
InvokeStrategy: *invokeStrategy,
FunctionTimeout: fnTimeout,
IdleTimeout: &fnIdleTimeout,
},
}
err = util.ApplyLabelsAndAnnotations(input, &opts.function.ObjectMeta)
if err != nil {
return err
}
container := &apiv1.Container{
Name: fnName,
Image: imageName,
Ports: []apiv1.ContainerPort{
{
Name: "http-env",
ContainerPort: int32(port),
},
},
}
if command != "" {
container.Command = strings.Split(command, " ")
}
if args != "" {
container.Args = strings.Split(args, " ")
}
opts.function.Spec.PodSpec = &apiv1.PodSpec{
Containers: []apiv1.Container{*container},
}
return nil
}
// run write the resource to a spec file or create a fission CRD with remote fission server.
// It also prints warning/error if necessary.
func (opts *RunContainerSubCommand) run(input cli.Input) error {
// if we're writing a spec, don't create the function
// save to spec file or display the spec to console
if input.Bool(flagkey.SpecDry) {
return spec.SpecDry(*opts.function)
}
if input.Bool(flagkey.SpecSave) {
err := spec.SpecSave(*opts.function, opts.specFile)
if err != nil {
return errors.Wrap(err, "error saving function spec")
}
return nil
}
_, err := opts.Client().V1().Function().Create(opts.function)
if err != nil {
return errors.Wrap(err, "error creating function")
}
fmt.Printf("function '%v' created\n", opts.function.ObjectMeta.Name)
return nil
}
@@ -0,0 +1,197 @@
/*
Copyright 2019 The Fission Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package function
import (
"fmt"
"strings"
"github.com/pkg/errors"
apiv1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
fv1 "github.com/fission/fission/pkg/apis/core/v1"
"github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
"github.com/fission/fission/pkg/fission-cli/cmd"
"github.com/fission/fission/pkg/fission-cli/console"
flagkey "github.com/fission/fission/pkg/fission-cli/flag/key"
"github.com/fission/fission/pkg/fission-cli/util"
)
type UpdateContainerSubCommand struct {
cmd.CommandActioner
function *fv1.Function
}
func UpdateContainer(input cli.Input) error {
return (&UpdateContainerSubCommand{}).do(input)
}
func (opts *UpdateContainerSubCommand) do(input cli.Input) error {
err := opts.complete(input)
if err != nil {
return err
}
return opts.run(input)
}
func (opts *UpdateContainerSubCommand) complete(input cli.Input) error {
fnName := input.String(flagkey.FnName)
fnNamespace := input.String(flagkey.NamespaceFunction)
function, err := opts.Client().V1().Function().Get(&metav1.ObjectMeta{
Name: input.String(flagkey.FnName),
Namespace: input.String(flagkey.NamespaceFunction),
})
if err != nil {
return errors.Wrap(err, fmt.Sprintf("read function '%v'", fnName))
}
if fv1.ExecutorTypeContainer != function.Spec.InvokeStrategy.ExecutionStrategy.ExecutorType {
return fmt.Errorf("executor type for function is not %s", fv1.ExecutorTypeContainer)
}
imageName := input.String(flagkey.FnImageName)
port := input.Int(flagkey.FnPort)
command := input.String(flagkey.FnCommand)
args := input.String(flagkey.FnArgs)
secretNames := input.StringSlice(flagkey.FnSecret)
cfgMapNames := input.StringSlice(flagkey.FnCfgMap)
var secrets []fv1.SecretReference
var configMaps []fv1.ConfigMapReference
if len(secretNames) > 0 {
// check that the referenced secret is in the same ns as the function, if not give a warning.
for _, secretName := range secretNames {
err := opts.Client().V1().Misc().SecretExists(&metav1.ObjectMeta{
Namespace: fnNamespace,
Name: secretName,
})
if k8serrors.IsNotFound(err) {
console.Warn(fmt.Sprintf("secret %s not found in Namespace: %s. Secret needs to be present in the same namespace as function", secretName, fnNamespace))
}
}
for _, secretName := range secretNames {
newSecret := fv1.SecretReference{
Name: secretName,
Namespace: fnNamespace,
}
secrets = append(secrets, newSecret)
}
function.Spec.Secrets = secrets
}
if len(cfgMapNames) > 0 {
// check that the referenced cfgmap is in the same ns as the function, if not give a warning.
for _, cfgMapName := range cfgMapNames {
err := opts.Client().V1().Misc().ConfigMapExists(&metav1.ObjectMeta{
Namespace: fnNamespace,
Name: cfgMapName,
})
if k8serrors.IsNotFound(err) {
console.Warn(fmt.Sprintf("ConfigMap %s not found in Namespace: %s. ConfigMap needs to be present in the same namespace as the function", cfgMapName, fnNamespace))
}
}
for _, cfgMapName := range cfgMapNames {
newCfgMap := fv1.ConfigMapReference{
Name: cfgMapName,
Namespace: fnNamespace,
}
configMaps = append(configMaps, newCfgMap)
}
function.Spec.ConfigMaps = configMaps
}
if input.IsSet(flagkey.FnExecutionTimeout) {
fnTimeout := input.Int(flagkey.FnExecutionTimeout)
if fnTimeout <= 0 {
return errors.Errorf("--%v must be greater than 0", flagkey.FnExecutionTimeout)
}
function.Spec.FunctionTimeout = fnTimeout
}
if input.IsSet(flagkey.FnIdleTimeout) {
fnTimeout := input.Int(flagkey.FnIdleTimeout)
function.Spec.IdleTimeout = &fnTimeout
}
strategy, err := getInvokeStrategy(input, &function.Spec.InvokeStrategy)
if err != nil {
return err
}
function.Spec.InvokeStrategy = *strategy
resReqs, err := util.GetResourceReqs(input, &function.Spec.Resources)
if err != nil {
return err
}
function.Spec.Resources = *resReqs
if len(function.Spec.PodSpec.Containers) > 1 {
return errors.Errorf("function %s has more than one container, only one container is supported", fnName)
}
container := &function.Spec.PodSpec.Containers[0]
if imageName != "" {
container.Image = imageName
}
if port != 0 {
if len(container.Ports) > 1 {
return errors.Errorf("function %s has more than one port, only one port is supported", fnName)
}
container.Ports = []apiv1.ContainerPort{
{
Name: "http-env",
ContainerPort: int32(port),
},
}
}
if command != "" {
container.Command = strings.Split(command, " ")
}
if args != "" {
container.Args = strings.Split(args, " ")
}
function.Spec.Environment = fv1.EnvironmentReference{}
function.Spec.Package = fv1.FunctionPackageRef{}
opts.function = function
err = util.ApplyLabelsAndAnnotations(input, &opts.function.ObjectMeta)
if err != nil {
return err
}
return nil
}
func (opts *UpdateContainerSubCommand) run(input cli.Input) error {
_, err := opts.Client().V1().Function().Update(opts.function)
if err != nil {
return errors.Wrap(err, "error updating function")
}
return nil
}