Skip to content

Commit

Permalink
Add default function config to frontend spec (#1589)
Browse files Browse the repository at this point in the history
  • Loading branch information
sahare92 committed Apr 1, 2020
1 parent 39d8c87 commit 3307aa8
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 20 deletions.
28 changes: 28 additions & 0 deletions pkg/dashboard/resource/frontendspec.go
Expand Up @@ -21,7 +21,9 @@ import (

"github.com/nuclio/nuclio/pkg/dashboard"
"github.com/nuclio/nuclio/pkg/functionconfig"
"github.com/nuclio/nuclio/pkg/platform/abstract"
"github.com/nuclio/nuclio/pkg/platformconfig"
"github.com/nuclio/nuclio/pkg/processor/trigger"
"github.com/nuclio/nuclio/pkg/restful"

"github.com/nuclio/errors"
Expand Down Expand Up @@ -58,13 +60,16 @@ func (fesr *frontendSpecResource) getFrontendSpec(request *http.Request) (*restf
"scaleResources": scaleResources,
}

defaultFunctionConfig := fesr.getDefaultFunctionConfig()

frontendSpec := map[string]restful.Attributes{
"frontendSpec": { // frontendSpec is the ID of this singleton resource
"externalIPAddresses": externalIPAddresses,
"namespace": fesr.getNamespaceOrDefault(""),
"defaultHTTPIngressHostTemplate": fesr.getPlatform().GetDefaultHTTPIngressHostTemplate(),
"imageNamePrefixTemplate": fesr.getPlatform().GetImageNamePrefixTemplate(),
"scaleToZero": scaleToZeroAttribute,
"defaultFunctionConfig": defaultFunctionConfig,
},
}

Expand All @@ -76,6 +81,29 @@ func (fesr *frontendSpecResource) getFrontendSpec(request *http.Request) (*restf
}, nil
}

func (fesr *frontendSpecResource) getDefaultFunctionConfig() map[string]interface{} {
one := 1
defaultWorkerAvailabilityTimeoutMilliseconds := trigger.DefaultWorkerAvailabilityTimeoutMilliseconds
defaultFunctionSpec := functionconfig.Spec{
MinReplicas: &one,
MaxReplicas: &one,
ReadinessTimeoutSeconds: abstract.DefaultReadinessTimeoutSeconds,
TargetCPU: abstract.DefaultTargetCPU,
Triggers: map[string]functionconfig.Trigger{

// notice that this is a mapping between trigger kind and its default values
"http": {
WorkerAvailabilityTimeoutMilliseconds: &defaultWorkerAvailabilityTimeoutMilliseconds,
},
"cron": {
WorkerAvailabilityTimeoutMilliseconds: &defaultWorkerAvailabilityTimeoutMilliseconds,
},
},
}

return map[string]interface{}{"attributes": functionconfig.Config{Spec: defaultFunctionSpec}}
}

// returns a list of custom routes for the resource
func (fesr *frontendSpecResource) GetCustomRoutes() ([]restful.CustomRoute, error) {

Expand Down
66 changes: 51 additions & 15 deletions pkg/dashboard/test/server_test.go
Expand Up @@ -1681,21 +1681,57 @@ func (suite *miscTestSuite) TestGetFrontendSpec() {

expectedStatusCode := http.StatusOK
expectedResponseBody := `{
"externalIPAddresses": ["address1", "address2", "address3"],
"defaultHTTPIngressHostTemplate": "{{ .FunctionName }}.{{ .ProjectName }}.{{ .Namespace }}.test-system.com",
"imageNamePrefixTemplate": "{{ .ProjectName }}-{{ .FunctionName }}-",
"namespace": "",
"scaleToZero": {
"mode": "enabled",
"inactivityWindowPresets": ["1m", "2m"],
"scaleResources": [
{
"metricName": "metric_name",
"windowSize": "1m",
"threshold": 0
}
]
}
"defaultFunctionConfig": {
"attributes": {
"metadata": {},
"spec": {
"resources": {},
"minReplicas": 1,
"maxReplicas": 1,
"targetCPU": 75,
"triggers": {
"cron": {
"class": "",
"kind": "",
"name": "",
"workerAvailabilityTimeoutMilliseconds": 10000
},
"http": {
"class": "",
"kind": "",
"name": "",
"workerAvailabilityTimeoutMilliseconds": 10000
}
},
"build": {},
"platform": {},
"readinessTimeoutSeconds": 60,
"eventTimeout": ""
}
}
},
"defaultHTTPIngressHostTemplate": "{{ .FunctionName }}.{{ .ProjectName }}.{{ .Namespace }}.test-system.com",
"externalIPAddresses": [
"address1",
"address2",
"address3"
],
"imageNamePrefixTemplate": "{{ .ProjectName }}-{{ .FunctionName }}-",
"namespace": "",
"scaleToZero": {
"inactivityWindowPresets": [
"1m",
"2m"
],
"mode": "enabled",
"scaleResources": [
{
"metricName": "metric_name",
"windowSize": "1m",
"threshold": 0
}
]
}
}`

suite.sendRequest("GET",
Expand Down
5 changes: 3 additions & 2 deletions pkg/nuctl/command/deploy.go
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/nuclio/nuclio/pkg/common"
"github.com/nuclio/nuclio/pkg/functionconfig"
"github.com/nuclio/nuclio/pkg/platform"
"github.com/nuclio/nuclio/pkg/platform/abstract"

"github.com/nuclio/errors"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -211,15 +212,15 @@ func addDeployFlags(cmd *cobra.Command,
cmd.Flags().IntVarP(&commandeer.replicas, "replicas", "", -1, "Set to any non-negative integer to use a static number of replicas")
cmd.Flags().IntVar(&commandeer.minReplicas, "min-replicas", -1, "Minimal number of function replicas")
cmd.Flags().IntVar(&commandeer.maxReplicas, "max-replicas", -1, "Maximal number of function replicas")
cmd.Flags().IntVar(&functionConfig.Spec.TargetCPU, "target-cpu", 75, "Target CPU when auto-scaling, in percentage")
cmd.Flags().IntVar(&functionConfig.Spec.TargetCPU, "target-cpu", abstract.DefaultTargetCPU, "Target CPU when auto-scaling, in percentage")
cmd.Flags().BoolVar(&functionConfig.Spec.Publish, "publish", false, "Publish the function")
cmd.Flags().StringVar(&commandeer.encodedDataBindings, "data-bindings", "{}", "JSON-encoded data bindings for the function")
cmd.Flags().StringVar(&commandeer.encodedTriggers, "triggers", "{}", "JSON-encoded triggers for the function")
cmd.Flags().StringVar(&commandeer.encodedFunctionPlatformConfig, "platform-config", "{}", "JSON-encoded platform specific configuration")
cmd.Flags().StringVar(&functionConfig.Spec.Image, "run-image", "", "Name of an existing image to deploy (default - build a new image to deploy)")
cmd.Flags().StringVar(&functionConfig.Spec.RunRegistry, "run-registry", os.Getenv("NUCTL_RUN_REGISTRY"), "URL of a registry for pulling the image, if differs from -r/--registry (env: NUCTL_RUN_REGISTRY)")
cmd.Flags().StringVar(&commandeer.encodedRuntimeAttributes, "runtime-attrs", "{}", "JSON-encoded runtime attributes for the function")
cmd.Flags().IntVar(&functionConfig.Spec.ReadinessTimeoutSeconds, "readiness-timeout", 60, "maximum wait time for the function to be ready")
cmd.Flags().IntVar(&functionConfig.Spec.ReadinessTimeoutSeconds, "readiness-timeout", abstract.DefaultReadinessTimeoutSeconds, "maximum wait time for the function to be ready")
cmd.Flags().StringVar(&commandeer.projectName, "project-name", "", "name of project to which this function belongs to")
cmd.Flags().Var(&commandeer.volumes, "volume", "Volumes for the deployment function (src1=dest1[,src2=dest2,...])")
cmd.Flags().Var(&commandeer.resourceLimits, "resource-limit", "Limits resources in the format of resource-name=quantity (e.g. cpu=3)")
Expand Down
5 changes: 5 additions & 0 deletions pkg/platform/abstract/platform.go
Expand Up @@ -42,6 +42,11 @@ import (
// Base for all platforms
//

const (
DefaultReadinessTimeoutSeconds = 60
DefaultTargetCPU = 75
)

type Platform struct {
Logger logger.Logger
platform platform.Platform
Expand Down
3 changes: 2 additions & 1 deletion pkg/platform/kube/controller/nucliofunction.go
Expand Up @@ -22,6 +22,7 @@ import (
"time"

"github.com/nuclio/nuclio/pkg/functionconfig"
"github.com/nuclio/nuclio/pkg/platform/abstract"
nuclioio "github.com/nuclio/nuclio/pkg/platform/kube/apis/nuclio.io/v1beta1"
"github.com/nuclio/nuclio/pkg/platform/kube/functionres"
"github.com/nuclio/nuclio/pkg/platform/kube/operator"
Expand Down Expand Up @@ -122,7 +123,7 @@ func (fo *functionOperator) CreateOrUpdate(ctx context.Context, object runtime.O
// wait for up to 60 seconds or whatever was set in the spec
readinessTimeout := function.Spec.ReadinessTimeoutSeconds
if readinessTimeout == 0 {
readinessTimeout = 60
readinessTimeout = abstract.DefaultReadinessTimeoutSeconds
}

waitContext, cancel := context.WithDeadline(ctx, time.Now().Add(time.Duration(readinessTimeout)*time.Second))
Expand Down
3 changes: 2 additions & 1 deletion pkg/platform/kube/functionres/lazy.go
Expand Up @@ -28,6 +28,7 @@ import (
"time"

"github.com/nuclio/nuclio/pkg/functionconfig"
"github.com/nuclio/nuclio/pkg/platform/abstract"
nuclioio "github.com/nuclio/nuclio/pkg/platform/kube/apis/nuclio.io/v1beta1"
nuclioio_client "github.com/nuclio/nuclio/pkg/platform/kube/client/clientset/versioned"
"github.com/nuclio/nuclio/pkg/platformconfig"
Expand Down Expand Up @@ -798,7 +799,7 @@ func (lc *lazyClient) createOrUpdateHorizontalPodAutoscaler(functionLabels label

targetCPU := int32(function.Spec.TargetCPU)
if targetCPU == 0 {
targetCPU = 75
targetCPU = abstract.DefaultTargetCPU
}

getHorizontalPodAutoscaler := func() (interface{}, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/platform/local/platform.go
Expand Up @@ -566,7 +566,7 @@ func (p *Platform) deployFunction(createFunctionOptions *platform.CreateFunction
if createFunctionOptions.FunctionConfig.Spec.ReadinessTimeoutSeconds != 0 {
readinessTimeout = time.Duration(createFunctionOptions.FunctionConfig.Spec.ReadinessTimeoutSeconds) * time.Second
} else {
readinessTimeout = 60 * time.Second
readinessTimeout = abstract.DefaultReadinessTimeoutSeconds * time.Second
}

if err = p.dockerClient.AwaitContainerHealth(containerID, &readinessTimeout); err != nil {
Expand Down

0 comments on commit 3307aa8

Please sign in to comment.