registry.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 deployment
  14. import (
  15. "fmt"
  16. "k8s.io/kubernetes/pkg/api"
  17. "k8s.io/kubernetes/pkg/api/rest"
  18. "k8s.io/kubernetes/pkg/apis/extensions"
  19. )
  20. // Registry is an interface for things that know how to store Deployments.
  21. type Registry interface {
  22. ListDeployments(ctx api.Context, options *api.ListOptions) (*extensions.DeploymentList, error)
  23. GetDeployment(ctx api.Context, deploymentID string) (*extensions.Deployment, error)
  24. CreateDeployment(ctx api.Context, deployment *extensions.Deployment) (*extensions.Deployment, error)
  25. UpdateDeployment(ctx api.Context, deployment *extensions.Deployment) (*extensions.Deployment, error)
  26. DeleteDeployment(ctx api.Context, deploymentID 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 types will panic.
  33. func NewRegistry(s rest.StandardStorage) Registry {
  34. return &storage{s}
  35. }
  36. func (s *storage) ListDeployments(ctx api.Context, options *api.ListOptions) (*extensions.DeploymentList, error) {
  37. if options != nil && options.FieldSelector != nil && !options.FieldSelector.Empty() {
  38. return nil, fmt.Errorf("field selector not supported yet")
  39. }
  40. obj, err := s.List(ctx, options)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return obj.(*extensions.DeploymentList), err
  45. }
  46. func (s *storage) GetDeployment(ctx api.Context, deploymentID string) (*extensions.Deployment, error) {
  47. obj, err := s.Get(ctx, deploymentID)
  48. if err != nil {
  49. return nil, err
  50. }
  51. return obj.(*extensions.Deployment), nil
  52. }
  53. func (s *storage) CreateDeployment(ctx api.Context, deployment *extensions.Deployment) (*extensions.Deployment, error) {
  54. obj, err := s.Create(ctx, deployment)
  55. if err != nil {
  56. return nil, err
  57. }
  58. return obj.(*extensions.Deployment), nil
  59. }
  60. func (s *storage) UpdateDeployment(ctx api.Context, deployment *extensions.Deployment) (*extensions.Deployment, error) {
  61. obj, _, err := s.Update(ctx, deployment.Name, rest.DefaultUpdatedObjectInfo(deployment, api.Scheme))
  62. if err != nil {
  63. return nil, err
  64. }
  65. return obj.(*extensions.Deployment), nil
  66. }
  67. func (s *storage) DeleteDeployment(ctx api.Context, deploymentID string) error {
  68. _, err := s.Delete(ctx, deploymentID, nil)
  69. return err
  70. }