registry.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 configmap
  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 for things that know how to store ConfigMaps.
  20. type Registry interface {
  21. ListConfigMaps(ctx api.Context, options *api.ListOptions) (*api.ConfigMapList, error)
  22. WatchConfigMaps(ctx api.Context, options *api.ListOptions) (watch.Interface, error)
  23. GetConfigMap(ctx api.Context, name string) (*api.ConfigMap, error)
  24. CreateConfigMap(ctx api.Context, cfg *api.ConfigMap) (*api.ConfigMap, error)
  25. UpdateConfigMap(ctx api.Context, cfg *api.ConfigMap) (*api.ConfigMap, error)
  26. DeleteConfigMap(ctx api.Context, name 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) ListConfigMaps(ctx api.Context, options *api.ListOptions) (*api.ConfigMapList, error) {
  38. obj, err := s.List(ctx, options)
  39. if err != nil {
  40. return nil, err
  41. }
  42. return obj.(*api.ConfigMapList), err
  43. }
  44. func (s *storage) WatchConfigMaps(ctx api.Context, options *api.ListOptions) (watch.Interface, error) {
  45. return s.Watch(ctx, options)
  46. }
  47. func (s *storage) GetConfigMap(ctx api.Context, name string) (*api.ConfigMap, error) {
  48. obj, err := s.Get(ctx, name)
  49. if err != nil {
  50. return nil, err
  51. }
  52. return obj.(*api.ConfigMap), nil
  53. }
  54. func (s *storage) CreateConfigMap(ctx api.Context, cfg *api.ConfigMap) (*api.ConfigMap, error) {
  55. obj, err := s.Create(ctx, cfg)
  56. if err != nil {
  57. return nil, err
  58. }
  59. return obj.(*api.ConfigMap), nil
  60. }
  61. func (s *storage) UpdateConfigMap(ctx api.Context, cfg *api.ConfigMap) (*api.ConfigMap, error) {
  62. obj, _, err := s.Update(ctx, cfg.Name, rest.DefaultUpdatedObjectInfo(cfg, api.Scheme))
  63. if err != nil {
  64. return nil, err
  65. }
  66. return obj.(*api.ConfigMap), nil
  67. }
  68. func (s *storage) DeleteConfigMap(ctx api.Context, name string) error {
  69. _, err := s.Delete(ctx, name, nil)
  70. return err
  71. }