convert.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. Copyright 2019 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 webhook
  14. import (
  15. v1 "k8s.io/api/admission/v1"
  16. "k8s.io/api/admission/v1beta1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. )
  19. func convertAdmissionRequestToV1(r *v1beta1.AdmissionRequest) *v1.AdmissionRequest {
  20. return &v1.AdmissionRequest{
  21. Kind: r.Kind,
  22. Namespace: r.Namespace,
  23. Name: r.Name,
  24. Object: r.Object,
  25. Resource: r.Resource,
  26. Operation: v1.Operation(r.Operation),
  27. UID: r.UID,
  28. DryRun: r.DryRun,
  29. OldObject: r.OldObject,
  30. Options: r.Options,
  31. RequestKind: r.RequestKind,
  32. RequestResource: r.RequestResource,
  33. RequestSubResource: r.RequestSubResource,
  34. SubResource: r.SubResource,
  35. UserInfo: r.UserInfo,
  36. }
  37. }
  38. func convertAdmissionRequestToV1beta1(r *v1.AdmissionRequest) *v1beta1.AdmissionRequest {
  39. return &v1beta1.AdmissionRequest{
  40. Kind: r.Kind,
  41. Namespace: r.Namespace,
  42. Name: r.Name,
  43. Object: r.Object,
  44. Resource: r.Resource,
  45. Operation: v1beta1.Operation(r.Operation),
  46. UID: r.UID,
  47. DryRun: r.DryRun,
  48. OldObject: r.OldObject,
  49. Options: r.Options,
  50. RequestKind: r.RequestKind,
  51. RequestResource: r.RequestResource,
  52. RequestSubResource: r.RequestSubResource,
  53. SubResource: r.SubResource,
  54. UserInfo: r.UserInfo,
  55. }
  56. }
  57. func convertAdmissionResponseToV1(r *v1beta1.AdmissionResponse) *v1.AdmissionResponse {
  58. var pt *v1.PatchType
  59. if r.PatchType != nil {
  60. t := v1.PatchType(*r.PatchType)
  61. pt = &t
  62. }
  63. return &v1.AdmissionResponse{
  64. UID: r.UID,
  65. Allowed: r.Allowed,
  66. AuditAnnotations: r.AuditAnnotations,
  67. Patch: r.Patch,
  68. PatchType: pt,
  69. Result: r.Result,
  70. }
  71. }
  72. func convertAdmissionResponseToV1beta1(r *v1.AdmissionResponse) *v1beta1.AdmissionResponse {
  73. var pt *v1beta1.PatchType
  74. if r.PatchType != nil {
  75. t := v1beta1.PatchType(*r.PatchType)
  76. pt = &t
  77. }
  78. return &v1beta1.AdmissionResponse{
  79. UID: r.UID,
  80. Allowed: r.Allowed,
  81. AuditAnnotations: r.AuditAnnotations,
  82. Patch: r.Patch,
  83. PatchType: pt,
  84. Result: r.Result,
  85. }
  86. }
  87. func toV1AdmissionResponse(err error) *v1.AdmissionResponse {
  88. return &v1.AdmissionResponse{
  89. Result: &metav1.Status{
  90. Message: err.Error(),
  91. },
  92. }
  93. }