RunUseNamespace executes the functionality "devspace use namespace"
(f factory.Factory, cobraCmd *cobra.Command, args []string)
| 52 | |
| 53 | // RunUseNamespace executes the functionality "devspace use namespace" |
| 54 | func (cmd *namespaceCmd) RunUseNamespace(f factory.Factory, cobraCmd *cobra.Command, args []string) error { |
| 55 | // Get default context |
| 56 | log := f.GetLog() |
| 57 | client, err := f.NewKubeDefaultClient() |
| 58 | if err != nil { |
| 59 | return err |
| 60 | } |
| 61 | |
| 62 | // Check if current kube context belongs to a space |
| 63 | kubeLoader := client.KubeConfigLoader() |
| 64 | |
| 65 | // Load kube-config |
| 66 | kubeConfig, err := kubeLoader.LoadRawConfig() |
| 67 | if err != nil { |
| 68 | return errors.Errorf("Unable to load kube-config: %v", err) |
| 69 | } |
| 70 | |
| 71 | if kubeConfig.Contexts[client.CurrentContext()] == nil { |
| 72 | return errors.Errorf("Couldn't find kube context '%s' in kube config", client.CurrentContext()) |
| 73 | } |
| 74 | |
| 75 | // Remember current default namespace |
| 76 | oldDefaultNamespace := kubeConfig.Contexts[client.CurrentContext()].Namespace |
| 77 | |
| 78 | namespace := "" |
| 79 | if len(args) > 0 { |
| 80 | namespace = args[0] |
| 81 | if cmd.Create { |
| 82 | ns := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}} |
| 83 | _, err := client.KubeClient().CoreV1().Namespaces().Create(context.TODO(), ns, metav1.CreateOptions{}) |
| 84 | if err != nil && !kerrors.IsAlreadyExists(err) { |
| 85 | return errors.Errorf("Unable to create namespace: %v", err) |
| 86 | } |
| 87 | } |
| 88 | } else if !cmd.Reset { |
| 89 | namespaceList, err := client.KubeClient().CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{}) |
| 90 | if err != nil { |
| 91 | return errors.Errorf("Unable to list namespaces: %v", err) |
| 92 | } |
| 93 | |
| 94 | namespaces := []string{} |
| 95 | for _, ns := range namespaceList.Items { |
| 96 | namespaces = append(namespaces, ns.Name) |
| 97 | } |
| 98 | |
| 99 | namespace, err = log.Question(&survey.QuestionOptions{ |
| 100 | Question: "Which namespace do you want to use?", |
| 101 | Options: namespaces, |
| 102 | Sort: true, |
| 103 | }) |
| 104 | if err != nil { |
| 105 | return err |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if oldDefaultNamespace != namespace { |
| 110 | // Set namespace as default for used kube-context |
| 111 | kubeConfig.Contexts[client.CurrentContext()].Namespace = namespace |
no test coverage detected