mirror of
https://github.com/docker/buildx.git
synced 2024-11-22 15:37:16 +08:00
Merge pull request #1414 from crazy-max/move-k8s-config
kubernetes: move context config logic to its own pkg
This commit is contained in:
commit
df3b868fe7
@ -2,9 +2,7 @@ package commands
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net/url"
|
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/docker/buildx/build"
|
"github.com/docker/buildx/build"
|
||||||
"github.com/docker/buildx/driver"
|
"github.com/docker/buildx/driver"
|
||||||
@ -16,7 +14,6 @@ import (
|
|||||||
"github.com/docker/buildx/util/progress"
|
"github.com/docker/buildx/util/progress"
|
||||||
"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"
|
||||||
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/moby/buildkit/util/grpcerrors"
|
"github.com/moby/buildkit/util/grpcerrors"
|
||||||
@ -25,7 +22,6 @@ import (
|
|||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
"google.golang.org/grpc/codes"
|
"google.golang.org/grpc/codes"
|
||||||
"k8s.io/client-go/tools/clientcmd"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// validateEndpoint validates that endpoint is either a context or a docker host
|
// validateEndpoint validates that endpoint is either a context or a docker host
|
||||||
@ -114,12 +110,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 = configFromContext(n.Endpoint, contextStore)
|
kcc, err = ctxkube.ConfigFromEndpoint(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 = configFromContext("default", contextStore)
|
kcc, err = ctxkube.ConfigFromEndpoint("default", contextStore)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Error(err)
|
logrus.Error(err)
|
||||||
}
|
}
|
||||||
@ -160,22 +156,6 @@ 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 != "" {
|
|
||||||
_ = os.Setenv(clientcmd.RecommendedConfigPathEnvVar, kubeconfig)
|
|
||||||
}
|
|
||||||
rules := clientcmd.NewDefaultClientConfigLoadingRules()
|
|
||||||
apiConfig, err := rules.Load()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return clientcmd.NewDefaultClientConfig(*apiConfig, &clientcmd.ConfigOverrides{}), nil
|
|
||||||
}
|
|
||||||
return ctxkube.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()
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
package context
|
package context
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/docker/cli/cli/command"
|
"github.com/docker/cli/cli/command"
|
||||||
"github.com/docker/cli/cli/context"
|
"github.com/docker/cli/cli/context"
|
||||||
@ -153,3 +155,20 @@ func NewKubernetesConfig(configPath string) clientcmd.ClientConfig {
|
|||||||
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeConfig},
|
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeConfig},
|
||||||
&clientcmd.ConfigOverrides{})
|
&clientcmd.ConfigOverrides{})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ConfigFromEndpoint loads kubernetes config from endpoint
|
||||||
|
func ConfigFromEndpoint(endpointName string, s store.Reader) (clientcmd.ClientConfig, error) {
|
||||||
|
if strings.HasPrefix(endpointName, "kubernetes://") {
|
||||||
|
u, _ := url.Parse(endpointName)
|
||||||
|
if kubeconfig := u.Query().Get("kubeconfig"); kubeconfig != "" {
|
||||||
|
_ = os.Setenv(clientcmd.RecommendedConfigPathEnvVar, kubeconfig)
|
||||||
|
}
|
||||||
|
rules := clientcmd.NewDefaultClientConfigLoadingRules()
|
||||||
|
apiConfig, err := rules.Load()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return clientcmd.NewDefaultClientConfig(*apiConfig, &clientcmd.ConfigOverrides{}), nil
|
||||||
|
}
|
||||||
|
return ConfigFromContext(endpointName, s)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user