From 4787dd0f53e4bb60676c551ace31ab99f2ea5e4c Mon Sep 17 00:00:00 2001 From: thiagomedina Date: Tue, 12 Aug 2025 12:21:03 -0300 Subject: [PATCH 1/3] feat: add include flag to export --- pkg/commands/service/export.go | 39 +++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/pkg/commands/service/export.go b/pkg/commands/service/export.go index 6f7db33a2f..a3da43e71e 100644 --- a/pkg/commands/service/export.go +++ b/pkg/commands/service/export.go @@ -18,6 +18,7 @@ import ( "context" "errors" "fmt" + "strings" clientserving "knative.dev/client/pkg/serving" @@ -70,8 +71,9 @@ var IgnoredRevisionLabels = []string{ } const ( - ModeReplay = "replay" - ModeExport = "export" + ModeReplay = "replay" + ModeExport = "export" + SecurityContext = "securityContext" ) // NewServiceExportCommand returns a new command for exporting a service. @@ -94,7 +96,11 @@ func NewServiceExportCommand(p *commands.KnParams) *cobra.Command { kn service export foo --with-revisions --mode=export -n bar -o json # Export services in kubectl friendly format, as a list kind, one service item for each revision (Beta) - kn service export foo --with-revisions --mode=replay -n bar -o json`, + kn service export foo --with-revisions --mode=replay -n bar -o json + + # Export a service with securityContext (Beta) + kn service export foo --with-revisions --mode=replay --include securityContext -n bar -o json`, + RunE: func(cmd *cobra.Command, args []string) error { if len(args) != 1 { return errors.New("'kn service export' requires name of the service as single argument") @@ -129,6 +135,7 @@ func NewServiceExportCommand(p *commands.KnParams) *cobra.Command { commands.AddNamespaceFlags(flags, false) flags.Bool("with-revisions", false, "Export all routed revisions (Beta)") flags.String("mode", "", "Format for exporting all routed revisions. One of replay|export (Beta)") + flags.StringSlice("include", []string{}, "Specify comma separated list of key=value") machineReadablePrintFlags.AddFlags(command) return command } @@ -139,6 +146,11 @@ func exportService(cmd *cobra.Command, service *servingv1.Service, client client return err } + includeFlags, err := cmd.Flags().GetStringSlice("include") + if err != nil { + return err + } + mode, err := cmd.Flags().GetString("mode") if err != nil { return err @@ -149,6 +161,11 @@ func exportService(cmd *cobra.Command, service *servingv1.Service, client client if err != nil { return err } + if servicesList, ok := svcList.(*servingv1.ServiceList); ok { + for i := range servicesList.Items { + cleanupServiceBeforeExport(&servicesList.Items[i], includeFlags) + } + } return printer.PrintObj(svcList, cmd.OutOrStdout()) } // default is export mode @@ -156,6 +173,8 @@ func exportService(cmd *cobra.Command, service *servingv1.Service, client client if err != nil { return err } + + cleanupServiceBeforeExport(&knExport.Spec.Service, includeFlags) //print kn export return printer.PrintObj(knExport, cmd.OutOrStdout()) } @@ -390,3 +409,17 @@ func stripIgnoredLabelsFromRevisionTemplate(template *servingv1.RevisionTemplate delete(template.ObjectMeta.Labels, label) } } + +func cleanupServiceBeforeExport(svc *servingv1.Service, includeFlags []string) { + hasFlag := make(map[string]bool) + for _, flag := range includeFlags { + hasFlag[strings.ToLower(flag)] = true + } + + if ok := hasFlag[strings.ToLower(SecurityContext)]; !ok { + svc.Spec.Template.Spec.SecurityContext = nil + for i := range svc.Spec.Template.Spec.Containers { + svc.Spec.Template.Spec.Containers[i].SecurityContext = nil + } + } +} From b2a36e819a430ec44f12d2ba113db9a6925c62b4 Mon Sep 17 00:00:00 2001 From: thiagomedina Date: Tue, 12 Aug 2025 13:12:55 -0300 Subject: [PATCH 2/3] fix: improve help message --- pkg/commands/service/export.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/commands/service/export.go b/pkg/commands/service/export.go index a3da43e71e..6847540556 100644 --- a/pkg/commands/service/export.go +++ b/pkg/commands/service/export.go @@ -135,7 +135,7 @@ func NewServiceExportCommand(p *commands.KnParams) *cobra.Command { commands.AddNamespaceFlags(flags, false) flags.Bool("with-revisions", false, "Export all routed revisions (Beta)") flags.String("mode", "", "Format for exporting all routed revisions. One of replay|export (Beta)") - flags.StringSlice("include", []string{}, "Specify comma separated list of key=value") + flags.StringSlice("include", []string{}, "Specify a list of fields to include. e.g.: --include securityContext,namespace") machineReadablePrintFlags.AddFlags(command) return command } From 6a622a91cd9aa354736cd8e93da97da40785db9c Mon Sep 17 00:00:00 2001 From: thiagomedina Date: Mon, 13 Oct 2025 10:56:39 -0300 Subject: [PATCH 3/3] run ./hack/build.sh --- docs/cmd/kn_service_export.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/cmd/kn_service_export.md b/docs/cmd/kn_service_export.md index 9cdb3abec1..30196021b0 100644 --- a/docs/cmd/kn_service_export.md +++ b/docs/cmd/kn_service_export.md @@ -21,6 +21,9 @@ kn service export NAME # Export services in kubectl friendly format, as a list kind, one service item for each revision (Beta) kn service export foo --with-revisions --mode=replay -n bar -o json + + # Export a service with securityContext (Beta) + kn service export foo --with-revisions --mode=replay --include securityContext -n bar -o json ``` ### Options @@ -28,6 +31,7 @@ kn service export NAME ``` --allow-missing-template-keys If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats. (default true) -h, --help help for export + --include strings Specify a list of fields to include. e.g.: --include securityContext,namespace --mode string Format for exporting all routed revisions. One of replay|export (Beta) -n, --namespace string Specify the namespace to operate in. -o, --output string Output format. One of: (json, yaml, name, go-template, go-template-file, template, templatefile, jsonpath, jsonpath-as-json, jsonpath-file).