context.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // +build kube
  2. /*
  3. Copyright 2020 Docker Compose CLI authors
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package kube
  15. import (
  16. "github.com/AlecAivazis/survey/v2/terminal"
  17. "github.com/docker/compose-cli/api/context/store"
  18. "github.com/docker/compose-cli/api/errdefs"
  19. "github.com/docker/compose-cli/utils/prompt"
  20. "github.com/docker/compose-cli/kube/charts/kubernetes"
  21. )
  22. // ContextParams options for creating a Kubernetes context
  23. type ContextParams struct {
  24. ContextName string
  25. Description string
  26. KubeconfigPath string
  27. FromEnvironment bool
  28. }
  29. // CreateContextData create Docker context data
  30. func (cp ContextParams) CreateContextData() (interface{}, string, error) {
  31. if cp.FromEnvironment {
  32. // we use the current kubectl context from a $KUBECONFIG path
  33. return store.KubeContext{
  34. FromEnvironment: cp.FromEnvironment,
  35. }, cp.Description, nil
  36. }
  37. user := prompt.User{}
  38. selectContext := func() error {
  39. contexts, err := kubernetes.ListAvailableKubeConfigContexts(cp.KubeconfigPath)
  40. if err != nil {
  41. return err
  42. }
  43. selected, err := user.Select("Select kubeconfig context", contexts)
  44. if err != nil {
  45. if err == terminal.InterruptErr {
  46. return errdefs.ErrCanceled
  47. }
  48. return err
  49. }
  50. cp.ContextName = contexts[selected]
  51. return nil
  52. }
  53. if cp.KubeconfigPath != "" {
  54. err := selectContext()
  55. if err != nil {
  56. return nil, "", err
  57. }
  58. } else {
  59. // interactive
  60. var options []string
  61. var actions []func() error
  62. options = append(options, "Context from kubeconfig file")
  63. actions = append(actions, selectContext)
  64. options = append(options, "Kubernetes environment variables")
  65. actions = append(actions, func() error {
  66. cp.FromEnvironment = true
  67. return nil
  68. })
  69. selected, err := user.Select("Create a Docker context using:", options)
  70. if err != nil {
  71. if err == terminal.InterruptErr {
  72. return nil, "", errdefs.ErrCanceled
  73. }
  74. return nil, "", err
  75. }
  76. err = actions[selected]()
  77. if err != nil {
  78. return nil, "", err
  79. }
  80. }
  81. return store.KubeContext{
  82. ContextName: cp.ContextName,
  83. KubeconfigPath: cp.KubeconfigPath,
  84. FromEnvironment: cp.FromEnvironment,
  85. }, cp.Description, nil
  86. }