Пример #1
0
func handleAttachPod(f *cmdutil.Factory, c *client.Client, ns, name string, opts *AttachOptions, quiet bool) error {
	pod, err := waitForPodRunning(c, ns, name, opts.Out, quiet)
	if err != nil {
		return err
	}
	ctrName, err := opts.GetContainerName(pod)
	if err != nil {
		return err
	}
	if pod.Status.Phase == api.PodSucceeded || pod.Status.Phase == api.PodFailed {
		req, err := f.LogsForObject(pod, &api.PodLogOptions{Container: ctrName})
		if err != nil {
			return err
		}
		readCloser, err := req.Stream()
		if err != nil {
			return err
		}
		defer readCloser.Close()
		_, err = io.Copy(opts.Out, readCloser)
		return err
	}

	clientset, err := f.ClientSet()
	if err != nil {
		return nil
	}
	opts.PodClient = clientset.Core()

	opts.PodName = name
	opts.Namespace = ns
	// TODO: opts.Run sets opts.Err to nil, we need to find a better way
	stderr := opts.Err
	if err := opts.Run(); err != nil {
		fmt.Fprintf(stderr, "Error attaching, falling back to logs: %v\n", err)
		req, err := f.LogsForObject(pod, &api.PodLogOptions{Container: ctrName})
		if err != nil {
			return err
		}
		readCloser, err := req.Stream()
		if err != nil {
			return err
		}
		defer readCloser.Close()
		_, err = io.Copy(opts.Out, readCloser)
		return err
	}
	return nil
}
Пример #2
0
func handleAttachPod(f *cmdutil.Factory, c *client.Client, pod *api.Pod, opts *AttachOptions, quiet bool) error {
	status, err := waitForPodRunning(c, pod, opts.Out, quiet)
	if err != nil {
		return err
	}
	ctrName, err := opts.GetContainerName(pod)
	if err != nil {
		return err
	}
	if status == api.PodSucceeded || status == api.PodFailed {
		req, err := f.LogsForObject(pod, &api.PodLogOptions{Container: ctrName})
		if err != nil {
			return err
		}
		readCloser, err := req.Stream()
		if err != nil {
			return err
		}
		defer readCloser.Close()
		_, err = io.Copy(opts.Out, readCloser)
		return err
	}
	opts.Client = c
	opts.PodName = pod.Name
	opts.Namespace = pod.Namespace
	if err := opts.Run(); err != nil {
		fmt.Fprintf(opts.Out, "Error attaching, falling back to logs: %v\n", err)
		req, err := f.LogsForObject(pod, &api.PodLogOptions{Container: ctrName})
		if err != nil {
			return err
		}
		readCloser, err := req.Stream()
		if err != nil {
			return err
		}
		defer readCloser.Close()
		_, err = io.Copy(opts.Out, readCloser)
		return err
	}
	return nil
}
Пример #3
0
func dumpClusterInfo(f cmdutil.Factory, cmd *cobra.Command, args []string, out io.Writer) error {
	clientset, err := f.ClientSet()
	if err != nil {
		return err
	}

	printer, _, err := kubectl.GetPrinter("json", "", false, true)
	if err != nil {
		return err
	}

	nodes, err := clientset.Core().Nodes().List(api.ListOptions{})
	if err != nil {
		return err
	}

	if err := printer.PrintObj(nodes, setupOutputWriter(cmd, out, "nodes.json")); err != nil {
		return err
	}

	var namespaces []string
	if cmdutil.GetFlagBool(cmd, "all-namespaces") {
		namespaceList, err := clientset.Core().Namespaces().List(api.ListOptions{})
		if err != nil {
			return err
		}
		for ix := range namespaceList.Items {
			namespaces = append(namespaces, namespaceList.Items[ix].Name)
		}
	} else {
		namespaces = cmdutil.GetFlagStringSlice(cmd, "namespaces")
		if len(namespaces) == 0 {
			cmdNamespace, _, err := f.DefaultNamespace()
			if err != nil {
				return err
			}
			namespaces = []string{
				api.NamespaceSystem,
				cmdNamespace,
			}
		}
	}
	for _, namespace := range namespaces {
		// TODO: this is repetitive in the extreme.  Use reflection or
		// something to make this a for loop.
		events, err := clientset.Core().Events(namespace).List(api.ListOptions{})
		if err != nil {
			return err
		}
		if err := printer.PrintObj(events, setupOutputWriter(cmd, out, path.Join(namespace, "events.json"))); err != nil {
			return err
		}

		rcs, err := clientset.Core().ReplicationControllers(namespace).List(api.ListOptions{})
		if err != nil {
			return err
		}
		if err := printer.PrintObj(rcs, setupOutputWriter(cmd, out, path.Join(namespace, "replication-controllers.json"))); err != nil {
			return err
		}

		svcs, err := clientset.Core().Services(namespace).List(api.ListOptions{})
		if err != nil {
			return err
		}
		if err := printer.PrintObj(svcs, setupOutputWriter(cmd, out, path.Join(namespace, "services.json"))); err != nil {
			return err
		}

		sets, err := clientset.Extensions().DaemonSets(namespace).List(api.ListOptions{})
		if err != nil {
			return err
		}
		if err := printer.PrintObj(sets, setupOutputWriter(cmd, out, path.Join(namespace, "daemonsets.json"))); err != nil {
			return err
		}

		deps, err := clientset.Extensions().Deployments(namespace).List(api.ListOptions{})
		if err != nil {
			return err
		}
		if err := printer.PrintObj(deps, setupOutputWriter(cmd, out, path.Join(namespace, "deployments.json"))); err != nil {
			return err
		}

		rps, err := clientset.Extensions().ReplicaSets(namespace).List(api.ListOptions{})
		if err != nil {
			return err
		}
		if err := printer.PrintObj(rps, setupOutputWriter(cmd, out, path.Join(namespace, "replicasets.json"))); err != nil {
			return err
		}

		pods, err := clientset.Core().Pods(namespace).List(api.ListOptions{})
		if err != nil {
			return err
		}

		if err := printer.PrintObj(pods, setupOutputWriter(cmd, out, path.Join(namespace, "pods.json"))); err != nil {
			return err
		}

		for ix := range pods.Items {
			pod := &pods.Items[ix]
			writer := setupOutputWriter(cmd, out, path.Join(namespace, pod.Name, "logs.txt"))
			writer.Write([]byte(fmt.Sprintf("==== START logs for %s/%s ====\n", pod.Namespace, pod.Name)))
			request, err := f.LogsForObject(pod, &api.PodLogOptions{})
			if err != nil {
				return err
			}

			data, err := request.DoRaw()
			if err != nil {
				return err
			}
			writer.Write(data)
			writer.Write([]byte(fmt.Sprintf("==== END logs for %s/%s ====\n", pod.Namespace, pod.Name)))
		}
	}
	dir := cmdutil.GetFlagString(cmd, "output-directory")
	if len(dir) == 0 {
		dir = "."
	}
	if dir != "-" {
		fmt.Fprintf(out, "Cluster info dumped to %s", dir)
	}
	return nil
}