From dc52d5bc8ae48fd5781b06aca71540b93af3e612 Mon Sep 17 00:00:00 2001 From: Navid Shaikh Date: Wed, 20 Jan 2021 14:37:53 +0530 Subject: [PATCH] Do not print serviceUID and configUID labels in service export result (#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 https://github.com/knative/serving/pull/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 --- CHANGELOG.adoc | 10 ++++++++ pkg/kn/commands/service/export.go | 42 +++++++++++++++++++++++++++---- test/e2e/service_export_test.go | 25 +++++++++++++++--- 3 files changed, 69 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 755c28fd6d..7cef303949 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -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%"] |=== diff --git a/pkg/kn/commands/service/export.go b/pkg/kn/commands/service/export.go index 55326677b0..f8e92b0bbd 100644 --- a/pkg/kn/commands/service/export.go +++ b/pkg/kn/commands/service/export.go @@ -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 { @@ -161,7 +180,7 @@ func exportLatestService(latestSvc *servingv1.Service, withRoutes bool) *serving } stripIgnoredAnnotationsFromService(&exportedSvc) - + stripIgnoredLabelsFromService(&exportedSvc) return &exportedSvc } @@ -177,6 +196,7 @@ func exportRevision(revision *servingv1.Revision) servingv1.Revision { exportedRevision.Spec = revision.Spec stripIgnoredAnnotationsFromRevision(&exportedRevision) + stripIgnoredLabelsFromRevision(&exportedRevision) return exportedRevision } @@ -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) + } +} diff --git a/test/e2e/service_export_test.go b/test/e2e/service_export_test.go index d15a9bd9d4..f17c9766f2 100644 --- a/test/e2e/service_export_test.go +++ b/test/e2e/service_export_test.go @@ -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} @@ -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)