@@ -15,13 +15,10 @@ import (
1515 "sigs.k8s.io/controller-runtime/pkg/client"
1616 ctrllog "sigs.k8s.io/controller-runtime/pkg/log"
1717
18- "github.com/kong/kong-operator/v2/api/common/consts"
1918 commonv1alpha1 "github.com/kong/kong-operator/v2/api/common/v1alpha1"
20- configurationv1alpha1 "github.com/kong/kong-operator/v2/api/configuration/v1alpha1"
2119 konnectv1alpha1 "github.com/kong/kong-operator/v2/api/konnect/v1alpha1"
2220 ctrlconsts "github.com/kong/kong-operator/v2/controller/consts"
2321 "github.com/kong/kong-operator/v2/controller/pkg/patch"
24- "github.com/kong/kong-operator/v2/internal/utils/crossnamespace"
2522 k8sutils "github.com/kong/kong-operator/v2/pkg/utils/kubernetes"
2623)
2724
@@ -31,21 +28,30 @@ func handleEventGatewayRef(
3128 cl client.Client ,
3229 obj k8sutils.ConditionsAwareObject ,
3330) (ctrl.Result , error ) {
34- cert , ok := any (obj ).(* konnectv1alpha1.KonnectEventDataPlaneCertificate )
31+ // TODO: refactor this to be more generic and reusable for other types of references.
32+ type TObj interface {
33+ k8sutils.ConditionsAwareObject
34+ eventGatewayRefAccessor
35+ GetTypeName () string
36+ GetGatewayID () string
37+ SetGatewayID (id string )
38+ }
39+ o , ok := any (obj ).(TObj )
3540 if ! ok {
3641 return ctrl.Result {}, & UnsupportedGeneratedReferenceTypeError {
3742 TypeName : fmt .Sprintf ("%T" , obj ),
3843 }
3944 }
4045
41- if res , err := ensureKongReferenceGrantForEventGatewayRef (ctx , cl , cert ); err != nil || ! res .IsZero () {
46+ gatewayRef := o .GetEventGatewayRef ()
47+ if res , err := ensureKongReferenceGrantForParentRef (ctx , cl , o , gatewayRef ); err != nil || ! res .IsZero () {
4248 return res , err
4349 }
4450
45- gateway , nn , err := getEventGatewayForRef (ctx , cl , cert . Spec . GatewayRef , cert .GetNamespace ())
51+ gateway , nn , err := getEventGatewayForRef (ctx , cl , gatewayRef , o .GetNamespace ())
4652 if err != nil {
4753 if res , errStatus := patch .StatusWithCondition (
48- ctx , cl , cert ,
54+ ctx , cl , o ,
4955 konnectv1alpha1 .EventGatewayRefValidConditionType ,
5056 metav1 .ConditionFalse ,
5157 konnectv1alpha1 .EventGatewayRefReasonInvalid ,
@@ -59,7 +65,7 @@ func handleEventGatewayRef(
5965 if delTimestamp := gateway .GetDeletionTimestamp (); ! delTimestamp .IsZero () {
6066 msg := fmt .Sprintf ("Referenced KonnectEventGateway %s is being deleted" , nn )
6167 if res , errStatus := patch .StatusWithCondition (
62- ctx , cl , cert ,
68+ ctx , cl , o ,
6369 konnectv1alpha1 .EventGatewayRefValidConditionType ,
6470 metav1 .ConditionFalse ,
6571 konnectv1alpha1 .EventGatewayRefReasonInvalid ,
@@ -76,7 +82,7 @@ func handleEventGatewayRef(
7682 cond , ok := k8sutils .GetCondition (konnectv1alpha1 .KonnectEntityProgrammedConditionType , gateway )
7783 if ! ok || cond .Status != metav1 .ConditionTrue {
7884 if res , errStatus := patch .StatusWithCondition (
79- ctx , cl , cert ,
85+ ctx , cl , o ,
8086 konnectv1alpha1 .EventGatewayRefValidConditionType ,
8187 metav1 .ConditionFalse ,
8288 konnectv1alpha1 .EventGatewayRefReasonNotProgrammed ,
@@ -93,7 +99,7 @@ func handleEventGatewayRef(
9399 Msg : "Referenced KonnectEventGateway does not have a Konnect ID yet" ,
94100 }
95101 if res , errStatus := patch .StatusWithCondition (
96- ctx , cl , cert ,
102+ ctx , cl , o ,
97103 konnectv1alpha1 .EventGatewayRefValidConditionType ,
98104 metav1 .ConditionFalse ,
99105 konnectv1alpha1 .EventGatewayRefReasonInvalid ,
@@ -104,12 +110,9 @@ func handleEventGatewayRef(
104110 return ctrl.Result {}, err
105111 }
106112
107- old := cert .DeepCopy ()
108- if cert .Status .GatewayID == nil {
109- cert .Status .GatewayID = & konnectv1alpha1.KonnectEntityRef {}
110- }
111- cert .Status .GatewayID .ID = gateway .GetKonnectID ()
112- _ , err = patch .ApplyStatusPatchIfNotEmpty (ctx , cl , ctrllog .FromContext (ctx ), cert , old )
113+ old := o .DeepCopyObject ().(TObj )
114+ o .SetGatewayID (gateway .GetKonnectID ())
115+ _ , err = patch .ApplyStatusPatchIfNotEmpty (ctx , cl , ctrllog .FromContext (ctx ), o , old )
113116 if err != nil {
114117 if apierrors .IsConflict (err ) {
115118 return ctrl.Result {RequeueAfter : ctrlconsts .RequeueWithoutBackoff }, nil
@@ -118,7 +121,7 @@ func handleEventGatewayRef(
118121 }
119122
120123 if res , errStatus := patch .StatusWithCondition (
121- ctx , cl , cert ,
124+ ctx , cl , o ,
122125 konnectv1alpha1 .EventGatewayRefValidConditionType ,
123126 metav1 .ConditionTrue ,
124127 konnectv1alpha1 .EventGatewayRefReasonValid ,
@@ -130,72 +133,6 @@ func handleEventGatewayRef(
130133 return ctrl.Result {}, nil
131134}
132135
133- func ensureKongReferenceGrantForEventGatewayRef (
134- ctx context.Context ,
135- cl client.Client ,
136- ent * konnectv1alpha1.KonnectEventDataPlaneCertificate ,
137- ) (ctrl.Result , error ) {
138- ref := ent .Spec .GatewayRef
139- if ref .Type != commonv1alpha1 .ObjectRefTypeNamespacedRef ||
140- ref .NamespacedRef == nil ||
141- ref .NamespacedRef .Namespace == nil ||
142- * ref .NamespacedRef .Namespace == ent .GetNamespace () {
143- if res , errStatus := patch .StatusWithoutCondition (
144- ctx , cl , ent ,
145- configurationv1alpha1 .KongReferenceGrantConditionTypeResolvedRefs ,
146- ); errStatus != nil || ! res .IsZero () {
147- return res , errStatus
148- }
149- return ctrl.Result {}, nil
150- }
151-
152- targetNamespace := * ref .NamespacedRef .Namespace
153- err := crossnamespace .CheckKongReferenceGrantForResource (
154- ctx ,
155- cl ,
156- ent .GetNamespace (),
157- targetNamespace ,
158- ref .NamespacedRef .Name ,
159- metav1 .GroupVersionKind (ent .GetObjectKind ().GroupVersionKind ()),
160- metav1 .GroupVersionKind (konnectv1alpha1 .GroupVersion .WithKind ("KonnectEventGateway" )),
161- )
162- if crossnamespace .IsReferenceNotGranted (err ) {
163- if res , errStatus := patch .StatusWithCondition (
164- ctx , cl , ent ,
165- consts .ConditionType (configurationv1alpha1 .KongReferenceGrantConditionTypeResolvedRefs ),
166- metav1 .ConditionFalse ,
167- configurationv1alpha1 .KongReferenceGrantReasonRefNotPermitted ,
168- fmt .Sprintf (
169- "KongReferenceGrants do not allow access to KonnectEventGateway %s/%s" ,
170- targetNamespace ,
171- ref .NamespacedRef .Name ,
172- ),
173- ); errStatus != nil || ! res .IsZero () {
174- return res , errStatus
175- }
176- return ctrl.Result {RequeueAfter : ctrlconsts .RequeueWithoutBackoff }, nil
177- }
178- if err != nil {
179- return ctrl.Result {}, err
180- }
181-
182- if res , errStatus := patch .StatusWithCondition (
183- ctx , cl , ent ,
184- consts .ConditionType (configurationv1alpha1 .KongReferenceGrantConditionTypeResolvedRefs ),
185- metav1 .ConditionTrue ,
186- configurationv1alpha1 .KongReferenceGrantReasonResolvedRefs ,
187- fmt .Sprintf (
188- "KongReferenceGrants allow access to KonnectEventGateway %s/%s" ,
189- targetNamespace ,
190- ref .NamespacedRef .Name ,
191- ),
192- ); errStatus != nil || ! res .IsZero () {
193- return res , errStatus
194- }
195-
196- return ctrl.Result {}, nil
197- }
198-
199136func getEventGatewayForRef (
200137 ctx context.Context ,
201138 cl client.Client ,
0 commit comments