Skip to content

Commit

Permalink
feat: add support for labels in func.yaml
Browse files Browse the repository at this point in the history
This change adds support for setting labels on deployed functions. It does
not include any command line ability to do so. It was unclear how to best
test this, as YAML marshaling will blow up if that bit is incorrect.

I suppose the ideal test would be to actually add a label in an integration
test and check to see if it's there on deployment.

Signed-off-by: Lance Ball <lball@redhat.com>
  • Loading branch information
lance committed Jun 3, 2021
1 parent 553dfa8 commit 4a94b14
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 22 deletions.
5 changes: 3 additions & 2 deletions cmd/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package cmd

import (
"context"
fn "github.com/boson-project/func"
"io/ioutil"
"os"
"path/filepath"
"testing"

fn "github.com/boson-project/func"
)

type testRemover struct {
Expand Down Expand Up @@ -53,6 +54,7 @@ builderMap:
default: quay.io/boson/faas-go-builder
envs: []
annotations: {}
labels: {}
`
tmpDir, err := ioutil.TempDir("", "bar")
if err != nil {
Expand All @@ -72,7 +74,6 @@ annotations: {}
}
f.Close()


oldWD, err := os.Getwd()
if err != nil {
t.Fatal(err)
Expand Down
3 changes: 3 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type config struct {
Volumes Volumes `yaml:"volumes"`
Envs Envs `yaml:"envs"`
Annotations map[string]string `yaml:"annotations"`
Labels map[string]string `yaml:"labels"`
// Add new values to the toConfig/fromConfig functions.
}

Expand Down Expand Up @@ -130,6 +131,7 @@ func fromConfig(c config) (f Function) {
Volumes: c.Volumes,
Envs: c.Envs,
Annotations: c.Annotations,
Labels: c.Labels,
}
}

Expand All @@ -147,6 +149,7 @@ func toConfig(f Function) config {
Volumes: f.Volumes,
Envs: f.Envs,
Annotations: f.Annotations,
Labels: f.Labels,
}
}

Expand Down
4 changes: 4 additions & 0 deletions function.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ type Function struct {
// Map containing user-supplied annotations
// Example: { "division": "finance" }
Annotations map[string]string

// Map of user-supplied labels
// Example: { "tier": "backend" }
Labels map[string]string
}

// NewFunction loads a Function from a path on disk. use .Initialized() to determine if
Expand Down
48 changes: 29 additions & 19 deletions knative/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (d *Deployer) Deploy(ctx context.Context, f fn.Function) (result fn.Deploym
referencedSecrets := sets.NewString()
referencedConfigMaps := sets.NewString()

service, err := generateNewService(f.Name, f.ImageWithDigest(), f.Runtime, f.Envs, f.Volumes, f.Annotations)
service, err := generateNewService(f)
if err != nil {
err = fmt.Errorf("knative deployer failed to generate the Knative Service: %v", err)
return fn.DeploymentResult{}, err
Expand Down Expand Up @@ -116,7 +116,7 @@ func (d *Deployer) Deploy(ctx context.Context, f fn.Function) (result fn.Deploym
return fn.DeploymentResult{}, err
}

_, err = client.UpdateServiceWithRetry(ctx, f.Name, updateService(f.ImageWithDigest(), newEnv, newEnvFrom, newVolumes, newVolumeMounts, f.Annotations), 3)
_, err = client.UpdateServiceWithRetry(ctx, f.Name, updateService(f, newEnv, newEnvFrom, newVolumes, newVolumeMounts), 3)
if err != nil {
err = fmt.Errorf("knative deployer failed to update the Knative Service: %v", err)
return fn.DeploymentResult{}, err
Expand Down Expand Up @@ -145,42 +145,49 @@ func probeFor(url string) *corev1.Probe {
}
}

func generateNewService(name, image, runtime string, envs fn.Envs, volumes fn.Volumes, annotations map[string]string) (*servingv1.Service, error) {
func generateNewService(f fn.Function) (*servingv1.Service, error) {
containers := []corev1.Container{
{
Image: image,
Image: f.ImageWithDigest(),
},
}

if runtime != "quarkus" {
if f.Runtime != "quarkus" {
containers[0].LivenessProbe = probeFor("/health/liveness")
containers[0].ReadinessProbe = probeFor("/health/readiness")
}

referencedSecrets := sets.NewString()
referencedConfigMaps := sets.NewString()

newEnv, newEnvFrom, err := processEnvs(envs, &referencedSecrets, &referencedConfigMaps)
newEnv, newEnvFrom, err := processEnvs(f.Envs, &referencedSecrets, &referencedConfigMaps)
if err != nil {
return nil, err
}
containers[0].Env = newEnv
containers[0].EnvFrom = newEnvFrom

newVolumes, newVolumeMounts, err := processVolumes(volumes, &referencedSecrets, &referencedConfigMaps)
newVolumes, newVolumeMounts, err := processVolumes(f.Volumes, &referencedSecrets, &referencedConfigMaps)
if err != nil {
return nil, err
}
containers[0].VolumeMounts = newVolumeMounts
labels := map[string]string{
"boson.dev/function": "true",
"boson.dev/runtime": f.Runtime,
}

if f.Labels != nil {
for k, v := range f.Labels {
labels[k] = v
}
}

service := &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: map[string]string{
"boson.dev/function": "true",
"boson.dev/runtime": runtime,
},
Annotations: annotations,
Name: f.Name,
Labels: labels,
Annotations: f.Annotations,
},
Spec: v1.ServiceSpec{
ConfigurationSpec: v1.ConfigurationSpec{
Expand All @@ -199,20 +206,23 @@ func generateNewService(name, image, runtime string, envs fn.Envs, volumes fn.Vo
return service, nil
}

func updateService(image string, newEnv []corev1.EnvVar, newEnvFrom []corev1.EnvFromSource, newVolumes []corev1.Volume, newVolumeMounts []corev1.VolumeMount,
annotations map[string]string) func(service *servingv1.Service) (*servingv1.Service, error) {
func updateService(f fn.Function, newEnv []corev1.EnvVar, newEnvFrom []corev1.EnvFromSource, newVolumes []corev1.Volume, newVolumeMounts []corev1.VolumeMount) func(service *servingv1.Service) (*servingv1.Service, error) {
return func(service *servingv1.Service) (*servingv1.Service, error) {
// Removing the name so the k8s server can fill it in with generated name,
// this prevents conflicts in Revision name when updating the KService from multiple places.
service.Spec.Template.Name = ""

// Don't bother being as clever as we are with env variables
// Just set the annotations to be whatever we find in func.yaml
for k, v := range annotations {
// Just set the annotations and labels to be whatever we find in func.yaml
for k, v := range f.Annotations {
service.ObjectMeta.Annotations[k] = v
}

err := flags.UpdateImage(&service.Spec.Template.Spec.PodSpec, image)
for k, v := range f.Labels {
service.ObjectMeta.Labels[k] = v
}

err := flags.UpdateImage(&service.Spec.Template.Spec.PodSpec, f.ImageWithDigest())
if err != nil {
return service, err
}
Expand Down Expand Up @@ -509,7 +519,7 @@ func checkSecretsConfigMapsArePresent(ctx context.Context, namespace string, ref
}

if errMsg != "" {
return fmt.Errorf("\n"+errMsg)
return fmt.Errorf("\n" + errMsg)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion pkged.go

Large diffs are not rendered by default.

0 comments on commit 4a94b14

Please sign in to comment.