Skip to content

Commit e4fb348

Browse files
Debug should not have 15s timeout
Also remove duplicated code and support ImageStreamImage
1 parent f51703b commit e4fb348

File tree

8 files changed

+38
-126
lines changed

8 files changed

+38
-126
lines changed

docs/generated/oc_by_example_content.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -2130,7 +2130,7 @@ Generate a new token for a service account.
21302130
# Generate a new token for service account 'default'
21312131
oc serviceaccounts new-token 'default'
21322132
2133-
# Generate a new token for service account 'default' and apply
2133+
# Generate a new token for service account 'default' and apply
21342134
# labels 'foo' and 'bar' to the new token for identification
21352135
# oc serviceaccounts new-token 'default' --labels foo=foo-value,bar=bar-value
21362136

docs/man/man1/oc-serviceaccounts-new-token.1

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ be applied to any created token so that tokens created with this command can be
107107
# Generate a new token for service account 'default'
108108
oc serviceaccounts new\-token 'default'
109109

110-
# Generate a new token for service account 'default' and apply
110+
# Generate a new token for service account 'default' and apply
111111
# labels 'foo' and 'bar' to the new token for identification
112112
# oc serviceaccounts new\-token 'default' \-\-labels foo=foo\-value,bar=bar\-value
113113

docs/man/man1/openshift-cli-serviceaccounts-new-token.1

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ be applied to any created token so that tokens created with this command can be
107107
# Generate a new token for service account 'default'
108108
openshift cli serviceaccounts new\-token 'default'
109109

110-
# Generate a new token for service account 'default' and apply
110+
# Generate a new token for service account 'default' and apply
111111
# labels 'foo' and 'bar' to the new token for identification
112112
# openshift cli serviceaccounts new\-token 'default' \-\-labels foo=foo\-value,bar=bar\-value
113113

pkg/cmd/cli/cmd/debug.go

+7-110
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@ import (
1414
kapierrors "k8s.io/kubernetes/pkg/api/errors"
1515
"k8s.io/kubernetes/pkg/api/unversioned"
1616
"k8s.io/kubernetes/pkg/client/restclient"
17+
kclient "k8s.io/kubernetes/pkg/client/unversioned"
1718
"k8s.io/kubernetes/pkg/fields"
1819
kcmd "k8s.io/kubernetes/pkg/kubectl/cmd"
1920
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
2021
"k8s.io/kubernetes/pkg/kubectl/resource"
22+
"k8s.io/kubernetes/pkg/runtime"
2123
"k8s.io/kubernetes/pkg/util/interrupt"
2224
"k8s.io/kubernetes/pkg/util/term"
23-
"k8s.io/kubernetes/pkg/util/wait"
2425
"k8s.io/kubernetes/pkg/watch"
2526

2627
cmdutil "github.com/openshift/origin/pkg/cmd/util"
2728
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
28-
"github.com/openshift/origin/pkg/generate/app"
29+
generateapp "github.com/openshift/origin/pkg/generate/app"
2930
imageapi "github.com/openshift/origin/pkg/image/api"
30-
"k8s.io/kubernetes/pkg/runtime"
3131
)
3232

3333
type DebugOptions struct {
@@ -102,7 +102,7 @@ the shell.`
102102
// NewCmdDebug creates a command for debugging pods.
103103
func NewCmdDebug(fullName string, f *clientcmd.Factory, in io.Reader, out, errout io.Writer) *cobra.Command {
104104
options := &DebugOptions{
105-
Timeout: 30 * time.Second,
105+
Timeout: 15 * time.Minute,
106106
Attach: kcmd.AttachOptions{
107107
In: in,
108108
Out: out,
@@ -238,7 +238,7 @@ func (o *DebugOptions) Complete(cmd *cobra.Command, f *clientcmd.Factory, args [
238238
ObjectMeta: template.ObjectMeta,
239239
Spec: template.Spec,
240240
}
241-
pod.Name, pod.Namespace = infos[0].Name, infos[0].Namespace
241+
pod.Name, pod.Namespace = fmt.Sprintf("%s-debug", generateapp.MakeSimpleName(infos[0].Name)), infos[0].Namespace
242242
o.Attach.Pod = pod
243243

244244
o.AsNonRoot = !o.AsRoot && cmd.Flag("as-root").Changed
@@ -288,106 +288,6 @@ func (o DebugOptions) Validate() error {
288288
return nil
289289
}
290290

291-
// WatchConditionFunc returns true if the condition has been reached, false if it has not been reached yet,
292-
// or an error if the condition cannot be checked and should terminate.
293-
type WatchConditionFunc func(event watch.Event) (bool, error)
294-
295-
// Until reads items from the watch until each provided condition succeeds, and then returns the last watch
296-
// encountered. The first condition that returns an error terminates the watch (and the event is also returned).
297-
// If no event has been received, the returned event will be nil.
298-
// TODO: move to pkg/watch upstream
299-
func Until(timeout time.Duration, watcher watch.Interface, conditions ...WatchConditionFunc) (*watch.Event, error) {
300-
ch := watcher.ResultChan()
301-
defer watcher.Stop()
302-
var after <-chan time.Time
303-
if timeout > 0 {
304-
after = time.After(timeout)
305-
} else {
306-
ch := make(chan time.Time)
307-
close(ch)
308-
after = ch
309-
}
310-
var lastEvent *watch.Event
311-
for _, condition := range conditions {
312-
for {
313-
select {
314-
case event, ok := <-ch:
315-
if !ok {
316-
return lastEvent, wait.ErrWaitTimeout
317-
}
318-
lastEvent = &event
319-
// TODO: check for watch expired error and retry watch from latest point?
320-
done, err := condition(event)
321-
if err != nil {
322-
return lastEvent, err
323-
}
324-
if done {
325-
return lastEvent, nil
326-
}
327-
case <-after:
328-
return lastEvent, wait.ErrWaitTimeout
329-
}
330-
}
331-
}
332-
return lastEvent, wait.ErrWaitTimeout
333-
}
334-
335-
// ErrPodCompleted is returned by PodRunning or PodContainerRunning to indicate that
336-
// the pod has already reached completed state.
337-
var ErrPodCompleted = fmt.Errorf("pod ran to completion")
338-
339-
// TODO: move to pkg/client/conditions.go upstream
340-
//
341-
// Example of a running condition, will be used elsewhere
342-
//
343-
// PodRunning returns true if the pod is running, false if the pod has not yet reached running state,
344-
// returns ErrPodCompleted if the pod has run to completion, or an error in any other case.
345-
// func PodRunning(event watch.Event) (bool, error) {
346-
// switch event.Type {
347-
// case watch.Deleted:
348-
// return false, kapierrors.NewNotFound(unversioned.GroupResource{Resource: "pods"}, "")
349-
// }
350-
// switch t := event.Object.(type) {
351-
// case *kapi.Pod:
352-
// switch t.Status.Phase {
353-
// case kapi.PodRunning:
354-
// return true, nil
355-
// case kapi.PodFailed, kapi.PodSucceeded:
356-
// return false, ErrPodCompleted
357-
// }
358-
// }
359-
// return false, nil
360-
// }
361-
362-
// PodContainerRunning returns false until the named container has ContainerStatus running (at least once),
363-
// and will return an error if the pod is deleted, runs to completion, or the container pod is not available.
364-
func PodContainerRunning(containerName string) WatchConditionFunc {
365-
return func(event watch.Event) (bool, error) {
366-
switch event.Type {
367-
case watch.Deleted:
368-
return false, kapierrors.NewNotFound(unversioned.GroupResource{Resource: "pods"}, "")
369-
}
370-
switch t := event.Object.(type) {
371-
case *kapi.Pod:
372-
switch t.Status.Phase {
373-
case kapi.PodRunning, kapi.PodPending:
374-
case kapi.PodFailed, kapi.PodSucceeded:
375-
return false, ErrPodCompleted
376-
default:
377-
return false, nil
378-
}
379-
for _, s := range t.Status.ContainerStatuses {
380-
if s.Name != containerName {
381-
continue
382-
}
383-
return s.State.Running != nil, nil
384-
}
385-
return false, nil
386-
}
387-
return false, nil
388-
}
389-
}
390-
391291
// SingleObject returns a ListOptions for watching a single object.
392292
// TODO: move to pkg/api/helpers.go upstream.
393293
func SingleObject(meta kapi.ObjectMeta) kapi.ListOptions {
@@ -439,7 +339,7 @@ func (o *DebugOptions) Debug() error {
439339
return err
440340
}
441341
fmt.Fprintf(o.Attach.Err, "Waiting for pod to start ...\n")
442-
switch containerRunningEvent, err := Until(o.Timeout, w, PodContainerRunning(o.Attach.ContainerName)); {
342+
switch containerRunningEvent, err := watch.Until(o.Timeout, w, kclient.PodContainerRunning(o.Attach.ContainerName)); {
443343
// api didn't error right away but the pod wasn't even created
444344
case kapierrors.IsNotFound(err):
445345
msg := fmt.Sprintf("unable to create the debug pod %q", pod.Name)
@@ -448,7 +348,7 @@ func (o *DebugOptions) Debug() error {
448348
}
449349
return fmt.Errorf(msg)
450350
// switch to logging output
451-
case err == ErrPodCompleted, !o.Attach.Stdin:
351+
case err == kclient.ErrPodCompleted, !o.Attach.Stdin:
452352
_, err := kcmd.LogsOptions{
453353
Object: pod,
454354
Options: &kapi.PodLogOptions{
@@ -566,9 +466,6 @@ func (o *DebugOptions) transformPodForDebug(annotations map[string]string) (*kap
566466
pod.ResourceVersion = ""
567467
pod.Spec.RestartPolicy = kapi.RestartPolicyNever
568468

569-
// shorten segments to handle long names and names with bad characters
570-
pod.Name, _ = app.NewUniqueNameGenerator(fmt.Sprintf("%s-debug", pod.Name)).Generate(nil)
571-
572469
pod.Status = kapi.PodStatus{}
573470
pod.UID = ""
574471
pod.CreationTimestamp = unversioned.Time{}

pkg/cmd/cli/sa/newtoken.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
1818
"k8s.io/kubernetes/pkg/watch"
1919

20-
"github.com/openshift/origin/pkg/cmd/cli/cmd"
2120
"github.com/openshift/origin/pkg/cmd/util"
2221
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
2322
"github.com/openshift/origin/pkg/serviceaccounts"
@@ -44,7 +43,7 @@ be applied to any created token so that tokens created with this command can be
4443
newServiceAccountTokenExamples = ` # Generate a new token for service account 'default'
4544
%[1]s 'default'
4645
47-
# Generate a new token for service account 'default' and apply
46+
# Generate a new token for service account 'default' and apply
4847
# labels 'foo' and 'bar' to the new token for identification
4948
# %[1]s 'default' --labels foo=foo-value,bar=bar-value
5049
`
@@ -201,7 +200,7 @@ func waitForToken(token *api.Secret, serviceAccount *api.ServiceAccount, timeout
201200
return nil, fmt.Errorf("could not begin watch for token: %v", err)
202201
}
203202

204-
event, err := cmd.Until(timeout, watcher, func(event watch.Event) (bool, error) {
203+
event, err := watch.Until(timeout, watcher, func(event watch.Event) (bool, error) {
205204
if event.Type == watch.Error {
206205
return false, fmt.Errorf("encountered error while watching for token: %v", event.Object)
207206
}

pkg/cmd/util/clientcmd/factory.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,17 @@ func (w *Factory) ApproximatePodTemplateForObject(object runtime.Object) (*api.P
614614
},
615615
},
616616
}, nil
617-
617+
case *imageapi.ImageStreamImage:
618+
// create a minimal pod spec that uses the image referenced by the istag without any introspection
619+
// it possible that we could someday do a better job introspecting it
620+
return &api.PodTemplateSpec{
621+
Spec: api.PodSpec{
622+
RestartPolicy: api.RestartPolicyNever,
623+
Containers: []api.Container{
624+
{Name: "container-00", Image: t.Image.DockerImageReference},
625+
},
626+
},
627+
}, nil
618628
case *deployapi.DeploymentConfig:
619629
fallback := t.Spec.Template
620630

pkg/generate/app/pipeline.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -254,20 +254,27 @@ func (g PipelineGroup) String() string {
254254
return strings.Join(s, "+")
255255
}
256256

257+
// MakeSimpleName strips any non-alphanumeric characters out of a string and returns
258+
// either an empty string or a string which is valid for most Kubernetes resources.
259+
func MakeSimpleName(name string) string {
260+
name = strings.ToLower(name)
261+
name = invalidServiceChars.ReplaceAllString(name, "")
262+
name = strings.TrimFunc(name, func(r rune) bool { return r == '-' })
263+
if len(name) > kuval.DNS952LabelMaxLength {
264+
name = name[:kuval.DNS952LabelMaxLength]
265+
}
266+
return name
267+
}
268+
257269
var invalidServiceChars = regexp.MustCompile("[^-a-z0-9]")
258270

259271
func makeValidServiceName(name string) (string, string) {
260272
if len(validation.ValidateServiceName(name, false)) == 0 {
261273
return name, ""
262274
}
263-
name = strings.ToLower(name)
264-
name = invalidServiceChars.ReplaceAllString(name, "")
265-
name = strings.TrimFunc(name, func(r rune) bool { return r == '-' })
266-
switch {
267-
case len(name) == 0:
275+
name = MakeSimpleName(name)
276+
if len(name) == 0 {
268277
return "", "service-"
269-
case len(name) > kuval.DNS952LabelMaxLength:
270-
name = name[:kuval.DNS952LabelMaxLength]
271278
}
272279
return name, ""
273280
}

test/util/namespace.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
kclient "k8s.io/kubernetes/pkg/client/unversioned"
99
"k8s.io/kubernetes/pkg/watch"
1010

11-
"github.com/openshift/origin/pkg/cmd/cli/cmd"
1211
"github.com/openshift/origin/pkg/cmd/util"
1312
)
1413

@@ -46,7 +45,7 @@ func DeleteAndWaitForNamespaceTermination(c *kclient.Client, name string) error
4645
if err := c.Namespaces().Delete(name); err != nil {
4746
return err
4847
}
49-
_, err = cmd.Until(30*time.Second, w, func(event watch.Event) (bool, error) {
48+
_, err = watch.Until(30*time.Second, w, func(event watch.Event) (bool, error) {
5049
if event.Type != watch.Deleted {
5150
return false, nil
5251
}

0 commit comments

Comments
 (0)