From 793718e51065d6c05f044c5d172c52caf79aac0a Mon Sep 17 00:00:00 2001 From: Shashankft9 Date: Mon, 15 Apr 2024 15:27:27 +0530 Subject: [PATCH] basic setup with experimental features working, removing general subsection in favour of original code --- pkg/kn/commands/container/add.go | 3 +- pkg/kn/commands/service/apply.go | 3 +- .../service/configuration_edit_flags.go | 12 ++--- pkg/kn/commands/service/create.go | 29 +++++++++++- pkg/kn/commands/service/update.go | 3 +- pkg/kn/commands/source/container/create.go | 3 +- pkg/kn/commands/source/container/update.go | 3 +- pkg/kn/flags/podspec.go | 44 +++++++++---------- 8 files changed, 60 insertions(+), 40 deletions(-) diff --git a/pkg/kn/commands/container/add.go b/pkg/kn/commands/container/add.go index be30ab5beb..3293a60395 100644 --- a/pkg/kn/commands/container/add.go +++ b/pkg/kn/commands/container/add.go @@ -76,9 +76,8 @@ func NewContainerAddCommand(p *commands.KnParams) *cobra.Command { }, } fss := cliflag.NamedFlagSets{} - generalFlagSet := fss.FlagSet("general") experimentalFlagSet := fss.FlagSet("experimental") - podSpecFlags.AddFlags(cmd.Flags(), generalFlagSet, experimentalFlagSet) + podSpecFlags.AddFlags(cmd.Flags(), experimentalFlagSet) podSpecFlags.AddUpdateFlags(cmd.Flags()) // Volume is not part of ContainerSpec cmd.Flag("volume").Hidden = true diff --git a/pkg/kn/commands/service/apply.go b/pkg/kn/commands/service/apply.go index 00ceeea0eb..b48986ce03 100644 --- a/pkg/kn/commands/service/apply.go +++ b/pkg/kn/commands/service/apply.go @@ -101,10 +101,9 @@ func NewServiceApplyCommand(p *commands.KnParams) *cobra.Command { }, } fss := cliflag.NamedFlagSets{} - generalFlagSet := fss.FlagSet("general") experimentalFlagSet := fss.FlagSet("experimental") commands.AddNamespaceFlags(serviceApplyCommand.Flags(), false) - applyFlags.AddCreateFlags(serviceApplyCommand, generalFlagSet, experimentalFlagSet) + applyFlags.AddCreateFlags(serviceApplyCommand, experimentalFlagSet) waitFlags.AddConditionWaitFlags(serviceApplyCommand, commands.WaitDefaultTimeout, "apply", "service", "ready") cols, _, _ := term.TerminalSize(serviceApplyCommand.OutOrStdout()) setUsageAndHelpFunc(serviceApplyCommand, fss, cols) diff --git a/pkg/kn/commands/service/configuration_edit_flags.go b/pkg/kn/commands/service/configuration_edit_flags.go index dde8a34c5d..ca16c7ccf3 100644 --- a/pkg/kn/commands/service/configuration_edit_flags.go +++ b/pkg/kn/commands/service/configuration_edit_flags.go @@ -79,8 +79,8 @@ func (p *ConfigurationEditFlags) markFlagMakesRevision(f string) { } // addSharedFlags adds the flags common between create & update. -func (p *ConfigurationEditFlags) addSharedFlags(command *cobra.Command, generalFlagSet, experimentalFlagSet *pflag.FlagSet) { - flagNames := p.PodSpecFlags.AddFlags(command.Flags(), generalFlagSet, experimentalFlagSet) +func (p *ConfigurationEditFlags) addSharedFlags(command *cobra.Command, experimentalFlagSet *pflag.FlagSet) { + flagNames := p.PodSpecFlags.AddFlags(command.Flags(), experimentalFlagSet) for _, name := range flagNames { p.markFlagMakesRevision(name) } @@ -198,8 +198,8 @@ func (p *ConfigurationEditFlags) addSharedFlags(command *cobra.Command, generalF } // AddUpdateFlags adds the flags specific to update. -func (p *ConfigurationEditFlags) AddUpdateFlags(command *cobra.Command, generalFlagSet, experimentalFlagSet *pflag.FlagSet) { - p.addSharedFlags(command, generalFlagSet, experimentalFlagSet) +func (p *ConfigurationEditFlags) AddUpdateFlags(command *cobra.Command, experimentalFlagSet *pflag.FlagSet) { + p.addSharedFlags(command, experimentalFlagSet) flagNames := p.PodSpecFlags.AddUpdateFlags(command.Flags()) for _, name := range flagNames { @@ -219,8 +219,8 @@ func (p *ConfigurationEditFlags) AddUpdateFlags(command *cobra.Command, generalF } // AddCreateFlags adds the flags specific to create -func (p *ConfigurationEditFlags) AddCreateFlags(command *cobra.Command, generalFlagSet, experimentalFlagSet *pflag.FlagSet) { - p.addSharedFlags(command, generalFlagSet, experimentalFlagSet) +func (p *ConfigurationEditFlags) AddCreateFlags(command *cobra.Command, experimentalFlagSet *pflag.FlagSet) { + p.addSharedFlags(command, experimentalFlagSet) flagNames := p.PodSpecFlags.AddCreateFlags(command.Flags()) for _, name := range flagNames { diff --git a/pkg/kn/commands/service/create.go b/pkg/kn/commands/service/create.go index 9dff7ff0f5..35fb143e02 100644 --- a/pkg/kn/commands/service/create.go +++ b/pkg/kn/commands/service/create.go @@ -15,9 +15,11 @@ package service import ( + "bytes" "context" "errors" "fmt" + "github.com/spf13/pflag" "io" cliflag "k8s.io/component-base/cli/flag" "k8s.io/component-base/term" @@ -173,11 +175,10 @@ func NewServiceCreateCommand(p *commands.KnParams) *cobra.Command { }, } fss := cliflag.NamedFlagSets{} - generalFlagSet := fss.FlagSet("general") experimentalFlagSet := fss.FlagSet("experimental") commands.AddNamespaceFlags(serviceCreateCommand.Flags(), false) commands.AddGitOpsFlags(serviceCreateCommand.Flags()) - editFlags.AddCreateFlags(serviceCreateCommand, generalFlagSet, experimentalFlagSet) + editFlags.AddCreateFlags(serviceCreateCommand, experimentalFlagSet) trafficFlags.AddTagFlag(serviceCreateCommand) waitFlags.AddConditionWaitFlags(serviceCreateCommand, commands.WaitDefaultTimeout, "create", "service", "ready") cols, _, _ := term.TerminalSize(serviceCreateCommand.OutOrStdout()) @@ -215,10 +216,34 @@ func setUsageAndHelpFunc(cmd *cobra.Command, fss cliflag.NamedFlagSets, cols int if cmd.HasAvailableSubCommands() { fmt.Fprintf(cmd.OutOrStdout(), "%s\n", strings.Join(generatesAvailableSubCommands(cmd), "\n")) } + printGeneralFlags(cmd.OutOrStdout(), cmd.Flags(), cols) cliflag.PrintSections(cmd.OutOrStdout(), fss, cols) }) } +func printGeneralFlags(w io.Writer, fs *pflag.FlagSet, cols int) { + wideFS := pflag.NewFlagSet("", pflag.ExitOnError) + wideFS.AddFlagSet(fs) + + var zzz string + if cols > 24 { + zzz = strings.Repeat("z", cols-24) + wideFS.Int(zzz, 0, strings.Repeat("z", cols-24)) + } + + var buf bytes.Buffer + fmt.Fprintf(&buf, "\n%s flags:\n\n%s", "General", wideFS.FlagUsagesWrapped(cols)) + + if cols > 24 { + i := strings.Index(buf.String(), zzz) + lines := strings.Split(buf.String()[:i], "\n") + fmt.Fprint(w, strings.Join(lines[:len(lines)-1], "\n")) + fmt.Fprintln(w) + } else { + fmt.Fprint(w, buf.String()) + } +} + func createService(ctx context.Context, client clientservingv1.KnServingClient, service *servingv1.Service, waitFlags commands.WaitFlags, out io.Writer, targetFlag string) error { err := client.CreateService(ctx, service) if err != nil { diff --git a/pkg/kn/commands/service/update.go b/pkg/kn/commands/service/update.go index 84d8586cea..38ba2f8c3a 100644 --- a/pkg/kn/commands/service/update.go +++ b/pkg/kn/commands/service/update.go @@ -160,11 +160,10 @@ func NewServiceUpdateCommand(p *commands.KnParams) *cobra.Command { }, } fss := cliflag.NamedFlagSets{} - generalFlagSet := fss.FlagSet("general") experimentalFlagSet := fss.FlagSet("experimental") commands.AddNamespaceFlags(serviceUpdateCommand.Flags(), false) commands.AddGitOpsFlags(serviceUpdateCommand.Flags()) - editFlags.AddUpdateFlags(serviceUpdateCommand, generalFlagSet, experimentalFlagSet) + editFlags.AddUpdateFlags(serviceUpdateCommand, experimentalFlagSet) waitFlags.AddConditionWaitFlags(serviceUpdateCommand, commands.WaitDefaultTimeout, "update", "service", "ready") trafficFlags.Add(serviceUpdateCommand) cols, _, _ := term.TerminalSize(serviceUpdateCommand.OutOrStdout()) diff --git a/pkg/kn/commands/source/container/create.go b/pkg/kn/commands/source/container/create.go index 3f4b305726..bd311b6b40 100644 --- a/pkg/kn/commands/source/container/create.go +++ b/pkg/kn/commands/source/container/create.go @@ -86,9 +86,8 @@ func NewContainerCreateCommand(p *commands.KnParams) *cobra.Command { } commands.AddNamespaceFlags(cmd.Flags(), false) fss := cliflag.NamedFlagSets{} - generalFlagSet := fss.FlagSet("general") experimentalFlagSet := fss.FlagSet("experimental") - podFlags.AddFlags(cmd.Flags(), generalFlagSet, experimentalFlagSet) + podFlags.AddFlags(cmd.Flags(), experimentalFlagSet) podFlags.AddUpdateFlags(cmd.Flags()) sinkFlags.Add(cmd) cmd.MarkFlagRequired("image") diff --git a/pkg/kn/commands/source/container/update.go b/pkg/kn/commands/source/container/update.go index ff40ba195f..107cdc8df3 100644 --- a/pkg/kn/commands/source/container/update.go +++ b/pkg/kn/commands/source/container/update.go @@ -96,8 +96,7 @@ func NewContainerUpdateCommand(p *commands.KnParams) *cobra.Command { commands.AddNamespaceFlags(cmd.Flags(), false) fss := cliflag.NamedFlagSets{} experimentalFlagSet := fss.FlagSet("experimental") - generalFlagSet := fss.FlagSet("general") - podFlags.AddFlags(cmd.Flags(), generalFlagSet, experimentalFlagSet) + podFlags.AddFlags(cmd.Flags(), experimentalFlagSet) podFlags.AddUpdateFlags(cmd.Flags()) sinkFlags.Add(cmd) return cmd diff --git a/pkg/kn/flags/podspec.go b/pkg/kn/flags/podspec.go index 9eff18bdc0..715bbb9ed2 100644 --- a/pkg/kn/flags/podspec.go +++ b/pkg/kn/flags/podspec.go @@ -133,20 +133,20 @@ func (p *PodSpecFlags) AddCreateFlags(flagset *pflag.FlagSet) []string { } // AddFlags will add PodSpec related flags to FlagSet -func (p *PodSpecFlags) AddFlags(flagset *pflag.FlagSet, generalFlagSet, experimentalFlagSet *pflag.FlagSet) []string { +func (p *PodSpecFlags) AddFlags(flagset *pflag.FlagSet, experimentalFlagSet *pflag.FlagSet) []string { flagNames := []string{} - generalFlagSet.VarP(&p.Image, "image", "", "Image to run.") + flagset.VarP(&p.Image, "image", "", "Image to run.") flagNames = append(flagNames, "image") - generalFlagSet.StringVar(&p.ImagePullPolicy, "pull-policy", "", + flagset.StringVar(&p.ImagePullPolicy, "pull-policy", "", "Image pull policy. Valid values (case insensitive): Always | Never | IfNotPresent") - generalFlagSet.StringVarP(&p.EnvFile, "env-file", "", "", "Path to a file containing environment variables (e.g. --env-file=/home/knative/service1/env).") + flagset.StringVarP(&p.EnvFile, "env-file", "", "", "Path to a file containing environment variables (e.g. --env-file=/home/knative/service1/env).") flagNames = append(flagNames, "env-file") - generalFlagSet.StringArrayVarP(&p.Mount, "mount", "", []string{}, + flagset.StringArrayVarP(&p.Mount, "mount", "", []string{}, "Mount a ConfigMap (prefix cm: or config-map:), a Secret (prefix secret: or sc:), an EmptyDir (prefix ed: or emptyDir:), "+ "a PersistentVolumeClaim (prefix pvc: or persistentVolumeClaim) or an existing Volume (without any prefix) on the specified directory. "+ "Example: --mount /mydir=cm:myconfigmap, --mount /mydir=secret:mysecret, --mount /mydir=emptyDir:myvol "+ @@ -159,7 +159,7 @@ func (p *PodSpecFlags) AddFlags(flagset *pflag.FlagSet, generalFlagSet, experime "For unmounting a directory, append \"-\", e.g. --mount /mydir-, which also removes any auto-generated volume.") flagNames = append(flagNames, "mount") - generalFlagSet.StringArrayVarP(&p.Volume, "volume", "", []string{}, + flagset.StringArrayVarP(&p.Volume, "volume", "", []string{}, "Add a volume from a ConfigMap (prefix cm: or config-map:) a Secret (prefix secret: or sc:), "+ "an EmptyDir (prefix ed: or emptyDir:) or a PersistentVolumeClaim (prefix pvc: or persistentVolumeClaim). "+ "PersistentVolumeClaim and EmptyDir only works if the feature gate is enabled in knative serving. Example: --volume myvolume=cm:myconfigmap, --volume myvolume=secret:mysecret or --volume emptyDir:myvol:size=1Gi,type=Memory. "+ @@ -167,24 +167,24 @@ func (p *PodSpecFlags) AddFlags(flagset *pflag.FlagSet, generalFlagSet, experime "To unset a ConfigMap/Secret reference, append \"-\" to the name, e.g. --volume myvolume-.") flagNames = append(flagNames, "volume") - generalFlagSet.StringArrayVarP(&p.Command, "cmd", "", []string{}, + flagset.StringArrayVarP(&p.Command, "cmd", "", []string{}, "Specify command to be used as entrypoint instead of default one. "+ "Example: --cmd /app/start or --cmd sh --cmd /app/start.sh or --cmd /app/start --arg myArg to pass additional arguments.") flagNames = append(flagNames, "cmd") - generalFlagSet.StringArrayVarP(&p.Arg, "arg", "", []string{}, + flagset.StringArrayVarP(&p.Arg, "arg", "", []string{}, "Add argument to the container command. "+ "Example: --arg myArg1 --arg --myArg2 --arg myArg3=3. "+ "You can use this flag multiple times.") flagNames = append(flagNames, "arg") // DEPRECATED since 1.0 - generalFlagSet.StringVarP(&p.ExtraContainers, "extra-containers", "", "", + flagset.StringVarP(&p.ExtraContainers, "extra-containers", "", "", "Deprecated, use --containers instead.") - generalFlagSet.MarkHidden("extra-containers") + flagset.MarkHidden("extra-containers") flagNames = append(flagNames, "containers") - generalFlagSet.StringVarP(&p.ExtraContainers, "containers", "", "", + flagset.StringVarP(&p.ExtraContainers, "containers", "", "", "Specify path to file including definition for additional containers, alternatively use '-' to read from stdin. "+ "Example: --containers ./containers.yaml or --containers -.") flagNames = append(flagNames, "containers") @@ -194,20 +194,20 @@ func (p *PodSpecFlags) AddFlags(flagset *pflag.FlagSet, generalFlagSet, experime "Format: [http,https]:host:port:path, exec:cmd[,cmd,...], tcp:host:port." commonProbeOptsDesc := "Common opts (comma separated, case insensitive): InitialDelaySeconds=, FailureThreshold=, " + "SuccessThreshold=, PeriodSeconds=, TimeoutSeconds=" - generalFlagSet.StringVarP(&p.LivenessProbe, "probe-liveness", "", "", "Add liveness probe to Service deployment. "+ + flagset.StringVarP(&p.LivenessProbe, "probe-liveness", "", "", "Add liveness probe to Service deployment. "+ commonProbeDescription) flagNames = append(flagNames, "probe-liveness") - generalFlagSet.StringVarP(&p.LivenessProbeOpts, "probe-liveness-opts", "", "", "Add common options to liveness probe. "+ + flagset.StringVarP(&p.LivenessProbeOpts, "probe-liveness-opts", "", "", "Add common options to liveness probe. "+ commonProbeOptsDesc) flagNames = append(flagNames, "probe-liveness-opts") - generalFlagSet.StringVarP(&p.ReadinessProbe, "probe-readiness", "", "", "Add readiness probe to Service deployment. "+ + flagset.StringVarP(&p.ReadinessProbe, "probe-readiness", "", "", "Add readiness probe to Service deployment. "+ commonProbeDescription) flagNames = append(flagNames, "probe-readiness") - generalFlagSet.StringVarP(&p.ReadinessProbeOpts, "probe-readiness-opts", "", "", "Add common options to readiness probe. "+ + flagset.StringVarP(&p.ReadinessProbeOpts, "probe-readiness-opts", "", "", "Add common options to readiness probe. "+ commonProbeOptsDesc) flagNames = append(flagNames, "probe-liveness-opts") - generalFlagSet.StringSliceVar(&p.Resources.Limits, + flagset.StringSliceVar(&p.Resources.Limits, "limit", nil, "The resource requirement limits for this Service. For example, 'cpu=100m,memory=256Mi'. "+ @@ -215,7 +215,7 @@ func (p *PodSpecFlags) AddFlags(flagset *pflag.FlagSet, generalFlagSet, experime "To unset a resource limit, append \"-\" to the resource name, e.g. '--limit memory-'.") flagNames = append(flagNames, "limit") - generalFlagSet.StringSliceVar(&p.Resources.Requests, + flagset.StringSliceVar(&p.Resources.Requests, "request", nil, "The resource requirement requests for this Service. For example, 'cpu=100m,memory=256Mi'. "+ @@ -223,24 +223,24 @@ func (p *PodSpecFlags) AddFlags(flagset *pflag.FlagSet, generalFlagSet, experime "To unset a resource request, append \"-\" to the resource name, e.g. '--request cpu-'.") flagNames = append(flagNames, "request") - generalFlagSet.StringVarP(&p.Port, "port", "p", "", "The port where application listens on, in the format 'NAME:PORT', where 'NAME' is optional. Examples: '--port h2c:8080' , '--port 8080'.") + flagset.StringVarP(&p.Port, "port", "p", "", "The port where application listens on, in the format 'NAME:PORT', where 'NAME' is optional. Examples: '--port h2c:8080' , '--port 8080'.") flagNames = append(flagNames, "port") - generalFlagSet.StringVar(&p.ServiceAccountName, + flagset.StringVar(&p.ServiceAccountName, "service-account", "", "Service account name to set. An empty argument (\"\") clears the service account. The referenced service account must exist in the service's namespace.") flagNames = append(flagNames, "service-account") - generalFlagSet.StringVar(&p.ImagePullSecrets, + flagset.StringVar(&p.ImagePullSecrets, "pull-secret", "", "Image pull secret to set. An empty argument (\"\") clears the pull secret. The referenced secret must exist in the service's namespace.") flagNames = append(flagNames, "pull-secret") - generalFlagSet.Int64VarP(&p.User, "user", "", 0, "The user ID to run the container (e.g., 1001).") + flagset.Int64VarP(&p.User, "user", "", 0, "The user ID to run the container (e.g., 1001).") flagNames = append(flagNames, "user") - generalFlagSet.StringVar(&p.SecurityContext, "security-context", "none", "Predefined security context for the service. Accepted values: 'none' for no security context "+ + flagset.StringVar(&p.SecurityContext, "security-context", "none", "Predefined security context for the service. Accepted values: 'none' for no security context "+ "and 'strict' for dropping all capabilities, running as non-root, and no privilege escalation.") flagNames = append(flagNames, "security-context")