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

Commit 50792c4

Browse files
author
aiordache
committed
Add Kubernetes backend
Signed-off-by: aiordache <[email protected]>
1 parent ef22ce2 commit 50792c4

File tree

22 files changed

+3344
-5
lines changed

22 files changed

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

go.mod

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,13 @@ 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
48+
github.com/prometheus/common v0.10.0
5049
github.com/prometheus/tsdb v0.10.0
5150
github.com/sanathkr/go-yaml v0.0.0-20170819195128-ed9d249f429b
5251
github.com/sirupsen/logrus v1.7.0
@@ -57,13 +56,15 @@ require (
5756
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b
5857
golang.org/x/oauth2 v0.0.0-20201109201403-9fd604954f58
5958
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
60-
google.golang.org/genproto v0.0.0-20201110150050-8816d57aaa9a // indirect
6159
google.golang.org/grpc v1.33.2
6260
google.golang.org/protobuf v1.25.0
6361
gopkg.in/ini.v1 v1.62.0
62+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
6463
gotest.tools v2.2.0+incompatible
6564
gotest.tools/v3 v3.0.3
66-
k8s.io/client-go v0.20.1 // indirect
65+
helm.sh/helm/v3 v3.5.0
66+
k8s.io/api v0.20.1
67+
k8s.io/apimachinery v0.20.1
6768
sigs.k8s.io/kustomize/kyaml v0.10.5
6869
)
6970

0 commit comments

Comments
 (0)