Skip to content

Commit

Permalink
Prevent service-specific labels from being put on the revision templa…
Browse files Browse the repository at this point in the history
…te (#672)

See: knative/serving#6865

Signed-off-by: Doug Davis <[email protected]>
  • Loading branch information
Doug Davis authored Feb 17, 2020
1 parent a220a88 commit 83b926b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
12 changes: 11 additions & 1 deletion pkg/serving/config_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,12 @@ func UpdateResources(template *servingv1.RevisionTemplateSpec, requestsResourceL
return nil
}

// ServiceOnlyLabels should only appear on the Service and NOT on the
// Revision template
var ServiceOnlyLabels = map[string]struct{}{
serving.GroupName + "/visibility": {},
}

// UpdateLabels updates the labels identically on a service and template.
// Does not overwrite the entire Labels field, only makes the requested updates
func UpdateLabels(service *servingv1.Service, template *servingv1.RevisionTemplateSpec, toUpdate map[string]string, toRemove []string) error {
Expand All @@ -373,7 +379,11 @@ func UpdateLabels(service *servingv1.Service, template *servingv1.RevisionTempla
}
for key, value := range toUpdate {
service.ObjectMeta.Labels[key] = value
template.ObjectMeta.Labels[key] = value

// Only add it to the template if it's not in our ServiceOnly list
if _, ok := ServiceOnlyLabels[key]; !ok {
template.ObjectMeta.Labels[key] = value
}
}
for _, key := range toRemove {
delete(service.ObjectMeta.Labels, key)
Expand Down
23 changes: 21 additions & 2 deletions pkg/serving/config_changes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,25 @@ func TestUpdateLabelsNew(t *testing.T) {
"a": "foo",
"b": "bar",
}
tLabels := labels // revision template labels

// Only test service-specific labels if we have any
if len(ServiceOnlyLabels) != 0 {
// Make a copy of the expected labels so we can modify the original
// list w/o changing what's expected for the revsion template
tLabels = map[string]string{}
for k, v := range labels {
tLabels[k] = v
}

// Just add a random value from the list to make sure it doesn't show
// up in the revision template
for k := range ServiceOnlyLabels {
labels[k] = "testing"
break
}
}

err := UpdateLabels(service, template, labels, []string{})
assert.NilError(t, err)

Expand All @@ -340,8 +359,8 @@ func TestUpdateLabelsNew(t *testing.T) {
}

actual = template.ObjectMeta.Labels
if !reflect.DeepEqual(labels, actual) {
t.Fatalf("Template labels did not match expected %v found %v", labels, actual)
if !reflect.DeepEqual(tLabels, actual) {
t.Fatalf("Template labels did not match expected %v found %v", tLabels, actual)
}
}

Expand Down

0 comments on commit 83b926b

Please sign in to comment.