Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use k8s cluster config when buildx used in k8s cluster #368

Merged
merged 1 commit into from
Aug 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion commands/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ func driversForNodeGroup(ctx context.Context, dockerCli command.Cli, ng *store.N
dockerapi.NegotiateAPIVersion(ctx)

contextStore := dockerCli.ContextStore()
kcc, err := kubernetes.ConfigFromContext(n.Endpoint, contextStore)

var kcc driver.KubeClientConfig
kcc, err = kubernetes.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".
Expand All @@ -187,6 +189,10 @@ func driversForNodeGroup(ctx context.Context, dockerCli command.Cli, ng *store.N
logrus.Error(err)
}
}
if kcc == nil {
kcc = driver.KubeClientConfigInCluster{}
}

d, err := driver.GetDriver(ctx, "buildx_buildkit_"+n.Name, f, dockerapi, kcc, n.Flags, n.ConfigFile, n.DriverOpts, contextPathHash)
if err != nil {
di.Err = err
Expand Down
28 changes: 25 additions & 3 deletions driver/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package driver

import (
"context"
"io/ioutil"
"sort"
"strings"

"k8s.io/client-go/rest"

dockerclient "github.com/docker/docker/client"
"github.com/pkg/errors"
"k8s.io/client-go/tools/clientcmd"
)

type Factory interface {
Expand All @@ -22,11 +25,30 @@ type BuildkitConfig struct {
// Rootless bool
}

type KubeClientConfig interface {
ClientConfig() (*rest.Config, error)
Namespace() (string, bool, error)
}

type KubeClientConfigInCluster struct{}

func (k KubeClientConfigInCluster) ClientConfig() (*rest.Config, error) {
return rest.InClusterConfig()
}

func (k KubeClientConfigInCluster) Namespace() (string, bool, error) {
namespace, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
if err != nil {
return "", false, err
}
return strings.TrimSpace(string(namespace)), true, nil
}

type InitConfig struct {
// This object needs updates to be generic for different drivers
Name string
DockerAPI dockerclient.APIClient
KubeClientConfig clientcmd.ClientConfig
KubeClientConfig KubeClientConfig
BuildkitFlags []string
ConfigFile string
DriverOpts map[string]string
Expand Down Expand Up @@ -76,7 +98,7 @@ func GetFactory(name string, instanceRequired bool) Factory {
return nil
}

func GetDriver(ctx context.Context, name string, f Factory, api dockerclient.APIClient, kcc clientcmd.ClientConfig, flags []string, config string, do map[string]string, contextPathHash string) (Driver, error) {
func GetDriver(ctx context.Context, name string, f Factory, api dockerclient.APIClient, kcc KubeClientConfig, flags []string, config string, do map[string]string, contextPathHash string) (Driver, error) {
ic := InitConfig{
DockerAPI: api,
KubeClientConfig: kcc,
Expand Down