add --subresource to kubectl apply

Kubernetes-commit: 55ba8b2cbb375aaedcd01dd5d4dd0f81ef0415bf
This commit is contained in:
David Eads 2024-09-25 17:23:09 -04:00 committed by Kubernetes Publisher
parent 867c9194ad
commit d6ad8b12b7
1 changed files with 22 additions and 7 deletions

View File

@ -50,6 +50,7 @@ import (
"k8s.io/kubectl/pkg/util/i18n" "k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/openapi" "k8s.io/kubectl/pkg/util/openapi"
"k8s.io/kubectl/pkg/util/prune" "k8s.io/kubectl/pkg/util/prune"
"k8s.io/kubectl/pkg/util/slice"
"k8s.io/kubectl/pkg/util/templates" "k8s.io/kubectl/pkg/util/templates"
"k8s.io/kubectl/pkg/validation" "k8s.io/kubectl/pkg/validation"
) )
@ -71,6 +72,7 @@ type ApplyFlags struct {
All bool All bool
Overwrite bool Overwrite bool
OpenAPIPatch bool OpenAPIPatch bool
Subresource string
PruneAllowlist []string PruneAllowlist []string
@ -97,6 +99,7 @@ type ApplyOptions struct {
All bool All bool
Overwrite bool Overwrite bool
OpenAPIPatch bool OpenAPIPatch bool
Subresource string
ValidationDirective string ValidationDirective string
Validator validation.Schema Validator validation.Schema
@ -178,6 +181,8 @@ var (
var ApplySetToolVersion = version.Get().GitVersion var ApplySetToolVersion = version.Get().GitVersion
var supportedSubresources = []string{"status", "scale"}
// NewApplyFlags returns a default ApplyFlags // NewApplyFlags returns a default ApplyFlags
func NewApplyFlags(streams genericiooptions.IOStreams) *ApplyFlags { func NewApplyFlags(streams genericiooptions.IOStreams) *ApplyFlags {
return &ApplyFlags{ return &ApplyFlags{
@ -235,6 +240,7 @@ func (flags *ApplyFlags) AddFlags(cmd *cobra.Command) {
cmdutil.AddPruningFlags(cmd, &flags.Prune, &flags.PruneAllowlist, &flags.All, &flags.ApplySetRef) cmdutil.AddPruningFlags(cmd, &flags.Prune, &flags.PruneAllowlist, &flags.All, &flags.ApplySetRef)
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.Overwrite, "overwrite", flags.Overwrite, "Automatically resolve conflicts between the modified and live configuration by using values from the modified configuration")
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.") 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.")
cmdutil.AddSubresourceFlags(cmd, &flags.Subresource, "If specified, apply will operate on the subresource of the requested object. Only allowed when using --server-side.", supportedSubresources...)
} }
// ToOptions converts from CLI inputs to runtime inputs // ToOptions converts from CLI inputs to runtime inputs
@ -356,6 +362,7 @@ func (flags *ApplyFlags) ToOptions(f cmdutil.Factory, cmd *cobra.Command, baseNa
All: flags.All, All: flags.All,
Overwrite: flags.Overwrite, Overwrite: flags.Overwrite,
OpenAPIPatch: flags.OpenAPIPatch, OpenAPIPatch: flags.OpenAPIPatch,
Subresource: flags.Subresource,
Recorder: recorder, Recorder: recorder,
Namespace: namespace, Namespace: namespace,
@ -438,6 +445,12 @@ func (o *ApplyOptions) Validate() error {
} }
} }
} }
if len(o.Subresource) > 0 && !slice.ContainsString(supportedSubresources, o.Subresource, nil) {
return fmt.Errorf("invalid subresource value: %q. Must be one of %v", o.Subresource, supportedSubresources)
}
if len(o.Subresource) > 0 && !o.ServerSideApply {
return fmt.Errorf("--subresource can only be specified for --server-side")
}
return nil return nil
} }
@ -577,13 +590,15 @@ func (o *ApplyOptions) applyOneObject(info *resource.Info) error {
options := metav1.PatchOptions{ options := metav1.PatchOptions{
Force: &o.ForceConflicts, Force: &o.ForceConflicts,
} }
obj, err := helper.Patch( obj, err := helper.
info.Namespace, WithSubresource(o.Subresource).
info.Name, Patch(
types.ApplyPatchType, info.Namespace,
data, info.Name,
&options, types.ApplyPatchType,
) data,
&options,
)
if err != nil { if err != nil {
if isIncompatibleServerError(err) { if isIncompatibleServerError(err) {
err = fmt.Errorf("Server-side apply not available on the server: (%v)", err) err = fmt.Errorf("Server-side apply not available on the server: (%v)", err)