/* Copyright 2017 The Kubernetes 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" "github.com/containerd/containerd" "github.com/kubernetes-incubator/cri-containerd/pkg/store" ) // Sandbox contains all resources associated with the sandbox. All methods to // mutate the internal state are thread safe. type Sandbox struct { // Metadata is the metadata of the sandbox, it is immutable after created. Metadata // Container is the containerd sandbox container client Container containerd.Container // CNI network namespace client NetNS *NetNS } // Store stores all sandboxes. type Store struct { lock sync.RWMutex sandboxes map[string]Sandbox // TODO(random-liu): Add trunc index. } // NewStore creates a sandbox store. func NewStore() *Store { return &Store{sandboxes: make(map[string]Sandbox)} } // Add a sandbox into the store. func (s *Store) Add(sb Sandbox) error { s.lock.Lock() defer s.lock.Unlock() if _, ok := s.sandboxes[sb.ID]; ok { return store.ErrAlreadyExist } s.sandboxes[sb.ID] = sb return nil } // Get returns the sandbox with specified id. Returns nil // if the sandbox doesn't exist. func (s *Store) Get(id string) (Sandbox, error) { s.lock.RLock() defer s.lock.RUnlock() if sb, ok := s.sandboxes[id]; ok { return sb, nil } return Sandbox{}, store.ErrNotExist } // List lists all sandboxes. func (s *Store) List() []Sandbox { s.lock.RLock() defer s.lock.RUnlock() var sandboxes []Sandbox for _, sb := range s.sandboxes { sandboxes = append(sandboxes, sb) } return sandboxes } // Delete deletes the sandbox with specified id. func (s *Store) Delete(id string) { s.lock.Lock() defer s.lock.Unlock() delete(s.sandboxes, id) }