versioncheck.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. Copyright 2023 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 internal
  14. import (
  15. "fmt"
  16. "k8s.io/apimachinery/pkg/api/errors"
  17. "k8s.io/apimachinery/pkg/runtime"
  18. "k8s.io/apimachinery/pkg/runtime/schema"
  19. )
  20. type versionCheckManager struct {
  21. fieldManager Manager
  22. gvk schema.GroupVersionKind
  23. }
  24. var _ Manager = &versionCheckManager{}
  25. // NewVersionCheckManager creates a manager that makes sure that the
  26. // applied object is in the proper version.
  27. func NewVersionCheckManager(fieldManager Manager, gvk schema.GroupVersionKind) Manager {
  28. return &versionCheckManager{fieldManager: fieldManager, gvk: gvk}
  29. }
  30. // Update implements Manager.
  31. func (f *versionCheckManager) Update(liveObj, newObj runtime.Object, managed Managed, manager string) (runtime.Object, Managed, error) {
  32. // Nothing to do for updates, this is checked in many other places.
  33. return f.fieldManager.Update(liveObj, newObj, managed, manager)
  34. }
  35. // Apply implements Manager.
  36. func (f *versionCheckManager) Apply(liveObj, appliedObj runtime.Object, managed Managed, fieldManager string, force bool) (runtime.Object, Managed, error) {
  37. if gvk := appliedObj.GetObjectKind().GroupVersionKind(); gvk != f.gvk {
  38. return nil, nil, errors.NewBadRequest(fmt.Sprintf("invalid object type: %v", gvk))
  39. }
  40. return f.fieldManager.Apply(liveObj, appliedObj, managed, fieldManager, force)
  41. }