Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose attachable pod discovery to factory #16109

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 6 additions & 27 deletions pkg/kubectl/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/meta"
client "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/kubectl"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/resource"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/runtime"
)

Expand Down Expand Up @@ -170,7 +168,7 @@ func Run(f *cmdutil.Factory, cmdIn io.Reader, cmdOut, cmdErr io.Writer, cmd *cob
}
}

obj, kind, mapper, mapping, err := createGeneratedObject(f, cmd, generator, names, params, cmdutil.GetFlagString(cmd, "overrides"), namespace)
obj, _, mapper, mapping, err := createGeneratedObject(f, cmd, generator, names, params, cmdutil.GetFlagString(cmd, "overrides"), namespace)
if err != nil {
return err
}
Expand Down Expand Up @@ -202,15 +200,12 @@ func Run(f *cmdutil.Factory, cmdIn io.Reader, cmdOut, cmdErr io.Writer, cmd *cob
return err
}
opts.Client = client
// TODO: this should be abstracted into Factory to support other types
switch t := obj.(type) {
case *api.ReplicationController:
return handleAttachReplicationController(client, t, opts)
case *api.Pod:
return handleAttachPod(client, t, opts)
default:
return fmt.Errorf("cannot attach to %s: not implemented", kind)

attachablePod, err := f.AttachablePodForObject(obj)
if err != nil {
return err
}
return handleAttachPod(client, attachablePod, opts)
}

outputFormat := cmdutil.GetFlagString(cmd, "output")
Expand Down Expand Up @@ -249,22 +244,6 @@ func waitForPodRunning(c *client.Client, pod *api.Pod, out io.Writer) (status ap
}
}

func handleAttachReplicationController(c *client.Client, controller *api.ReplicationController, opts *AttachOptions) error {
var pods *api.PodList
for pods == nil || len(pods.Items) == 0 {
var err error
if pods, err = c.Pods(controller.Namespace).List(labels.SelectorFromSet(controller.Spec.Selector), fields.Everything()); err != nil {
return err
}
if len(pods.Items) == 0 {
fmt.Fprint(opts.Out, "Waiting for pod to be scheduled\n")
time.Sleep(2 * time.Second)
}
}
pod := &pods.Items[0]
return handleAttachPod(c, pod, opts)
}

func handleAttachPod(c *client.Client, pod *api.Pod, opts *AttachOptions) error {
status, err := waitForPodRunning(c, pod, opts.Out)
if err != nil {
Expand Down
34 changes: 34 additions & 0 deletions pkg/kubectl/cmd/util/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"os/user"
"path"
"strconv"
"time"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand All @@ -37,8 +38,10 @@ import (
"k8s.io/kubernetes/pkg/api/validation"
client "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/kubectl"
"k8s.io/kubernetes/pkg/kubectl/resource"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util"
)
Expand Down Expand Up @@ -92,6 +95,8 @@ type Factory struct {
CanBeExposed func(kind string) error
// Check whether the kind of resources could be autoscaled
CanBeAutoscaled func(kind string) error
// AttachablePodForObject returns the pod to which to attach given an object.
AttachablePodForObject func(object runtime.Object) (*api.Pod, error)
}

// NewFactory creates a factory with the default Kubernetes resources defined
Expand Down Expand Up @@ -268,6 +273,35 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
}
return nil
},
AttachablePodForObject: func(object runtime.Object) (*api.Pod, error) {
client, err := clients.ClientForVersion("")
if err != nil {
return nil, err
}
switch t := object.(type) {
case *api.ReplicationController:
var pods *api.PodList
for pods == nil || len(pods.Items) == 0 {
var err error
if pods, err = client.Pods(t.Namespace).List(labels.SelectorFromSet(t.Spec.Selector), fields.Everything()); err != nil {
return nil, err
}
if len(pods.Items) == 0 {
time.Sleep(2 * time.Second)
}
}
pod := &pods.Items[0]
return pod, nil
case *api.Pod:
return t, nil
default:
_, kind, err := api.Scheme.ObjectVersionAndKind(object)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("cannot attach to %s: not implemented", kind)
}
},
}
}

Expand Down