context.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 resources
  15. import (
  16. "fmt"
  17. "os"
  18. "github.com/docker/compose-cli/api/context/store"
  19. "k8s.io/cli-runtime/pkg/genericclioptions"
  20. "k8s.io/client-go/tools/clientcmd"
  21. "k8s.io/client-go/tools/clientcmd/api"
  22. )
  23. // ListAvailableKubeConfigContexts list kube contexts
  24. func ListAvailableKubeConfigContexts(kubeconfig string) ([]string, error) {
  25. config, err := getKubeConfig(kubeconfig)
  26. if err != nil {
  27. return nil, err
  28. }
  29. contexts := []string{}
  30. for k := range config.Contexts {
  31. contexts = append(contexts, k)
  32. }
  33. return contexts, nil
  34. }
  35. // LoadConfig returns kubeconfig data referenced in the docker context
  36. func LoadConfig(ctx store.KubeContext) (*genericclioptions.ConfigFlags, error) {
  37. if ctx.FromEnvironment {
  38. return nil, nil
  39. }
  40. config, err := getKubeConfig(ctx.KubeconfigPath)
  41. if err != nil {
  42. return nil, err
  43. }
  44. contextName := ctx.ContextName
  45. if contextName == "" {
  46. contextName = config.CurrentContext
  47. }
  48. context, ok := config.Contexts[contextName]
  49. if !ok {
  50. return nil, fmt.Errorf("context name %s not found in kubeconfig", contextName)
  51. }
  52. cluster, ok := config.Clusters[context.Cluster]
  53. if !ok {
  54. return nil, fmt.Errorf("cluster %s not found for context %s", context.Cluster, contextName)
  55. }
  56. // bind to kubernetes config flags
  57. return &genericclioptions.ConfigFlags{
  58. Context: &ctx.ContextName,
  59. KubeConfig: &ctx.KubeconfigPath,
  60. Namespace: &context.Namespace,
  61. ClusterName: &context.Cluster,
  62. APIServer: &cluster.Server,
  63. CAFile: &cluster.CertificateAuthority,
  64. }, nil
  65. }
  66. func getKubeConfig(kubeconfig string) (*api.Config, error) {
  67. config, err := clientcmd.NewDefaultPathOptions().GetStartingConfig()
  68. if err != nil {
  69. return nil, err
  70. }
  71. if kubeconfig != "" {
  72. f, err := os.Stat(kubeconfig)
  73. if os.IsNotExist(err) {
  74. return nil, err
  75. }
  76. if f.IsDir() {
  77. return nil, fmt.Errorf("%s not a config file", kubeconfig)
  78. }
  79. config = clientcmd.GetConfigFromFileOrDie(kubeconfig)
  80. }
  81. return config, nil
  82. }