How to use current method of kconfig Package

Best Syzkaller code snippet using kconfig.current

km.go

Source:km.go Github

copy

Full Screen

...65// comment from k8s.io/cli-runtime66func (k *KubernetesConfigManager) ToRESTConfig() (*rest.Config, error) {67 return k.ConfigFlags().ToRESTConfig()68}69// GetStartingKubeconfig is used to adjust the current Kubernetes config. You can then70// call ModifyKubeconfig() with the modified configuration71func (k *KubernetesConfigManager) GetStartingKubeconfig() (*clientcmdapi.Config, error) {72 return k.ToRawKubeConfigLoader().ConfigAccess().GetStartingConfig()73}74// ModifyKubeconfig takes a modified configuration and seves it to disk75func (k *KubernetesConfigManager) ModifyKubeconfig(newConfig *clientcmdapi.Config) error {76 return clientcmd.ModifyConfig(k.ToRawKubeConfigLoader().ConfigAccess(), *newConfig, true)77}78// GetCurrentCluster returns configuration information about the current cluster79func (k *KubernetesConfigManager) GetCurrentCluster() (*clientcmdapi.Cluster, error) {80 kConfig, err := k.ToRawKubeConfigLoader().RawConfig()81 if err != nil {82 return nil, err83 }84 currentContext, err := k.GetKubernetesCurrentContext()85 if err != nil {86 return nil, err87 }88 clusterInfoName := kConfig.Contexts[currentContext].Cluster89 if len(clusterInfoName) == 0 {90 return nil, fmt.Errorf("Current cluster is not set in Kubeconfig")91 }92 if clusterInfo, ok := kConfig.Clusters[clusterInfoName]; ok {93 return clusterInfo, nil94 }95 return nil, fmt.Errorf("Current user information not found in Kubeconfig")96}97// GetCurrentAuthInfo returns configuration information about the current user98func (k *KubernetesConfigManager) GetCurrentAuthInfo() (*clientcmdapi.AuthInfo, error) {99 kConfig, err := k.ToRawKubeConfigLoader().RawConfig()100 if err != nil {101 return nil, err102 }103 currentContext, err := k.GetKubernetesCurrentContext()104 if err != nil {105 return nil, err106 }107 authInfoName := kConfig.Contexts[currentContext].AuthInfo108 if len(authInfoName) == 0 {109 return nil, fmt.Errorf("Current user is not set in Kubeconfig")110 }111 if authInfo, ok := kConfig.AuthInfos[authInfoName]; ok {112 return authInfo, nil113 }114 return nil, fmt.Errorf("Current user information not found in Kubeconfig")115}116// KubectlFlagsToCliArgs rebuilds the flags as cli args117func (k *KubernetesConfigManager) KubectlFlagsToCliArgs() string {118 var args string119 if len(*k.kubeCliOpts.KubeConfig) != 0 {120 args = "--kubeconfig=" + *k.kubeCliOpts.KubeConfig + " "121 }122 if len(*k.kubeCliOpts.Context) != 0 {123 args += "--context=" + *k.kubeCliOpts.Context + " "124 }125 if len(*k.kubeCliOpts.BearerToken) != 0 {126 args += "--token=" + *k.kubeCliOpts.BearerToken + " "127 }128 if len(*k.kubeCliOpts.APIServer) != 0 {129 args += "--server=" + *k.kubeCliOpts.APIServer + " "130 }131 if len(*k.kubeCliOpts.CAFile) != 0 {132 args += "--certificate-authority=" + *k.kubeCliOpts.CAFile + " "133 }134 if len(*k.kubeCliOpts.AuthInfoName) != 0 {135 args += "--user=" + *k.kubeCliOpts.AuthInfoName + " "136 }137 if len(*k.kubeCliOpts.CertFile) != 0 {138 args += "--client-certificate=" + *k.kubeCliOpts.CertFile + " "139 }140 if len(*k.kubeCliOpts.KeyFile) != 0 {141 args += "--client-key=" + *k.kubeCliOpts.KeyFile + " "142 }143 if len(*k.kubeCliOpts.Namespace) != 0 {144 args += "--namespace=" + *k.kubeCliOpts.Namespace + " "145 }146 return args147}148// SaveAuthInfoForKubeUser saves the pxc configuration in the kubeconfig file as a new user entry.149// Supply locationOfOrigin so that the Kubernetes saves the object with the appropriate user. LocationOfOrigin150// is found in each of the user objects in the kubernetes Config object.151func (k *KubernetesConfigManager) SaveAuthInfoForKubeUser(user, locationOfOrigin string, a *AuthInfo) error {152 pxcName := KubeconfigUserPrefix + user153 oldConfig, err := k.GetStartingKubeconfig()154 if err != nil {155 return err156 }157 // If one already exists it will be overwritten, if not create a new object158 if v := oldConfig.AuthInfos[pxcName]; v == nil {159 oldConfig.AuthInfos[pxcName] = clientcmdapi.NewAuthInfo()160 }161 // Store the pxc auth162 oldConfig.AuthInfos[pxcName].LocationOfOrigin = locationOfOrigin163 oldConfig.AuthInfos[pxcName].AuthProvider = &clientcmdapi.AuthProviderConfig{164 Name: "portworx",165 // Change the pxc AuthInfo to a map166 Config: a.toMap(),167 }168 // Save the information in the kubeconfig169 return k.ModifyKubeconfig(oldConfig)170}171// SaveClusterInKubeconfig stores pxc cluster configuration information in Kubeconfig172func (k *KubernetesConfigManager) SaveClusterInKubeconfig(clusterName, location string, c *Cluster) error {173 pxcName := KubeconfigUserPrefix + clusterName174 oldConfig, err := k.GetStartingKubeconfig()175 if err != nil {176 return err177 }178 if v := oldConfig.Clusters[pxcName]; v == nil {179 oldConfig.Clusters[pxcName] = clientcmdapi.NewCluster()180 }181 encodedString, err := c.toEncodedString()182 if err != nil {183 return err184 }185 oldConfig.Clusters[pxcName].LocationOfOrigin = location186 oldConfig.Clusters[pxcName].Server = "portworx-server"187 oldConfig.Clusters[pxcName].CertificateAuthorityData = []byte(encodedString)188 return k.ModifyKubeconfig(oldConfig)189}190// DeleteClusterInKubeconfig deletes the saved Portworx configuration in the kubeconfig191func (k *KubernetesConfigManager) DeleteClusterInKubeconfig(clusterName string) error {192 pxcName := KubeconfigUserPrefix + clusterName193 oldConfig, err := k.GetStartingKubeconfig()194 if err != nil {195 return err196 }197 if v := oldConfig.Clusters[pxcName]; v == nil {198 return nil199 }200 delete(oldConfig.Clusters, pxcName)201 return k.ModifyKubeconfig(oldConfig)202}203// DeleteAuthInfoInKubeconfig deletes the saved Portworx configuration in the kubeconfig204func (k *KubernetesConfigManager) DeleteAuthInfoInKubeconfig(authInfoName string) error {205 pxcName := KubeconfigUserPrefix + authInfoName206 oldConfig, err := k.GetStartingKubeconfig()207 if err != nil {208 return err209 }210 if v := oldConfig.AuthInfos[pxcName]; v == nil {211 return nil212 }213 delete(oldConfig.AuthInfos, pxcName)214 return k.ModifyKubeconfig(oldConfig)215}216// GetKubernetesCurrentContext returns the context currently selected by either the config217// file or from the command line218func (k *KubernetesConfigManager) GetKubernetesCurrentContext() (string, error) {219 var contextName string220 kConfig, err := k.ToRawKubeConfigLoader().RawConfig()221 if err != nil {222 return "", err223 }224 // Check if the was passed in the CLI flags225 if k.kubeCliOpts.Context != nil && len(*k.kubeCliOpts.Context) != 0 {226 contextName = *k.kubeCliOpts.Context227 } else {228 // Read it from the kubeconfig file229 contextName = kConfig.CurrentContext230 }231 if len(contextName) == 0 {232 return "", fmt.Errorf("Current context is not set or kubeconfig is missing")233 }234 logrus.Infof("CurrentContext = %s\n", contextName)235 // Check that it is actually on the kubeconfig file236 if _, ok := kConfig.Contexts[contextName]; !ok {237 return "", fmt.Errorf("context %q does not exist", contextName)238 }239 return contextName, nil240}241// Namespace returns the namespace resulting from the merged242// result of all overrides and a boolean indicating if it was243// overridden244func (k *KubernetesConfigManager) Namespace() (string, bool, error) {245 n, b, e := k.ToRawKubeConfigLoader().Namespace()246 logrus.Infof("Kubernetes namespace: ns=%s b=%v e=%v", n, b, e)247 return n, b, e248}249// ConfigSaveCluster saves the cluster configuration as part of an extension to the250// current context cluster in the Kubeconfig251func (k *KubernetesConfigManager) ConfigSaveCluster(clusterInfo *Cluster) error {252 cc := k.ToRawKubeConfigLoader()253 // This is the raw kubeconfig which may have been overridden by CLI args254 kconfig, err := cc.RawConfig()255 if err != nil {256 return err257 }258 // Get the current context259 currentContextName, err := k.GetKubernetesCurrentContext()260 if err != nil {261 return err262 }263 // Get the current context object264 currentContext := kconfig.Contexts[currentContextName]265 // Override the name to the name of the current cluster266 clusterInfo.Name = currentContext.Cluster267 // Get the location of the kubeconfig for this specific object. This is necessary268 // because KUBECONFIG can have many kubeconfigs, example: KUBECONFIG=kube1.conf:kube2.conf269 location := kconfig.Clusters[currentContext.Cluster].LocationOfOrigin270 // Storage the information to the appropriate kubeconfig271 if err := k.SaveClusterInKubeconfig(currentContext.Cluster, location, clusterInfo); err != nil {272 return err273 }274 logrus.Infof("Portworx server information saved in %s for Kubernetes cluster %s\n",275 location,276 currentContext.Cluster)277 return nil278}279func (k *KubernetesConfigManager) ConfigDeleteCluster(name string) error {280 cc := k.ToRawKubeConfigLoader()281 // This is the raw kubeconfig which may have been overridden by CLI args282 kconfig, err := cc.RawConfig()283 if err != nil {284 return err285 }286 // Get the current context287 currentContextName, err := k.GetKubernetesCurrentContext()288 if err != nil {289 return err290 }291 currentContext := kconfig.Contexts[currentContextName]292 // Get the location of the kubeconfig for this specific object. This is necessary293 // because KUBECONFIG can have many kubeconfigs, example: KUBECONFIG=kube1.conf:kube2.conf294 location := kconfig.Clusters[currentContext.Cluster].LocationOfOrigin295 // Storage the information to the appropriate kubeconfig296 if err := k.DeleteClusterInKubeconfig(currentContext.Cluster); err != nil {297 return err298 }299 logrus.Infof("Portworx server information removed from %s for Kubernetes cluster %s\n",300 location,301 currentContext.Cluster)302 return nil303}304func (k *KubernetesConfigManager) ConfigLoad() (*Config, error) {305 clusterConfig := newConfig()306 // Get the current context, either from the file or from the args to the CLI307 contextName, err := k.GetKubernetesCurrentContext()308 if err != nil {309 return nil, fmt.Errorf("failed to get kubectl context: %v", err)310 }311 // If the current context is not set, just create an empty object.312 // This happens when there is no kubeconfig.313 if len(contextName) == 0 {314 clusterConfig.CurrentContext = contextName315 clusterConfig.Contexts[contextName] = &Context{316 AuthInfo: "",317 Cluster: "",318 }319 return clusterConfig, nil320 }321 clientConfig := k.ConfigFlags().ToRawKubeConfigLoader()322 kConfig, err := clientConfig.RawConfig()323 if err != nil {324 return nil, fmt.Errorf("unable to read kubernetes configuration: %v", err)325 }326 // Load all contexts327 for k, v := range kConfig.Contexts {328 clusterConfig.Contexts[k] = &Context{329 Name: k,330 AuthInfo: v.AuthInfo,331 Cluster: v.Cluster,332 }333 }334 // Initialize the context335 clusterConfig.CurrentContext = contextName336 clusterConfig.Contexts[contextName] = &Context{337 AuthInfo: kConfig.Contexts[contextName].AuthInfo,338 Cluster: kConfig.Contexts[contextName].Cluster,339 }340 // Load all the pxc authentication information from the kubeconfig file341 for k, v := range kConfig.AuthInfos {342 if strings.HasPrefix(k, KubeconfigUserPrefix) && v.AuthProvider != nil {343 logrus.Debugf("Loading user %s from %s", k, v.LocationOfOrigin)344 pxcAuthInfo := NewAuthInfoFromMap(v.AuthProvider.Config)345 clusterConfig.AuthInfos[pxcAuthInfo.Name] = pxcAuthInfo346 } else if _, ok := clusterConfig.AuthInfos[k]; !ok {347 clusterConfig.AuthInfos[k] = NewAuthInfo()348 clusterConfig.AuthInfos[k].Name = k349 }350 }351 // Load all the pxc cluster information from the kubeconfig file352 for k, c := range kConfig.Clusters {353 if strings.HasPrefix(k, KubeconfigUserPrefix) {354 pxcClusterInfo, err := NewClusterFromEncodedString(string(c.CertificateAuthorityData))355 if err == nil {356 logrus.Debugf("Loading cluster %s from %s", k, c.LocationOfOrigin)357 clusterConfig.Clusters[pxcClusterInfo.Name] = pxcClusterInfo358 } else {359 logrus.Debugf("Unable to load cluster %s from %s", k, c.LocationOfOrigin)360 }361 } else if _, ok := clusterConfig.Clusters[k]; !ok {362 clusterConfig.Clusters[k] = NewDefaultCluster()363 clusterConfig.Clusters[k].Name = k364 }365 }366 return clusterConfig, nil367}368func (k *KubernetesConfigManager) ConfigSaveAuthInfo(authInfo *AuthInfo) error {369 if authInfo == nil {370 panic("authInfo required")371 }372 cc := k.ToRawKubeConfigLoader()373 save := false374 // This is the raw kubeconfig which may have been overridden by CLI args375 kconfig, err := cc.RawConfig()376 if err != nil {377 return err378 }379 // Get the current context380 currentContextName, err := k.GetKubernetesCurrentContext()381 if err != nil {382 return err383 }384 currentContext := kconfig.Contexts[currentContextName]385 // Initialize authInfo object386 authInfo.Name = currentContext.AuthInfo387 // Check for token388 if len(authInfo.Token) != 0 {389 save = true390 // TODO: Validate if the token is expired391 }392 // Check for Kubernetes secret and secret namespace393 if len(authInfo.KubernetesAuthInfo.SecretNamespace) != 0 &&394 len(authInfo.KubernetesAuthInfo.SecretName) != 0 {395 save = true396 } else if len(authInfo.KubernetesAuthInfo.SecretNamespace) == 0 && len(authInfo.KubernetesAuthInfo.SecretName) != 0 {397 return fmt.Errorf("Must supply secret namespace with secret name")398 } else if len(authInfo.KubernetesAuthInfo.SecretNamespace) != 0 && len(authInfo.KubernetesAuthInfo.SecretName) == 0 {399 return fmt.Errorf("Must supply secret name with secret namespace")400 }401 // Check if any information necessary was passed402 if !save {403 return fmt.Errorf("Must supply authentication information")404 }405 // Get the location of the kubeconfig for this specific authInfo. This is necessary406 // because KUBECONFIG can have many kubeconfigs, example: KUBECONFIG=kube1.conf:kube2.conf407 location := kconfig.AuthInfos[currentContext.AuthInfo].LocationOfOrigin408 // Storage the information to the appropriate kubeconfig409 if err := k.SaveAuthInfoForKubeUser(currentContext.AuthInfo, location, authInfo); err != nil {410 return err411 }412 logrus.Infof("Portworx login information saved in %s for Kubernetes user context %s\n",413 location,414 currentContext.AuthInfo)415 return nil416}417// ConfigSaveContext does not do anything in kubectl plugin mode because it is managed418// by kubectl419func (k *KubernetesConfigManager) ConfigSaveContext(c *Context) error {420 return fmt.Errorf("Use <kubectl config set-context> to set the context instead")421}422// ConfigDeleteAuthInfo deletes auth information for the current context423func (k *KubernetesConfigManager) ConfigDeleteAuthInfo(name string) error {424 cc := k.ToRawKubeConfigLoader()425 // This is the raw kubeconfig which may have been overridden by CLI args426 kconfig, err := cc.RawConfig()427 if err != nil {428 return err429 }430 // Get the current context431 currentContextName, err := k.GetKubernetesCurrentContext()432 if err != nil {433 return err434 }435 currentContext := kconfig.Contexts[currentContextName]436 // Get the location of the kubeconfig for this specific object. This is necessary437 // because KUBECONFIG can have many kubeconfigs, example: KUBECONFIG=kube1.conf:kube2.conf438 location := kconfig.AuthInfos[currentContext.AuthInfo].LocationOfOrigin439 // Storage the information to the appropriate kubeconfig440 if err := k.DeleteAuthInfoInKubeconfig(currentContext.AuthInfo); err != nil {441 return err442 }443 logrus.Infof("Portworx server information removed from %s for Kubernetes cluster %s\n",444 location,445 currentContext.Cluster)446 return nil447}448// ConfigDeleteContext deletes auth information for the current context449func (k *KubernetesConfigManager) ConfigDeleteContext(name string) error {450 return fmt.Errorf("Use kubectl config to manage context")451}452// ConfigUseContext is not supported by kubectl plugin453func (k *KubernetesConfigManager) ConfigUseContext(name string) error {454 return fmt.Errorf("Use kubectl to set the current context")455}456// ConfigGetCurrentContext returns the current context set by kubectl457func (k *KubernetesConfigManager) ConfigGetCurrentContext() (string, error) {458 return k.GetKubernetesCurrentContext()459}...

Full Screen

Full Screen

kconfig.go

Source:kconfig.go Github

copy

Full Screen

...223 kp.parseConfigType(cmd)224 }225}226func (kp *kconfigParser) parseConfigType(typ string) {227 cur := kp.current()228 switch typ {229 case "tristate":230 cur.Type = TypeTristate231 kp.tryParsePrompt()232 case "def_tristate":233 cur.Type = TypeTristate234 kp.parseDefaultValue()235 case "bool":236 cur.Type = TypeBool237 kp.tryParsePrompt()238 case "def_bool":239 cur.Type = TypeBool240 kp.parseDefaultValue()241 case "int":242 cur.Type = TypeInt243 kp.tryParsePrompt()244 case "def_int":245 cur.Type = TypeInt246 kp.parseDefaultValue()247 case "hex":248 cur.Type = TypeHex249 kp.tryParsePrompt()250 case "def_hex":251 cur.Type = TypeHex252 kp.parseDefaultValue()253 case "string":254 cur.Type = TypeString255 kp.tryParsePrompt()256 case "def_string":257 cur.Type = TypeString258 kp.parseDefaultValue()259 default:260 kp.parseProperty(typ)261 }262}263func (kp *kconfigParser) parseProperty(prop string) {264 cur := kp.current()265 switch prop {266 case "prompt":267 kp.tryParsePrompt()268 case "depends":269 kp.MustConsume("on")270 cur.dependsOn = exprAnd(cur.dependsOn, kp.parseExpr())271 case "visible":272 kp.MustConsume("if")273 cur.visibleIf = exprAnd(cur.visibleIf, kp.parseExpr())274 case "select", "imply":275 _ = kp.Ident()276 if kp.TryConsume("if") {277 _ = kp.parseExpr()278 }279 case "option":280 // It can be 'option foo', or 'option bar="BAZ"'.281 kp.ConsumeLine()282 case "modules":283 case "optional":284 case "default":285 kp.parseDefaultValue()286 case "range":287 _, _ = kp.parseExpr(), kp.parseExpr() // from, to288 if kp.TryConsume("if") {289 _ = kp.parseExpr()290 }291 case "help", "---help---":292 // Help rules are tricky: end of help is identified by smaller indentation level293 // as would be rendered on a terminal with 8-column tabs setup, minus empty lines.294 for kp.nextLine() {295 if kp.eol() {296 continue297 }298 kp.helpIdent = kp.identLevel()299 kp.ConsumeLine()300 break301 }302 default:303 kp.failf("unknown line")304 }305}306func (kp *kconfigParser) includeSource(file string) {307 kp.newCurrent(nil)308 file = kp.expandString(file)309 file = filepath.Join(kp.baseDir, file)310 data, err := ioutil.ReadFile(file)311 if err != nil {312 kp.failf("%v", err)313 return314 }315 kp.includes = append(kp.includes, kp.parser)316 kp.parser = newParser(data, file)317 kp.parseFile()318 err = kp.err319 kp.parser = kp.includes[len(kp.includes)-1]320 kp.includes = kp.includes[:len(kp.includes)-1]321 if kp.err == nil {322 kp.err = err323 }324}325func (kp *kconfigParser) pushCurrent(m *Menu) {326 kp.endCurrent()327 kp.cur = m328 kp.stack = append(kp.stack, m)329}330func (kp *kconfigParser) popCurrent() {331 kp.endCurrent()332 if len(kp.stack) < 2 {333 kp.failf("unbalanced endmenu")334 return335 }336 last := kp.stack[len(kp.stack)-1]337 kp.stack = kp.stack[:len(kp.stack)-1]338 top := kp.stack[len(kp.stack)-1]339 last.Parent = top340 top.Elems = append(top.Elems, last)341}342func (kp *kconfigParser) newCurrent(m *Menu) {343 kp.endCurrent()344 kp.cur = m345}346func (kp *kconfigParser) current() *Menu {347 if kp.cur == nil {348 kp.failf("config property outside of config")349 return &Menu{}350 }351 return kp.cur352}353func (kp *kconfigParser) endCurrent() {354 if kp.cur == nil {355 return356 }357 if len(kp.stack) == 0 {358 kp.failf("unbalanced endmenu")359 return360 }361 top := kp.stack[len(kp.stack)-1]362 if top != kp.cur {363 kp.cur.Parent = top364 top.Elems = append(top.Elems, kp.cur)365 }366 kp.cur = nil367}368func (kp *kconfigParser) tryParsePrompt() {369 if str, ok := kp.TryQuotedString(); ok {370 prompt := prompt{371 text: str,372 }373 if kp.TryConsume("if") {374 prompt.cond = kp.parseExpr()375 }376 kp.current().prompts = append(kp.current().prompts, prompt)377 }378}379func (kp *kconfigParser) parseDefaultValue() {380 def := defaultVal{val: kp.parseExpr()}381 if kp.TryConsume("if") {382 def.cond = kp.parseExpr()383 }384 kp.current().defaults = append(kp.current().defaults, def)385}386func (kp *kconfigParser) expandString(str string) string {387 str = strings.Replace(str, "$(SRCARCH)", kp.target.KernelHeaderArch, -1)388 str = strings.Replace(str, "$SRCARCH", kp.target.KernelHeaderArch, -1)389 return str390}...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful