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

Do not print serviceUID and configUID labels in service export result #1194

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a link on the API or docs repo where these are to be found?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

they are in the main branch of serving (and will be part of 0.21 release) knative/serving#10539
/cc @duglin might know better if the docs etc are in order

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) {
navidshaikh marked this conversation as resolved.
Show resolved Hide resolved
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