container_reference_manager.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package container
  14. import (
  15. "sync"
  16. "k8s.io/kubernetes/pkg/api"
  17. )
  18. // RefManager manages the references for the containers.
  19. // The references are used for reporting events such as creation,
  20. // failure, etc. This manager is thread-safe, no locks are necessary
  21. // for the caller.
  22. type RefManager struct {
  23. sync.RWMutex
  24. containerIDToRef map[ContainerID]*api.ObjectReference
  25. }
  26. // NewRefManager creates and returns a container reference manager
  27. // with empty contents.
  28. func NewRefManager() *RefManager {
  29. return &RefManager{containerIDToRef: make(map[ContainerID]*api.ObjectReference)}
  30. }
  31. // SetRef stores a reference to a pod's container, associating it with the given container ID.
  32. func (c *RefManager) SetRef(id ContainerID, ref *api.ObjectReference) {
  33. c.Lock()
  34. defer c.Unlock()
  35. c.containerIDToRef[id] = ref
  36. }
  37. // ClearRef forgets the given container id and its associated container reference.
  38. func (c *RefManager) ClearRef(id ContainerID) {
  39. c.Lock()
  40. defer c.Unlock()
  41. delete(c.containerIDToRef, id)
  42. }
  43. // GetRef returns the container reference of the given ID, or (nil, false) if none is stored.
  44. func (c *RefManager) GetRef(id ContainerID) (ref *api.ObjectReference, ok bool) {
  45. c.RLock()
  46. defer c.RUnlock()
  47. ref, ok = c.containerIDToRef[id]
  48. return ref, ok
  49. }