Skip to content

Commit

Permalink
basic setup with experimental features working, removing general subs…
Browse files Browse the repository at this point in the history
…ection in favour of original code
  • Loading branch information
Shashankft9 committed Apr 15, 2024
1 parent 4f844f2 commit 793718e
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 40 deletions.
3 changes: 1 addition & 2 deletions pkg/kn/commands/container/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions pkg/kn/commands/service/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions pkg/kn/commands/service/configuration_edit_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
29 changes: 27 additions & 2 deletions pkg/kn/commands/service/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions pkg/kn/commands/service/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
3 changes: 1 addition & 2 deletions pkg/kn/commands/source/container/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 1 addition & 2 deletions pkg/kn/commands/source/container/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 22 additions & 22 deletions pkg/kn/flags/podspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 "+
Expand All @@ -159,32 +159,32 @@ 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. "+
"You can use this flag multiple times. "+
"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")
Expand All @@ -194,53 +194,53 @@ 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=<int_value>, FailureThreshold=<int_value>, " +
"SuccessThreshold=<int_value>, PeriodSeconds=<int_value>, TimeoutSeconds=<int_value>"
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'. "+
"You can use this flag multiple times. "+
"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'. "+
"You can use this flag multiple times. "+
"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")

Expand Down

0 comments on commit 793718e

Please sign in to comment.