1
0

create_kube.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 context
  15. import (
  16. "github.com/docker/compose-cli/pkg/api"
  17. "github.com/pkg/errors"
  18. "github.com/spf13/cobra"
  19. "github.com/docker/compose-cli/api/context/store"
  20. "github.com/docker/compose-cli/kube"
  21. )
  22. func init() {
  23. extraCommands = append(extraCommands, createKubeCommand)
  24. extraHelp = append(extraHelp, `
  25. Create a Kubernetes context:
  26. $ docker context create kubernetes CONTEXT [flags]
  27. (see docker context create kubernetes --help)
  28. `)
  29. }
  30. func createKubeCommand() *cobra.Command {
  31. var opts kube.ContextParams
  32. cmd := &cobra.Command{
  33. Use: "kubernetes CONTEXT [flags]",
  34. Short: "Create context for a Kubernetes Cluster",
  35. Args: cobra.ExactArgs(1),
  36. RunE: func(cmd *cobra.Command, args []string) error {
  37. return runCreateKube(args[0], opts)
  38. },
  39. }
  40. addDescriptionFlag(cmd, &opts.Description)
  41. cmd.Flags().StringVar(&opts.KubeConfigPath, "kubeconfig", "", "The endpoint of the Kubernetes manager")
  42. cmd.Flags().StringVar(&opts.KubeContextName, "kubecontext", "", "The name of the context to use in kubeconfig")
  43. cmd.Flags().BoolVar(&opts.FromEnvironment, "from-env", false, "Get endpoint and creds from env vars")
  44. return cmd
  45. }
  46. func runCreateKube(contextName string, opts kube.ContextParams) error {
  47. if contextExists(contextName) {
  48. return errors.Wrapf(api.ErrAlreadyExists, "context %q", contextName)
  49. }
  50. contextData, description, err := opts.CreateContextData()
  51. if err != nil {
  52. return err
  53. }
  54. return createDockerContext(contextName, store.KubeContextType, description, contextData)
  55. }