コード例 #1
0
ファイル: new.go プロジェクト: DirectXMan12/kubernetes
func initExample(cmdNamespace string, resource string, f *cmdutil.Factory, cmd *cobra.Command) (runtime.Object, error) {
	mapper, _ := f.Object()
	version, kind, err := mapper.VersionAndKindForResource(resource)
	if err != nil {
		return nil, err
	}

	exBuilder := f.ExampleBuilder()
	obj, _, err := exBuilder.NewExample(version, kind)
	if err != nil {
		return nil, err
	}

	mapping, err := mapper.RESTMapping(kind, version)
	if err != nil {
		return nil, err
	}

	err = mapping.SetAPIVersion(obj, version)
	if err != nil {
		return nil, err
	}
	err = mapping.SetKind(obj, kind)
	if err != nil {
		return nil, err
	}
	err = mapping.SetNamespace(obj, cmdNamespace)
	if err != nil {
		return nil, err
	}

	nameFlag := cmdutil.GetFlagString(cmd, "name")
	if nameFlag != "" {
		err = mapping.SetName(obj, nameFlag)
	} else {
		err = mapping.SetName(obj, " ")
	}
	if err != nil {
		return nil, err
	}

	labelsFlag := cmdutil.GetFlagString(cmd, "labels")
	if labelsFlag != "" {
		parsedLabels, err := kubectl.ParseLabels(labelsFlag)
		if err != nil {
			return nil, err
		}

		err = mapping.SetLabels(obj, parsedLabels)
		if err != nil {
			return nil, err
		}
	}

	return obj, nil
}