Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix autoscaling annotations in Service metadata #1021

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions pkg/kn/commands/service/configuration_edit_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,11 +446,8 @@ func (p *ConfigurationEditFlags) Apply(
if cmd.Flags().Changed("annotation") && containsAnnotation(p.Annotations, autoscaling.InitialScaleAnnotationKey) {
return fmt.Errorf("only one of the --scale-init or --annotation %s can be specified", autoscaling.InitialScaleAnnotationKey)
}
annotationsMap := map[string]string{
autoscaling.InitialScaleAnnotationKey: strconv.Itoa(p.ScaleInit),
}

err = servinglib.UpdateAnnotations(service, template, annotationsMap, []string{})
err = servinglib.UpdateRevisionTemplateAnnotation(template, autoscaling.InitialScaleAnnotationKey, strconv.Itoa(p.ScaleInit))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, we should have two utils

  • UpdateRevisionAnnotations
  • UpdateServiceAnnotations
    Each taking respective object, toUpdate and toRemove map.
    The callers of these utils can then decide if the given annotations should be set on either or both (explaining why in comments).

Copy link
Contributor Author

@dsimansk dsimansk Sep 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the current codebase there's only one place that UpdateAnnotations is used and needs to be changed to filter autoscaling annotations. I tried to aim for quick win to unblock CI etc. However, I do agree that separate Update* util methods are likely better solution to expose filtering logic at caller level. I'll update the PR shortly. 😉

if err != nil {
return err
}
Expand Down
6 changes: 0 additions & 6 deletions pkg/kn/commands/service/service_update_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1480,9 +1480,6 @@ func TestServiceUpdateInitialScaleMock(t *testing.T) {
newService := getService(svcName)
template := &newService.Spec.Template
template.Spec.Containers[0].Image = "gcr.io/foo/bar:baz"
newService.ObjectMeta.Annotations = map[string]string{
"autoscaling.knative.dev/initialScale": "1",
}
template.ObjectMeta.Annotations = map[string]string{
"autoscaling.knative.dev/initialScale": "1",
clientserving.UserImageAnnotationKey: "gcr.io/foo/bar:baz",
Expand All @@ -1491,9 +1488,6 @@ func TestServiceUpdateInitialScaleMock(t *testing.T) {
updatedService := getService(svcName)
template = &updatedService.Spec.Template
template.Spec.Containers[0].Image = "gcr.io/foo/bar:baz"
updatedService.ObjectMeta.Annotations = map[string]string{
"autoscaling.knative.dev/initialScale": "2",
}
template.ObjectMeta.Annotations = map[string]string{
"autoscaling.knative.dev/initialScale": "2",
clientserving.UserImageAnnotationKey: "gcr.io/foo/bar:baz",
Expand Down
18 changes: 16 additions & 2 deletions pkg/serving/config_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,25 +453,39 @@ func UpdateLabels(labelsMap map[string]string, add map[string]string, remove []s
}

// UpdateAnnotations updates the annotations identically on a service and template.
// Unless it's Autoscaling annotation that's allowed only on template.
// Does not overwrite the entire Annotations field, only makes the requested updates.
func UpdateAnnotations(
service *servingv1.Service,
template *servingv1.RevisionTemplateSpec,
toUpdate map[string]string,
toRemove []string) error {

if service.ObjectMeta.Annotations == nil {
// filter Autoscaling annotations
autoscalingAnnotations := make(map[string]string)
for key, value := range toUpdate {
if strings.HasPrefix(key, autoscaling.GroupName) {
autoscalingAnnotations[key] = value
delete(toUpdate, key)
}
}

if service.ObjectMeta.Annotations == nil && len(toUpdate) > 0 {
service.ObjectMeta.Annotations = make(map[string]string)
}

if template.ObjectMeta.Annotations == nil {
if template.ObjectMeta.Annotations == nil && (len(toUpdate) > 0 || len(autoscalingAnnotations) > 0) {
template.ObjectMeta.Annotations = make(map[string]string)
}

for key, value := range toUpdate {
service.ObjectMeta.Annotations[key] = value
template.ObjectMeta.Annotations[key] = value
}
// add only to template
for key, value := range autoscalingAnnotations {
template.ObjectMeta.Annotations[key] = value
}

for _, key := range toRemove {
delete(service.ObjectMeta.Annotations, key)
Expand Down