storedefault.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. Copyright 2020 Docker, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package store
  14. import (
  15. "bytes"
  16. "encoding/json"
  17. "os/exec"
  18. "github.com/pkg/errors"
  19. )
  20. // Represents a context as created by the docker cli
  21. type defaultContext struct {
  22. Metadata ContextMetadata
  23. Endpoints endpoints
  24. }
  25. // Normally (in docker/cli code), the endpoints are mapped as map[string]interface{}
  26. // but docker cli contexts always have a "docker" and "kubernetes" key so we
  27. // create real types for those to no have to juggle around with interfaces.
  28. type endpoints struct {
  29. Docker endpoint `json:"docker,omitempty"`
  30. Kubernetes endpoint `json:"kubernetes,omitempty"`
  31. }
  32. // Both "docker" and "kubernetes" endpoints in the docker cli created contexts
  33. // have a "Host", only kubernetes has the "DefaultNamespace", we put both of
  34. // those here for easier manipulation and to not have to create two distinct
  35. // structs
  36. type endpoint struct {
  37. Host string
  38. DefaultNamespace string
  39. }
  40. func dockerDefaultContext() (*DockerContext, error) {
  41. // ensure we run this using default context, in current context has been damaged / removed in store
  42. cmd := exec.Command("com.docker.cli", "--context", "default", "context", "inspect", "default")
  43. var stdout bytes.Buffer
  44. cmd.Stdout = &stdout
  45. err := cmd.Run()
  46. if err != nil {
  47. return nil, err
  48. }
  49. var ctx []defaultContext
  50. err = json.Unmarshal(stdout.Bytes(), &ctx)
  51. if err != nil {
  52. return nil, err
  53. }
  54. if len(ctx) != 1 {
  55. return nil, errors.New("found more than one default context")
  56. }
  57. defaultCtx := ctx[0]
  58. meta := DockerContext{
  59. Name: "default",
  60. Endpoints: map[string]interface{}{
  61. "docker": &Endpoint{
  62. Host: defaultCtx.Endpoints.Docker.Host,
  63. },
  64. "kubernetes": &Endpoint{
  65. Host: defaultCtx.Endpoints.Kubernetes.Host,
  66. DefaultNamespace: defaultCtx.Endpoints.Kubernetes.DefaultNamespace,
  67. },
  68. },
  69. Metadata: ContextMetadata{
  70. Type: DefaultContextType,
  71. Description: "Current DOCKER_HOST based configuration",
  72. StackOrchestrator: defaultCtx.Metadata.StackOrchestrator,
  73. },
  74. }
  75. return &meta, nil
  76. }