...
1
16
17 package ingress
18
19 import (
20 "context"
21 "fmt"
22
23 apiequality "k8s.io/apimachinery/pkg/api/equality"
24 "k8s.io/apimachinery/pkg/runtime"
25 "k8s.io/apimachinery/pkg/util/validation/field"
26 "k8s.io/apiserver/pkg/storage/names"
27 "k8s.io/kubernetes/pkg/api/legacyscheme"
28 "k8s.io/kubernetes/pkg/apis/networking"
29 "k8s.io/kubernetes/pkg/apis/networking/validation"
30 "sigs.k8s.io/structured-merge-diff/v4/fieldpath"
31 )
32
33 const (
34 annotationIngressClass = "kubernetes.io/ingress.class"
35 )
36
37
38 type ingressStrategy struct {
39 runtime.ObjectTyper
40 names.NameGenerator
41 }
42
43
44 var Strategy = ingressStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
45
46
47 func (ingressStrategy) NamespaceScoped() bool {
48 return true
49 }
50
51
52
53 func (ingressStrategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set {
54 fields := map[fieldpath.APIVersion]*fieldpath.Set{
55 "extensions/v1beta1": fieldpath.NewSet(
56 fieldpath.MakePathOrDie("status"),
57 ),
58 "networking.k8s.io/v1beta1": fieldpath.NewSet(
59 fieldpath.MakePathOrDie("status"),
60 ),
61 "networking.k8s.io/v1": fieldpath.NewSet(
62 fieldpath.MakePathOrDie("status"),
63 ),
64 }
65
66 return fields
67 }
68
69
70 func (ingressStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
71 ingress := obj.(*networking.Ingress)
72
73 ingress.Status = networking.IngressStatus{}
74
75 ingress.Generation = 1
76 }
77
78
79 func (ingressStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
80 newIngress := obj.(*networking.Ingress)
81 oldIngress := old.(*networking.Ingress)
82
83 newIngress.Status = oldIngress.Status
84
85
86
87
88 if !apiequality.Semantic.DeepEqual(oldIngress.Spec, newIngress.Spec) {
89 newIngress.Generation = oldIngress.Generation + 1
90 }
91
92 }
93
94
95 func (ingressStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
96 ingress := obj.(*networking.Ingress)
97 return validation.ValidateIngressCreate(ingress)
98 }
99
100
101 func (ingressStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string {
102 var warnings []string
103 ingress := obj.(*networking.Ingress)
104 _, annotationIsSet := ingress.Annotations[annotationIngressClass]
105 if annotationIsSet && ingress.Spec.IngressClassName == nil {
106 warnings = append(warnings, fmt.Sprintf("annotation %q is deprecated, please use 'spec.ingressClassName' instead", annotationIngressClass))
107 }
108 return warnings
109 }
110
111
112 func (ingressStrategy) Canonicalize(obj runtime.Object) {
113 }
114
115
116 func (ingressStrategy) AllowCreateOnUpdate() bool {
117 return false
118 }
119
120
121 func (ingressStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
122 return validation.ValidateIngressUpdate(obj.(*networking.Ingress), old.(*networking.Ingress))
123 }
124
125
126 func (ingressStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string {
127 return nil
128 }
129
130
131 func (ingressStrategy) AllowUnconditionalUpdate() bool {
132 return true
133 }
134
135 type ingressStatusStrategy struct {
136 ingressStrategy
137 }
138
139
140 var StatusStrategy = ingressStatusStrategy{Strategy}
141
142
143
144 func (ingressStatusStrategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set {
145 fields := map[fieldpath.APIVersion]*fieldpath.Set{
146 "extensions/v1beta1": fieldpath.NewSet(
147 fieldpath.MakePathOrDie("spec"),
148 ),
149 "networking.k8s.io/v1beta1": fieldpath.NewSet(
150 fieldpath.MakePathOrDie("spec"),
151 ),
152 "networking.k8s.io/v1": fieldpath.NewSet(
153 fieldpath.MakePathOrDie("spec"),
154 ),
155 }
156
157 return fields
158 }
159
160
161 func (ingressStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
162 newIngress := obj.(*networking.Ingress)
163 oldIngress := old.(*networking.Ingress)
164
165 newIngress.Spec = oldIngress.Spec
166 }
167
168
169 func (ingressStatusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
170 return validation.ValidateIngressStatusUpdate(obj.(*networking.Ingress), old.(*networking.Ingress))
171 }
172
173
174 func (ingressStatusStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string {
175 return nil
176 }
177
View as plain text