registry.go 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. // If you make changes to this file, you should also make the corresponding change in ReplicaSet.
  14. package controller
  15. import (
  16. "fmt"
  17. "k8s.io/kubernetes/pkg/api"
  18. "k8s.io/kubernetes/pkg/api/rest"
  19. "k8s.io/kubernetes/pkg/watch"
  20. )
  21. // Registry is an interface for things that know how to store ReplicationControllers.
  22. type Registry interface {
  23. ListControllers(ctx api.Context, options *api.ListOptions) (*api.ReplicationControllerList, error)
  24. WatchControllers(ctx api.Context, options *api.ListOptions) (watch.Interface, error)
  25. GetController(ctx api.Context, controllerID string) (*api.ReplicationController, error)
  26. CreateController(ctx api.Context, controller *api.ReplicationController) (*api.ReplicationController, error)
  27. UpdateController(ctx api.Context, controller *api.ReplicationController) (*api.ReplicationController, error)
  28. DeleteController(ctx api.Context, controllerID string) error
  29. }
  30. // storage puts strong typing around storage calls
  31. type storage struct {
  32. rest.StandardStorage
  33. }
  34. // NewRegistry returns a new Registry interface for the given Storage. Any mismatched
  35. // types will panic.
  36. func NewRegistry(s rest.StandardStorage) Registry {
  37. return &storage{s}
  38. }
  39. func (s *storage) ListControllers(ctx api.Context, options *api.ListOptions) (*api.ReplicationControllerList, error) {
  40. if options != nil && options.FieldSelector != nil && !options.FieldSelector.Empty() {
  41. return nil, fmt.Errorf("field selector not supported yet")
  42. }
  43. obj, err := s.List(ctx, options)
  44. if err != nil {
  45. return nil, err
  46. }
  47. return obj.(*api.ReplicationControllerList), err
  48. }
  49. func (s *storage) WatchControllers(ctx api.Context, options *api.ListOptions) (watch.Interface, error) {
  50. return s.Watch(ctx, options)
  51. }
  52. func (s *storage) GetController(ctx api.Context, controllerID string) (*api.ReplicationController, error) {
  53. obj, err := s.Get(ctx, controllerID)
  54. if err != nil {
  55. return nil, err
  56. }
  57. return obj.(*api.ReplicationController), nil
  58. }
  59. func (s *storage) CreateController(ctx api.Context, controller *api.ReplicationController) (*api.ReplicationController, error) {
  60. obj, err := s.Create(ctx, controller)
  61. if err != nil {
  62. return nil, err
  63. }
  64. return obj.(*api.ReplicationController), nil
  65. }
  66. func (s *storage) UpdateController(ctx api.Context, controller *api.ReplicationController) (*api.ReplicationController, error) {
  67. obj, _, err := s.Update(ctx, controller.Name, rest.DefaultUpdatedObjectInfo(controller, api.Scheme))
  68. if err != nil {
  69. return nil, err
  70. }
  71. return obj.(*api.ReplicationController), nil
  72. }
  73. func (s *storage) DeleteController(ctx api.Context, controllerID string) error {
  74. _, err := s.Delete(ctx, controllerID, nil)
  75. return err
  76. }