RunUseContext executes the functionality "devspace use namespace"
(f factory.Factory, args []string)
| 44 | |
| 45 | // RunUseContext executes the functionality "devspace use namespace" |
| 46 | func (cmd *ContextCmd) RunUseContext(f factory.Factory, args []string) error { |
| 47 | // Load kube-config |
| 48 | log := f.GetLog() |
| 49 | kubeLoader := f.NewKubeConfigLoader() |
| 50 | kubeConfig, err := kubeLoader.LoadRawConfig() |
| 51 | if err != nil { |
| 52 | return errors.Wrap(err, "load kube config") |
| 53 | } |
| 54 | |
| 55 | var context string |
| 56 | if len(args) > 0 { |
| 57 | // First arg is context name |
| 58 | context = args[0] |
| 59 | } else { |
| 60 | contexts := []string{} |
| 61 | for ctx := range kubeConfig.Contexts { |
| 62 | contexts = append(contexts, ctx) |
| 63 | } |
| 64 | |
| 65 | context, err = log.Question(&survey.QuestionOptions{ |
| 66 | Question: "Which context do you want to use?", |
| 67 | DefaultValue: kubeConfig.CurrentContext, |
| 68 | Options: contexts, |
| 69 | Sort: true, |
| 70 | }) |
| 71 | if err != nil { |
| 72 | return err |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // check if context exists |
| 77 | _, found := kubeConfig.Contexts[context] |
| 78 | if !found { |
| 79 | return fmt.Errorf("couldn't find context %s in kube config", context) |
| 80 | } |
| 81 | |
| 82 | // Save old context |
| 83 | oldContext := kubeConfig.CurrentContext |
| 84 | |
| 85 | // Set current kube-context |
| 86 | kubeConfig.CurrentContext = context |
| 87 | |
| 88 | if oldContext != context { |
| 89 | // Save updated kube-config |
| 90 | _ = kubeLoader.SaveConfig(kubeConfig) |
| 91 | |
| 92 | log.Infof("Your kube-context has been updated to '%s'", ansi.Color(kubeConfig.CurrentContext, "white+b")) |
| 93 | log.Infof("\r To revert this operation, run: %s\n", ansi.Color("devspace use context "+oldContext, "white+b")) |
| 94 | } |
| 95 | |
| 96 | // clear project kube context |
| 97 | configLoader, err := f.NewConfigLoader(cmd.ConfigPath) |
| 98 | if err != nil { |
| 99 | return err |
| 100 | } |
| 101 | err = ClearProjectKubeContext(configLoader, log) |
| 102 | if err != nil { |
| 103 | return errors.Wrap(err, "clear generated kube context") |
no test coverage detected