registry.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. Copyright 2014 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 namespace
  14. import (
  15. "k8s.io/kubernetes/pkg/api"
  16. "k8s.io/kubernetes/pkg/api/rest"
  17. "k8s.io/kubernetes/pkg/watch"
  18. )
  19. // Registry is an interface implemented by things that know how to store Namespace objects.
  20. type Registry interface {
  21. ListNamespaces(ctx api.Context, options *api.ListOptions) (*api.NamespaceList, error)
  22. WatchNamespaces(ctx api.Context, options *api.ListOptions) (watch.Interface, error)
  23. GetNamespace(ctx api.Context, namespaceID string) (*api.Namespace, error)
  24. CreateNamespace(ctx api.Context, namespace *api.Namespace) error
  25. UpdateNamespace(ctx api.Context, namespace *api.Namespace) error
  26. DeleteNamespace(ctx api.Context, namespaceID string) error
  27. }
  28. // storage puts strong typing around storage calls
  29. type storage struct {
  30. rest.StandardStorage
  31. }
  32. // NewRegistry returns a new Registry interface for the given Storage. Any mismatched
  33. // types will panic.
  34. func NewRegistry(s rest.StandardStorage) Registry {
  35. return &storage{s}
  36. }
  37. func (s *storage) ListNamespaces(ctx api.Context, options *api.ListOptions) (*api.NamespaceList, error) {
  38. obj, err := s.List(ctx, options)
  39. if err != nil {
  40. return nil, err
  41. }
  42. return obj.(*api.NamespaceList), nil
  43. }
  44. func (s *storage) WatchNamespaces(ctx api.Context, options *api.ListOptions) (watch.Interface, error) {
  45. return s.Watch(ctx, options)
  46. }
  47. func (s *storage) GetNamespace(ctx api.Context, namespaceName string) (*api.Namespace, error) {
  48. obj, err := s.Get(ctx, namespaceName)
  49. if err != nil {
  50. return nil, err
  51. }
  52. return obj.(*api.Namespace), nil
  53. }
  54. func (s *storage) CreateNamespace(ctx api.Context, namespace *api.Namespace) error {
  55. _, err := s.Create(ctx, namespace)
  56. return err
  57. }
  58. func (s *storage) UpdateNamespace(ctx api.Context, namespace *api.Namespace) error {
  59. _, _, err := s.Update(ctx, namespace.Name, rest.DefaultUpdatedObjectInfo(namespace, api.Scheme))
  60. return err
  61. }
  62. func (s *storage) DeleteNamespace(ctx api.Context, namespaceID string) error {
  63. _, err := s.Delete(ctx, namespaceID, nil)
  64. return err
  65. }