Skip to content
Open
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
39 changes: 36 additions & 3 deletions pkg/commands/service/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"errors"
"fmt"
"strings"

clientserving "knative.dev/client/pkg/serving"

Expand Down Expand Up @@ -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.
Expand All @@ -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")
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -149,13 +161,20 @@ 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
knExport, err := exportForKNImport(cmd.Context(), service.DeepCopy(), client, withRevisions)
if err != nil {
return err
}

cleanupServiceBeforeExport(&knExport.Spec.Service, includeFlags)
//print kn export
return printer.PrintObj(knExport, cmd.OutOrStdout())
}
Expand Down Expand Up @@ -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
}
}
}
Loading