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

fixes #2817 container run with empty label #2823

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 4 additions & 0 deletions apis/opts/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func ParseLabels(labels []string) (map[string]string, error) {
func parseLabel(label string) ([]string, error) {
fields := strings.SplitN(label, "=", 2)
if len(fields) != 2 {
if len(fields) == 1 {
fields = append(fields, "")
return fields, nil
}
return nil, fmt.Errorf("invalid label %s: label must be in format of key=value", label)
}
return fields, nil
Expand Down
22 changes: 19 additions & 3 deletions apis/opts/labels_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package opts

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -46,11 +45,28 @@ func TestParseLabels(t *testing.T) {
err: nil,
},
},
// only input key
{
input: []string{"ThisIsALableWithoutEqualMark"},
expected: result{
labels: nil,
err: fmt.Errorf("invalid label ThisIsALableWithoutEqualMark: label must be in format of key=value"),
labels: map[string]string{
"ThisIsALableWithoutEqualMark": "",
},
err: nil,
},
},
{
input: []string{},
expected: result{
labels: map[string]string{},
err: nil,
},
},
{
input: nil,
expected: result{
labels: map[string]string{},
err: nil,
},
},
}
Expand Down
5 changes: 5 additions & 0 deletions ctrd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ type Client struct {
watch *watch
lock *containerLock

// insecureRegistries stores the insecure registries
insecureRegistries []string

// containerd grpc pool
pool []scheduler.Factory
scheduler scheduler.Scheduler
Expand All @@ -64,6 +67,7 @@ func NewClient(opts ...ClientOpt) (APIClient, error) {
rpcAddr: unixSocketPath,
grpcClientPoolCapacity: defaultGrpcClientPoolCapacity,
maxStreamsClient: defaultMaxStreamsClient,
insecureRegistries: []string{},
}

for _, opt := range opts {
Expand All @@ -79,6 +83,7 @@ func NewClient(opts ...ClientOpt) (APIClient, error) {
watch: &watch{
containers: make(map[string]*containerPack),
},
insecureRegistries: copts.insecureRegistries,
}

for i := 0; i < copts.grpcClientPoolCapacity; i++ {
Expand Down
48 changes: 47 additions & 1 deletion ctrd/client_opts.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package ctrd

import "fmt"
import (
"fmt"
"net"
"strconv"
"strings"
)

type clientOpts struct {
rpcAddr string
grpcClientPoolCapacity int
maxStreamsClient int
defaultns string
insecureRegistries []string
}

// ClientOpt allows caller to set options for containerd client.
Expand Down Expand Up @@ -59,3 +65,43 @@ func WithDefaultNamespace(ns string) ClientOpt {
return nil
}
}

// WithInsecureRegistries sets the insecure registries to allow http request
// and skip secure verify.
func WithInsecureRegistries(endpoints []string) ClientOpt {
return func(c *clientOpts) error {
registries := make([]string, 0, len(endpoints))

for _, r := range endpoints {
if strings.Contains(strings.ToLower(r), "://") {
return fmt.Errorf("insecure registry %s should not contain any '://'", r)
}

if err := validateHostPort(r); err != nil {
return err
}
registries = append(registries, r)
}
c.insecureRegistries = registries
return nil
}
}

func validateHostPort(s string) error {
_, port, err := net.SplitHostPort(s)
if err != nil {
port = ""
}

if port != "" {
v, err := strconv.Atoi(port)
if err != nil {
return err
}

if v < 0 || v > 65535 {
return fmt.Errorf("invalid port %q", port)
}
}
return nil
}
50 changes: 50 additions & 0 deletions ctrd/client_opts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package ctrd

import "testing"

func TestWithInsecureRegistries(t *testing.T) {
testCases := []struct {
endpoints []string
hasError bool
}{
{
endpoints: []string{"localhost:5000"},
hasError: false,
},
{
endpoints: []string{"localhost:5000/v1"},
hasError: true,
},
{
endpoints: []string{"myregistry.com"},
hasError: false,
},
{
endpoints: []string{"myregistry.com:5000"},
hasError: false,
},
{
endpoints: []string{"myregistry.com:5000/v1"},
hasError: true,
},
{
endpoints: []string{"http://myregistry.com:5000"},
hasError: true,
},
{
endpoints: []string{"dummy://myregistry.com:5000"},
hasError: true,
},
{
endpoints: []string{"myregistry.com:65536"},
hasError: true,
},
}

for _, tc := range testCases {
err := WithInsecureRegistries(tc.endpoints)(&clientOpts{})
if (err != nil) != tc.hasError {
t.Fatalf("expected hasError = %v, but got error = %v", tc.hasError, err)
}
}
}
4 changes: 2 additions & 2 deletions ctrd/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (c *Client) PushImage(ctx context.Context, ref string, authConfig *types.Au

pushTracker := docker.NewInMemoryTracker()

resolver, err := resolver(authConfig, docker.ResolverOptions{
resolver, err := c.getResolver(authConfig, ref, docker.ResolverOptions{
Tracker: pushTracker,
})
if err != nil {
Expand Down Expand Up @@ -248,7 +248,7 @@ func (c *Client) FetchImage(ctx context.Context, ref string, authConfig *types.A
return nil, fmt.Errorf("failed to get a containerd grpc client: %v", err)
}

resolver, err := resolver(authConfig, docker.ResolverOptions{})
resolver, err := c.getResolver(authConfig, ref, docker.ResolverOptions{})
if err != nil {
return nil, err
}
Expand Down
51 changes: 32 additions & 19 deletions ctrd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/tls"
"net"
"net/http"
"net/url"
"syscall"
"time"

Expand All @@ -17,6 +18,7 @@ import (
"github.com/containerd/containerd/runtime/linux/runctypes"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

func withExitShimV1CheckpointTaskOpts() containerd.CheckpointTaskOpts {
Expand All @@ -28,32 +30,36 @@ func withExitShimV1CheckpointTaskOpts() containerd.CheckpointTaskOpts {
}
}

func resolver(authConfig *types.AuthConfig, resolverOpt docker.ResolverOptions) (remotes.Resolver, error) {
// isInsecureDomain will return true if the domain of reference is in the
// insecure registry. The insecure registry will accept HTTP or HTTPS with
// certificates from unknown CAs.
func (c *Client) isInsecureDomain(ref string) bool {
u, err := url.Parse("dummy://" + ref)
if err != nil {
logrus.Warning("failed to parse reference(%s) into url: %v", ref, err)
return false
}

for _, r := range c.insecureRegistries {
if r == u.Host {
return true
}
}
return false
}

func (c *Client) getResolver(authConfig *types.AuthConfig, ref string, resolverOpt docker.ResolverOptions) (remotes.Resolver, error) {
var (
// TODO
username = ""
secret = ""
refresh = ""
insecure = false
insecure = c.isInsecureDomain(ref)
)

if authConfig != nil {
username = authConfig.Username
secret = authConfig.Password
}

// FIXME
_ = refresh

options := docker.ResolverOptions{
PlainHTTP: resolverOpt.PlainHTTP,
Tracker: resolverOpt.Tracker,
}
options.Credentials = func(host string) (string, string, error) {
// Only one host
return username, secret, nil
}

tr := &http.Transport{
Proxy: proxyFromEnvironment,
DialContext: (&net.Dialer{
Expand All @@ -70,10 +76,17 @@ func resolver(authConfig *types.AuthConfig, resolverOpt docker.ResolverOptions)
ExpectContinueTimeout: 5 * time.Second,
}

options.Client = &http.Client{
Transport: tr,
options := docker.ResolverOptions{
Tracker: resolverOpt.Tracker,
PlainHTTP: insecure,
Credentials: func(host string) (string, string, error) {
// Only one host
return username, secret, nil
},
Client: &http.Client{
Transport: tr,
},
}

return docker.NewResolver(options), nil
}

Expand Down
4 changes: 4 additions & 0 deletions daemon/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ type Config struct {

// CgroupDriver sets cgroup driver for all containers
CgroupDriver string `json:"cgroup-driver,omitempty"`

// InsecureRegistries sets insecure registries to allow to pull
// insecure registries.
InsecureRegistries []string `json:"insecure-registries,omitempty"`
}

// GetCgroupDriver gets cgroup driver used in runc.
Expand Down
1 change: 1 addition & 0 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func NewDaemon(cfg *config.Config) *Daemon {
ctrdClient, err := ctrd.NewClient(
ctrd.WithRPCAddr(cfg.ContainerdAddr),
ctrd.WithDefaultNamespace(cfg.DefaultNamespace),
ctrd.WithInsecureRegistries(cfg.InsecureRegistries),
)
if err != nil {
logrus.Errorf("failed to new containerd's client: %v", err)
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ func setupFlags(cmd *cobra.Command) {
// to k8s.io
flagSet.StringVar(&cfg.DefaultNamespace, "default-namespace", namespaces.Default, "default-namespace is passed to containerd, the default value is 'default'")
flagSet.StringVar(&cfg.CgroupDriver, "cgroup-driver", "cgroupfs", "Set cgroup driver for all containers(cgroupfs|systemd), default cgroupfs")

// registry
flagSet.StringArrayVar(&cfg.InsecureRegistries, "insecure-registries", []string{}, "enable insecure registry")
}

// runDaemon prepares configs, setups essential details and runs pouchd daemon.
Expand Down