Skip to content

Commit 7f8defe

Browse files
committed
update debug cmd to use externals
1 parent 7144438 commit 7f8defe

File tree

1 file changed

+98
-83
lines changed

1 file changed

+98
-83
lines changed

pkg/oc/cli/debug/debug.go

Lines changed: 98 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
2727
"k8s.io/kubernetes/pkg/api/legacyscheme"
2828
kapi "k8s.io/kubernetes/pkg/apis/core"
29-
coreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion"
29+
kapiv1 "k8s.io/kubernetes/pkg/apis/core/v1"
3030
"k8s.io/kubernetes/pkg/kubectl"
3131
kcmd "k8s.io/kubernetes/pkg/kubectl/cmd"
3232
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
@@ -38,63 +38,22 @@ import (
3838
"k8s.io/kubernetes/pkg/kubectl/util/term"
3939
"k8s.io/kubernetes/pkg/util/interrupt"
4040

41+
appsapiv1 "github.com/openshift/api/apps/v1"
4142
appsv1 "github.com/openshift/api/apps/v1"
43+
imageapiv1 "github.com/openshift/api/image/v1"
4244
imagev1 "github.com/openshift/api/image/v1"
45+
appsclientv1 "github.com/openshift/client-go/apps/clientset/versioned/typed/apps/v1"
46+
imageclientv1 "github.com/openshift/client-go/image/clientset/versioned/typed/image/v1"
4347
appsapi "github.com/openshift/origin/pkg/apps/apis/apps"
44-
appsclientinternal "github.com/openshift/origin/pkg/apps/generated/internalclientset"
45-
appsclient "github.com/openshift/origin/pkg/apps/generated/internalclientset/typed/apps/internalversion"
4648
appsutil "github.com/openshift/origin/pkg/apps/util"
4749
imageapi "github.com/openshift/origin/pkg/image/apis/image"
48-
imageclientinternal "github.com/openshift/origin/pkg/image/generated/internalclientset"
49-
imageclient "github.com/openshift/origin/pkg/image/generated/internalclientset/typed/image/internalversion"
50+
internalimage "github.com/openshift/origin/pkg/image/apis/image"
51+
oapiv1 "github.com/openshift/origin/pkg/image/apis/image/v1"
5052
generateapp "github.com/openshift/origin/pkg/oc/lib/newapp/app"
5153
utilenv "github.com/openshift/origin/pkg/oc/util/env"
5254
"github.com/openshift/origin/pkg/oc/util/ocscheme"
5355
)
5456

55-
type DebugOptions struct {
56-
PrintFlags *genericclioptions.PrintFlags
57-
58-
Attach kcmd.AttachOptions
59-
60-
CoreClient coreclient.CoreInterface
61-
CoreV1Client corev1client.CoreV1Interface
62-
AppsClient appsclient.AppsInterface
63-
ImageClient imageclient.ImageInterface
64-
65-
Printer printers.ResourcePrinter
66-
LogsForObject polymorphichelpers.LogsForObjectFunc
67-
68-
NoStdin bool
69-
ForceTTY bool
70-
DisableTTY bool
71-
Timeout time.Duration
72-
73-
Command []string
74-
Annotations map[string]string
75-
AsRoot bool
76-
AsNonRoot bool
77-
AsUser int64
78-
KeepLabels bool // TODO: evaluate selecting the right labels automatically
79-
KeepAnnotations bool
80-
KeepLiveness bool
81-
KeepReadiness bool
82-
KeepInitContainers bool
83-
OneContainer bool
84-
NodeName string
85-
AddEnv []kapi.EnvVar
86-
RemoveEnv []string
87-
Resources []string
88-
Builder func() *resource.Builder
89-
Namespace string
90-
ExplicitNamespace bool
91-
DryRun bool
92-
FullCmdName string
93-
94-
resource.FilenameOptions
95-
genericclioptions.IOStreams
96-
}
97-
9857
const (
9958
debugPodLabelName = "debug.openshift.io/name"
10059

@@ -139,6 +98,48 @@ var (
13998
%[1]s dc/test -o yaml`)
14099
)
141100

101+
type DebugOptions struct {
102+
PrintFlags *genericclioptions.PrintFlags
103+
104+
Attach kcmd.AttachOptions
105+
106+
CoreClient corev1client.CoreV1Interface
107+
AppsClient appsclientv1.AppsV1Interface
108+
ImageClient imageclientv1.ImageV1Interface
109+
110+
Printer printers.ResourcePrinter
111+
LogsForObject polymorphichelpers.LogsForObjectFunc
112+
113+
NoStdin bool
114+
ForceTTY bool
115+
DisableTTY bool
116+
Timeout time.Duration
117+
118+
Command []string
119+
Annotations map[string]string
120+
AsRoot bool
121+
AsNonRoot bool
122+
AsUser int64
123+
KeepLabels bool // TODO: evaluate selecting the right labels automatically
124+
KeepAnnotations bool
125+
KeepLiveness bool
126+
KeepReadiness bool
127+
KeepInitContainers bool
128+
OneContainer bool
129+
NodeName string
130+
AddEnv []corev1.EnvVar
131+
RemoveEnv []string
132+
Resources []string
133+
Builder func() *resource.Builder
134+
Namespace string
135+
ExplicitNamespace bool
136+
DryRun bool
137+
FullCmdName string
138+
139+
resource.FilenameOptions
140+
genericclioptions.IOStreams
141+
}
142+
142143
func NewDebugOptions(streams genericclioptions.IOStreams) *DebugOptions {
143144
return &DebugOptions{
144145
PrintFlags: genericclioptions.NewPrintFlags("").WithTypeSetter(ocscheme.PrintingInternalScheme),
@@ -256,11 +257,24 @@ func (o *DebugOptions) Complete(cmd *cobra.Command, f kcmdutil.Factory, args []s
256257

257258
o.Builder = f.NewBuilder
258259

259-
o.AddEnv, o.RemoveEnv, err = utilenv.ParseEnv(envArgs, nil)
260+
// TODO: not sure what to do with this helper - new duplicate for external versions?
261+
internalAddEnv, removeEnv, err := utilenv.ParseEnv(envArgs, nil)
260262
if err != nil {
261263
return err
262264
}
263265

266+
// convert EnvVars that we should add, from internal to external
267+
for _, internal := range internalAddEnv {
268+
external := corev1.EnvVar{}
269+
if err := kapiv1.Convert_core_EnvVar_To_v1_EnvVar(&internal, &external, nil); err != nil {
270+
return err
271+
}
272+
273+
o.AddEnv = append(o.AddEnv, external)
274+
}
275+
276+
o.RemoveEnv = removeEnv
277+
264278
cmdParent := cmd.Parent()
265279
if cmdParent != nil && len(cmdParent.CommandPath()) > 0 && kcmdutil.IsSiblingCommandExists(cmd, "describe") {
266280
o.FullCmdName = cmdParent.CommandPath()
@@ -290,29 +304,22 @@ func (o *DebugOptions) Complete(cmd *cobra.Command, f kcmdutil.Factory, args []s
290304
}
291305
o.Attach.Config = config
292306

293-
kc, err := f.ClientSet()
307+
o.CoreClient, err = corev1client.NewForConfig(config)
294308
if err != nil {
295309
return err
296310
}
297-
o.Attach.PodClient = kc.Core()
298-
o.CoreClient = kc.Core()
299311

300-
o.CoreV1Client, err = corev1client.NewForConfig(config)
301-
if err != nil {
302-
return err
303-
}
312+
o.Attach.PodClient = o.CoreClient
304313

305-
appsClient, err := appsclientinternal.NewForConfig(config)
314+
o.AppsClient, err = appsclientv1.NewForConfig(config)
306315
if err != nil {
307316
return err
308317
}
309-
o.AppsClient = appsClient.Apps()
310318

311-
imageClient, err := imageclientinternal.NewForConfig(config)
319+
o.ImageClient, err = imageclientv1.NewForConfig(config)
312320
if err != nil {
313321
return err
314322
}
315-
o.ImageClient = imageClient.Image()
316323

317324
return nil
318325
}
@@ -351,11 +358,11 @@ func (o *DebugOptions) RunDebug() error {
351358
if err != nil {
352359
glog.V(4).Infof("Unable to get exact template, but continuing with fallback: %v", err)
353360
}
354-
template := &kapi.PodTemplateSpec{}
361+
template := &corev1.PodTemplateSpec{}
355362
if err := legacyscheme.Scheme.Convert(templateV1, template, nil); err != nil {
356363
return err
357364
}
358-
pod := &kapi.Pod{
365+
pod := &corev1.Pod{
359366
ObjectMeta: template.ObjectMeta,
360367
Spec: template.Spec,
361368
}
@@ -471,7 +478,7 @@ func (o *DebugOptions) RunDebug() error {
471478
// (via the "openshift.io/deployment-config.name" annotation), then tries to
472479
// find the ImageStream from which the DeploymentConfig is deploying, then tries
473480
// to find a match for the Container's image in the ImageStream's Images.
474-
func (o *DebugOptions) getContainerImageViaDeploymentConfig(pod *kapi.Pod, container *kapi.Container) (*imageapi.Image, error) {
481+
func (o *DebugOptions) getContainerImageViaDeploymentConfig(pod *corev1.Pod, container *corev1.Container) (*imageapiv1.Image, error) {
475482
ref, err := imageapi.ParseDockerImageReference(container.Image)
476483
if err != nil {
477484
return nil, err
@@ -492,7 +499,7 @@ func (o *DebugOptions) getContainerImageViaDeploymentConfig(pod *kapi.Pod, conta
492499
}
493500

494501
for _, trigger := range dc.Spec.Triggers {
495-
if trigger.Type == appsapi.DeploymentTriggerOnImageChange &&
502+
if trigger.Type == appsapiv1.DeploymentTriggerOnImageChange &&
496503
trigger.ImageChangeParams != nil &&
497504
trigger.ImageChangeParams.From.Kind == "ImageStreamTag" {
498505

@@ -523,15 +530,15 @@ func (o *DebugOptions) getContainerImageViaDeploymentConfig(pod *kapi.Pod, conta
523530
// set to false. The request will not succeed if the backing repository
524531
// requires Insecure to be set to true, which cannot be hard-coded for security
525532
// reasons.
526-
func (o *DebugOptions) getContainerImageViaImageStreamImport(container *kapi.Container) (*imageapi.Image, error) {
527-
isi := &imageapi.ImageStreamImport{
533+
func (o *DebugOptions) getContainerImageViaImageStreamImport(container *corev1.Container) (*imageapiv1.Image, error) {
534+
isi := &imageapiv1.ImageStreamImport{
528535
ObjectMeta: metav1.ObjectMeta{
529536
Name: "oc-debug",
530537
},
531-
Spec: imageapi.ImageStreamImportSpec{
532-
Images: []imageapi.ImageImportSpec{
538+
Spec: imageapiv1.ImageStreamImportSpec{
539+
Images: []imageapiv1.ImageImportSpec{
533540
{
534-
From: kapi.ObjectReference{
541+
From: corev1.ObjectReference{
535542
Kind: "DockerImage",
536543
Name: container.Image,
537544
},
@@ -552,7 +559,7 @@ func (o *DebugOptions) getContainerImageViaImageStreamImport(container *kapi.Con
552559
return nil, nil
553560
}
554561

555-
func (o *DebugOptions) getContainerImageCommand(pod *kapi.Pod, container *kapi.Container) ([]string, error) {
562+
func (o *DebugOptions) getContainerImageCommand(pod *corev1.Pod, container *corev1.Container) ([]string, error) {
556563
if len(container.Command) > 0 {
557564
return container.Command, nil
558565
}
@@ -562,16 +569,24 @@ func (o *DebugOptions) getContainerImageCommand(pod *kapi.Pod, container *kapi.C
562569
image, _ = o.getContainerImageViaImageStreamImport(container)
563570
}
564571

565-
if image == nil || image.DockerImageMetadata.Config == nil {
572+
// TODO: find a way to do this with the external api
573+
// convert image to internal in order to extract its imagemetadata config
574+
internalImage := &internalimage.Image{}
575+
if err := oapiv1.Convert_v1_Image_To_image_Image(image, internalImage, nil); err != nil {
576+
return nil, err
577+
}
578+
579+
// TODO: DockerImageMetadata is a RawExtension in the external api - how to access config?
580+
if internalImage == nil || internalImage.DockerImageMetadata.Config == nil {
566581
return nil, errors.New("error: no usable image found")
567582
}
568583

569-
config := image.DockerImageMetadata.Config
584+
config := internalImage.DockerImageMetadata.Config
570585
return append(config.Entrypoint, config.Cmd...), nil
571586
}
572587

573588
// transformPodForDebug alters the input pod to be debuggable
574-
func (o *DebugOptions) transformPodForDebug(annotations map[string]string) (*kapi.Pod, []string) {
589+
func (o *DebugOptions) transformPodForDebug(annotations map[string]string) (*corev1.Pod, []string) {
575590
pod := o.Attach.Pod
576591

577592
if !o.KeepInitContainers {
@@ -600,7 +615,7 @@ func (o *DebugOptions) transformPodForDebug(annotations map[string]string) (*kap
600615
container.LivenessProbe = nil
601616
}
602617

603-
var newEnv []kapi.EnvVar
618+
var newEnv []corev1.EnvVar
604619
if len(o.RemoveEnv) > 0 {
605620
for i := range container.Env {
606621
skip := false
@@ -624,7 +639,7 @@ func (o *DebugOptions) transformPodForDebug(annotations map[string]string) (*kap
624639
container.Env = newEnv
625640

626641
if container.SecurityContext == nil {
627-
container.SecurityContext = &kapi.SecurityContext{}
642+
container.SecurityContext = &corev1.SecurityContext{}
628643
}
629644
switch {
630645
case o.AsNonRoot:
@@ -640,7 +655,7 @@ func (o *DebugOptions) transformPodForDebug(annotations map[string]string) (*kap
640655
}
641656

642657
if o.OneContainer {
643-
pod.Spec.Containers = []kapi.Container{*container}
658+
pod.Spec.Containers = []corev1.Container{*container}
644659
}
645660

646661
// reset the pod
@@ -661,9 +676,9 @@ func (o *DebugOptions) transformPodForDebug(annotations map[string]string) (*kap
661676
pod.Spec.NodeName = o.NodeName
662677

663678
pod.ResourceVersion = ""
664-
pod.Spec.RestartPolicy = kapi.RestartPolicyNever
679+
pod.Spec.RestartPolicy = corev1.RestartPolicyNever
665680

666-
pod.Status = kapi.PodStatus{}
681+
pod.Status = corev1.PodStatus{}
667682
pod.UID = ""
668683
pod.CreationTimestamp = metav1.Time{}
669684
pod.SelfLink = ""
@@ -676,7 +691,7 @@ func (o *DebugOptions) transformPodForDebug(annotations map[string]string) (*kap
676691

677692
// createPod creates the debug pod, and will attempt to delete an existing debug
678693
// pod with the same name, but will return an error in any other case.
679-
func (o *DebugOptions) createPod(pod *kapi.Pod) (*kapi.Pod, error) {
694+
func (o *DebugOptions) createPod(pod *corev1.Pod) (*corev1.Pod, error) {
680695
namespace, name := pod.Namespace, pod.Name
681696

682697
// create the pod
@@ -701,7 +716,7 @@ func (o *DebugOptions) createPod(pod *kapi.Pod) (*kapi.Pod, error) {
701716
return o.Attach.PodClient.Pods(namespace).Create(pod)
702717
}
703718

704-
func containerForName(pod *kapi.Pod, name string) *kapi.Container {
719+
func containerForName(pod *corev1.Pod, name string) *corev1.Container {
705720
for i, c := range pod.Spec.Containers {
706721
if c.Name == name {
707722
return &pod.Spec.Containers[i]
@@ -715,7 +730,7 @@ func containerForName(pod *kapi.Pod, name string) *kapi.Container {
715730
return nil
716731
}
717732

718-
func containerNames(pod *kapi.Pod) []string {
733+
func containerNames(pod *corev1.Pod) []string {
719734
var names []string
720735
for _, c := range pod.Spec.Containers {
721736
names = append(names, c.Name)
@@ -751,14 +766,14 @@ func (o *DebugOptions) approximatePodTemplateForObject(object runtime.Object) (*
751766
fallback := t.Spec.Template
752767

753768
latestDeploymentName := appsutil.LatestDeploymentNameForConfigV1(t)
754-
deployment, err := o.CoreV1Client.ReplicationControllers(t.Namespace).Get(latestDeploymentName, metav1.GetOptions{})
769+
deployment, err := o.CoreClient.ReplicationControllers(t.Namespace).Get(latestDeploymentName, metav1.GetOptions{})
755770
if err != nil {
756771
return fallback, err
757772
}
758773

759774
fallback = deployment.Spec.Template
760775

761-
pods, err := o.CoreV1Client.Pods(deployment.Namespace).List(metav1.ListOptions{LabelSelector: labels.SelectorFromSet(deployment.Spec.Selector).String()})
776+
pods, err := o.CoreClient.Pods(deployment.Namespace).List(metav1.ListOptions{LabelSelector: labels.SelectorFromSet(deployment.Spec.Selector).String()})
762777
if err != nil {
763778
return fallback, err
764779
}

0 commit comments

Comments
 (0)