Merge pull request #101034 from verb/1.22-ec-api
Switch alpha Pod ephemeralcontainers API to use Pod kind Kubernetes-commit: 972ee2d425333d0b30f340328ef6c7dc40fd2715
This commit is contained in:
commit
43b9acf6cb
|
@ -1040,11 +1040,11 @@
|
|||
},
|
||||
{
|
||||
"ImportPath": "k8s.io/api",
|
||||
"Rev": "b64c6b5ea77c"
|
||||
"Rev": "95cee4894d89"
|
||||
},
|
||||
{
|
||||
"ImportPath": "k8s.io/apimachinery",
|
||||
"Rev": "f3a344a9640c"
|
||||
"Rev": "ba18a62c2bc2"
|
||||
},
|
||||
{
|
||||
"ImportPath": "k8s.io/cli-runtime",
|
||||
|
@ -1052,7 +1052,7 @@
|
|||
},
|
||||
{
|
||||
"ImportPath": "k8s.io/client-go",
|
||||
"Rev": "ae85206fc58c"
|
||||
"Rev": "071918f6b2dc"
|
||||
},
|
||||
{
|
||||
"ImportPath": "k8s.io/code-generator",
|
||||
|
|
10
go.mod
10
go.mod
|
@ -32,10 +32,10 @@ require (
|
|||
github.com/stretchr/testify v1.6.1
|
||||
golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073
|
||||
gopkg.in/yaml.v2 v2.4.0
|
||||
k8s.io/api v0.0.0-20210518101555-b64c6b5ea77c
|
||||
k8s.io/api v0.0.0-20210518101556-95cee4894d89
|
||||
k8s.io/apimachinery v0.0.0-20210518100450-f3a344a9640c
|
||||
k8s.io/cli-runtime v0.0.0-20210518124920-7bf98babcc43
|
||||
k8s.io/client-go v0.0.0-20210518102854-ae85206fc58c
|
||||
k8s.io/client-go v0.0.0-20210518102859-071918f6b2dc
|
||||
k8s.io/component-base v0.0.0-20210518111224-873d5fac2edc
|
||||
k8s.io/component-helpers v0.0.0-20210518112010-d82b95e556d3
|
||||
k8s.io/klog/v2 v2.8.0
|
||||
|
@ -48,10 +48,10 @@ require (
|
|||
)
|
||||
|
||||
replace (
|
||||
k8s.io/api => k8s.io/api v0.0.0-20210518101555-b64c6b5ea77c
|
||||
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20210518100450-f3a344a9640c
|
||||
k8s.io/api => k8s.io/api v0.0.0-20210518101556-95cee4894d89
|
||||
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20210518100450-ba18a62c2bc2
|
||||
k8s.io/cli-runtime => k8s.io/cli-runtime v0.0.0-20210518124920-7bf98babcc43
|
||||
k8s.io/client-go => k8s.io/client-go v0.0.0-20210518102854-ae85206fc58c
|
||||
k8s.io/client-go => k8s.io/client-go v0.0.0-20210518102859-071918f6b2dc
|
||||
k8s.io/code-generator => k8s.io/code-generator v0.0.0-20210518095631-cbf249e0c797
|
||||
k8s.io/component-base => k8s.io/component-base v0.0.0-20210518111224-873d5fac2edc
|
||||
k8s.io/component-helpers => k8s.io/component-helpers v0.0.0-20210518112010-d82b95e556d3
|
||||
|
|
12
go.sum
12
go.sum
|
@ -730,14 +730,14 @@ honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWh
|
|||
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
|
||||
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
|
||||
k8s.io/api v0.0.0-20210518101555-b64c6b5ea77c h1:UKWH16VfbGANijR6vN0rRuSX6ua0JsmWw4y1y2bA0TA=
|
||||
k8s.io/api v0.0.0-20210518101555-b64c6b5ea77c/go.mod h1:XAKoutoISH8fQ++BLv5O5gKGpZHoBcQoUKE1SaFlcZc=
|
||||
k8s.io/apimachinery v0.0.0-20210518100450-f3a344a9640c h1:Ow/BjHSawZF4mTos3y4rN/sCIpUwGf9loxsyiVKGk+g=
|
||||
k8s.io/apimachinery v0.0.0-20210518100450-f3a344a9640c/go.mod h1:fBRSkoylGO2QUTae8Wb2wac6pZ83/r+tL6HFSXGbzfs=
|
||||
k8s.io/api v0.0.0-20210518101556-95cee4894d89 h1:uQC3v+9tBtiF1mXGGdM3vN7gDW7StAJCFO4sO0USjhE=
|
||||
k8s.io/api v0.0.0-20210518101556-95cee4894d89/go.mod h1:ZFbvp5WWMe1V9auNefRgVPZzDDSzTMLlulKIDnadhQU=
|
||||
k8s.io/apimachinery v0.0.0-20210518100450-ba18a62c2bc2 h1:KvBnUwNEF2aGJHZbEJW5ck1Tq1R04dSsMPHOSQc2gYo=
|
||||
k8s.io/apimachinery v0.0.0-20210518100450-ba18a62c2bc2/go.mod h1:fBRSkoylGO2QUTae8Wb2wac6pZ83/r+tL6HFSXGbzfs=
|
||||
k8s.io/cli-runtime v0.0.0-20210518124920-7bf98babcc43 h1:rvo2r9odSa1awhkI8wQ0XBbb+Vy0CwHsdIYrIGgQ3ww=
|
||||
k8s.io/cli-runtime v0.0.0-20210518124920-7bf98babcc43/go.mod h1:TVaZyT6o4qN5cBDPx3OPOHkNnV8qtHDbaSxnUUfXpyo=
|
||||
k8s.io/client-go v0.0.0-20210518102854-ae85206fc58c h1:uwVGBydk6kvokuc5xUUqACEJ5boOhkhE6m5XF0lNClU=
|
||||
k8s.io/client-go v0.0.0-20210518102854-ae85206fc58c/go.mod h1:TIT0Nhjy4Ca/+JzqsvJna+RDfBh6RBRfcF6/dmID9HU=
|
||||
k8s.io/client-go v0.0.0-20210518102859-071918f6b2dc h1:Q3Dlu4T3gZJKvSWtkqguBxyHojTzqtfp6UbycFShbmc=
|
||||
k8s.io/client-go v0.0.0-20210518102859-071918f6b2dc/go.mod h1:YynJhqjXSnTaGvKwByJj9Btu0hoqz5jA4+J6w2ZKCcU=
|
||||
k8s.io/code-generator v0.0.0-20210518095631-cbf249e0c797/go.mod h1:tHNeGA58jE3nJvZLDN0c/5k7P3NlYdjbWuJYK9QiU3s=
|
||||
k8s.io/component-base v0.0.0-20210518111224-873d5fac2edc h1:l8dXdnxBZWoO8Fj65CMQvYsZ4cEggM3tz4Cu4LhkZoQ=
|
||||
k8s.io/component-base v0.0.0-20210518111224-873d5fac2edc/go.mod h1:ymQYdNBH0dq2yRw/5R5jN9wKhQTkhOiDQqFKmycAwRk=
|
||||
|
|
|
@ -18,6 +18,7 @@ package debug
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
|
@ -31,7 +32,9 @@ import (
|
|||
"k8s.io/apimachinery/pkg/fields"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
utilrand "k8s.io/apimachinery/pkg/util/rand"
|
||||
"k8s.io/apimachinery/pkg/util/strategicpatch"
|
||||
"k8s.io/apimachinery/pkg/watch"
|
||||
"k8s.io/cli-runtime/pkg/genericclioptions"
|
||||
"k8s.io/cli-runtime/pkg/resource"
|
||||
|
@ -381,26 +384,39 @@ func (o *DebugOptions) visitPod(ctx context.Context, pod *corev1.Pod) (*corev1.P
|
|||
|
||||
// debugByEphemeralContainer runs an EphemeralContainer in the target Pod for use as a debug container
|
||||
func (o *DebugOptions) debugByEphemeralContainer(ctx context.Context, pod *corev1.Pod) (*corev1.Pod, string, error) {
|
||||
pods := o.podClient.Pods(pod.Namespace)
|
||||
ec, err := pods.GetEphemeralContainers(ctx, pod.Name, metav1.GetOptions{})
|
||||
klog.V(2).Infof("existing ephemeral containers: %v", pod.Spec.EphemeralContainers)
|
||||
podJS, err := json.Marshal(pod)
|
||||
if err != nil {
|
||||
// The pod has already been fetched at this point, so a NotFound error indicates the ephemeralcontainers subresource wasn't found.
|
||||
if serr, ok := err.(*errors.StatusError); ok && serr.Status().Reason == metav1.StatusReasonNotFound {
|
||||
return nil, "", fmt.Errorf("error creating JSON for pod: %v", err)
|
||||
}
|
||||
|
||||
debugContainer := o.generateDebugContainer(pod)
|
||||
klog.V(2).Infof("new ephemeral container: %#v", debugContainer)
|
||||
debugPod := pod.DeepCopy()
|
||||
debugPod.Spec.EphemeralContainers = append(debugPod.Spec.EphemeralContainers, *debugContainer)
|
||||
debugJS, err := json.Marshal(debugPod)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("error creating JSON for debug container: %v", err)
|
||||
}
|
||||
|
||||
patch, err := strategicpatch.CreateTwoWayMergePatch(podJS, debugJS, pod)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("error creating patch to add debug container: %v", err)
|
||||
}
|
||||
klog.V(2).Infof("generated strategic merge patch for debug container: %s", patch)
|
||||
|
||||
pods := o.podClient.Pods(pod.Namespace)
|
||||
result, err := pods.Patch(ctx, pod.Name, types.StrategicMergePatchType, patch, metav1.PatchOptions{}, "ephemeralcontainers")
|
||||
if err != nil {
|
||||
// The apiserver will return a 404 when the EphemeralContainers feature is disabled because the `/ephemeralcontainers` subresource
|
||||
// is missing. Unlike the 404 returned by a missing pod, the status details will be empty.
|
||||
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)
|
||||
}
|
||||
return nil, "", err
|
||||
}
|
||||
klog.V(2).Infof("existing ephemeral containers: %v", ec.EphemeralContainers)
|
||||
|
||||
debugContainer := o.generateDebugContainer(pod)
|
||||
klog.V(2).Infof("new ephemeral container: %#v", debugContainer)
|
||||
ec.EphemeralContainers = append(ec.EphemeralContainers, *debugContainer)
|
||||
_, err = pods.UpdateEphemeralContainers(ctx, pod.Name, ec, metav1.UpdateOptions{})
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("error updating ephemeral containers: %v", err)
|
||||
}
|
||||
|
||||
return pod, debugContainer.Name, nil
|
||||
return result, debugContainer.Name, nil
|
||||
}
|
||||
|
||||
// debugByCopy runs a copy of the target Pod with a debug container added or an original container modified
|
||||
|
|
Loading…
Reference in New Issue