Merge pull request #103292 from verb/1.22-kubectl-debug-compat
Add backwards compatibility for ephemeral containers in kubectl debug Kubernetes-commit: 6fc7dd5137956b93843a8cb459e106719e237a57
This commit is contained in:
commit
718c271c44
8
go.mod
8
go.mod
|
|
@ -33,8 +33,8 @@ require (
|
|||
gopkg.in/yaml.v2 v2.4.0
|
||||
k8s.io/api v0.0.0-20210705094346-9f69feafab6a
|
||||
k8s.io/apimachinery v0.0.0-20210701054147-830375057167
|
||||
k8s.io/cli-runtime v0.0.0-20210705095914-805b0c59d6fc
|
||||
k8s.io/client-go v0.0.0-20210702094607-ca3a47f0b44a
|
||||
k8s.io/cli-runtime v0.0.0-20210706175927-064ccf28ed41
|
||||
k8s.io/client-go v0.0.0-20210706214629-68cb2ddef93f
|
||||
k8s.io/component-base v0.0.0-20210702054709-fddd92849e34
|
||||
k8s.io/component-helpers v0.0.0-20210701055214-c69fdc006e3d
|
||||
k8s.io/klog/v2 v2.9.0
|
||||
|
|
@ -49,8 +49,8 @@ require (
|
|||
replace (
|
||||
k8s.io/api => k8s.io/api v0.0.0-20210705094346-9f69feafab6a
|
||||
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20210701054147-830375057167
|
||||
k8s.io/cli-runtime => k8s.io/cli-runtime v0.0.0-20210705095914-805b0c59d6fc
|
||||
k8s.io/client-go => k8s.io/client-go v0.0.0-20210702094607-ca3a47f0b44a
|
||||
k8s.io/cli-runtime => k8s.io/cli-runtime v0.0.0-20210706175927-064ccf28ed41
|
||||
k8s.io/client-go => k8s.io/client-go v0.0.0-20210706214629-68cb2ddef93f
|
||||
k8s.io/code-generator => k8s.io/code-generator v0.0.0-20210704214018-b1b4942f7194
|
||||
k8s.io/component-base => k8s.io/component-base v0.0.0-20210702054709-fddd92849e34
|
||||
k8s.io/component-helpers => k8s.io/component-helpers v0.0.0-20210701055214-c69fdc006e3d
|
||||
|
|
|
|||
8
go.sum
8
go.sum
|
|
@ -742,10 +742,10 @@ k8s.io/api v0.0.0-20210705094346-9f69feafab6a h1:IF+SZibisSFDDUi+Gll4HgpnMjzP2lp
|
|||
k8s.io/api v0.0.0-20210705094346-9f69feafab6a/go.mod h1:zoURDvOPW5UMFZr2YUU/sStjYnWSPt+x+MM4R94ATgQ=
|
||||
k8s.io/apimachinery v0.0.0-20210701054147-830375057167 h1:fob/j8+uMBIVvyo+9bG7GvjFSj0LX3RNuSXW+RcUrwo=
|
||||
k8s.io/apimachinery v0.0.0-20210701054147-830375057167/go.mod h1:O3oNtNadZdeOMxHFVxOreoznohCpy0z6mocxbZr7oJ0=
|
||||
k8s.io/cli-runtime v0.0.0-20210705095914-805b0c59d6fc h1:gDW/F7LieuWLzMaR/duFQjDkEBUGXydoDSkvLEb6nMs=
|
||||
k8s.io/cli-runtime v0.0.0-20210705095914-805b0c59d6fc/go.mod h1:YX5Kh1Kiu00ltxu67hahuxvcU23P/CQ0q1994L4hx4c=
|
||||
k8s.io/client-go v0.0.0-20210702094607-ca3a47f0b44a h1:VFneXCpCgcDAzER+i0riTedinxoT/TK4ZrA/IfyWWwI=
|
||||
k8s.io/client-go v0.0.0-20210702094607-ca3a47f0b44a/go.mod h1:q7LwMTm+ipCQu7UHo7eO1qrjkqaEjj/WmgmKGZGzmcA=
|
||||
k8s.io/cli-runtime v0.0.0-20210706175927-064ccf28ed41 h1:0me3gLmsHF+cyQ6tSqfhTkeLNhRyZXEskfdBS+r5C1w=
|
||||
k8s.io/cli-runtime v0.0.0-20210706175927-064ccf28ed41/go.mod h1:YX5Kh1Kiu00ltxu67hahuxvcU23P/CQ0q1994L4hx4c=
|
||||
k8s.io/client-go v0.0.0-20210706214629-68cb2ddef93f h1:seULy+bqIY8G2Q98h/e3WaQdkgNIIQUqy8MSUP2TReY=
|
||||
k8s.io/client-go v0.0.0-20210706214629-68cb2ddef93f/go.mod h1:sbkKCOXetW3FaqcSYCG3ghdfOCSOKTGwitXlliklRrc=
|
||||
k8s.io/code-generator v0.0.0-20210704214018-b1b4942f7194/go.mod h1:eV77Y09IopzeXOJzndrDyCI88UBok2h6WxAlBwpxa+o=
|
||||
k8s.io/component-base v0.0.0-20210702054709-fddd92849e34 h1:L7kJX0+rX5SJ2ZUaFy8zarcUMW0+2AHkd3gyuIp0ZLM=
|
||||
k8s.io/component-base v0.0.0-20210702054709-fddd92849e34/go.mod h1:KuldLgSPMxWtcVmPn3hcUoWNJqm7HEdVcSID+ZdAB24=
|
||||
|
|
|
|||
|
|
@ -124,7 +124,7 @@ type DebugOptions struct {
|
|||
attachChanged bool
|
||||
shareProcessedChanged bool
|
||||
|
||||
podClient corev1client.PodsGetter
|
||||
podClient corev1client.CoreV1Interface
|
||||
|
||||
genericclioptions.IOStreams
|
||||
}
|
||||
|
|
@ -422,12 +422,54 @@ func (o *DebugOptions) debugByEphemeralContainer(ctx context.Context, pod *corev
|
|||
if serr, ok := err.(*errors.StatusError); ok && serr.Status().Reason == metav1.StatusReasonNotFound && serr.ErrStatus.Details.Name == "" {
|
||||
return nil, "", fmt.Errorf("ephemeral containers are disabled for this cluster (error from server: %q).", err)
|
||||
}
|
||||
|
||||
// The Kind used for the /ephemeralcontainers subresource changed in 1.22. When presented with an unexpected
|
||||
// Kind the api server will respond with a not-registered error. When this happens we can optimistically try
|
||||
// using the old API.
|
||||
if runtime.IsNotRegisteredError(err) {
|
||||
klog.V(1).Infof("Falling back to legacy API because server returned error: %v", err)
|
||||
return o.debugByEphemeralContainerLegacy(ctx, pod, debugContainer)
|
||||
}
|
||||
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
return result, debugContainer.Name, nil
|
||||
}
|
||||
|
||||
// debugByEphemeralContainerLegacy adds debugContainer as an ephemeral container using the pre-1.22 /ephemeralcontainers API
|
||||
// This may be removed when we no longer wish to support releases prior to 1.22.
|
||||
func (o *DebugOptions) debugByEphemeralContainerLegacy(ctx context.Context, pod *corev1.Pod, debugContainer *corev1.EphemeralContainer) (*corev1.Pod, string, error) {
|
||||
// We no longer have the v1.EphemeralContainers Kind since it was removed in 1.22, but
|
||||
// we can present a JSON 6902 patch that the api server will apply.
|
||||
patch, err := json.Marshal([]map[string]interface{}{{
|
||||
"op": "add",
|
||||
"path": "/ephemeralContainers/-",
|
||||
"value": debugContainer,
|
||||
}})
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("error creating JSON 6902 patch for old /ephemeralcontainers API: %s", err)
|
||||
}
|
||||
|
||||
result := o.podClient.RESTClient().Patch(types.JSONPatchType).
|
||||
Namespace(pod.Namespace).
|
||||
Resource("pods").
|
||||
Name(pod.Name).
|
||||
SubResource("ephemeralcontainers").
|
||||
Body(patch).
|
||||
Do(ctx)
|
||||
if err := result.Error(); err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
newPod, err := o.podClient.Pods(pod.Namespace).Get(ctx, pod.Name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
return newPod, debugContainer.Name, nil
|
||||
}
|
||||
|
||||
// debugByCopy runs a copy of the target Pod with a debug container added or an original container modified
|
||||
func (o *DebugOptions) debugByCopy(ctx context.Context, pod *corev1.Pod) (*corev1.Pod, string, error) {
|
||||
copied, dc, err := o.generatePodCopyWithDebugContainer(pod)
|
||||
|
|
|
|||
Loading…
Reference in New Issue