本文整理匯總了Golang中github.com/projectatomic/atomic-enterprise/pkg/cmd/util/clientcmd.Factory.Clients方法的典型用法代碼示例。如果您正苦於以下問題:Golang Factory.Clients方法的具體用法?Golang Factory.Clients怎麽用?Golang Factory.Clients使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/projectatomic/atomic-enterprise/pkg/cmd/util/clientcmd.Factory
的用法示例。
在下文中一共展示了Factory.Clients方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: RunWhoAmI
func RunWhoAmI(f *clientcmd.Factory, out io.Writer, cmd *cobra.Command, args []string, o *WhoAmIOptions) error {
if kcmdutil.GetFlagBool(cmd, "token") {
cfg, err := f.OpenShiftClientConfig.ClientConfig()
if err != nil {
return err
}
if len(cfg.BearerToken) == 0 {
return fmt.Errorf("no token is currently in use for this session")
}
fmt.Fprintf(out, "%s\n", cfg.BearerToken)
return nil
}
if kcmdutil.GetFlagBool(cmd, "context") {
cfg, err := f.OpenShiftClientConfig.RawConfig()
if err != nil {
return err
}
if len(cfg.CurrentContext) == 0 {
return fmt.Errorf("no context has been set")
}
fmt.Fprintf(out, "%s\n", cfg.CurrentContext)
return nil
}
client, _, err := f.Clients()
if err != nil {
return err
}
o.UserInterface = client.Users()
o.Out = out
_, err = o.WhoAmI()
return err
}
示例2: NewCmdWhoCan
// NewCmdWhoCan implements the OpenShift cli who-can command
func NewCmdWhoCan(name, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
options := &whoCanOptions{}
cmd := &cobra.Command{
Use: "who-can VERB RESOURCE",
Short: "List who can perform the specified action on a resource",
Long: "List who can perform the specified action on a resource",
Run: func(cmd *cobra.Command, args []string) {
if err := options.complete(args); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
}
var err error
if options.client, _, err = f.Clients(); err != nil {
kcmdutil.CheckErr(err)
}
if options.bindingNamespace, err = f.DefaultNamespace(); err != nil {
kcmdutil.CheckErr(err)
}
if err := options.run(); err != nil {
kcmdutil.CheckErr(err)
}
},
}
return cmd
}
示例3: setupAppConfig
func setupAppConfig(f *clientcmd.Factory, c *cobra.Command, args []string, config *newcmd.AppConfig) error {
namespace, err := f.DefaultNamespace()
if err != nil {
return err
}
dockerClient, _, err := dockerutil.NewHelper().GetClient()
if err == nil {
if err = dockerClient.Ping(); err == nil {
config.SetDockerClient(dockerClient)
}
}
if err != nil {
glog.V(2).Infof("No local Docker daemon detected: %v", err)
}
osclient, _, err := f.Clients()
if err != nil {
return err
}
config.SetOpenShiftClient(osclient, namespace)
unknown := config.AddArguments(args)
if len(unknown) != 0 {
return cmdutil.UsageError(c, "Did not recognize the following arguments: %v", unknown)
}
return nil
}
示例4: NewCmdRequestProject
func NewCmdRequestProject(name, fullName, oscLoginName, oscProjectName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
options := &NewProjectOptions{}
options.Out = out
cmd := &cobra.Command{
Use: fmt.Sprintf("%s NAME [--display-name=DISPLAYNAME] [--description=DESCRIPTION]", name),
Short: "Request a new project",
Long: fmt.Sprintf(requestProjectLong, oscLoginName, oscProjectName),
Example: fmt.Sprintf(requestProjectExample, fullName),
Run: func(cmd *cobra.Command, args []string) {
if err := options.complete(cmd, f); err != nil {
kcmdutil.CheckErr(err)
}
var err error
if options.Client, _, err = f.Clients(); err != nil {
kcmdutil.CheckErr(err)
}
if err := options.Run(); err != nil {
kcmdutil.CheckErr(err)
}
},
}
cmd.Flags().StringVar(&options.DisplayName, "display-name", "", "Project display name")
cmd.Flags().StringVar(&options.Description, "description", "", "Project description")
return cmd
}
示例5: NewCmdNewProject
// NewCmdNewProject implements the OpenShift cli new-project command
func NewCmdNewProject(name, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
options := &NewProjectOptions{}
cmd := &cobra.Command{
Use: name + " NAME [--display-name=DISPLAYNAME] [--description=DESCRIPTION]",
Short: "Create a new project",
Long: newProjectLong,
Run: func(cmd *cobra.Command, args []string) {
if err := options.complete(args); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
}
var err error
if options.Client, _, err = f.Clients(); err != nil {
kcmdutil.CheckErr(err)
}
// We can't depend on len(options.NodeSelector) > 0 as node-selector="" is valid
// and we want to populate node selector as project annotation only if explicitly set by user
useNodeSelector := cmd.Flag("node-selector").Changed
if err := options.Run(useNodeSelector); err != nil {
kcmdutil.CheckErr(err)
}
},
}
cmd.Flags().StringVar(&options.AdminRole, "admin-role", bootstrappolicy.AdminRoleName, "Project admin role name in the cluster policy")
cmd.Flags().StringVar(&options.AdminUser, "admin", "", "Project admin username")
cmd.Flags().StringVar(&options.DisplayName, "display-name", "", "Project display name")
cmd.Flags().StringVar(&options.Description, "description", "", "Project description")
cmd.Flags().StringVar(&options.NodeSelector, "node-selector", "", "Restrict pods onto nodes matching given label selector. Format: '<key1>=<value1>, <key2>=<value2>...'. Specifying \"\" means any node, not default. If unspecified, cluster default node selector will be used.")
return cmd
}
示例6: Complete
func (o *RoleModificationOptions) Complete(f *clientcmd.Factory, args []string, target *[]string, targetName string, isNamespaced bool) error {
if len(args) < 2 {
return fmt.Errorf("You must specify at least two arguments: <role> <%s> [%s]...", targetName, targetName)
}
o.RoleName = args[0]
*target = append(*target, args[1:]...)
osClient, _, err := f.Clients()
if err != nil {
return err
}
if isNamespaced {
roleBindingNamespace, err := f.DefaultNamespace()
if err != nil {
return err
}
o.RoleBindingAccessor = NewLocalRoleBindingAccessor(roleBindingNamespace, osClient)
} else {
o.RoleBindingAccessor = NewClusterRoleBindingAccessor(osClient)
}
return nil
}
示例7: Complete
func (o *ProjectOptions) Complete(f *clientcmd.Factory, args []string, out io.Writer) error {
var err error
argsLength := len(args)
switch {
case argsLength > 1:
return errors.New("Only one argument is supported (project name or context name).")
case argsLength == 1:
o.ProjectName = args[0]
}
o.Config, err = f.OpenShiftClientConfig.RawConfig()
if err != nil {
return err
}
o.ClientConfig, err = f.OpenShiftClientConfig.ClientConfig()
if err != nil {
return err
}
o.Client, _, err = f.Clients()
if err != nil {
return err
}
o.Out = out
return nil
}
示例8: Complete
func (n *NodeOptions) Complete(f *clientcmd.Factory, c *cobra.Command, args []string, out io.Writer) error {
defaultNamespace, err := f.DefaultNamespace()
if err != nil {
return err
}
_, kc, err := f.Clients()
if err != nil {
return err
}
cmdPrinter, output, err := kcmdutil.PrinterForCommand(c)
if err != nil {
return err
}
mapper, typer := f.Object()
n.DefaultNamespace = defaultNamespace
n.Kclient = kc
n.Writer = out
n.Mapper = mapper
n.Typer = typer
n.RESTClientFactory = f.Factory.RESTClient
n.Printer = f.Printer
n.NodeNames = []string{}
n.CmdPrinter = cmdPrinter
n.CmdPrinterOutput = false
if output {
n.CmdPrinterOutput = true
}
if len(args) != 0 {
n.NodeNames = append(n.NodeNames, args...)
}
return nil
}
示例9: RunImportImage
// RunImportImage contains all the necessary functionality for the OpenShift cli import-image command.
func RunImportImage(f *clientcmd.Factory, out io.Writer, cmd *cobra.Command, args []string) error {
if len(args) == 0 || len(args[0]) == 0 {
return cmdutil.UsageError(cmd, "you must specify the name of an image stream.")
}
streamName := args[0]
namespace, err := f.DefaultNamespace()
if err != nil {
return err
}
osClient, _, err := f.Clients()
if err != nil {
return err
}
imageStreamClient := osClient.ImageStreams(namespace)
stream, err := imageStreamClient.Get(streamName)
if err != nil {
return err
}
if len(stream.Spec.DockerImageRepository) == 0 {
return errors.New("only image streams with spec.dockerImageRepository set may have images imported")
}
if stream.Annotations == nil {
stream.Annotations = make(map[string]string)
}
stream.Annotations[imageapi.DockerImageRepositoryCheckAnnotation] = ""
updatedStream, err := imageStreamClient.Update(stream)
if err != nil {
return err
}
resourceVersion := updatedStream.ResourceVersion
fmt.Fprintln(cmd.Out(), "Waiting for the import to complete, CTRL+C to stop waiting.")
updatedStream, err = waitForImport(imageStreamClient, stream.Name, resourceVersion)
if err != nil {
return fmt.Errorf("unable to determine if the import completed successfully - please run 'oc describe -n %s imagestream/%s' to see if the tags were updated as expected: %v", stream.Namespace, stream.Name, err)
}
fmt.Fprintln(cmd.Out(), "The import completed successfully.\n")
d := describe.ImageStreamDescriber{osClient}
info, err := d.Describe(updatedStream.Namespace, updatedStream.Name)
if err != nil {
return err
}
fmt.Fprintln(out, info)
return nil
}
示例10: Complete
func (o *RemoveFromProjectOptions) Complete(f *clientcmd.Factory, args []string, target *[]string, targetName string) error {
if len(args) < 1 {
return fmt.Errorf("You must specify at least one argument: <%s> [%s]...", targetName, targetName)
}
*target = append(*target, args...)
var err error
if o.Client, _, err = f.Clients(); err != nil {
return err
}
if o.BindingNamespace, err = f.DefaultNamespace(); err != nil {
return err
}
return nil
}
示例11: Complete
func (o *DeployOptions) Complete(f *clientcmd.Factory, args []string, out io.Writer) error {
var err error
o.osClient, o.kubeClient, err = f.Clients()
if err != nil {
return err
}
o.namespace, err = f.DefaultNamespace()
if err != nil {
return err
}
o.out = out
if len(args) > 0 {
o.deploymentConfigName = args[0]
}
return nil
}
示例12: RunBuildLogs
// RunBuildLogs contains all the necessary functionality for the OpenShift cli build-logs command
func RunBuildLogs(f *clientcmd.Factory, out io.Writer, cmd *cobra.Command, opts api.BuildLogOptions, args []string) error {
if len(args) != 1 {
return cmdutil.UsageError(cmd, "A build name is required")
}
namespace, err := f.DefaultNamespace()
if err != nil {
return err
}
c, _, err := f.Clients()
if err != nil {
return err
}
readCloser, err := c.BuildLogs(namespace).Get(args[0], opts).Stream()
if err != nil {
return err
}
defer readCloser.Close()
_, err = io.Copy(out, readCloser)
return err
}
示例13: RunBuildChain
// RunBuildChain contains all the necessary functionality for the OpenShift
// experimental build-chain command
func RunBuildChain(f *clientcmd.Factory, cmd *cobra.Command, args []string) error {
all := cmdutil.GetFlagBool(cmd, "all")
allTags := cmdutil.GetFlagBool(cmd, "all-tags")
if len(args) > 1 ||
(len(args) == 1 && all) ||
(len(args) == 0 && allTags) ||
(all && allTags) {
return cmdutil.UsageError(cmd, "Must pass nothing, an ImageStream name:tag combination, or specify the --all flag")
}
oc, _, err := f.Clients()
if err != nil {
return err
}
// Retrieve namespace(s)
namespace := cmdutil.GetFlagString(cmd, "namespace")
if len(namespace) == 0 {
namespace, err = f.DefaultNamespace()
if err != nil {
return err
}
}
namespaces := make([]string, 0)
if all {
projectList, err := oc.Projects().List(labels.Everything(), fields.Everything())
if err != nil {
return err
}
for _, project := range projectList.Items {
glog.V(4).Infof("Found namespace %s", project.Name)
namespaces = append(namespaces, project.Name)
}
}
if len(namespaces) == 0 {
namespaces = append(namespaces, namespace)
}
// Get all build configurations
buildConfigList := make([]buildapi.BuildConfig, 0)
for _, namespace := range namespaces {
cfgList, err := oc.BuildConfigs(namespace).List(labels.Everything(), fields.Everything())
if err != nil {
return err
}
buildConfigList = append(buildConfigList, cfgList.Items...)
}
// Parse user input and validate specified image stream
streams := make(map[string][]string)
if !all && len(args) != 0 {
name, specifiedTag, err := parseTag(args[0])
if err != nil {
return err
}
// Validate the specified image stream
is, err := oc.ImageStreams(namespace).Get(name)
if err != nil {
return err
}
stream := join(namespace, name)
// Validate specified tag
tags := make([]string, 0)
exists := false
for tag := range is.Status.Tags {
tags = append(tags, tag)
if specifiedTag == tag {
exists = true
}
}
if !exists && !allTags {
// The specified tag isn't a part of our image stream
return fmt.Errorf("no tag %s exists in %s", specifiedTag, stream)
} else if !allTags {
// Use only the specified tag
tags = []string{specifiedTag}
}
// Set the specified stream as the only one to output dependencies for
streams[stream] = tags
} else {
streams = getStreams(buildConfigList)
}
if len(streams) == 0 {
return fmt.Errorf("no ImageStream available for building its dependency tree")
}
output := cmdutil.GetFlagString(cmd, "output")
for stream, tags := range streams {
for _, tag := range tags {
glog.V(4).Infof("Checking dependencies of stream %s tag %s", stream, tag)
root, err := findStreamDeps(stream, tag, buildConfigList)
if err != nil {
return err
}
//.........這裏部分代碼省略.........
示例14: RunCmdRouter
// RunCmdRouter contains all the necessary functionality for the OpenShift cli router command
func RunCmdRouter(f *clientcmd.Factory, cmd *cobra.Command, out io.Writer, cfg *RouterConfig, args []string) error {
var name string
switch len(args) {
case 0:
name = "router"
case 1:
name = args[0]
default:
return cmdutil.UsageError(cmd, "You may pass zero or one arguments to provide a name for the router")
}
if len(cfg.StatsUsername) > 0 {
if strings.Contains(cfg.StatsUsername, ":") {
return cmdutil.UsageError(cmd, "username %s must not contain ':'", cfg.StatsUsername)
}
}
ports, err := app.ContainerPortsFromString(cfg.Ports)
if err != nil {
glog.Fatal(err)
}
if cfg.StatsPort > 0 {
ports = append(ports, kapi.ContainerPort{
Name: "stats",
HostPort: cfg.StatsPort,
ContainerPort: cfg.StatsPort,
Protocol: kapi.ProtocolTCP,
})
}
label := map[string]string{"router": name}
if cfg.Labels != defaultLabel {
valid, remove, err := app.LabelsFromSpec(strings.Split(cfg.Labels, ","))
if err != nil {
glog.Fatal(err)
}
if len(remove) > 0 {
return cmdutil.UsageError(cmd, "You may not pass negative labels in %q", cfg.Labels)
}
label = valid
}
nodeSelector := map[string]string{}
if len(cfg.Selector) > 0 {
valid, remove, err := app.LabelsFromSpec(strings.Split(cfg.Selector, ","))
if err != nil {
glog.Fatal(err)
}
if len(remove) > 0 {
return cmdutil.UsageError(cmd, "You may not pass negative labels in selector %q", cfg.Selector)
}
nodeSelector = valid
}
image := cfg.ImageTemplate.ExpandOrDie(cfg.Type)
namespace, err := f.OpenShiftClientConfig.Namespace()
if err != nil {
return fmt.Errorf("error getting client: %v", err)
}
_, kClient, err := f.Clients()
if err != nil {
return fmt.Errorf("error getting client: %v", err)
}
p, output, err := cmdutil.PrinterForCommand(cmd)
if err != nil {
return fmt.Errorf("unable to configure printer: %v", err)
}
generate := output
if !generate {
_, err = kClient.Services(namespace).Get(name)
if err != nil {
if !errors.IsNotFound(err) {
return fmt.Errorf("can't check for existing router %q: %v", name, err)
}
generate = true
}
}
if generate {
if cfg.DryRun && !output {
return fmt.Errorf("router %q does not exist (no service)", name)
}
// create new router
if len(cfg.Credentials) == 0 {
return fmt.Errorf("router could not be created; you must specify a .kubeconfig file path containing credentials for connecting the router to the master with --credentials")
}
clientConfigLoadingRules := &kclientcmd.ClientConfigLoadingRules{ExplicitPath: cfg.Credentials, Precedence: []string{}}
credentials, err := clientConfigLoadingRules.Load()
if err != nil {
return fmt.Errorf("router could not be created; the provided credentials %q could not be loaded: %v", cfg.Credentials, err)
}
config, err := kclientcmd.NewDefaultClientConfig(*credentials, &kclientcmd.ConfigOverrides{}).ClientConfig()
if err != nil {
//.........這裏部分代碼省略.........
示例15: validate
// validate adds one layer of validation prior to calling the upstream
// expose command. Used only for validating services that are about
// to be exposed as routes.
func validate(cmd *cobra.Command, f *clientcmd.Factory, args []string) error {
if cmdutil.GetFlagString(cmd, "generator") != "route/v1" {
if len(cmdutil.GetFlagString(cmd, "hostname")) > 0 {
return fmt.Errorf("cannot use --hostname without generating a route")
}
return nil
}
if err := validateFlags(cmd); err != nil {
return err
}
namespace, err := f.DefaultNamespace()
if err != nil {
return err
}
_, kc, err := f.Clients()
if err != nil {
return err
}
mapper, typer := f.Object()
r := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand()).
ContinueOnError().
NamespaceParam(namespace).DefaultNamespace().
ResourceTypeOrNameArgs(false, args...).
Flatten().
Do()
err = r.Err()
if err != nil {
return err
}
mapping, err := r.ResourceMapping()
if err != nil {
return err
}
infos, err := r.Infos()
if err != nil {
return err
}
if len(infos) > 1 {
return fmt.Errorf("multiple resources provided: %v", args)
}
info := infos[0]
switch mapping.Kind {
case "Service":
svc, err := kc.Services(info.Namespace).Get(info.Name)
if err != nil {
return err
}
supportsTCP := false
for _, port := range svc.Spec.Ports {
if port.Protocol == kapi.ProtocolTCP {
supportsTCP = true
break
}
}
if !supportsTCP {
return fmt.Errorf("service %s doesn't support TCP", info.Name)
}
default:
return fmt.Errorf("cannot expose a %s as a route", mapping.Kind)
}
return nil
}