diff --git a/engine/virt/virt.go b/engine/virt/virt.go index fa2b797ac..bd7f9d0ca 100644 --- a/engine/virt/virt.go +++ b/engine/virt/virt.go @@ -42,7 +42,7 @@ func MakeClient(ctx context.Context, config coretypes.Config, nodename, endpoint if strings.HasPrefix(endpoint, HTTPPrefixKey) { uri = fmt.Sprintf("http://%s/%s", strings.TrimLeft(endpoint, HTTPPrefixKey), config.Virt.APIVersion) } else if strings.HasPrefix(endpoint, GRPCPrefixKey) { - uri = "grpc://" + strings.TrimLeft(endpoint, HTTPPrefixKey) + uri = "grpc://" + strings.TrimLeft(endpoint, GRPCPrefixKey) } else { return nil, fmt.Errorf("invalid endpoint: %s", endpoint) } diff --git a/store/etcdv3/container_test.go b/store/etcdv3/container_test.go index 28d59629c..db266c5f9 100644 --- a/store/etcdv3/container_test.go +++ b/store/etcdv3/container_test.go @@ -87,11 +87,13 @@ func TestContainer(t *testing.T) { ID: container.ID, Running: true, } - err = m.SetContainerStatus(ctx, container, 0) + err = m.SetContainerStatus(ctx, container, 10) assert.NoError(t, err) rs, err := m.GetContainerStatus(ctx, container.ID) assert.NoError(t, err) assert.Equal(t, rs.ID, container.ID) + _, err = m.GetContainerStatus(ctx, "xxxxx") + assert.Error(t, err) container2 := &types.Container{ ID: container.ID, Nodename: "n2", diff --git a/store/etcdv3/mercury.go b/store/etcdv3/mercury.go index c420266f1..c6f7f71f9 100644 --- a/store/etcdv3/mercury.go +++ b/store/etcdv3/mercury.go @@ -53,29 +53,31 @@ func New(config types.Config, embededStorage bool) (*Mercury, error) { var err error var tlsConfig *tls.Config - if config.Etcd.Ca != "" && config.Etcd.Key != "" && config.Etcd.Cert != "" { - tlsInfo := transport.TLSInfo{ - TrustedCAFile: config.Etcd.Ca, - KeyFile: config.Etcd.Key, - CertFile: config.Etcd.Cert, + if embededStorage { + cliv3 = embeded.NewCluster() + log.Info("[Mercury] use embeded cluster") + } else { + if config.Etcd.Ca != "" && config.Etcd.Key != "" && config.Etcd.Cert != "" { + tlsInfo := transport.TLSInfo{ + TrustedCAFile: config.Etcd.Ca, + KeyFile: config.Etcd.Key, + CertFile: config.Etcd.Cert, + } + tlsConfig, err = tlsInfo.ClientConfig() + if err != nil { + return nil, err + } } - tlsConfig, err = tlsInfo.ClientConfig() - if err != nil { + if cliv3, err = clientv3.New(clientv3.Config{ + Endpoints: config.Etcd.Machines, + Username: config.Etcd.Auth.Username, + Password: config.Etcd.Auth.Password, + TLS: tlsConfig, + }); err != nil { return nil, err } } - if embededStorage { - cliv3 = embeded.NewCluster() - log.Info("[Mercury] use embeded cluster") - } else if cliv3, err = clientv3.New(clientv3.Config{ - Endpoints: config.Etcd.Machines, - Username: config.Etcd.Auth.Username, - Password: config.Etcd.Auth.Password, - TLS: tlsConfig, - }); err != nil { - return nil, err - } cliv3.KV = namespace.NewKV(cliv3.KV, config.Etcd.Prefix) cliv3.Watcher = namespace.NewWatcher(cliv3.Watcher, config.Etcd.Prefix) cliv3.Lease = namespace.NewLease(cliv3.Lease, config.Etcd.Prefix) diff --git a/types/container.go b/types/container.go index fb79de8d4..ad657e3f2 100644 --- a/types/container.go +++ b/types/container.go @@ -2,7 +2,6 @@ package types import ( "context" - "time" engine "github.com/projecteru2/core/engine" enginetypes "github.com/projecteru2/core/engine/types" @@ -53,10 +52,7 @@ func (c *Container) Inspect(ctx context.Context) (*enginetypes.VirtualizationInf if c.Engine == nil { return nil, ErrNilEngine } - // TODO remove it later like start stop and remove - inspectCtx, cancel := context.WithTimeout(ctx, 10*time.Second) - defer cancel() - return c.Engine.VirtualizationInspect(inspectCtx, c.ID) + return c.Engine.VirtualizationInspect(ctx, c.ID) } // Start a container diff --git a/types/errors.go b/types/errors.go index 9f99aee89..d6137ddab 100644 --- a/types/errors.go +++ b/types/errors.go @@ -68,6 +68,9 @@ var ( ErrInvalidBind = errors.New("invalid bind value") ErrIgnoreContainer = errors.New("ignore this container") + + ErrInvalidGitURL = errors.New("invalid git url format") + ErrInvalidContainerName = errors.New("invalid container name") ) // NewDetailedErr returns an error with details diff --git a/types/node.go b/types/node.go index 0267bf915..736d61226 100644 --- a/types/node.go +++ b/types/node.go @@ -2,7 +2,6 @@ package types import ( "context" - "time" "math" @@ -87,10 +86,7 @@ func (n *Node) Info(ctx context.Context) (*enginetypes.Info, error) { if n.Engine == nil { return nil, ErrNilEngine } - // TODO remove it later - infoCtx, cancel := context.WithTimeout(ctx, 120*time.Second) - defer cancel() - return n.Engine.Info(infoCtx) + return n.Engine.Info(ctx) } // SetCPUUsed set cpuusage diff --git a/types/node_test.go b/types/node_test.go index 66e95164a..6200a4c7e 100644 --- a/types/node_test.go +++ b/types/node_test.go @@ -2,6 +2,7 @@ package types import ( "context" + "math" "testing" enginemocks "github.com/projecteru2/core/engine/mocks" @@ -78,3 +79,17 @@ func TestSetNUMANodeMemory(t *testing.T) { assert.Len(t, node.NUMAMemory, 1) assert.Equal(t, node.NUMAMemory["n1"], int64(100)) } + +func TestStorage(t *testing.T) { + node := &Node{ + InitStorageCap: 0, + } + assert.Equal(t, node.StorageUsage(), 1.0) + assert.Equal(t, node.StorageUsed(), int64(0)) + assert.Equal(t, node.AvailableStorage(), int64(math.MaxInt64)) + node.InitStorageCap = 2 + node.StorageCap = 1 + assert.Equal(t, node.StorageUsage(), 0.5) + assert.Equal(t, node.StorageUsed(), int64(1)) + assert.Equal(t, node.AvailableStorage(), int64(1)) +} diff --git a/utils/utils.go b/utils/utils.go index f1ada3043..8892aea63 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -20,13 +20,10 @@ import ( type ctxKey string const ( - letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" - shortenLength = 7 - engineKey ctxKey = "engine" + letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + shortenLength = 7 // DefaultVersion for default version DefaultVersion = "latest" - // WrongVersion for wrong version - WrongVersion = "unknown" ) // RandomString random a string @@ -45,15 +42,13 @@ func RandomString(n int) string { // Tail return tail thing func Tail(path string) string { - parts := strings.Split(path, "/") - return parts[len(parts)-1] + return path[strings.LastIndex(path, "/")+1:] } // GetGitRepoName return git repo name func GetGitRepoName(url string) (string, error) { - if !(strings.Contains(url, "git@") || strings.Contains(url, "gitlab@") || strings.Contains(url, "https://")) || - !strings.HasSuffix(url, ".git") { - return "", fmt.Errorf("Bad git url format %q", url) + if !(strings.Contains(url, "git@") || strings.Contains(url, "gitlab@") || strings.Contains(url, "https://")) || !strings.HasSuffix(url, ".git") { + return "", types.NewDetailedErr(types.ErrInvalidGitURL, url) } return strings.TrimSuffix(Tail(url), ".git"), nil @@ -64,13 +59,7 @@ func GetTag(image string) string { if !strings.Contains(image, ":") { return DefaultVersion } - - parts := strings.Split(image, ":") - if len(parts) != 2 { - return WrongVersion - } - - return parts[len(parts)-1] + return image[strings.LastIndex(image, ":")+1:] } // NormalizeImageName will normalize image name @@ -106,7 +95,7 @@ func ParseContainerName(containerName string) (string, string, string, error) { if length >= 3 { return strings.Join(splits[0:length-2], "_"), splits[length-2], splits[length-1], nil } - return "", "", "", fmt.Errorf("Bad containerName: %s", containerName) + return "", "", "", types.NewDetailedErr(types.ErrInvalidContainerName, containerName) } // MakePublishInfo generate publish info @@ -170,10 +159,7 @@ func DecodeMetaInLabel(labels map[string]string) *types.LabelMeta { // ShortID short container ID func ShortID(containerID string) string { - if len(containerID) > shortenLength { - return containerID[:shortenLength] - } - return containerID + return containerID[:Min(len(containerID), shortenLength)] } // FilterContainer filter container by labels @@ -248,15 +234,15 @@ func safeSplit(s string) []string { } else { result = append(result, i) } + continue + } + if !strings.HasSuffix(i, inquote) { + block += i + " " } else { - if !strings.HasSuffix(i, inquote) { - block += i + " " - } else { - block += strings.TrimSuffix(i, inquote) - inquote = "" - result = append(result, block) - block = "" - } + block += strings.TrimSuffix(i, inquote) + inquote = "" + result = append(result, block) + block = "" } } diff --git a/utils/utils_test.go b/utils/utils_test.go index e38c70841..164c8a8c2 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -52,7 +52,7 @@ func TestGetTag(t *testing.T) { v := GetTag("xx") assert.Equal(t, v, DefaultVersion) v = GetTag("xx:1:2") - assert.Equal(t, v, WrongVersion) + assert.Equal(t, v, "2") v = GetTag("xx:2") assert.Equal(t, v, "2") } @@ -138,6 +138,8 @@ func TestMetaInLabel(t *testing.T) { } meta2 := DecodeMetaInLabel(labels) assert.Equal(t, meta2.Publish[0], "5001") + meta3 := DecodeMetaInLabel(map[string]string{cluster.LabelMeta: ""}) + assert.Nil(t, meta3.HealthCheck) } func TestShortID(t *testing.T) { @@ -208,4 +210,5 @@ func TestMin(t *testing.T) { a = 1 b = 2 assert.Equal(t, 1, Min(a, b)) + assert.Equal(t, 1, Min(b, a)) }