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

Commit 2dc0616

Browse files
authored
Merge pull request #1157 from aiordache/kube_backend
Add Kubernetes backend
2 parents 9e1ec76 + 60aed92 commit 2dc0616

File tree

18 files changed

+3058
-5
lines changed

18 files changed

+3058
-5
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protos: ## Generate go code from .proto files
3939
cli: ## Compile the cli
4040
@docker build . --target cli \
4141
--platform local \
42-
--build-arg BUILD_TAGS=e2e \
42+
--build-arg BUILD_TAGS=e2e,kube \
4343
--build-arg GIT_TAG=$(GIT_TAG) \
4444
--output ./bin
4545

api/context/store/contextmetadata.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ type EcsContext struct {
5555
Profile string `json:",omitempty"`
5656
}
5757

58+
// KubeContext is the context for a kube backend
59+
type KubeContext struct {
60+
Endpoint string `json:",omitempty"`
61+
FromEnvironment bool
62+
}
63+
5864
// AwsContext is the context for the ecs plugin
5965
type AwsContext EcsContext
6066

api/context/store/store.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ const (
5555
// LocalContextType is the endpoint key in the context endpoints for a new
5656
// local backend
5757
LocalContextType = "local"
58+
// KubeContextType is the endpoint key in the context endpoints for a new
59+
// kube backend
60+
KubeContextType = "kubernetes"
5861
)
5962

6063
const (
@@ -328,5 +331,8 @@ func getters() map[string]func() interface{} {
328331
LocalContextType: func() interface{} {
329332
return &LocalContext{}
330333
},
334+
KubeContextType: func() interface{} {
335+
return &KubeContext{}
336+
},
331337
}
332338
}

cli/cmd/context/create_kube.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 context
20+
21+
import (
22+
"context"
23+
24+
"github.com/pkg/errors"
25+
"github.com/spf13/cobra"
26+
27+
"github.com/docker/compose-cli/api/context/store"
28+
"github.com/docker/compose-cli/api/errdefs"
29+
"github.com/docker/compose-cli/kube"
30+
)
31+
32+
func init() {
33+
extraCommands = append(extraCommands, createKubeCommand)
34+
extraHelp = append(extraHelp, `
35+
Create a Kubernetes context:
36+
$ docker context create kubernetes CONTEXT [flags]
37+
(see docker context create kubernetes --help)
38+
`)
39+
}
40+
41+
func createKubeCommand() *cobra.Command {
42+
var opts kube.ContextParams
43+
cmd := &cobra.Command{
44+
Use: "kubernetes CONTEXT [flags]",
45+
Short: "Create context for a Kubernetes Cluster",
46+
Args: cobra.ExactArgs(1),
47+
RunE: func(cmd *cobra.Command, args []string) error {
48+
opts.Name = args[0]
49+
50+
if opts.Endpoint != "" {
51+
opts.FromEnvironment = false
52+
}
53+
return runCreateKube(cmd.Context(), args[0], opts)
54+
},
55+
}
56+
57+
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")
60+
return cmd
61+
}
62+
63+
func runCreateKube(ctx context.Context, contextName string, opts kube.ContextParams) error {
64+
if contextExists(ctx, contextName) {
65+
return errors.Wrapf(errdefs.ErrAlreadyExists, "context %q", contextName)
66+
}
67+
68+
contextData, description, err := createContextData(ctx, opts)
69+
if err != nil {
70+
return err
71+
}
72+
return createDockerContext(ctx, contextName, store.KubeContextType, description, contextData)
73+
}
74+
75+
func createContextData(ctx context.Context, opts kube.ContextParams) (interface{}, string, error) {
76+
return store.KubeContext{
77+
Endpoint: opts.Endpoint,
78+
FromEnvironment: opts.FromEnvironment,
79+
}, opts.Description, nil
80+
}

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,12 @@ require (
3939
github.com/joho/godotenv v1.3.0
4040
github.com/labstack/echo v3.3.10+incompatible
4141
github.com/labstack/gommon v0.3.0 // indirect
42-
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
4342
github.com/moby/buildkit v0.8.1-0.20201205083753-0af7b1b9c693
4443
github.com/moby/term v0.0.0-20201110203204-bea5bbe245bf
4544
github.com/morikuni/aec v1.0.0
4645
github.com/opencontainers/go-digest v1.0.0
4746
github.com/opencontainers/image-spec v1.0.1
4847
github.com/pkg/errors v0.9.1
49-
github.com/prometheus/procfs v0.2.0 // indirect
5048
github.com/prometheus/tsdb v0.10.0
5149
github.com/sanathkr/go-yaml v0.0.0-20170819195128-ed9d249f429b
5250
github.com/sirupsen/logrus v1.7.0
@@ -57,13 +55,15 @@ require (
5755
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b
5856
golang.org/x/oauth2 v0.0.0-20201109201403-9fd604954f58
5957
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
60-
google.golang.org/genproto v0.0.0-20201110150050-8816d57aaa9a // indirect
6158
google.golang.org/grpc v1.33.2
6259
google.golang.org/protobuf v1.25.0
6360
gopkg.in/ini.v1 v1.62.0
61+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
6462
gotest.tools v2.2.0+incompatible
6563
gotest.tools/v3 v3.0.3
66-
k8s.io/client-go v0.20.1 // indirect
64+
helm.sh/helm/v3 v3.5.0
65+
k8s.io/api v0.20.1
66+
k8s.io/apimachinery v0.20.1
6767
sigs.k8s.io/kustomize/kyaml v0.10.5
6868
)
6969

0 commit comments

Comments
 (0)