Skip to content

Commit

Permalink
Move config to a dedicated pkg
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <[email protected]>
  • Loading branch information
crazy-max committed Sep 16, 2021
1 parent e07f388 commit be5c6d5
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 43 deletions.
20 changes: 15 additions & 5 deletions cmd/buildkitd/config/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package config

import (
"github.com/moby/buildkit/util/resolver"
)

// Config provides containerd configuration data for the server
type Config struct {
Debug bool `toml:"debug"`
Expand All @@ -21,7 +17,7 @@ type Config struct {
Containerd ContainerdConfig `toml:"containerd"`
} `toml:"worker"`

Registries map[string]resolver.RegistryConfig `toml:"registry"`
Registries map[string]RegistryConfig `toml:"registry"`

DNS *DNSConfig `toml:"dns"`
}
Expand Down Expand Up @@ -113,3 +109,17 @@ type DNSConfig struct {
Options []string `toml:"options"`
SearchDomains []string `toml:"searchDomains"`
}

type RegistryConfig struct {
Mirrors []string `toml:"mirrors"`
PlainHTTP *bool `toml:"http"`
Insecure *bool `toml:"insecure"`
RootCAs []string `toml:"ca"`
KeyPairs []TLSKeyPair `toml:"keypair"`
TLSConfigDir []string `toml:"tlsconfigdir"`
}

type TLSKeyPair struct {
Key string `toml:"key"`
Certificate string `toml:"cert"`
}
6 changes: 3 additions & 3 deletions cmd/buildkitd/config/gcpolicy_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
package config

import (
"syscall"
"golang.org/x/sys/unix"
)

func DetectDefaultGCCap(root string) int64 {
var st syscall.Statfs_t
if err := syscall.Statfs(root, &st); err != nil {
var st unix.Statfs_t
if err := unix.Statfs(root, &st); err != nil {
return defaultCap
}
diskSize := int64(st.Bsize) * int64(st.Blocks)
Expand Down
14 changes: 12 additions & 2 deletions cmd/buildkitd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/solver/bboltcachestorage"
"github.com/moby/buildkit/util/apicaps"
"github.com/moby/buildkit/util/appconfig"
"github.com/moby/buildkit/util/appcontext"
"github.com/moby/buildkit/util/appdefaults"
"github.com/moby/buildkit/util/archutil"
Expand Down Expand Up @@ -204,7 +205,7 @@ func main() {
ctx, cancel := context.WithCancel(appcontext.Context())
defer cancel()

cfg, err := LoadFile(c.GlobalString("config"))
cfg, err := appconfig.LoadFile(c.GlobalString("config"))
if err != nil {
return err
}
Expand Down Expand Up @@ -368,7 +369,7 @@ func defaultConfigPath() string {
}

func defaultConf() (config.Config, error) {
cfg, err := LoadFile(defaultConfigPath())
cfg, err := appconfig.LoadFile(defaultConfigPath())
if err != nil {
var pe *os.PathError
if !errors.As(err, &pe) {
Expand Down Expand Up @@ -777,6 +778,15 @@ func getDNSConfig(cfg *config.DNSConfig) *oci.DNSConfig {
return dns
}

// parseBoolOrAuto returns (nil, nil) if s is "auto"
func parseBoolOrAuto(s string) (*bool, error) {
if s == "" || strings.ToLower(s) == "auto" {
return nil, nil
}
b, err := strconv.ParseBool(s)
return &b, err
}

func runTraceController(p string, exp sdktrace.SpanExporter) error {
server := grpc.NewServer()
tracev1.RegisterTraceServiceServer(server, &traceCollector{exporter: exp})
Expand Down
15 changes: 3 additions & 12 deletions cmd/buildkitd/config.go → util/appconfig/load.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package main
package appconfig

import (
"io"
"os"
"strconv"
"strings"

"github.com/moby/buildkit/cmd/buildkitd/config"
"github.com/pelletier/go-toml"
"github.com/pkg/errors"
)

// Load loads buildkitd config
func Load(r io.Reader) (config.Config, error) {
var c config.Config
t, err := toml.LoadReader(r)
Expand All @@ -24,6 +23,7 @@ func Load(r io.Reader) (config.Config, error) {
return c, nil
}

// LoadFile loads buildkitd config file
func LoadFile(fp string) (config.Config, error) {
f, err := os.Open(fp)
if err != nil {
Expand All @@ -35,12 +35,3 @@ func LoadFile(fp string) (config.Config, error) {
defer f.Close()
return Load(f)
}

// parseBoolOrAuto returns (nil, nil) if s is "auto"
func parseBoolOrAuto(s string) (*bool, error) {
if s == "" || strings.ToLower(s) == "auto" {
return nil, nil
}
b, err := strconv.ParseBool(s)
return &b, err
}
4 changes: 2 additions & 2 deletions cmd/buildkitd/config_test.go → util/appconfig/load_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package appconfig

import (
"bytes"
Expand All @@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/require"
)

func TestConfig(t *testing.T) {
func TestLoad(t *testing.T) {

const testConfig = `
root = "/foo/bar"
Expand Down
3 changes: 2 additions & 1 deletion util/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/containerd/containerd/remotes/docker"
"github.com/docker/distribution/reference"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/util/appconfig"
"github.com/moby/buildkit/util/flightcontrol"
"github.com/moby/buildkit/util/imageutil"
"github.com/moby/buildkit/util/progress"
Expand Down Expand Up @@ -54,7 +55,7 @@ func Push(ctx context.Context, sm *session.Manager, sid string, provider content
if insecure {
insecureTrue := true
httpTrue := true
hosts = resolver.NewRegistryConfig(map[string]resolver.RegistryConfig{
hosts = resolver.NewRegistryConfig(map[string]appconfig.RegistryConfig{
reference.Domain(parsed): {
Insecure: &insecureTrue,
PlainHTTP: &httpTrue,
Expand Down
23 changes: 5 additions & 18 deletions util/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import (
"time"

"github.com/containerd/containerd/remotes/docker"
"github.com/moby/buildkit/cmd/buildkitd/config"
"github.com/moby/buildkit/util/tracing"
"github.com/pkg/errors"
)

func fillInsecureOpts(host string, c RegistryConfig, h docker.RegistryHost) ([]docker.RegistryHost, error) {
func fillInsecureOpts(host string, c config.RegistryConfig, h docker.RegistryHost) ([]docker.RegistryHost, error) {
var hosts []docker.RegistryHost

tc, err := loadTLSConfig(c)
Expand Down Expand Up @@ -64,7 +65,7 @@ func fillInsecureOpts(host string, c RegistryConfig, h docker.RegistryHost) ([]d
return hosts, nil
}

func loadTLSConfig(c RegistryConfig) (*tls.Config, error) {
func loadTLSConfig(c config.RegistryConfig) (*tls.Config, error) {
for _, d := range c.TLSConfigDir {
fs, err := ioutil.ReadDir(d)
if err != nil && !errors.Is(err, os.ErrNotExist) && !errors.Is(err, os.ErrPermission) {
Expand All @@ -75,7 +76,7 @@ func loadTLSConfig(c RegistryConfig) (*tls.Config, error) {
c.RootCAs = append(c.RootCAs, filepath.Join(d, f.Name()))
}
if strings.HasSuffix(f.Name(), ".cert") {
c.KeyPairs = append(c.KeyPairs, TLSKeyPair{
c.KeyPairs = append(c.KeyPairs, config.TLSKeyPair{
Certificate: filepath.Join(d, f.Name()),
Key: filepath.Join(d, strings.TrimSuffix(f.Name(), ".cert")+".key"),
})
Expand Down Expand Up @@ -114,22 +115,8 @@ func loadTLSConfig(c RegistryConfig) (*tls.Config, error) {
return tc, nil
}

type RegistryConfig struct {
Mirrors []string `toml:"mirrors"`
PlainHTTP *bool `toml:"http"`
Insecure *bool `toml:"insecure"`
RootCAs []string `toml:"ca"`
KeyPairs []TLSKeyPair `toml:"keypair"`
TLSConfigDir []string `toml:"tlsconfigdir"`
}

type TLSKeyPair struct {
Key string `toml:"key"`
Certificate string `toml:"cert"`
}

// NewRegistryConfig converts registry config to docker.RegistryHosts callback
func NewRegistryConfig(m map[string]RegistryConfig) docker.RegistryHosts {
func NewRegistryConfig(m map[string]config.RegistryConfig) docker.RegistryHosts {
return docker.Registries(
func(host string) ([]docker.RegistryHost, error) {
c, ok := m[host]
Expand Down

0 comments on commit be5c6d5

Please sign in to comment.