context.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. "fmt"
  17. "github.com/AlecAivazis/survey/v2/terminal"
  18. "github.com/docker/compose-cli/api/context/store"
  19. "github.com/docker/compose-cli/kube/resources"
  20. "github.com/docker/compose-cli/pkg/api"
  21. "github.com/docker/compose-cli/pkg/prompt"
  22. )
  23. // ContextParams options for creating a Kubernetes context
  24. type ContextParams struct {
  25. KubeContextName string
  26. Description string
  27. KubeConfigPath string
  28. FromEnvironment bool
  29. }
  30. // CreateContextData create Docker context data
  31. func (cp ContextParams) CreateContextData() (interface{}, string, error) {
  32. if cp.FromEnvironment {
  33. // we use the current kubectl context from a $KUBECONFIG path
  34. return store.KubeContext{
  35. FromEnvironment: cp.FromEnvironment,
  36. }, cp.getDescription(), nil
  37. }
  38. user := prompt.User{}
  39. selectContext := func() error {
  40. contexts, err := resources.ListAvailableKubeConfigContexts(cp.KubeConfigPath)
  41. if err != nil {
  42. return err
  43. }
  44. selected, err := user.Select("Select kubeconfig context", contexts)
  45. if err != nil {
  46. if err == terminal.InterruptErr {
  47. return api.ErrCanceled
  48. }
  49. return err
  50. }
  51. cp.KubeContextName = contexts[selected]
  52. return nil
  53. }
  54. if cp.KubeConfigPath != "" {
  55. if cp.KubeContextName != "" {
  56. return store.KubeContext{
  57. ContextName: cp.KubeContextName,
  58. KubeconfigPath: cp.KubeConfigPath,
  59. FromEnvironment: cp.FromEnvironment,
  60. }, cp.getDescription(), nil
  61. }
  62. err := selectContext()
  63. if err != nil {
  64. return nil, "", err
  65. }
  66. } else {
  67. // interactive
  68. var options []string
  69. var actions []func() error
  70. options = append(options, "Context from kubeconfig file")
  71. actions = append(actions, selectContext)
  72. options = append(options, "Kubernetes environment variables")
  73. actions = append(actions, func() error {
  74. cp.FromEnvironment = true
  75. return nil
  76. })
  77. selected, err := user.Select("Create a Docker context using:", options)
  78. if err != nil {
  79. if err == terminal.InterruptErr {
  80. return nil, "", api.ErrCanceled
  81. }
  82. return nil, "", err
  83. }
  84. err = actions[selected]()
  85. if err != nil {
  86. return nil, "", err
  87. }
  88. }
  89. return store.KubeContext{
  90. ContextName: cp.KubeContextName,
  91. KubeconfigPath: cp.KubeConfigPath,
  92. FromEnvironment: cp.FromEnvironment,
  93. }, cp.getDescription(), nil
  94. }
  95. func (cp ContextParams) getDescription() string {
  96. if cp.Description != "" {
  97. return cp.Description
  98. }
  99. if cp.FromEnvironment {
  100. return "From environment variables"
  101. }
  102. configFile := "default kube config"
  103. if cp.KubeConfigPath != "" {
  104. configFile = cp.KubeConfigPath
  105. }
  106. return fmt.Sprintf("%s (in %s)", cp.KubeContextName, configFile)
  107. }