Skip to content

Commit

Permalink
add validation for AddNode (#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
DuodenumL authored Dec 16, 2021
1 parent cbe538e commit d949540
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 14 deletions.
2 changes: 1 addition & 1 deletion scheduler/complex/potassium.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (m *Potassium) SelectCPUNodes(ctx context.Context, scheduleInfos []resource
}
log.Infof(ctx, "[SelectCPUNodes] resources %v, need cpu: %f memory: %d", resources, quota, memory)
if quota <= 0 {
return nil, nil, 0, errors.WithStack(types.ErrNegativeQuota)
return nil, nil, 0, errors.WithStack(types.ErrNegativeCPU)
}
if len(scheduleInfos) == 0 {
return nil, nil, 0, errors.WithStack(types.ErrZeroNodes)
Expand Down
8 changes: 7 additions & 1 deletion store/etcdv3/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ func (m *Mercury) AddPod(ctx context.Context, name, desc string) (*types.Pod, er
if err != nil {
return nil, err
}
_, err = m.Put(ctx, key, string(bytes))
resp, err := m.BatchCreate(ctx, map[string]string{key: string(bytes)})
if err != nil {
return nil, err
}
if !resp.Succeeded {
return nil, types.ErrTxnConditionFailed
}
return pod, err
}

Expand Down
3 changes: 3 additions & 0 deletions store/etcdv3/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ func TestPod(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, pod.Name, podname)

_, err = m.AddPod(ctx, podname, "CPU")
assert.Equal(t, err, types.ErrKeyExists)

pod2, err := m.GetPod(ctx, podname)
assert.NoError(t, err)
assert.Equal(t, pod2.Name, podname)
Expand Down
2 changes: 1 addition & 1 deletion store/redis/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (r *Rediaron) AddPod(ctx context.Context, name, desc string) (*types.Pod, e
if err != nil {
return nil, err
}
_, err = r.cli.Set(ctx, key, string(bytes), 0).Result()
err = r.BatchCreate(ctx, map[string]string{key: string(bytes)})
return pod, err
}

Expand Down
3 changes: 3 additions & 0 deletions store/redis/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ func (s *RediaronTestSuite) TestPod() {
s.NoError(err)
s.Equal(pod.Name, podname)

_, err = s.rediaron.AddPod(ctx, podname, "CPU")
s.Equal(err, ErrAlreadyExists)

pod2, err := s.rediaron.GetPod(ctx, podname)
s.NoError(err)
s.Equal(pod2.Name, podname)
Expand Down
24 changes: 13 additions & 11 deletions types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ var (
ErrInsufficientNodes = errors.New("not enough nodes")
ErrAlreadyFilled = errors.New("Cannot alloc a fill node plan, each node has enough workloads")

ErrNegativeMemory = errors.New("memory must be positive")
ErrNegativeStorage = errors.New("storage must be positive")
ErrNegativeQuota = errors.New("quota must be positive")

ErrZeroNodes = errors.New("no nodes provide to choose some")

ErrNodeFormat = errors.New("bad endpoint name")
Expand Down Expand Up @@ -119,13 +115,19 @@ func NewDetailedErr(err error, details interface{}) error {

// validation errors
var (
ErrEmptyNodeName = errors.New("node name is empty")
ErrEmptyAppName = errors.New("app name is empty")
ErrEmptyPodName = errors.New("pod name is empty")
ErrEmptyNodeEndpoint = errors.New("node endpoint is empty")
ErrEmptyImage = errors.New("image is empty")
ErrEmptyCount = errors.New("count is 0")
ErrEmptyWorkloadID = errors.New("workload id is empty")
ErrEmptyNodeName = errors.New("node name is empty")
ErrEmptyAppName = errors.New("app name is empty")
ErrEmptyPodName = errors.New("pod name is empty")
ErrEmptyNodeEndpoint = errors.New("node endpoint is empty")
ErrEmptyImage = errors.New("image is empty")
ErrEmptyCount = errors.New("count is 0")
ErrEmptyWorkloadID = errors.New("workload id is empty")
ErrNegativeCPU = errors.New("cpu is negative")
ErrNegativeShare = errors.New("share is negative")
ErrNegativeMemory = errors.New("memory is negative")
ErrNegativeNUMAMemory = errors.New("numa memory is negative")
ErrNegativeStorage = errors.New("storage is negative")
ErrNegativeVolumeSize = errors.New("volume size is negative")

ErrEmptyEntrypointName = errors.New("entrypoint name is empty")
ErrUnderlineInEntrypointName = errors.New("entrypoint name has '_' character")
Expand Down
22 changes: 22 additions & 0 deletions types/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,28 @@ func (o *AddNodeOptions) Validate() error {
if o.Endpoint == "" {
return errors.WithStack(ErrEmptyNodeEndpoint)
}
if o.CPU < 0 {
return errors.WithStack(ErrNegativeCPU)
}
if o.Share < 0 {
return errors.WithStack(ErrNegativeShare)
}
if o.Memory < 0 {
return errors.WithStack(ErrNegativeMemory)
}
for _, m := range o.NumaMemory {
if m < 0 {
return errors.WithStack(ErrNegativeNUMAMemory)
}
}
for _, size := range o.Volume {
if size < 0 {
return errors.WithStack(ErrNegativeVolumeSize)
}
}
if o.Storage < 0 {
return errors.WithStack(ErrNegativeStorage)
}
return nil
}

Expand Down
19 changes: 19 additions & 0 deletions types/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,25 @@ func TestValidatingAddNodeOptions(t *testing.T) {
assert.Equal(ErrEmptyNodeEndpoint, errors.Unwrap(o.Validate()))

o.Endpoint = "tcp://endpoint:2376"
o.CPU = -1
o.Share = -1
o.Memory = -1
o.NumaMemory = NUMAMemory{"0": -1}
o.Volume = VolumeMap{"/data": -1}
o.Storage = -1

assert.Equal(ErrNegativeCPU, errors.Unwrap(o.Validate()))
o.CPU = 1
assert.Equal(ErrNegativeShare, errors.Unwrap(o.Validate()))
o.Share = 100
assert.Equal(ErrNegativeMemory, errors.Unwrap(o.Validate()))
o.Memory = 100
assert.Equal(ErrNegativeNUMAMemory, errors.Unwrap(o.Validate()))
o.NumaMemory = nil
assert.Equal(ErrNegativeVolumeSize, errors.Unwrap(o.Validate()))
o.Volume = nil
assert.Equal(ErrNegativeStorage, errors.Unwrap(o.Validate()))
o.Storage = 1
assert.NoError(o.Validate())
}

Expand Down

0 comments on commit d949540

Please sign in to comment.