Exemple #1
0
// RunConvert implements the generic Convert command
func (o *ConvertOptions) RunConvert() error {
	infos, err := o.builder.Do().Infos()
	if err != nil {
		return err
	}

	objects, err := resource.AsVersionedObject(infos, false, o.outputVersion)
	if err != nil {
		return err
	}

	return o.printer.PrintObj(objects, o.out)
}
Exemple #2
0
func (d *Deployer) listHandler(request *restful.Request, response *restful.Response) {
	var list runtime.Object
	start := time.Now()
	defer func() {
		WriteAccessLog(start, request, response)
	}()
	mapper, typer := d.Factory.Object()

	//  request parameters
	//	namespace string # whether query between all-spaces
	//	outFormat string # format as json, yaml, etc.
	//	template  string # whether use template to show result.
	//	kind      string # resource kind for request.
	//	name      string # resources name, multiple can separate by ,
	//	selector  string # info selectors saved in etcd.

	selector := request.QueryParameter("selector")
	namespace := request.QueryParameter("namespace")
	name := request.QueryParameter("name")
	kind := request.PathParameter("kind")
	template := request.QueryParameter("template")

	if namespace == "" {
		cmdNamespace, _, err := d.Factory.DefaultNamespace()
		if err != nil {
			message := fmt.Sprintf("get defaule namespace error %#v", err)
			response.WriteAsJson(ApiResult{message, list, false})
			return
		}
		namespace = cmdNamespace
	}

	var args = []string{kind}
	if len(name) > 0 {
		args = append(args, strings.Split(name, ",")...)
	}

	if len(kind) == 0 {
		message := fmt.Sprintf("get resource kind unspecified")
		response.WriteAsJson(ApiResult{message, list, false})
		return
	}
	builder := resource.NewBuilder(mapper, typer, d.Factory.ClientMapperForCommand()).
		NamespaceParam(namespace).DefaultNamespace().AllNamespaces(namespace == "all").
		SelectorParam(selector).
		ResourceTypeOrNameArgs(true, args...).
		ContinueOnError().
		Latest()

	_, generic, err := PrinterForCommand("json", template)
	if err != nil {
		message := fmt.Sprintf("get resource kind unspecified: %#v", err)
		response.WriteAsJson(ApiResult{message, list, false})
		return
	}

	if generic {

		clientConfig, err := d.Factory.ClientConfig()
		if err != nil {
			message := fmt.Sprintf("connect to API server failed: %#v", err)
			response.WriteAsJson(ApiResult{message, list, false})
			return
		}

		version := clientConfig.Version

		singular := false
		r := builder.Flatten().Do()
		infos, err := r.IntoSingular(&singular).Infos()
		if err != nil {
			message := fmt.Sprintf("change into Singular failed: %#v", err)
			response.WriteAsJson(ApiResult{message, list, false})
			return
		}

		// the outermost object will be converted to the output-version, but inner
		// objects can use their mappings
		obj, err := resource.AsVersionedObject(infos, !singular, version)
		if err != nil {
			message := fmt.Sprintf("conversion object version failed: %#v", err)
			response.WriteAsJson(ApiResult{message, list, false})
			return
		}
		response.WriteAsJson(ApiResult{"", obj, true})
		return
	}

	// use the default printer for each object
	builder.Do().Visit(func(r *resource.Info) error {
		_, err := d.PrinterForMapping("json", template, r.Mapping, namespace == "all")
		if err != nil {
			response.WriteAsJson(ApiResult{err.Error(), r.Object, false})
			return err
		}
		response.WriteAsJson(ApiResult{"", r.Object, true})
		return nil
	})

}
Exemple #3
0
// RunGet implements the generic Get command
// TODO: convert all direct flag accessors to a struct and pass that instead of cmd
func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, options *GetOptions) error {
	selector := cmdutil.GetFlagString(cmd, "selector")
	allNamespaces := cmdutil.GetFlagBool(cmd, "all-namespaces")
	mapper, typer := f.Object()

	cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
	if err != nil {
		return err
	}

	if len(args) == 0 && len(options.Filenames) == 0 {
		fmt.Fprint(out, "You must specify the type of resource to get. ", valid_resources, `   * componentstatuses (aka 'cs')
   * endpoints (aka 'ep')
`)
		return cmdutil.UsageError(cmd, "Required resource not specified.")
	}

	// handle watch separately since we cannot watch multiple resource types
	isWatch, isWatchOnly := cmdutil.GetFlagBool(cmd, "watch"), cmdutil.GetFlagBool(cmd, "watch-only")
	if isWatch || isWatchOnly {
		r := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand()).
			NamespaceParam(cmdNamespace).DefaultNamespace().AllNamespaces(allNamespaces).
			FilenameParam(enforceNamespace, options.Filenames...).
			SelectorParam(selector).
			ResourceTypeOrNameArgs(true, args...).
			SingleResourceType().
			Latest().
			Do()
		err := r.Err()
		if err != nil {
			return err
		}
		infos, err := r.Infos()
		if err != nil {
			return err
		}
		if len(infos) != 1 {
			return fmt.Errorf("watch is only supported on individual resources and resource collections - %d resources were found", len(infos))
		}
		info := infos[0]
		mapping := info.ResourceMapping()
		printer, err := f.PrinterForMapping(cmd, mapping, allNamespaces)
		if err != nil {
			return err
		}

		obj, err := r.Object()
		if err != nil {
			return err
		}
		rv, err := mapping.MetadataAccessor.ResourceVersion(obj)
		if err != nil {
			return err
		}

		// print the current object
		if !isWatchOnly {
			if err := printer.PrintObj(obj, out); err != nil {
				return fmt.Errorf("unable to output the provided object: %v", err)
			}
		}

		// print watched changes
		w, err := r.Watch(rv)
		if err != nil {
			return err
		}

		kubectl.WatchLoop(w, func(e watch.Event) error {
			return printer.PrintObj(e.Object, out)
		})
		return nil
	}

	b := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand()).
		NamespaceParam(cmdNamespace).DefaultNamespace().AllNamespaces(allNamespaces).
		FilenameParam(enforceNamespace, options.Filenames...).
		SelectorParam(selector).
		ResourceTypeOrNameArgs(true, args...).
		ContinueOnError().
		Latest()
	printer, generic, err := cmdutil.PrinterForCommand(cmd)
	if err != nil {
		return err
	}

	if generic {
		clientConfig, err := f.ClientConfig()
		if err != nil {
			return err
		}
		defaultVersion := clientConfig.Version

		singular := false
		r := b.Flatten().Do()
		infos, err := r.IntoSingular(&singular).Infos()
		if err != nil {
			return err
		}

		// the outermost object will be converted to the output-version, but inner
		// objects can use their mappings
		version := cmdutil.OutputVersion(cmd, defaultVersion)
		obj, err := resource.AsVersionedObject(infos, !singular, version)
		if err != nil {
			return err
		}

		return printer.PrintObj(obj, out)
	}

	// use the default printer for each object
	printer = nil
	var lastMapping *meta.RESTMapping
	w := kubectl.GetNewTabWriter(out)
	defer w.Flush()
	return b.Flatten().Do().Visit(func(r *resource.Info, err error) error {
		if err != nil {
			return err
		}
		if printer == nil || lastMapping == nil || r.Mapping == nil || r.Mapping.Resource != lastMapping.Resource {
			printer, err = f.PrinterForMapping(cmd, r.Mapping, allNamespaces)
			if err != nil {
				return err
			}
			lastMapping = r.Mapping
		}
		if _, found := printer.(*kubectl.HumanReadablePrinter); found {
			return printer.PrintObj(r.Object, w)
		}
		return printer.PrintObj(r.Object, out)
	})
}