Skip to content

Commit

Permalink
minor refactor, bugfix virt prefix trim
Browse files Browse the repository at this point in the history
  • Loading branch information
CMGS committed Dec 5, 2019
1 parent 99e67a3 commit f32b42a
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 61 deletions.
2 changes: 1 addition & 1 deletion engine/virt/virt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 3 additions & 1 deletion store/etcdv3/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
38 changes: 20 additions & 18 deletions store/etcdv3/mercury.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions types/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package types

import (
"context"
"time"

engine "github.com/projecteru2/core/engine"
enginetypes "github.com/projecteru2/core/engine/types"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions types/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package types

import (
"context"
"time"

"math"

Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions types/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"context"
"math"
"testing"

enginemocks "github.com/projecteru2/core/engine/mocks"
Expand Down Expand Up @@ -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))
}
46 changes: 16 additions & 30 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = ""
}
}

Expand Down
5 changes: 4 additions & 1 deletion utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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))
}

0 comments on commit f32b42a

Please sign in to comment.