Skip to content

Commit

Permalink
feat: store kube config file to make buildx builder switchable
Browse files Browse the repository at this point in the history
Signed-off-by: Wang <morlay.null@gmail.com>
  • Loading branch information
morlay committed Dec 30, 2020
1 parent 74f76cf commit a339e41
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
10 changes: 9 additions & 1 deletion commands/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"encoding/csv"
"fmt"
"net/url"
"os"
"strings"

Expand Down Expand Up @@ -145,7 +146,14 @@ func runCreate(dockerCli command.Cli, in createOptions, args []string) error {

if in.driver == "kubernetes" {
// naming endpoint to make --append works
ep = fmt.Sprintf("%s://%s?deployment=%s", in.driver, in.name, in.nodeName)
ep = (&url.URL{
Scheme: in.driver,
Path: "/" + in.name,
RawQuery: (&url.Values{
"deployment": {in.nodeName},
"config-file": {os.Getenv("KUBECONFIG")},
}).Encode(),
}).String()
}

m, err := csvToMap(in.driverOpts)
Expand Down
23 changes: 21 additions & 2 deletions commands/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package commands

import (
"context"
"net/url"
"os"
"path/filepath"
"strings"

"github.com/docker/buildx/build"
"github.com/docker/buildx/driver"
Expand All @@ -12,11 +14,13 @@ import (
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/context/docker"
"github.com/docker/cli/cli/context/kubernetes"
ctxstore "github.com/docker/cli/cli/context/store"
dopts "github.com/docker/cli/opts"
dockerclient "github.com/docker/docker/client"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
"k8s.io/client-go/tools/clientcmd"
)

// getStore returns current builder instance store
Expand Down Expand Up @@ -192,12 +196,12 @@ func driversForNodeGroup(ctx context.Context, dockerCli command.Cli, ng *store.N
contextStore := dockerCli.ContextStore()

var kcc driver.KubeClientConfig
kcc, err = kubernetes.ConfigFromContext(n.Endpoint, contextStore)
kcc, err = configFromContext(n.Endpoint, contextStore)
if err != nil {
// err is returned if n.Endpoint is non-context name like "unix:///var/run/docker.sock".
// try again with name="default".
// FIXME: n should retain real context name.
kcc, err = kubernetes.ConfigFromContext("default", contextStore)
kcc, err = configFromContext("default", contextStore)
if err != nil {
logrus.Error(err)
}
Expand Down Expand Up @@ -237,6 +241,21 @@ func driversForNodeGroup(ctx context.Context, dockerCli command.Cli, ng *store.N
return dis, nil
}

func configFromContext(endpointName string, s ctxstore.Reader) (clientcmd.ClientConfig, error) {
if strings.HasPrefix(endpointName, "kubernetes://") {
u, _ := url.Parse(endpointName)

if configFile := u.Query().Get("config-file"); configFile != "" {
clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: configFile},
&clientcmd.ConfigOverrides{},
)
return clientConfig, nil
}
}
return kubernetes.ConfigFromContext(endpointName, s)
}

// clientForEndpoint returns a docker client for an endpoint
func clientForEndpoint(dockerCli command.Cli, name string) (dockerclient.APIClient, error) {
list, err := dockerCli.ContextStore().List()
Expand Down

0 comments on commit a339e41

Please sign in to comment.