本文整理匯總了Golang中github.com/openshift/origin/pkg/cmd/util/clientcmd.Factory.UpdatePodSpecForObject方法的典型用法代碼示例。如果您正苦於以下問題:Golang Factory.UpdatePodSpecForObject方法的具體用法?Golang Factory.UpdatePodSpecForObject怎麽用?Golang Factory.UpdatePodSpecForObject使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/openshift/origin/pkg/cmd/util/clientcmd.Factory
的用法示例。
在下文中一共展示了Factory.UpdatePodSpecForObject方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: RunEnv
// RunEnv contains all the necessary functionality for the OpenShift cli env command
func RunEnv(f *clientcmd.Factory, in io.Reader, out io.Writer, cmd *cobra.Command, args []string, envParams, filenames kutil.StringList) error {
resources, envArgs := []string{}, []string{}
first := true
for _, s := range args {
isEnv := strings.Contains(s, "=") || strings.HasSuffix(s, "-")
switch {
case first && isEnv:
first = false
fallthrough
case !first && isEnv:
envArgs = append(envArgs, s)
case first && !isEnv:
resources = append(resources, s)
case !first && !isEnv:
return cmdutil.UsageError(cmd, "all resources must be specified before environment changes: %s", s)
}
}
if len(filenames) == 0 && len(resources) < 1 {
return cmdutil.UsageError(cmd, "one or more resources must be specified as <resource> <name> or <resource>/<name>")
}
containerMatch := cmdutil.GetFlagString(cmd, "containers")
list := cmdutil.GetFlagBool(cmd, "list")
selector := cmdutil.GetFlagString(cmd, "selector")
all := cmdutil.GetFlagBool(cmd, "all")
//overwrite := cmdutil.GetFlagBool(cmd, "overwrite")
resourceVersion := cmdutil.GetFlagString(cmd, "resource-version")
outputFormat := cmdutil.GetFlagString(cmd, "output")
if list && len(outputFormat) > 0 {
return cmdutil.UsageError(cmd, "--list and --output may not be specified together")
}
clientConfig, err := f.ClientConfig()
if err != nil {
return err
}
outputVersion := cmdutil.OutputVersion(cmd, clientConfig.Version)
cmdNamespace, err := f.DefaultNamespace()
if err != nil {
return err
}
env, remove, err := parseEnv(append(envParams, envArgs...), in)
if err != nil {
return err
}
mapper, typer := f.Object()
b := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand()).
ContinueOnError().
NamespaceParam(cmdNamespace).DefaultNamespace().
FilenameParam(filenames...).
SelectorParam(selector).
ResourceTypeOrNameArgs(all, resources...).
Flatten()
one := false
infos, err := b.Do().IntoSingular(&one).Infos()
if err != nil {
return err
}
// only apply resource version locking on a single resource
if !one && len(resourceVersion) > 0 {
return cmdutil.UsageError(cmd, "--resource-version may only be used with a single resource")
}
skipped := 0
for _, info := range infos {
ok, err := f.UpdatePodSpecForObject(info.Object, func(spec *kapi.PodSpec) error {
containers, _ := selectContainers(spec.Containers, containerMatch)
if len(containers) == 0 {
fmt.Fprintf(cmd.Out(), "warning: %s/%s does not have any containers matching %q\n", info.Mapping.Resource, info.Name, containerMatch)
return nil
}
for _, c := range containers {
c.Env = updateEnv(c.Env, env, remove)
if list {
fmt.Fprintf(out, "# %s %s, container %s\n", info.Mapping.Resource, info.Name, c.Name)
for _, env := range c.Env {
// if env.ValueFrom != nil && env.ValueFrom.FieldRef != nil {
// fmt.Fprintf(cmd.Out(), "%s= # calculated from pod %s %s\n", env.Name, env.ValueFrom.FieldRef.FieldPath, env.ValueFrom.FieldRef.APIVersion)
// continue
// }
fmt.Fprintf(out, "%s=%s\n", env.Name, env.Value)
}
}
}
return nil
})
if !ok {
skipped++
continue
}
if err != nil {
//.........這裏部分代碼省略.........
示例2: RunEnv
// RunEnv contains all the necessary functionality for the OpenShift cli env command
// TODO: refactor to share the common "patch resource" pattern of probe
func RunEnv(f *clientcmd.Factory, in io.Reader, out io.Writer, cmd *cobra.Command, args []string, envParams, filenames []string) error {
resources, envArgs, ok := cmdutil.SplitEnvironmentFromResources(args)
if !ok {
return kcmdutil.UsageError(cmd, "all resources must be specified before environment changes: %s", strings.Join(args, " "))
}
if len(filenames) == 0 && len(resources) < 1 {
return kcmdutil.UsageError(cmd, "one or more resources must be specified as <resource> <name> or <resource>/<name>")
}
containerMatch := kcmdutil.GetFlagString(cmd, "containers")
list := kcmdutil.GetFlagBool(cmd, "list")
selector := kcmdutil.GetFlagString(cmd, "selector")
all := kcmdutil.GetFlagBool(cmd, "all")
//overwrite := kcmdutil.GetFlagBool(cmd, "overwrite")
resourceVersion := kcmdutil.GetFlagString(cmd, "resource-version")
outputFormat := kcmdutil.GetFlagString(cmd, "output")
if list && len(outputFormat) > 0 {
return kcmdutil.UsageError(cmd, "--list and --output may not be specified together")
}
clientConfig, err := f.ClientConfig()
if err != nil {
return err
}
cmdNamespace, explicit, err := f.DefaultNamespace()
if err != nil {
return err
}
env, remove, err := cmdutil.ParseEnv(append(envParams, envArgs...), in)
if err != nil {
return err
}
mapper, typer := f.Object()
b := resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), kapi.Codecs.UniversalDecoder()).
ContinueOnError().
NamespaceParam(cmdNamespace).DefaultNamespace().
FilenameParam(explicit, filenames...).
SelectorParam(selector).
ResourceTypeOrNameArgs(all, resources...).
Flatten()
one := false
infos, err := b.Do().IntoSingular(&one).Infos()
if err != nil {
return err
}
// only apply resource version locking on a single resource
if !one && len(resourceVersion) > 0 {
return kcmdutil.UsageError(cmd, "--resource-version may only be used with a single resource")
}
// Keep a copy of the original objects prior to updating their environment.
// Used in constructing the patch(es) that will be applied in the server.
oldObjects, err := resource.AsVersionedObjects(infos, clientConfig.GroupVersion.String(), kapi.Codecs.LegacyCodec(*clientConfig.GroupVersion))
if err != nil {
return err
}
if len(oldObjects) != len(infos) {
return fmt.Errorf("could not convert all objects to API version %q", clientConfig.GroupVersion)
}
oldData := make([][]byte, len(infos))
for i := range oldObjects {
old, err := json.Marshal(oldObjects[i])
if err != nil {
return err
}
oldData[i] = old
}
skipped := 0
for _, info := range infos {
ok, err := f.UpdatePodSpecForObject(info.Object, func(spec *kapi.PodSpec) error {
containers, _ := selectContainers(spec.Containers, containerMatch)
if len(containers) == 0 {
fmt.Fprintf(cmd.Out(), "warning: %s/%s does not have any containers matching %q\n", info.Mapping.Resource, info.Name, containerMatch)
return nil
}
for _, c := range containers {
c.Env = updateEnv(c.Env, env, remove)
if list {
fmt.Fprintf(out, "# %s %s, container %s\n", info.Mapping.Resource, info.Name, c.Name)
for _, env := range c.Env {
// if env.ValueFrom != nil && env.ValueFrom.FieldRef != nil {
// fmt.Fprintf(cmd.Out(), "%s= # calculated from pod %s %s\n", env.Name, env.ValueFrom.FieldRef.FieldPath, env.ValueFrom.FieldRef.APIVersion)
// continue
// }
fmt.Fprintf(out, "%s=%s\n", env.Name, env.Value)
}
}
}
return nil
})
//.........這裏部分代碼省略.........
示例3: RunEnv
//.........這裏部分代碼省略.........
one := false
infos, err := b.Do().IntoSingular(&one).Infos()
if err != nil {
return err
}
// only apply resource version locking on a single resource
if !one && len(resourceVersion) > 0 {
return kcmdutil.UsageError(cmd, "--resource-version may only be used with a single resource")
}
// Keep a copy of the original objects prior to updating their environment.
// Used in constructing the patch(es) that will be applied in the server.
gv := *clientConfig.GroupVersion
oldObjects, err := resource.AsVersionedObjects(infos, gv, kapi.Codecs.LegacyCodec(gv))
if err != nil {
return err
}
if len(oldObjects) != len(infos) {
return fmt.Errorf("could not convert all objects to API version %q", clientConfig.GroupVersion)
}
oldData := make([][]byte, len(infos))
for i := range oldObjects {
old, err := json.Marshal(oldObjects[i])
if err != nil {
return err
}
oldData[i] = old
}
skipped := 0
errored := []*resource.Info{}
for _, info := range infos {
ok, err := f.UpdatePodSpecForObject(info.Object, func(spec *kapi.PodSpec) error {
resolutionErrorsEncountered := false
containers, _ := selectContainers(spec.Containers, containerMatch)
if len(containers) == 0 {
fmt.Fprintf(errout, "warning: %s/%s does not have any containers matching %q\n", info.Mapping.Resource, info.Name, containerMatch)
return nil
}
for _, c := range containers {
if !overwrite {
if err := validateNoOverwrites(c.Env, env); err != nil {
errored = append(errored, info)
return err
}
}
c.Env = updateEnv(c.Env, env, remove)
if list {
resolveErrors := map[string][]string{}
store := newResourceStore()
fmt.Fprintf(out, "# %s %s, container %s\n", info.Mapping.Resource, info.Name, c.Name)
for _, env := range c.Env {
// Print the simple value
if env.ValueFrom == nil {
fmt.Fprintf(out, "%s=%s\n", env.Name, env.Value)
continue
}
// Print the reference version
if !resolve {
fmt.Fprintf(out, "# %s from %s\n", env.Name, getEnvVarRefString(env.ValueFrom))
continue