Skip to content

Commit

Permalink
Do not print serviceUID and configUID labels in service export result (
Browse files Browse the repository at this point in the history
…#1194)

* Ignore serviceUID and configUID labels in service export result

 While exporting the service using `service export` command, ignore
 the service labels with keys 'configUID' and `configUID`
 see knative/serving#10539 for further details.

* Fix the var names per golint recommendation

* Ignore the serviceUID and configUID labels from revision labels

* Add e2e for verifying configUID and serviceUID labels are absent

* Add CHANGELOG
  • Loading branch information
navidshaikh authored Jan 20, 2021
1 parent 2cb77f6 commit dc52d5b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 8 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
| https://github.com/knative/client/pull/[#]
////

## Unreleased
[cols="1,10,3", options="header", width="100%"]
|===
| | Description | PR

| 🐣
| Do not print serviceUID and configUID labels in service export result
| https://github.com/knative/client/pull/1194[#1194]
|===

## v0.20.0 (2021-01-14)
[cols="1,10,3", options="header", width="100%"]
|===
Expand Down
42 changes: 37 additions & 5 deletions pkg/kn/commands/service/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,36 @@ import (
servingv1 "knative.dev/serving/pkg/apis/serving/v1"
)

var IGNORED_SERVICE_ANNOTATIONS = []string{
// IgnoredServiceAnnotations defines the annotation keys which should be
// removed from service annotations before export
var IgnoredServiceAnnotations = []string{
"serving.knative.dev/creator",
"serving.knative.dev/lastModifier",
"kubectl.kubernetes.io/last-applied-configuration",
}
var IGNORED_REVISION_ANNOTATIONS = []string{

// IgnoredRevisionAnnotations defines the annotation keys which should be
// removed from revision annotations before export
var IgnoredRevisionAnnotations = []string{
"serving.knative.dev/lastPinned",
"serving.knative.dev/creator",
"serving.knative.dev/routingStateModified",
}

// IgnoredServiceLabels defines the label keys which should be removed
// from service labels before export
var IgnoredServiceLabels = []string{
"serving.knative.dev/configUID",
"serving.knative.dev/serviceUID",
}

// IgnoredRevisionLabels defines the label keys which should be removed
// from revision labels before export
var IgnoredRevisionLabels = []string{
"serving.knative.dev/configUID",
"serving.knative.dev/serviceUID",
}

// NewServiceExportCommand returns a new command for exporting a service.
func NewServiceExportCommand(p *commands.KnParams) *cobra.Command {

Expand Down Expand Up @@ -161,7 +180,7 @@ func exportLatestService(latestSvc *servingv1.Service, withRoutes bool) *serving
}

stripIgnoredAnnotationsFromService(&exportedSvc)

stripIgnoredLabelsFromService(&exportedSvc)
return &exportedSvc
}

Expand All @@ -177,6 +196,7 @@ func exportRevision(revision *servingv1.Revision) servingv1.Revision {

exportedRevision.Spec = revision.Spec
stripIgnoredAnnotationsFromRevision(&exportedRevision)
stripIgnoredLabelsFromRevision(&exportedRevision)
return exportedRevision
}

Expand Down Expand Up @@ -323,13 +343,25 @@ func revisionListSortFunc(revisionList *servingv1.RevisionList) func(i int, j in
}

func stripIgnoredAnnotationsFromService(svc *servingv1.Service) {
for _, annotation := range IGNORED_SERVICE_ANNOTATIONS {
for _, annotation := range IgnoredServiceAnnotations {
delete(svc.ObjectMeta.Annotations, annotation)
}
}

func stripIgnoredAnnotationsFromRevision(revision *servingv1.Revision) {
for _, annotation := range IGNORED_REVISION_ANNOTATIONS {
for _, annotation := range IgnoredRevisionAnnotations {
delete(revision.ObjectMeta.Annotations, annotation)
}
}

func stripIgnoredLabelsFromService(svc *servingv1.Service) {
for _, label := range IgnoredServiceLabels {
delete(svc.ObjectMeta.Labels, label)
}
}

func stripIgnoredLabelsFromRevision(rev *servingv1.Revision) {
for _, label := range IgnoredRevisionLabels {
delete(rev.ObjectMeta.Labels, label)
}
}
25 changes: 22 additions & 3 deletions test/e2e/service_export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,30 @@ func TestServiceExport(t *testing.T) {
test.WithRevisionEnv(corev1.EnvVar{Name: "a", Value: "mouse"}),
))),
), "--with-revisions", "--mode", "export", "-o", "yaml")

t.Log("create and export service 'foo' and verify that serviceUID and configUID labels are absent")
serviceCreateWithOptions(r, "foo")
output := serviceExportOutput(r, "foo", "-o", "json")
actSvc := servingv1.Service{}
err = json.Unmarshal([]byte(output), &actSvc)
assert.NilError(t, err)
_, ok := actSvc.Labels["serving.knative.dev/serviceUID"]
assert.Equal(t, ok, false)
_, ok = actSvc.Labels["serving.knative.dev/configUID"]
assert.Equal(t, ok, false)
_, ok = actSvc.Spec.ConfigurationSpec.Template.Labels["serving.knative.dev/servingUID"]
assert.Equal(t, ok, false)
_, ok = actSvc.Spec.ConfigurationSpec.Template.Labels["serving.knative.dev/configUID"]
assert.Equal(t, ok, false)
}

// Private methods
// serviceExportOutput returns the export output of given service
func serviceExportOutput(r *test.KnRunResultCollector, serviceName string, options ...string) string {
command := []string{"service", "export", serviceName}
command = append(command, options...)
out := r.KnTest().Kn().Run(command...)
return out.Stdout
}

func serviceExport(r *test.KnRunResultCollector, serviceName string, expService *servingv1.Service, options ...string) {
command := []string{"service", "export", serviceName}
Expand All @@ -254,8 +275,6 @@ func serviceExportWithRevisionList(r *test.KnRunResultCollector, serviceName str
r.AssertNoError(out)
}

// Private functions

func validateExportedService(t *testing.T, it *test.KnTest, out string, expService *servingv1.Service) {
actSvc := servingv1.Service{}
err := json.Unmarshal([]byte(out), &actSvc)
Expand Down

0 comments on commit dc52d5b

Please sign in to comment.