Avoid containerd access as much as possible.
Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
@@ -119,8 +119,6 @@ type StatusStorage interface {
|
||||
Delete() error
|
||||
}
|
||||
|
||||
// TODO(random-liu): Add factory function and configure checkpoint path.
|
||||
|
||||
// StoreStatus creates the storage containing the passed in container status with the
|
||||
// specified id.
|
||||
// The status MUST be created in one transaction.
|
||||
|
||||
@@ -52,6 +52,8 @@ type Metadata struct {
|
||||
Config *runtime.PodSandboxConfig
|
||||
// NetNSPath is the network namespace used by the sandbox.
|
||||
NetNSPath string
|
||||
// IP of Pod if it is attached to non host network
|
||||
IP string
|
||||
}
|
||||
|
||||
// MarshalJSON encodes Metadata into bytes in json format.
|
||||
|
||||
@@ -30,12 +30,21 @@ import (
|
||||
type Sandbox struct {
|
||||
// Metadata is the metadata of the sandbox, it is immutable after created.
|
||||
Metadata
|
||||
// Status stores the status of the sandbox.
|
||||
Status StatusStorage
|
||||
// Container is the containerd sandbox container client
|
||||
Container containerd.Container
|
||||
// CNI network namespace client
|
||||
NetNS *NetNS
|
||||
// IP of Pod if it is attached to non host network
|
||||
IP string
|
||||
}
|
||||
|
||||
// NewSandbox creates an internally used sandbox type. This functions reminds
|
||||
// the caller that a sandbox must have a status.
|
||||
func NewSandbox(metadata Metadata, status Status) Sandbox {
|
||||
return Sandbox{
|
||||
Metadata: metadata,
|
||||
Status: StoreStatus(status),
|
||||
}
|
||||
}
|
||||
|
||||
// Store stores all sandboxes.
|
||||
@@ -67,9 +76,22 @@ func (s *Store) Add(sb Sandbox) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get returns the sandbox with specified id. Returns nil
|
||||
// Get returns the sandbox with specified id. Returns store.ErrNotExist
|
||||
// if the sandbox doesn't exist.
|
||||
func (s *Store) Get(id string) (Sandbox, error) {
|
||||
sb, err := s.GetAll(id)
|
||||
if err != nil {
|
||||
return sb, err
|
||||
}
|
||||
if sb.Status.Get().State == StateUnknown {
|
||||
return Sandbox{}, store.ErrNotExist
|
||||
}
|
||||
return sb, nil
|
||||
}
|
||||
|
||||
// GetAll returns the sandbox with specified id, including sandbox in unknown
|
||||
// state. Returns store.ErrNotExist if the sandbox doesn't exist.
|
||||
func (s *Store) GetAll(id string) (Sandbox, error) {
|
||||
s.lock.RLock()
|
||||
defer s.lock.RUnlock()
|
||||
id, err := s.idIndex.Get(id)
|
||||
@@ -91,6 +113,9 @@ func (s *Store) List() []Sandbox {
|
||||
defer s.lock.RUnlock()
|
||||
var sandboxes []Sandbox
|
||||
for _, sb := range s.sandboxes {
|
||||
if sb.Status.Get().State == StateUnknown {
|
||||
continue
|
||||
}
|
||||
sandboxes = append(sandboxes, sb)
|
||||
}
|
||||
return sandboxes
|
||||
|
||||
@@ -26,72 +26,96 @@ import (
|
||||
)
|
||||
|
||||
func TestSandboxStore(t *testing.T) {
|
||||
metadatas := map[string]Metadata{
|
||||
"1": {
|
||||
ID: "1",
|
||||
Name: "Sandbox-1",
|
||||
Config: &runtime.PodSandboxConfig{
|
||||
Metadata: &runtime.PodSandboxMetadata{
|
||||
Name: "TestPod-1",
|
||||
Uid: "TestUid-1",
|
||||
Namespace: "TestNamespace-1",
|
||||
Attempt: 1,
|
||||
sandboxes := map[string]Sandbox{
|
||||
"1": NewSandbox(
|
||||
Metadata{
|
||||
ID: "1",
|
||||
Name: "Sandbox-1",
|
||||
Config: &runtime.PodSandboxConfig{
|
||||
Metadata: &runtime.PodSandboxMetadata{
|
||||
Name: "TestPod-1",
|
||||
Uid: "TestUid-1",
|
||||
Namespace: "TestNamespace-1",
|
||||
Attempt: 1,
|
||||
},
|
||||
},
|
||||
NetNSPath: "TestNetNS-1",
|
||||
},
|
||||
NetNSPath: "TestNetNS-1",
|
||||
},
|
||||
"2abcd": {
|
||||
ID: "2abcd",
|
||||
Name: "Sandbox-2abcd",
|
||||
Config: &runtime.PodSandboxConfig{
|
||||
Metadata: &runtime.PodSandboxMetadata{
|
||||
Name: "TestPod-2abcd",
|
||||
Uid: "TestUid-2abcd",
|
||||
Namespace: "TestNamespace-2abcd",
|
||||
Attempt: 2,
|
||||
Status{State: StateReady},
|
||||
),
|
||||
"2abcd": NewSandbox(
|
||||
Metadata{
|
||||
ID: "2abcd",
|
||||
Name: "Sandbox-2abcd",
|
||||
Config: &runtime.PodSandboxConfig{
|
||||
Metadata: &runtime.PodSandboxMetadata{
|
||||
Name: "TestPod-2abcd",
|
||||
Uid: "TestUid-2abcd",
|
||||
Namespace: "TestNamespace-2abcd",
|
||||
Attempt: 2,
|
||||
},
|
||||
},
|
||||
NetNSPath: "TestNetNS-2",
|
||||
},
|
||||
NetNSPath: "TestNetNS-2",
|
||||
},
|
||||
"4a333": {
|
||||
ID: "4a333",
|
||||
Name: "Sandbox-4a333",
|
||||
Config: &runtime.PodSandboxConfig{
|
||||
Metadata: &runtime.PodSandboxMetadata{
|
||||
Name: "TestPod-4a333",
|
||||
Uid: "TestUid-4a333",
|
||||
Namespace: "TestNamespace-4a333",
|
||||
Attempt: 3,
|
||||
Status{State: StateNotReady},
|
||||
),
|
||||
"4a333": NewSandbox(
|
||||
Metadata{
|
||||
ID: "4a333",
|
||||
Name: "Sandbox-4a333",
|
||||
Config: &runtime.PodSandboxConfig{
|
||||
Metadata: &runtime.PodSandboxMetadata{
|
||||
Name: "TestPod-4a333",
|
||||
Uid: "TestUid-4a333",
|
||||
Namespace: "TestNamespace-4a333",
|
||||
Attempt: 3,
|
||||
},
|
||||
},
|
||||
NetNSPath: "TestNetNS-3",
|
||||
},
|
||||
NetNSPath: "TestNetNS-3",
|
||||
},
|
||||
"4abcd": {
|
||||
ID: "4abcd",
|
||||
Name: "Sandbox-4abcd",
|
||||
Config: &runtime.PodSandboxConfig{
|
||||
Metadata: &runtime.PodSandboxMetadata{
|
||||
Name: "TestPod-4abcd",
|
||||
Uid: "TestUid-4abcd",
|
||||
Namespace: "TestNamespace-4abcd",
|
||||
Attempt: 1,
|
||||
Status{State: StateNotReady},
|
||||
),
|
||||
"4abcd": NewSandbox(
|
||||
Metadata{
|
||||
ID: "4abcd",
|
||||
Name: "Sandbox-4abcd",
|
||||
Config: &runtime.PodSandboxConfig{
|
||||
Metadata: &runtime.PodSandboxMetadata{
|
||||
Name: "TestPod-4abcd",
|
||||
Uid: "TestUid-4abcd",
|
||||
Namespace: "TestNamespace-4abcd",
|
||||
Attempt: 1,
|
||||
},
|
||||
},
|
||||
NetNSPath: "TestNetNS-4abcd",
|
||||
},
|
||||
NetNSPath: "TestNetNS-4abcd",
|
||||
},
|
||||
Status{State: StateReady},
|
||||
),
|
||||
}
|
||||
unknown := NewSandbox(
|
||||
Metadata{
|
||||
ID: "3defg",
|
||||
Name: "Sandbox-3defg",
|
||||
Config: &runtime.PodSandboxConfig{
|
||||
Metadata: &runtime.PodSandboxMetadata{
|
||||
Name: "TestPod-3defg",
|
||||
Uid: "TestUid-3defg",
|
||||
Namespace: "TestNamespace-3defg",
|
||||
Attempt: 1,
|
||||
},
|
||||
},
|
||||
NetNSPath: "TestNetNS-3defg",
|
||||
},
|
||||
Status{State: StateUnknown},
|
||||
)
|
||||
assert := assertlib.New(t)
|
||||
sandboxes := map[string]Sandbox{}
|
||||
for id := range metadatas {
|
||||
sandboxes[id] = Sandbox{Metadata: metadatas[id]}
|
||||
}
|
||||
|
||||
s := NewStore()
|
||||
|
||||
t.Logf("should be able to add sandbox")
|
||||
for _, sb := range sandboxes {
|
||||
assert.NoError(s.Add(sb))
|
||||
}
|
||||
assert.NoError(s.Add(unknown))
|
||||
|
||||
t.Logf("should be able to get sandbox")
|
||||
genTruncIndex := func(normalName string) string { return normalName[:(len(normalName)+1)/2] }
|
||||
@@ -101,6 +125,16 @@ func TestSandboxStore(t *testing.T) {
|
||||
assert.Equal(sb, got)
|
||||
}
|
||||
|
||||
t.Logf("should not be able to get unknown sandbox")
|
||||
got, err := s.Get(unknown.ID)
|
||||
assert.Equal(store.ErrNotExist, err)
|
||||
assert.Equal(Sandbox{}, got)
|
||||
|
||||
t.Logf("should be able to get unknown sandbox with GetAll")
|
||||
got, err = s.GetAll(unknown.ID)
|
||||
assert.NoError(err)
|
||||
assert.Equal(unknown, got)
|
||||
|
||||
t.Logf("should be able to list sandboxes")
|
||||
sbs := s.List()
|
||||
assert.Len(sbs, len(sandboxes))
|
||||
|
||||
100
pkg/store/sandbox/status.go
Normal file
100
pkg/store/sandbox/status.go
Normal file
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
Copyright 2018 The Containerd Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package sandbox
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// State is the sandbox state we use in cri-containerd.
|
||||
// It has unknown state defined.
|
||||
type State uint32
|
||||
|
||||
const (
|
||||
// StateUnknown is unknown state of sandbox. Sandbox
|
||||
// is in unknown state before its corresponding sandbox container
|
||||
// is created. Sandbox in unknown state should be ignored by most
|
||||
// functions, unless the caller needs to update sandbox state.
|
||||
StateUnknown State = iota
|
||||
// StateReady is ready state, it means sandbox container
|
||||
// is running.
|
||||
StateReady
|
||||
// StateNotReady is notready state, it ONLY means sandbox
|
||||
// container is not running.
|
||||
// StopPodSandbox should still be called for NOTREADY sandbox to
|
||||
// cleanup resources other than sandbox container, e.g. network namespace.
|
||||
// This is an assumption made in CRI.
|
||||
StateNotReady
|
||||
)
|
||||
|
||||
// Status is the status of a sandbox.
|
||||
type Status struct {
|
||||
// Pid is the init process id of the sandbox container.
|
||||
Pid uint32
|
||||
// CreatedAt is the created timestamp.
|
||||
CreatedAt time.Time
|
||||
// State is the state of the sandbox.
|
||||
State State
|
||||
}
|
||||
|
||||
// UpdateFunc is function used to update the sandbox status. If there
|
||||
// is an error, the update will be rolled back.
|
||||
type UpdateFunc func(Status) (Status, error)
|
||||
|
||||
// StatusStorage manages the sandbox status.
|
||||
// The status storage for sandbox is different from container status storage,
|
||||
// because we don't checkpoint sandbox status. If we need checkpoint in the
|
||||
// future, we should combine this with container status storage.
|
||||
type StatusStorage interface {
|
||||
// Get a sandbox status.
|
||||
Get() Status
|
||||
// Update the sandbox status. Note that the update MUST be applied
|
||||
// in one transaction.
|
||||
Update(UpdateFunc) error
|
||||
}
|
||||
|
||||
// StoreStatus creates the storage containing the passed in sandbox status with the
|
||||
// specified id.
|
||||
// The status MUST be created in one transaction.
|
||||
func StoreStatus(status Status) StatusStorage {
|
||||
return &statusStorage{status: status}
|
||||
}
|
||||
|
||||
type statusStorage struct {
|
||||
sync.RWMutex
|
||||
status Status
|
||||
}
|
||||
|
||||
// Get a copy of sandbox status.
|
||||
func (s *statusStorage) Get() Status {
|
||||
s.RLock()
|
||||
defer s.RUnlock()
|
||||
return s.status
|
||||
}
|
||||
|
||||
// Update the sandbox status.
|
||||
func (s *statusStorage) Update(u UpdateFunc) error {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
newStatus, err := u(s.status)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.status = newStatus
|
||||
return nil
|
||||
}
|
||||
61
pkg/store/sandbox/status_test.go
Normal file
61
pkg/store/sandbox/status_test.go
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
Copyright 2018 The Containerd Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package sandbox
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
assertlib "github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestStatus(t *testing.T) {
|
||||
testStatus := Status{
|
||||
Pid: 123,
|
||||
CreatedAt: time.Now(),
|
||||
State: StateUnknown,
|
||||
}
|
||||
updateStatus := Status{
|
||||
Pid: 456,
|
||||
CreatedAt: time.Now(),
|
||||
State: StateReady,
|
||||
}
|
||||
updateErr := errors.New("update error")
|
||||
assert := assertlib.New(t)
|
||||
|
||||
t.Logf("simple store and get")
|
||||
s := StoreStatus(testStatus)
|
||||
old := s.Get()
|
||||
assert.Equal(testStatus, old)
|
||||
|
||||
t.Logf("failed update should not take effect")
|
||||
err := s.Update(func(o Status) (Status, error) {
|
||||
o = updateStatus
|
||||
return o, updateErr
|
||||
})
|
||||
assert.Equal(updateErr, err)
|
||||
assert.Equal(testStatus, s.Get())
|
||||
|
||||
t.Logf("successful update should take effect but not checkpoint")
|
||||
err = s.Update(func(o Status) (Status, error) {
|
||||
o = updateStatus
|
||||
return o, nil
|
||||
})
|
||||
assert.NoError(err)
|
||||
assert.Equal(updateStatus, s.Get())
|
||||
}
|
||||
Reference in New Issue
Block a user