feat: store kube config file to make buildx builder switchable

Signed-off-by: Wang <morlay.null@gmail.com>
pull/497/head
Wang 4 years ago
parent 74f76cf4e9
commit 68cebffe13

@ -3,6 +3,7 @@ package commands
import ( import (
"encoding/csv" "encoding/csv"
"fmt" "fmt"
"net/url"
"os" "os"
"strings" "strings"
@ -145,7 +146,14 @@ func runCreate(dockerCli command.Cli, in createOptions, args []string) error {
if in.driver == "kubernetes" { if in.driver == "kubernetes" {
// naming endpoint to make --append works // 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},
"kubeconfig": {os.Getenv("KUBECONFIG")},
}).Encode(),
}).String()
} }
m, err := csvToMap(in.driverOpts) m, err := csvToMap(in.driverOpts)

@ -2,8 +2,10 @@ package commands
import ( import (
"context" "context"
"net/url"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"github.com/docker/buildx/build" "github.com/docker/buildx/build"
"github.com/docker/buildx/driver" "github.com/docker/buildx/driver"
@ -12,11 +14,13 @@ import (
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/context/docker" "github.com/docker/cli/cli/context/docker"
"github.com/docker/cli/cli/context/kubernetes" "github.com/docker/cli/cli/context/kubernetes"
ctxstore "github.com/docker/cli/cli/context/store"
dopts "github.com/docker/cli/opts" dopts "github.com/docker/cli/opts"
dockerclient "github.com/docker/docker/client" dockerclient "github.com/docker/docker/client"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
"k8s.io/client-go/tools/clientcmd"
) )
// getStore returns current builder instance store // getStore returns current builder instance store
@ -192,12 +196,12 @@ func driversForNodeGroup(ctx context.Context, dockerCli command.Cli, ng *store.N
contextStore := dockerCli.ContextStore() contextStore := dockerCli.ContextStore()
var kcc driver.KubeClientConfig var kcc driver.KubeClientConfig
kcc, err = kubernetes.ConfigFromContext(n.Endpoint, contextStore) kcc, err = configFromContext(n.Endpoint, contextStore)
if err != nil { if err != nil {
// err is returned if n.Endpoint is non-context name like "unix:///var/run/docker.sock". // err is returned if n.Endpoint is non-context name like "unix:///var/run/docker.sock".
// try again with name="default". // try again with name="default".
// FIXME: n should retain real context name. // FIXME: n should retain real context name.
kcc, err = kubernetes.ConfigFromContext("default", contextStore) kcc, err = configFromContext("default", contextStore)
if err != nil { if err != nil {
logrus.Error(err) logrus.Error(err)
} }
@ -237,6 +241,21 @@ func driversForNodeGroup(ctx context.Context, dockerCli command.Cli, ng *store.N
return dis, nil return dis, nil
} }
func configFromContext(endpointName string, s ctxstore.Reader) (clientcmd.ClientConfig, error) {
if strings.HasPrefix(endpointName, "kubernetes://") {
u, _ := url.Parse(endpointName)
if kubeconfig := u.Query().Get("kubeconfig"); kubeconfig != "" {
clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfig},
&clientcmd.ConfigOverrides{},
)
return clientConfig, nil
}
}
return kubernetes.ConfigFromContext(endpointName, s)
}
// clientForEndpoint returns a docker client for an endpoint // clientForEndpoint returns a docker client for an endpoint
func clientForEndpoint(dockerCli command.Cli, name string) (dockerclient.APIClient, error) { func clientForEndpoint(dockerCli command.Cli, name string) (dockerclient.APIClient, error) {
list, err := dockerCli.ContextStore().List() list, err := dockerCli.ContextStore().List()

Loading…
Cancel
Save