Skip to content

Commit

Permalink
Fix: fix apply and exec command
Browse files Browse the repository at this point in the history
Signed-off-by: songyang.song <[email protected]>
  • Loading branch information
songyang.song committed Feb 10, 2023
1 parent 27806d0 commit e64767d
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 87 deletions.
85 changes: 21 additions & 64 deletions saectl/internal/cmd/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ var (
# Apply the configuration from all files that end with '.json' - i.e. expand wildcard characters in file names
saectl apply -f '*.json'`))

warningNoLastAppliedConfigAnnotation = "Warning: resource %[1]s is missing the %[2]s annotation which is required by %[3]s apply. %[3]s apply should only be used on resources created declaratively by either %[3]s create --save-config or %[3]s apply. The missing annotation will be patched automatically.\n"
warningChangesOnDeletingResource = "Warning: Detected changes to resource %[1]s which is currently being deleted.\n"
//warningNoLastAppliedConfigAnnotation = "Warning: resource %[1]s is missing the %[2]s annotation which is required by %[3]s apply. %[3]s apply should only be used on resources created declaratively by either %[3]s create --save-config or %[3]s apply. The missing annotation will be patched automatically.\n"
warningChangesOnDeletingResource = "Warning: Detected changes to resource %[1]s which is currently being deleted.\n"
)

// NewApplyFlags returns a default ApplyFlags
Expand Down Expand Up @@ -175,31 +175,20 @@ func NewCmdApply(baseName string, f cmdutil.Factory, ioStreams genericclioptions

flags.AddFlags(cmd)

// apply subcommands
//cmd.AddCommand(NewCmdApplyViewLastApplied(flags.Factory, flags.IOStreams))
//cmd.AddCommand(NewCmdApplySetLastApplied(flags.Factory, flags.IOStreams))
//cmd.AddCommand(NewCmdApplyEditLastApplied(flags.Factory, flags.IOStreams))

return cmd
}

// AddFlags registers flags for a cli
func (flags *ApplyFlags) AddFlags(cmd *cobra.Command) {
// bind flag structs
flags.DeleteFlags.AddFlags(cmd)
//flags.RecordFlags.AddFlags(cmd)
flags.PrintFlags.AddFlags(cmd)

//cmdutil.AddValidateFlags(cmd)
//cmdutil.AddDryRunFlag(cmd)
//cmdutil.AddServerSideApplyFlags(cmd)
//cmdutil.AddFieldManagerFlagVar(cmd, &flags.FieldManager, FieldManagerClientSideApply)
cmdutil.AddDryRunFlag(cmd)
cmdutil.AddLabelSelectorFlagVar(cmd, &flags.Selector)

cmd.Flags().BoolVar(&flags.Overwrite, "overwrite", flags.Overwrite, "Automatically resolve conflicts between the modified and live configuration by using values from the modified configuration")
//cmd.Flags().BoolVar(&flags.Prune, "prune", flags.Prune, "Automatically delete resource objects, that do not appear in the configs and are created by either apply or create --save-config. Should be used with either -l or --all.")
cmd.Flags().BoolVar(&flags.All, "all", flags.All, "Select all resources in the namespace of the specified resource types.")
//cmd.Flags().StringArrayVar(&flags.PruneWhitelist, "prune-whitelist", flags.PruneWhitelist, "Overwrite the default whitelist with <group/version/kind> for --prune")
cmd.Flags().BoolVar(&flags.OpenAPIPatch, "openapi-patch", flags.OpenAPIPatch, "If true, use openapi to calculate diff when the openapi presents and the resource can be found in the openapi spec. Otherwise, fall back to use baked-in types.")
}

Expand All @@ -209,8 +198,6 @@ func (flags *ApplyFlags) ToOptions(cmd *cobra.Command, baseName string, args []s
return nil, cmdutil.UsageErrorf(cmd, "Unexpected args: %v", args)
}

serverSideApply := cmdutil.GetServerSideApplyFlag(cmd)
forceConflicts := cmdutil.GetForceConflictsFlag(cmd)
dryRunStrategy, err := cmdutil.GetDryRunStrategy(cmd)
if err != nil {
return nil, err
Expand All @@ -222,8 +209,6 @@ func (flags *ApplyFlags) ToOptions(cmd *cobra.Command, baseName string, args []s
}

dryRunVerifier := resource.NewQueryParamVerifier(dynamicClient, flags.Factory.OpenAPIGetter(), resource.QueryParamDryRun)
fieldValidationVerifier := resource.NewQueryParamVerifier(dynamicClient, flags.Factory.OpenAPIGetter(), resource.QueryParamFieldValidation)
fieldManager := GetApplyFieldManagerFlag(cmd, serverSideApply)

// allow for a success message operation to be specified at print time
toPrinter := func(operation string) (printers.ResourcePrinter, error) {
Expand All @@ -250,14 +235,6 @@ func (flags *ApplyFlags) ToOptions(cmd *cobra.Command, baseName string, args []s

openAPISchema, _ := flags.Factory.OpenAPISchema()

validationDirective, err := cmdutil.GetValidationDirective(cmd)
if err != nil {
return nil, err
}
validator, err := flags.Factory.Validator(validationDirective, fieldValidationVerifier)
if err != nil {
return nil, err
}
builder := flags.Factory.NewBuilder()
mapper, err := flags.Factory.ToRESTMapper()
if err != nil {
Expand All @@ -269,44 +246,31 @@ func (flags *ApplyFlags) ToOptions(cmd *cobra.Command, baseName string, args []s
return nil, err
}

if flags.Prune {
flags.PruneResources, err = prune.ParseResources(mapper, flags.PruneWhitelist)
if err != nil {
return nil, err
}
}

o := &ApplyOptions{
// Store baseName for use in printing warnings / messages involving the base command name.
// This is useful for downstream command that wrap this one.
cmdBaseName: baseName,

PrintFlags: flags.PrintFlags,

DeleteOptions: deleteOptions,
ToPrinter: toPrinter,
ServerSideApply: serverSideApply,
ForceConflicts: forceConflicts,
FieldManager: fieldManager,
Selector: flags.Selector,
DryRunStrategy: dryRunStrategy,
DryRunVerifier: dryRunVerifier,
Prune: flags.Prune,
PruneResources: flags.PruneResources,
All: flags.All,
Overwrite: flags.Overwrite,
OpenAPIPatch: flags.OpenAPIPatch,
PruneWhitelist: flags.PruneWhitelist,

Recorder: recorder,
Namespace: namespace,
EnforceNamespace: enforceNamespace,
Validator: validator,
ValidationDirective: validationDirective,
Builder: builder,
Mapper: mapper,
DynamicClient: dynamicClient,
OpenAPISchema: openAPISchema,
DeleteOptions: deleteOptions,
ToPrinter: toPrinter,
Selector: flags.Selector,
DryRunStrategy: dryRunStrategy,
DryRunVerifier: dryRunVerifier,

All: flags.All,
Overwrite: flags.Overwrite,
OpenAPIPatch: flags.OpenAPIPatch,

Recorder: recorder,
Namespace: namespace,
EnforceNamespace: enforceNamespace,

Builder: builder,
Mapper: mapper,
DynamicClient: dynamicClient,
OpenAPISchema: openAPISchema,

IOStreams: flags.IOStreams,

Expand Down Expand Up @@ -589,12 +553,6 @@ See https://kubernetes.io/docs/reference/using-api/server-side-apply/#conflicts`
}

if o.DryRunStrategy != cmdutil.DryRunClient {
metadata, _ := meta.Accessor(info.Object)
annotationMap := metadata.GetAnnotations()
if _, ok := annotationMap[corev1.LastAppliedConfigAnnotation]; !ok {
fmt.Fprintf(o.ErrOut, warningNoLastAppliedConfigAnnotation, info.ObjectName(), corev1.LastAppliedConfigAnnotation, o.cmdBaseName)
}

patcher, err := newPatcher(o, info, helper)
if err != nil {
return err
Expand All @@ -603,7 +561,6 @@ See https://kubernetes.io/docs/reference/using-api/server-side-apply/#conflicts`
if err != nil {
return cmdutil.AddSourceToErr(fmt.Sprintf("applying patch:\n%s\nto:\n%v\nfor:", patchBytes, info), info.Source, err)
}

info.Refresh(patchedObject, true)

WarnIfDeleting(info.Object, o.ErrOut)
Expand Down
2 changes: 0 additions & 2 deletions saectl/internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,7 @@ func NewCommand(o CtlOption) *cobra.Command {

templates.ActsAsRootCommand(cmds, filters, groups...)

// TODO: add completion for get cmd
cmds.AddCommand(apiresources.NewCmdAPIResources(f, o.IOStreams))
//cmds.AddCommand(apiresources.NewCmdAPIVersions(f, o.IOStreams))
cmds.SetGlobalNormalizationFunc(cliflag.WordSepNormalizeFunc)
return cmds
}
Expand Down
17 changes: 5 additions & 12 deletions saectl/internal/cmd/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ var (
# Create a deployment(represents an application in sae) using the data in .json
saectl create -f ./deployment.json
# Create a pod based on the JSON passed into stdin
cat pod.json | saectl create -f -
# Create a deployment based on the JSON passed into stdin
cat deployment.json | saectl create -f -
# Edit the data in registry.yaml in JSON then create the resource using the edited data
saectl create -f registry.yaml --edit -o json`))
Expand All @@ -75,7 +75,6 @@ var (
func NewCreateOptions(ioStreams genericclioptions.IOStreams) *CreateOptions {
return &CreateOptions{
PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(scheme.Scheme),
// RecordFlags: genericclioptions.NewRecordFlags(),

Recorder: genericclioptions.NoopRecorder{},

Expand Down Expand Up @@ -108,20 +107,14 @@ func NewCmdCreate(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cob

usage := "to use to create the resource"
cmdutil.AddFilenameOptionFlags(cmd, &o.FilenameOptions, usage)
//cmdutil.AddValidateFlags(cmd)
cmdutil.AddValidateFlags(cmd)
cmd.Flags().BoolVar(&o.EditBeforeCreate, "edit", o.EditBeforeCreate, "Edit the API resource before creating")
cmd.Flags().Bool("windows-line-endings", runtime.GOOS == "windows",
"Only relevant if --edit=true. Defaults to the line ending native to your platform.")
//cmdutil.AddDryRunFlag(cmd)
cmdutil.AddApplyAnnotationFlags(cmd)
cmdutil.AddDryRunFlag(cmd)
o.PrintFlags.AddFlags(cmd)

// TODO: 1. Storing objects to annotations is currently not supported
//cmdutil.AddApplyAnnotationFlags(cmd)
// TODO: 2. Label selector is not supported for now
//cmdutil.AddLabelSelectorFlagVar(cmd, &o.Selector)
// TODO: 3. Server-Side Apply is not support for now
//cmdutil.AddFieldManagerFlagVar(cmd, &o.fieldManager, "saectl-create")

// create subcommands
cmd.AddCommand(create.NewCmdCreateNamespace(f, ioStreams))
cmd.AddCommand(create.NewCmdCreateConfigMap(f, ioStreams))
Expand Down
1 change: 0 additions & 1 deletion saectl/internal/cmd/patch/patch.go

This file was deleted.

5 changes: 1 addition & 4 deletions saectl/internal/cmd/scale/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func NewCmdScale(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobr
validArgs := []string{"deployment"}

cmd := &cobra.Command{
Use: "scale [--resource-version=version] [--current-replicas=count] --replicas=COUNT (-f FILENAME | TYPE NAME)",
Use: "scale [--current-replicas=count] --replicas=COUNT (-f FILENAME | TYPE NAME)",
DisableFlagsInUseLine: true,
Short: i18n.T("Set a new size for a deployment, replica set, or replication controller"),
Long: scaleLong,
Expand All @@ -103,18 +103,15 @@ func NewCmdScale(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobr
},
}

//o.RecordFlags.AddFlags(cmd)
o.PrintFlags.AddFlags(cmd)

cmd.Flags().BoolVar(&o.All, "all", o.All, "Select all resources in the namespace of the specified resource types")
cmd.Flags().StringVar(&o.ResourceVersion, "resource-version", o.ResourceVersion, i18n.T("Precondition for resource version. Requires that the current resource version match this value in order to scale."))
cmd.Flags().IntVar(&o.CurrentReplicas, "current-replicas", o.CurrentReplicas, "Precondition for current size. Requires that the current size of the resource match this value in order to scale. -1 (default) for no condition.")
cmd.Flags().IntVar(&o.Replicas, "replicas", o.Replicas, "The new desired number of replicas. Required.")
cmd.MarkFlagRequired("replicas")
cmd.Flags().DurationVar(&o.Timeout, "timeout", 0, "The length of time to wait before giving up on a scale operation, zero means don't wait. Any other values should contain a corresponding time unit (e.g. 1s, 2m, 3h).")
cmdutil.AddFilenameOptionFlags(cmd, &o.FilenameOptions, "identifying the resource to set a new size")
cmdutil.AddDryRunFlag(cmd)
cmdutil.AddLabelSelectorFlagVar(cmd, &o.Selector)
return cmd
}

Expand Down
3 changes: 0 additions & 3 deletions saectl/internal/cmd/set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ func NewCmdSet(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Co
// add subcommands
cmd.AddCommand(set.NewCmdImage(f, streams))
cmd.AddCommand(set.NewCmdResources(f, streams))
//cmd.AddCommand(NewCmdSelector(f, streams))
//cmd.AddCommand(NewCmdSubject(f, streams))
//cmd.AddCommand(NewCmdServiceAccount(f, streams))
cmd.AddCommand(set.NewCmdEnv(f, streams))

return cmd
Expand Down
2 changes: 1 addition & 1 deletion saectl/pkg/config/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,5 +215,5 @@ func (c *ClientConfig) ConfigAccess() clientcmd.ConfigAccess {
var _ clientcmd.ClientConfig = &ClientConfig{}

func genSAEClusterServerAddress(region string) string {
return fmt.Sprintf("sae-share.%s.aliyuncs.com", region)
return fmt.Sprintf("sae.%s.aliyuncs.com", region)
}

0 comments on commit e64767d

Please sign in to comment.