Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 8497070

Browse files
aiordachegtardif
authored andcommitted
Implement kubernetes context create
Signed-off-by: aiordache <[email protected]>
1 parent d53d437 commit 8497070

File tree

6 files changed

+139
-22
lines changed

6 files changed

+139
-22
lines changed

api/context/store/contextmetadata.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ type EcsContext struct {
5757

5858
// KubeContext is the context for a kube backend
5959
type KubeContext struct {
60-
Endpoint string `json:",omitempty"`
60+
ContextName string `json:",omitempty"`
61+
KubeconfigPath string `json:",omitempty"`
6162
FromEnvironment bool
6263
}
6364

api/context/store/store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const (
5757
LocalContextType = "local"
5858
// KubeContextType is the endpoint key in the context endpoints for a new
5959
// kube backend
60-
KubeContextType = "kubernetes"
60+
KubeContextType = "k8s"
6161
)
6262

6363
const (

cli/cmd/context/create_kube.go

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,25 @@ func init() {
3333
extraCommands = append(extraCommands, createKubeCommand)
3434
extraHelp = append(extraHelp, `
3535
Create a Kubernetes context:
36-
$ docker context create kubernetes CONTEXT [flags]
37-
(see docker context create kubernetes --help)
36+
$ docker context create k8s CONTEXT [flags]
37+
(see docker context create k8s --help)
3838
`)
3939
}
4040

4141
func createKubeCommand() *cobra.Command {
4242
var opts kube.ContextParams
4343
cmd := &cobra.Command{
44-
Use: "kubernetes CONTEXT [flags]",
44+
Use: "k8s CONTEXT [flags]",
4545
Short: "Create context for a Kubernetes Cluster",
4646
Args: cobra.ExactArgs(1),
4747
RunE: func(cmd *cobra.Command, args []string) error {
48-
opts.Name = args[0]
49-
50-
if opts.Endpoint != "" {
51-
opts.FromEnvironment = false
52-
}
5348
return runCreateKube(cmd.Context(), args[0], opts)
5449
},
5550
}
5651

5752
addDescriptionFlag(cmd, &opts.Description)
58-
cmd.Flags().StringVar(&opts.Endpoint, "endpoint", "", "The endpoint of the Kubernetes manager")
59-
cmd.Flags().BoolVar(&opts.FromEnvironment, "from-env", true, "Get endpoint and creds from env vars")
53+
cmd.Flags().StringVar(&opts.KubeconfigPath, "kubeconfig", "", "The endpoint of the Kubernetes manager")
54+
cmd.Flags().BoolVar(&opts.FromEnvironment, "from-env", false, "Get endpoint and creds from env vars")
6055
return cmd
6156
}
6257

@@ -65,13 +60,9 @@ func runCreateKube(ctx context.Context, contextName string, opts kube.ContextPar
6560
return errors.Wrapf(errdefs.ErrAlreadyExists, "context %q", contextName)
6661
}
6762

68-
contextData, description := createContextData(opts)
63+
contextData, description, err := opts.CreateContextData()
64+
if err != nil {
65+
return err
66+
}
6967
return createDockerContext(ctx, contextName, store.KubeContextType, description, contextData)
7068
}
71-
72-
func createContextData(opts kube.ContextParams) (interface{}, string) {
73-
return store.KubeContext{
74-
Endpoint: opts.Endpoint,
75-
FromEnvironment: opts.FromEnvironment,
76-
}, opts.Description
77-
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ require (
6464
helm.sh/helm/v3 v3.5.0
6565
k8s.io/api v0.20.1
6666
k8s.io/apimachinery v0.20.1
67+
k8s.io/client-go v0.20.1
6768
sigs.k8s.io/kustomize/kyaml v0.10.5
6869
)
6970

kube/charts/kubernetes/context.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// +build kube
2+
3+
/*
4+
Copyright 2020 Docker Compose CLI authors
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package kubernetes
20+
21+
import (
22+
"fmt"
23+
"os"
24+
25+
"k8s.io/client-go/tools/clientcmd"
26+
)
27+
28+
func ListAvailableKubeConfigContexts(kubeconfig string) ([]string, error) {
29+
config, err := clientcmd.NewDefaultPathOptions().GetStartingConfig()
30+
if err != nil {
31+
return nil, err
32+
}
33+
if kubeconfig != "" {
34+
f, err := os.Stat(kubeconfig)
35+
if os.IsNotExist(err) {
36+
return nil, err
37+
}
38+
if f.IsDir() {
39+
return nil, fmt.Errorf("%s not a config file", kubeconfig)
40+
}
41+
42+
config = clientcmd.GetConfigFromFileOrDie(kubeconfig)
43+
}
44+
45+
contexts := []string{}
46+
for k := range config.Contexts {
47+
contexts = append(contexts, k)
48+
}
49+
return contexts, nil
50+
}

kube/context.go

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,84 @@
1818

1919
package kube
2020

21+
import (
22+
"github.com/AlecAivazis/survey/v2/terminal"
23+
"github.com/docker/compose-cli/api/context/store"
24+
"github.com/docker/compose-cli/api/errdefs"
25+
"github.com/docker/compose-cli/utils/prompt"
26+
27+
"github.com/docker/compose-cli/kube/charts/kubernetes"
28+
)
29+
2130
// ContextParams options for creating a Kubernetes context
2231
type ContextParams struct {
23-
Name string
32+
ContextName string
2433
Description string
25-
Endpoint string
34+
KubeconfigPath string
2635
FromEnvironment bool
2736
}
37+
38+
func (cp ContextParams) CreateContextData() (interface{}, string, error) {
39+
if cp.FromEnvironment {
40+
// we use the current kubectl context from a $KUBECONFIG path
41+
return store.KubeContext{
42+
FromEnvironment: cp.FromEnvironment,
43+
}, cp.Description, nil
44+
}
45+
user := prompt.User{}
46+
selectContext := func() error {
47+
contexts, err := kubernetes.ListAvailableKubeConfigContexts(cp.KubeconfigPath)
48+
if err != nil {
49+
return err
50+
}
51+
52+
selected, err := user.Select("Select kubeconfig context", contexts)
53+
if err != nil {
54+
if err == terminal.InterruptErr {
55+
return errdefs.ErrCanceled
56+
}
57+
return err
58+
}
59+
cp.ContextName = contexts[selected]
60+
return nil
61+
}
62+
63+
if cp.KubeconfigPath != "" {
64+
err := selectContext()
65+
if err != nil {
66+
return nil, "", err
67+
}
68+
} else {
69+
70+
// interactive
71+
var options []string
72+
var actions []func() error
73+
74+
options = append(options, "Context from kubeconfig file")
75+
actions = append(actions, selectContext)
76+
77+
options = append(options, "Kubernetes environment variables")
78+
actions = append(actions, func() error {
79+
cp.FromEnvironment = true
80+
return nil
81+
})
82+
83+
selected, err := user.Select("Create a Docker context using:", options)
84+
if err != nil {
85+
if err == terminal.InterruptErr {
86+
return nil, "", errdefs.ErrCanceled
87+
}
88+
return nil, "", err
89+
}
90+
91+
err = actions[selected]()
92+
if err != nil {
93+
return nil, "", err
94+
}
95+
}
96+
return store.KubeContext{
97+
ContextName: cp.ContextName,
98+
KubeconfigPath: cp.KubeconfigPath,
99+
FromEnvironment: cp.FromEnvironment,
100+
}, cp.Description, nil
101+
}

0 commit comments

Comments
 (0)