Skip to content

Commit 5feb0e2

Browse files
authored
feat: add support for annotations in func.yaml (#314)
This commit adds limited support for annotations in the func.yaml config file. The feature is limited, because it's only additive. A user can add an annotation `foo: bar` in the config and deploy the function, successfully setting that annotation on the Service. However, if they subsequently remove `foo: bar` from the config file, it will _not_ be removed from the deployment. This is because it's not possible to know, from the set of annotations that currently exist on the deployment, which ones were set by us and which were not. So, removing any annotations that are not in func.yaml is unsafe. It may be possible to store in a hidden file somewhere all of the user-supplied annotations, allowing us to diff func.yaml with that file, but I'm not sure I want to go down that path. It might just be best to document this limitation. We may also want to document that annotations added through func.yaml should be user supplied settings/values, and not annotations that are managed by knative (e.g. the autoscaling annotations). Fixes: #307 Signed-off-by: Lance Ball <lball@redhat.com>
1 parent 255b4fb commit 5feb0e2

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

config.go

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type config struct {
2323
Builder string `yaml:"builder"`
2424
BuilderMap map[string]string `yaml:"builderMap"`
2525
Env map[string]string `yaml:"env"`
26+
Annotations map[string]string `yaml:"annotations"`
2627
// Add new values to the toConfig/fromConfig functions.
2728
}
2829

@@ -61,6 +62,7 @@ func fromConfig(c config) (f Function) {
6162
Builder: c.Builder,
6263
BuilderMap: c.BuilderMap,
6364
Env: c.Env,
65+
Annotations: c.Annotations,
6466
}
6567
}
6668

@@ -76,6 +78,7 @@ func toConfig(f Function) config {
7678
Builder: f.Builder,
7779
BuilderMap: f.BuilderMap,
7880
Env: f.Env,
81+
Annotations: f.Annotations,
7982
}
8083
}
8184

function.go

+4
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ type Function struct {
5353
BuilderMap map[string]string
5454

5555
Env map[string]string
56+
57+
// Map containing user-supplied annotations
58+
// Example: { "division": "finance" }
59+
Annotations map[string]string
5660
}
5761

5862
// NewFunction loads a Function from a path on disk. use .Initialized() to determine if

knative/deployer.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (d *Deployer) Deploy(ctx context.Context, f bosonFunc.Function) (err error)
5252
if d.Verbose {
5353
fmt.Printf("Creating Knative Service: %v\n", f.Name)
5454
}
55-
service, err := generateNewService(f.Name, f.ImageWithDigest(), f.Runtime, f.Env)
55+
service, err := generateNewService(f.Name, f.ImageWithDigest(), f.Runtime, f.Env, f.Annotations)
5656
if err != nil {
5757
err = fmt.Errorf("knative deployer failed to generate the service: %v", err)
5858
return err
@@ -86,7 +86,7 @@ func (d *Deployer) Deploy(ctx context.Context, f bosonFunc.Function) (err error)
8686
}
8787
} else {
8888
// Update the existing Service
89-
err = client.UpdateServiceWithRetry(f.Name, updateService(f.ImageWithDigest(), f.Env), 3)
89+
err = client.UpdateServiceWithRetry(f.Name, updateService(f.ImageWithDigest(), f.Env, f.Annotations), 3)
9090
if err != nil {
9191
err = fmt.Errorf("knative deployer failed to update the service: %v", err)
9292
return err
@@ -114,7 +114,7 @@ func probeFor(url string) *corev1.Probe {
114114
}
115115
}
116116

117-
func generateNewService(name, image, runtime string, env map[string]string) (*servingv1.Service, error) {
117+
func generateNewService(name, image, runtime string, env map[string]string, annotations map[string]string) (*servingv1.Service, error) {
118118
containers := []corev1.Container{
119119
{
120120
Image: image,
@@ -133,6 +133,7 @@ func generateNewService(name, image, runtime string, env map[string]string) (*se
133133
"boson.dev/function": "true",
134134
"boson.dev/runtime": runtime,
135135
},
136+
Annotations: annotations,
136137
},
137138
Spec: v1.ServiceSpec{
138139
ConfigurationSpec: v1.ConfigurationSpec{
@@ -150,12 +151,18 @@ func generateNewService(name, image, runtime string, env map[string]string) (*se
150151
return setEnv(service, env)
151152
}
152153

153-
func updateService(image string, env map[string]string) func(service *servingv1.Service) (*servingv1.Service, error) {
154+
func updateService(image string, env map[string]string, annotations map[string]string) func(service *servingv1.Service) (*servingv1.Service, error) {
154155
return func(service *servingv1.Service) (*servingv1.Service, error) {
155156
// Removing the name so the k8s server can fill it in with generated name,
156157
// this prevents conflicts in Revision name when updating the KService from multiple places.
157158
service.Spec.Template.Name = ""
158159

160+
// Don't bother being as clever as we are with env variables
161+
// Just set the annotations to be whatever we find in func.yaml
162+
for k, v := range annotations {
163+
service.ObjectMeta.Annotations[k] = v
164+
}
165+
159166
err := flags.UpdateImage(&service.Spec.Template.Spec.PodSpec, image)
160167
if err != nil {
161168
return service, err

pkged.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)