Skip to content

Commit bb6bfba

Browse files
authored
Merge pull request kubernetes#127634 from deads2k/apply-subresource
add --subresource to kubectl apply
2 parents 3660a34 + 55ba8b2 commit bb6bfba

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: test-pod
5+
status:
6+
conditions:
7+
- type: example.io/Foo
8+
status: "True"
9+
message: message
10+
reason: reason
11+

staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import (
5050
"k8s.io/kubectl/pkg/util/i18n"
5151
"k8s.io/kubectl/pkg/util/openapi"
5252
"k8s.io/kubectl/pkg/util/prune"
53+
"k8s.io/kubectl/pkg/util/slice"
5354
"k8s.io/kubectl/pkg/util/templates"
5455
"k8s.io/kubectl/pkg/validation"
5556
)
@@ -71,6 +72,7 @@ type ApplyFlags struct {
7172
All bool
7273
Overwrite bool
7374
OpenAPIPatch bool
75+
Subresource string
7476

7577
PruneAllowlist []string
7678

@@ -97,6 +99,7 @@ type ApplyOptions struct {
9799
All bool
98100
Overwrite bool
99101
OpenAPIPatch bool
102+
Subresource string
100103

101104
ValidationDirective string
102105
Validator validation.Schema
@@ -178,6 +181,8 @@ var (
178181

179182
var ApplySetToolVersion = version.Get().GitVersion
180183

184+
var supportedSubresources = []string{"status", "scale"}
185+
181186
// NewApplyFlags returns a default ApplyFlags
182187
func NewApplyFlags(streams genericiooptions.IOStreams) *ApplyFlags {
183188
return &ApplyFlags{
@@ -235,6 +240,7 @@ func (flags *ApplyFlags) AddFlags(cmd *cobra.Command) {
235240
cmdutil.AddPruningFlags(cmd, &flags.Prune, &flags.PruneAllowlist, &flags.All, &flags.ApplySetRef)
236241
cmd.Flags().BoolVar(&flags.Overwrite, "overwrite", flags.Overwrite, "Automatically resolve conflicts between the modified and live configuration by using values from the modified configuration")
237242
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.")
243+
cmdutil.AddSubresourceFlags(cmd, &flags.Subresource, "If specified, apply will operate on the subresource of the requested object. Only allowed when using --server-side.", supportedSubresources...)
238244
}
239245

240246
// ToOptions converts from CLI inputs to runtime inputs
@@ -356,6 +362,7 @@ func (flags *ApplyFlags) ToOptions(f cmdutil.Factory, cmd *cobra.Command, baseNa
356362
All: flags.All,
357363
Overwrite: flags.Overwrite,
358364
OpenAPIPatch: flags.OpenAPIPatch,
365+
Subresource: flags.Subresource,
359366

360367
Recorder: recorder,
361368
Namespace: namespace,
@@ -438,6 +445,12 @@ func (o *ApplyOptions) Validate() error {
438445
}
439446
}
440447
}
448+
if len(o.Subresource) > 0 && !slice.ContainsString(supportedSubresources, o.Subresource, nil) {
449+
return fmt.Errorf("invalid subresource value: %q. Must be one of %v", o.Subresource, supportedSubresources)
450+
}
451+
if len(o.Subresource) > 0 && !o.ServerSideApply {
452+
return fmt.Errorf("--subresource can only be specified for --server-side")
453+
}
441454

442455
return nil
443456
}
@@ -577,13 +590,15 @@ func (o *ApplyOptions) applyOneObject(info *resource.Info) error {
577590
options := metav1.PatchOptions{
578591
Force: &o.ForceConflicts,
579592
}
580-
obj, err := helper.Patch(
581-
info.Namespace,
582-
info.Name,
583-
types.ApplyPatchType,
584-
data,
585-
&options,
586-
)
593+
obj, err := helper.
594+
WithSubresource(o.Subresource).
595+
Patch(
596+
info.Namespace,
597+
info.Name,
598+
types.ApplyPatchType,
599+
data,
600+
&options,
601+
)
587602
if err != nil {
588603
if isIncompatibleServerError(err) {
589604
err = fmt.Errorf("Server-side apply not available on the server: (%v)", err)

test/cmd/apply.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,10 @@ run_kubectl_server_side_apply_tests() {
413413
kubectl apply --server-side --field-manager=my-field-manager --force-conflicts -f hack/testdata/pod.yaml "${kube_flags[@]:?}"
414414
output_message=$(kubectl get -f hack/testdata/pod.yaml -o=jsonpath='{.metadata.managedFields[*].manager}' "${kube_flags[@]:?}" 2>&1)
415415
kube::test::if_has_string "${output_message}" 'my-field-manager'
416+
# can add pod condition
417+
kubectl apply --server-side --subresource=status --field-manager=my-field-manager -f hack/testdata/pod-apply-status.yaml "${kube_flags[@]:?}"
418+
output_message=$(kubectl get -f hack/testdata/pod.yaml -o=jsonpath='{.status.conditions[*].type}' "${kube_flags[@]:?}" 2>&1)
419+
kube::test::if_has_string "${output_message}" 'example.io/Foo'
416420
# Clean up
417421
kubectl delete pods test-pod "${kube_flags[@]:?}"
418422

0 commit comments

Comments
 (0)