(kubectl patch): Add descriptive message when patch type is unsupported

`kubectl patch` commands fails when patch type is strategic merge
patch for CRDs. This PR handles `UnsupportedMediaType` error and
shows descriptive message to user.

Kubernetes-commit: dc2c0ad831f71349befdfa57d4367c4ea57d4a6c
This commit is contained in:
Arda Güçlü 2022-09-19 10:15:04 +03:00 committed by Kubernetes Publisher
parent 6e2d4cb05e
commit f6d66bab8d
1 changed files with 5 additions and 0 deletions

View File

@ -23,9 +23,11 @@ import (
"strings"
jsonpatch "github.com/evanphx/json-patch"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"k8s.io/klog/v2"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
@ -272,6 +274,9 @@ func (o *PatchOptions) RunPatch() error {
WithSubresource(o.Subresource)
patchedObj, err := helper.Patch(namespace, name, patchType, patchBytes, nil)
if err != nil {
if apierrors.IsUnsupportedMediaType(err) {
return errors.Wrap(err, fmt.Sprintf("%s is not supported by %s", patchType, mapping.GroupVersionKind))
}
return err
}