mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 20:24:09 +00:00
Merge pull request #16109 from fabianofranz/move_attach_pod_selection_to_factory
Auto commit by PR queue bot
This commit is contained in:
commit
40b6f1cc28
@ -26,11 +26,9 @@ import (
|
|||||||
"k8s.io/kubernetes/pkg/api"
|
"k8s.io/kubernetes/pkg/api"
|
||||||
"k8s.io/kubernetes/pkg/api/meta"
|
"k8s.io/kubernetes/pkg/api/meta"
|
||||||
client "k8s.io/kubernetes/pkg/client/unversioned"
|
client "k8s.io/kubernetes/pkg/client/unversioned"
|
||||||
"k8s.io/kubernetes/pkg/fields"
|
|
||||||
"k8s.io/kubernetes/pkg/kubectl"
|
"k8s.io/kubernetes/pkg/kubectl"
|
||||||
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
||||||
"k8s.io/kubernetes/pkg/kubectl/resource"
|
"k8s.io/kubernetes/pkg/kubectl/resource"
|
||||||
"k8s.io/kubernetes/pkg/labels"
|
|
||||||
"k8s.io/kubernetes/pkg/runtime"
|
"k8s.io/kubernetes/pkg/runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -202,15 +200,12 @@ func Run(f *cmdutil.Factory, cmdIn io.Reader, cmdOut, cmdErr io.Writer, cmd *cob
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
opts.Client = client
|
opts.Client = client
|
||||||
// TODO: this should be abstracted into Factory to support other types
|
|
||||||
switch t := obj.(type) {
|
attachablePod, err := f.AttachablePodForObject(obj)
|
||||||
case *api.ReplicationController:
|
if err != nil {
|
||||||
return handleAttachReplicationController(client, t, opts)
|
return err
|
||||||
case *api.Pod:
|
|
||||||
return handleAttachPod(client, t, opts)
|
|
||||||
default:
|
|
||||||
return fmt.Errorf("cannot attach to %s: not implemented", kind)
|
|
||||||
}
|
}
|
||||||
|
return handleAttachPod(client, attachablePod, opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
outputFormat := cmdutil.GetFlagString(cmd, "output")
|
outputFormat := cmdutil.GetFlagString(cmd, "output")
|
||||||
@ -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 {
|
func handleAttachPod(c *client.Client, pod *api.Pod, opts *AttachOptions) error {
|
||||||
status, err := waitForPodRunning(c, pod, opts.Out)
|
status, err := waitForPodRunning(c, pod, opts.Out)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -27,6 +27,7 @@ import (
|
|||||||
"os/user"
|
"os/user"
|
||||||
"path"
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
@ -37,8 +38,10 @@ import (
|
|||||||
"k8s.io/kubernetes/pkg/api/validation"
|
"k8s.io/kubernetes/pkg/api/validation"
|
||||||
client "k8s.io/kubernetes/pkg/client/unversioned"
|
client "k8s.io/kubernetes/pkg/client/unversioned"
|
||||||
"k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
|
"k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
|
||||||
|
"k8s.io/kubernetes/pkg/fields"
|
||||||
"k8s.io/kubernetes/pkg/kubectl"
|
"k8s.io/kubernetes/pkg/kubectl"
|
||||||
"k8s.io/kubernetes/pkg/kubectl/resource"
|
"k8s.io/kubernetes/pkg/kubectl/resource"
|
||||||
|
"k8s.io/kubernetes/pkg/labels"
|
||||||
"k8s.io/kubernetes/pkg/runtime"
|
"k8s.io/kubernetes/pkg/runtime"
|
||||||
"k8s.io/kubernetes/pkg/util"
|
"k8s.io/kubernetes/pkg/util"
|
||||||
)
|
)
|
||||||
@ -92,6 +95,8 @@ type Factory struct {
|
|||||||
CanBeExposed func(kind string) error
|
CanBeExposed func(kind string) error
|
||||||
// Check whether the kind of resources could be autoscaled
|
// Check whether the kind of resources could be autoscaled
|
||||||
CanBeAutoscaled func(kind string) error
|
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
|
// NewFactory creates a factory with the default Kubernetes resources defined
|
||||||
@ -268,6 +273,35 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
|
|||||||
}
|
}
|
||||||
return nil
|
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)
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user