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). diff --git a/pkg/commands/service/export.go b/pkg/commands/service/export.go index 6f7db33a2f..6847540556 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 a list of fields to include. e.g.: --include securityContext,namespace") 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 + } + } +}