Add knative serving v1alpha1 to generate CRD command

This commit adds the ability to generate a knative serving
definition from the Function Store or a stack.yaml file with
a basic definition including: namespace, name, env-vars and
image.

Tested with Istio on Packet.net using figlet.

Signed-off-by: Alex Ellis <alexellis2@gmail.com>
This commit is contained in:
Alex Ellis
2019-05-11 11:46:49 +01:00
parent 9c5e480d82
commit b42d0703b6
2 changed files with 115 additions and 0 deletions

View File

@@ -10,6 +10,7 @@ import (
"github.com/openfaas/faas-cli/builder"
"github.com/openfaas/faas-cli/proxy"
"github.com/openfaas/faas-cli/schema"
knativev1alpha1 "github.com/openfaas/faas-cli/schema/knative/v1alpha1"
"github.com/openfaas/faas-cli/stack"
"github.com/pkg/errors"
"github.com/spf13/cobra"
@@ -164,6 +165,11 @@ func generateCRDYAML(services stack.Services, format schema.BuildFormat, apiVers
var objectsString string
if len(services.Functions) > 0 {
if apiVersion == knativev1alpha1.APIVersionLatest {
return generateknativev1alpha1ServingCRDYAML(services, format, api, functionNamespace, branch, version)
}
for name, function := range services.Functions {
//read environment variables from the file
fileEnvironment, err := readFiles(function.EnvironmentFile)
@@ -209,3 +215,64 @@ func generateCRDYAML(services stack.Services, format schema.BuildFormat, apiVers
return objectsString, nil
}
func generateknativev1alpha1ServingCRDYAML(services stack.Services, format schema.BuildFormat, apiVersion, namespace, branch, version string) (string, error) {
crds := []knativev1alpha1.ServingCRD{}
for name, function := range services.Functions {
fileEnvironment, err := readFiles(function.EnvironmentFile)
if err != nil {
return "", err
}
//combine all environment variables
allEnvironment, envErr := compileEnvironment([]string{}, function.Environment, fileEnvironment)
if envErr != nil {
return "", envErr
}
var env []knativev1alpha1.EnvPair
for k, v := range allEnvironment {
env = append(env, knativev1alpha1.EnvPair{Name: k, Value: v})
}
crd := knativev1alpha1.ServingCRD{
Metadata: schema.Metadata{
Name: name,
Namespace: namespace,
},
APIVersion: apiVersion,
Kind: "Service",
Spec: knativev1alpha1.ServingSpec{
RunLatest: knativev1alpha1.ServingSpecRunLatest{
Configuration: knativev1alpha1.ServingSpecRunLatestConfiguration{
RevisionTemplate: knativev1alpha1.ServingSpecRunLatestConfigurationRevisionTemplate{
Spec: knativev1alpha1.ServingSpecRunLatestConfigurationRevisionTemplateSpec{
Container: knativev1alpha1.ServingSpecRunLatestConfigurationRevisionTemplateSpecContainer{
Image: function.Image,
Env: env,
},
},
},
},
},
},
}
crds = append(crds, crd)
}
var objectsString string
for _, crd := range crds {
//Marshal the object definition to yaml
objectString, err := yaml.Marshal(crd)
if err != nil {
return "", err
}
objectsString += "---\n" + string(objectString)
}
return objectsString, nil
}

View File

@@ -0,0 +1,48 @@
// Copyright (c) OpenFaaS Author(s) 2019. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package v1alpha1
import "github.com/openfaas/faas-cli/schema"
const APIVersionLatest = "serving.knative.dev/v1alpha1"
//ServingSpec describe characteristics of the object
type ServingSpec struct {
RunLatest ServingSpecRunLatest `yaml:"runLatest"`
}
type ServingSpecRunLatest struct {
Configuration ServingSpecRunLatestConfiguration `yaml:"configuration"`
}
type ServingSpecRunLatestConfiguration struct {
RevisionTemplate ServingSpecRunLatestConfigurationRevisionTemplate `yaml:"revisionTemplate"`
}
type ServingSpecRunLatestConfigurationRevisionTemplate struct {
Spec ServingSpecRunLatestConfigurationRevisionTemplateSpec `yaml:"spec"`
}
type ServingSpecRunLatestConfigurationRevisionTemplateSpec struct {
Container ServingSpecRunLatestConfigurationRevisionTemplateSpecContainer `yaml:"container"`
}
type ServingSpecRunLatestConfigurationRevisionTemplateSpecContainer struct {
Image string `yaml:"image"`
Env []EnvPair `yaml:"env,omitempty"`
}
type EnvPair struct {
Name string `yaml:"name"`
Value string `yaml:"value"`
}
//ServingCRD root level YAML definition for the object
type ServingCRD struct {
//APIVersion CRD API version
APIVersion string `yaml:"apiVersion"`
//Kind kind of the object
Kind string `yaml:"kind"`
Metadata schema.Metadata `yaml:"metadata"`
Spec ServingSpec `yaml:"spec"`
}