本文整理汇总了Golang中k8s/io/kubernetes/pkg/kubectl/cmd/config.ModifyConfig函数的典型用法代码示例。如果您正苦于以下问题:Golang ModifyConfig函数的具体用法?Golang ModifyConfig怎么用?Golang ModifyConfig使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ModifyConfig函数的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: RunLogout
func (o LogoutOptions) RunLogout() error {
token := o.Config.BearerToken
client, err := client.New(o.Config)
if err != nil {
return err
}
userInfo, err := whoAmI(client)
if err != nil {
return err
}
if err := client.OAuthAccessTokens().Delete(token); err != nil {
return err
}
newConfig := *o.StartingKubeConfig
for key, value := range newConfig.AuthInfos {
if value.Token == token {
value.Token = ""
newConfig.AuthInfos[key] = value
// don't break, its possible that more than one user stanza has the same token.
}
}
if err := kcmdconfig.ModifyConfig(o.PathOptions, newConfig, true); err != nil {
return err
}
fmt.Fprintf(o.Out, "Logged %q out on %q\n", userInfo.Name, o.Config.Host)
return nil
}
示例2: SetupContext
// SetupContext creates a new cluster and context in ~/.kube using the provided API Server. If setCurrent is true, it is made the current context.
func SetupContext(clusterName, contextName, kubeAPIServer string, setCurrent bool) error {
pathOpts := kubectlcfg.NewDefaultPathOptions()
config, err := getConfig(pathOpts)
if err != nil {
return err
}
cluster, exists := config.Clusters[clusterName]
if !exists {
cluster = kubecfg.NewCluster()
}
// configure cluster
cluster.Server = kubeAPIServer
cluster.InsecureSkipTLSVerify = true
config.Clusters[clusterName] = cluster
context, exists := config.Contexts[contextName]
if !exists {
context = kubecfg.NewContext()
}
// configure context
context.Cluster = clusterName
config.Contexts[contextName] = context
// set as current if requested
if setCurrent {
config.CurrentContext = contextName
}
return kubectlcfg.ModifyConfig(pathOpts, *config, true)
}
示例3: SetCurrentContext
// SetCurrentContext changes the kubectl context to the given value
func SetCurrentContext(context string) error {
pathOpts := kubectlcfg.NewDefaultPathOptions()
config, err := getConfig(pathOpts)
if err != nil {
return err
}
config.CurrentContext = context
return kubectlcfg.ModifyConfig(pathOpts, *config, true)
}
示例4: SaveConfig
// Save all the information present in this helper to a config file. An explicit config
// file path can be provided, if not use the established conventions about config
// loading rules. Will create a new config file if one can't be found at all. Will only
// succeed if all required info is present.
func (o *LoginOptions) SaveConfig() (bool, error) {
if len(o.Username) == 0 {
return false, fmt.Errorf("Insufficient data to merge configuration.")
}
globalExistedBefore := true
if _, err := os.Stat(o.PathOptions.GlobalFile); os.IsNotExist(err) {
globalExistedBefore = false
}
newConfig, err := config.CreateConfig(o.Project, o.Config)
if err != nil {
return false, err
}
cwd, err := os.Getwd()
if err != nil {
return false, err
}
baseDir, err := cmdutil.MakeAbs(filepath.Dir(o.PathOptions.GetDefaultFilename()), cwd)
if err != nil {
return false, err
}
if err := config.RelativizeClientConfigPaths(newConfig, baseDir); err != nil {
return false, err
}
configToWrite, err := config.MergeConfig(*o.StartingKubeConfig, *newConfig)
if err != nil {
return false, err
}
if err := kubecmdconfig.ModifyConfig(o.PathOptions, *configToWrite, true); err != nil {
return false, err
}
created := false
if _, err := os.Stat(o.PathOptions.GlobalFile); err == nil {
created = created || !globalExistedBefore
}
return created, nil
}
示例5: RunProject
//.........这里部分代码省略.........
fmt.Fprintf(out, "Using project %q on server %q.\n", currentProject, clientCfg.Host)
}
} else {
if o.DisplayShort {
return fmt.Errorf("no project has been set")
}
fmt.Fprintf(out, "No project has been set. Pass a project name to make that the default.\n")
}
return nil
}
// We have an argument that can be either a context or project
argument := o.ProjectName
contextInUse := ""
namespaceInUse := ""
contextNameIsGenerated := false
// Check if argument is an existing context, if so just set it as the context in use.
// If not a context then we will try to handle it as a project.
if context, contextExists := config.Contexts[argument]; !o.ProjectOnly && contextExists {
contextInUse = argument
namespaceInUse = context.Namespace
config.CurrentContext = argument
} else {
if !o.SkipAccessValidation {
_, err := o.Client.Projects().Get(argument)
if err != nil {
if isNotFound, isForbidden := kapierrors.IsNotFound(err), clientcmd.IsForbidden(err); isNotFound || isForbidden {
var msg string
if isForbidden {
msg = fmt.Sprintf("You are not a member of project %q.", argument)
} else {
msg = fmt.Sprintf("A project named %q does not exist on %q.", argument, clientCfg.Host)
}
projects, err := getProjects(o.Client)
if err == nil {
switch len(projects) {
case 0:
msg += "\nYou are not a member of any projects. You can request a project to be created with the 'new-project' command."
case 1:
msg += fmt.Sprintf("\nYou have one project on this server: %s", api.DisplayNameAndNameForProject(&projects[0]))
default:
msg += "\nYour projects are:"
for _, project := range projects {
msg += fmt.Sprintf("\n* %s", api.DisplayNameAndNameForProject(&project))
}
}
}
if hasMultipleServers(config) {
msg += "\nTo see projects on another server, pass '--server=<server>'."
}
return errors.New(msg)
}
return err
}
}
projectName := argument
kubeconfig, err := cliconfig.CreateConfig(projectName, o.ClientConfig)
if err != nil {
return err
}
merged, err := cliconfig.MergeConfig(config, *kubeconfig)
if err != nil {
return err
}
config = *merged
namespaceInUse = projectName
contextInUse = merged.CurrentContext
contextNameIsGenerated = true
}
if err := kubecmdconfig.ModifyConfig(o.PathOptions, config, true); err != nil {
return err
}
if o.DisplayShort {
fmt.Fprintln(out, namespaceInUse)
return nil
}
if contextInUse != namespaceInUse && !contextNameIsGenerated {
if len(namespaceInUse) > 0 {
fmt.Fprintf(out, "Now using project %q from context named %q on server %q.\n", namespaceInUse, contextInUse, clientCfg.Host)
} else {
fmt.Fprintf(out, "Now using context named %q on server %q.\n", contextInUse, clientCfg.Host)
}
} else {
fmt.Fprintf(out, "Now using project %q on server %q.\n", namespaceInUse, clientCfg.Host)
}
return nil
}
示例6: RunProject
//.........这里部分代码省略.........
return nil
}
// We have an argument that can be either a context or project
argument := o.ProjectName
contextInUse := ""
namespaceInUse := ""
// Check if argument is an existing context, if so just set it as the context in use.
// If not a context then we will try to handle it as a project.
if context, contextExists := config.Contexts[argument]; !o.ProjectOnly && contextExists {
contextInUse = argument
namespaceInUse = context.Namespace
config.CurrentContext = argument
} else {
if !o.SkipAccessValidation {
_, err := o.Client.Projects().Get(argument)
if err != nil {
if isNotFound, isForbidden := kapierrors.IsNotFound(err), clientcmd.IsForbidden(err); isNotFound || isForbidden {
var msg string
if isForbidden {
msg = fmt.Sprintf("You are not a member of project %q.", argument)
} else {
msg = fmt.Sprintf("A project named %q does not exist on %q.", argument, clientCfg.Host)
}
projects, err := getProjects(o.Client)
if err == nil {
switch len(projects) {
case 0:
msg += "\nYou are not a member of any projects. You can request a project to be created with the 'new-project' command."
case 1:
msg += fmt.Sprintf("\nYou have one project on this server: %s", api.DisplayNameAndNameForProject(&projects[0]))
default:
msg += "\nYour projects are:"
for _, project := range projects {
msg += fmt.Sprintf("\n* %s", api.DisplayNameAndNameForProject(&project))
}
}
}
if hasMultipleServers(config) {
msg += "\nTo see projects on another server, pass '--server=<server>'."
}
return errors.New(msg)
}
return err
}
}
projectName := argument
kubeconfig, err := cliconfig.CreateConfig(projectName, o.ClientConfig)
if err != nil {
return err
}
merged, err := cliconfig.MergeConfig(config, *kubeconfig)
if err != nil {
return err
}
config = *merged
namespaceInUse = projectName
contextInUse = merged.CurrentContext
}
if err := kubecmdconfig.ModifyConfig(o.PathOptions, config, true); err != nil {
return err
}
if o.DisplayShort {
fmt.Fprintln(out, namespaceInUse)
return nil
}
// calculate what name we'd generate for the context. If the context has the same name, don't drop it into the output, because the user won't
// recognize the name since they didn't choose it.
defaultContextName := cliconfig.GetContextNickname(namespaceInUse, config.Contexts[contextInUse].Cluster, config.Contexts[contextInUse].AuthInfo)
switch {
// if there is no namespace, then the only information we can provide is the context and server
case (len(namespaceInUse) == 0):
fmt.Fprintf(out, "Now using context named %q on server %q.\n", contextInUse, clientCfg.Host)
// if they specified a project name and got a generated context, then only show the information they care about. They won't recognize
// a context name they didn't choose
case (argument == namespaceInUse) && (contextInUse == defaultContextName):
fmt.Fprintf(out, "Now using project %q on server %q.\n", namespaceInUse, clientCfg.Host)
// in all other cases, display all information
default:
fmt.Fprintf(out, "Now using project %q from context named %q on server %q.\n", namespaceInUse, contextInUse, clientCfg.Host)
}
return nil
}