Skip to content

Commit dcf0484

Browse files
AlinsRandspo
authored andcommitted
feat: add synchronization status to CRD (#2460)
(cherry picked from commit 1e1b8bc)
1 parent 9c94283 commit dcf0484

File tree

18 files changed

+989
-114
lines changed

18 files changed

+989
-114
lines changed

api/adc/types.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ const (
7777
PassHostRewrite = "rewrite"
7878
)
7979

80+
const (
81+
TypeRoute = "route"
82+
TypeService = "service"
83+
TypeConsumer = "consumer"
84+
TypeSSL = "ssl"
85+
TypeGlobalRule = "global_rule"
86+
TypePluginMetadata = "plugin_metadata"
87+
)
88+
8089
type Object interface {
8190
GetLabels() map[string]string
8291
}

internal/controller/apisixroute_controller.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232
networkingv1 "k8s.io/api/networking/v1"
3333
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3434
"k8s.io/apimachinery/pkg/runtime"
35-
"k8s.io/apimachinery/pkg/types"
35+
k8stypes "k8s.io/apimachinery/pkg/types"
3636
ctrl "sigs.k8s.io/controller-runtime"
3737
"sigs.k8s.io/controller-runtime/pkg/builder"
3838
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -45,6 +45,7 @@ import (
4545
"github.com/apache/apisix-ingress-controller/internal/controller/indexer"
4646
"github.com/apache/apisix-ingress-controller/internal/controller/status"
4747
"github.com/apache/apisix-ingress-controller/internal/provider"
48+
"github.com/apache/apisix-ingress-controller/internal/types"
4849
"github.com/apache/apisix-ingress-controller/internal/utils"
4950
pkgutils "github.com/apache/apisix-ingress-controller/pkg/utils"
5051
)
@@ -134,7 +135,7 @@ func (r *ApisixRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request)
134135
return ctrl.Result{}, err
135136
}
136137
if err = r.Provider.Update(ctx, tctx, &ar); err != nil {
137-
err = ReasonError{
138+
err = types.ReasonError{
138139
Reason: string(apiv2.ConditionReasonSyncFailed),
139140
Message: err.Error(),
140141
}
@@ -152,7 +153,7 @@ func (r *ApisixRouteReconciler) processApisixRoute(ctx context.Context, tc *prov
152153
for httpIndex, http := range in.Spec.HTTP {
153154
// check rule names
154155
if _, ok := rules[http.Name]; ok {
155-
return ReasonError{
156+
return types.ReasonError{
156157
Reason: string(apiv2.ConditionReasonInvalidSpec),
157158
Message: "duplicate route rule name",
158159
}
@@ -178,15 +179,15 @@ func (r *ApisixRouteReconciler) processApisixRoute(ctx context.Context, tc *prov
178179

179180
// check vars
180181
if _, err := http.Match.NginxVars.ToVars(); err != nil {
181-
return ReasonError{
182+
return types.ReasonError{
182183
Reason: string(apiv2.ConditionReasonInvalidSpec),
183184
Message: fmt.Sprintf(".spec.http[%d].match.exprs: %s", httpIndex, err.Error()),
184185
}
185186
}
186187

187188
// validate remote address
188189
if err := utils.ValidateRemoteAddrs(http.Match.RemoteAddrs); err != nil {
189-
return ReasonError{
190+
return types.ReasonError{
190191
Reason: string(apiv2.ConditionReasonInvalidSpec),
191192
Message: fmt.Sprintf(".spec.http[%d].match.remoteAddrs: %s", httpIndex, err.Error()),
192193
}
@@ -220,7 +221,7 @@ func (r *ApisixRouteReconciler) validatePluginConfig(ctx context.Context, tc *pr
220221
pcNN = utils.NamespacedName(&pc)
221222
)
222223
if err := r.Get(ctx, pcNN, &pc); err != nil {
223-
return ReasonError{
224+
return types.ReasonError{
224225
Reason: string(apiv2.ConditionReasonInvalidSpec),
225226
Message: fmt.Sprintf("failed to get ApisixPluginConfig: %s", pcNN),
226227
}
@@ -230,13 +231,13 @@ func (r *ApisixRouteReconciler) validatePluginConfig(ctx context.Context, tc *pr
230231
if in.Spec.IngressClassName != pc.Spec.IngressClassName && pc.Spec.IngressClassName != "" {
231232
var pcIC networkingv1.IngressClass
232233
if err := r.Get(ctx, client.ObjectKey{Name: pc.Spec.IngressClassName}, &pcIC); err != nil {
233-
return ReasonError{
234+
return types.ReasonError{
234235
Reason: string(apiv2.ConditionReasonInvalidSpec),
235236
Message: fmt.Sprintf("failed to get IngressClass %s for ApisixPluginConfig %s: %v", pc.Spec.IngressClassName, pcNN, err),
236237
}
237238
}
238239
if !matchesController(pcIC.Spec.Controller) {
239-
return ReasonError{
240+
return types.ReasonError{
240241
Reason: string(apiv2.ConditionReasonInvalidSpec),
241242
Message: fmt.Sprintf("ApisixPluginConfig %s references IngressClass %s with non-matching controller", pcNN, pc.Spec.IngressClassName),
242243
}
@@ -271,7 +272,7 @@ func (r *ApisixRouteReconciler) validateSecrets(ctx context.Context, tc *provide
271272
secretNN = utils.NamespacedName(&secret)
272273
)
273274
if err := r.Get(ctx, secretNN, &secret); err != nil {
274-
return ReasonError{
275+
return types.ReasonError{
275276
Reason: string(apiv2.ConditionReasonInvalidSpec),
276277
Message: fmt.Sprintf("failed to get Secret: %s", secretNN),
277278
}
@@ -282,18 +283,18 @@ func (r *ApisixRouteReconciler) validateSecrets(ctx context.Context, tc *provide
282283
}
283284

284285
func (r *ApisixRouteReconciler) validateBackends(ctx context.Context, tc *provider.TranslateContext, in *apiv2.ApisixRoute, http apiv2.ApisixRouteHTTP) error {
285-
var backends = make(map[types.NamespacedName]struct{})
286+
var backends = make(map[k8stypes.NamespacedName]struct{})
286287
for _, backend := range http.Backends {
287288
var (
288289
au apiv2.ApisixUpstream
289290
service corev1.Service
290-
serviceNN = types.NamespacedName{
291+
serviceNN = k8stypes.NamespacedName{
291292
Namespace: in.GetNamespace(),
292293
Name: backend.ServiceName,
293294
}
294295
)
295296
if _, ok := backends[serviceNN]; ok {
296-
return ReasonError{
297+
return types.ReasonError{
297298
Reason: string(apiv2.ConditionReasonInvalidSpec),
298299
Message: fmt.Sprintf("duplicate backend service: %s", serviceNN),
299300
}
@@ -344,7 +345,7 @@ func (r *ApisixRouteReconciler) validateBackends(ctx context.Context, tc *provid
344345
discoveryv1.LabelServiceName: service.Name,
345346
},
346347
); err != nil {
347-
return ReasonError{
348+
return types.ReasonError{
348349
Reason: string(apiv2.ConditionReasonInvalidSpec),
349350
Message: fmt.Sprintf("failed to list endpoint slices: %v", err),
350351
}
@@ -366,7 +367,7 @@ func (r *ApisixRouteReconciler) validateUpstreams(ctx context.Context, tc *provi
366367
}
367368
var (
368369
ups apiv2.ApisixUpstream
369-
upsNN = types.NamespacedName{
370+
upsNN = k8stypes.NamespacedName{
370371
Namespace: ar.GetNamespace(),
371372
Name: upstream.Name,
372373
}
@@ -384,7 +385,7 @@ func (r *ApisixRouteReconciler) validateUpstreams(ctx context.Context, tc *provi
384385
if node.Type == apiv2.ExternalTypeService {
385386
var (
386387
service corev1.Service
387-
serviceNN = types.NamespacedName{Namespace: ups.GetNamespace(), Name: node.Name}
388+
serviceNN = k8stypes.NamespacedName{Namespace: ups.GetNamespace(), Name: node.Name}
388389
)
389390
if err := r.Get(ctx, serviceNN, &service); err != nil {
390391
r.Log.Error(err, "failed to get service in ApisixUpstream", "ApisixUpstream", upsNN, "Service", serviceNN)
@@ -400,7 +401,7 @@ func (r *ApisixRouteReconciler) validateUpstreams(ctx context.Context, tc *provi
400401
if ups.Spec.TLSSecret != nil && ups.Spec.TLSSecret.Name != "" {
401402
var (
402403
secret corev1.Secret
403-
secretNN = types.NamespacedName{Namespace: cmp.Or(ups.Spec.TLSSecret.Namespace, ar.GetNamespace()), Name: ups.Spec.TLSSecret.Name}
404+
secretNN = k8stypes.NamespacedName{Namespace: cmp.Or(ups.Spec.TLSSecret.Namespace, ar.GetNamespace()), Name: ups.Spec.TLSSecret.Name}
404405
)
405406
if err := r.Get(ctx, secretNN, &secret); err != nil {
406407
r.Log.Error(err, "failed to get secret in ApisixUpstream", "ApisixUpstream", upsNN, "Secret", secretNN)
@@ -578,7 +579,7 @@ func (r *ApisixRouteReconciler) listApisixRoutesForPluginConfig(ctx context.Cont
578579
return pkgutils.DedupComparable(requests)
579580
}
580581

581-
func (r *ApisixRouteReconciler) getSubsetLabels(tctx *provider.TranslateContext, auNN types.NamespacedName, backend apiv2.ApisixRouteHTTPBackend) map[string]string {
582+
func (r *ApisixRouteReconciler) getSubsetLabels(tctx *provider.TranslateContext, auNN k8stypes.NamespacedName, backend apiv2.ApisixRouteHTTPBackend) map[string]string {
582583
if backend.Subset == "" {
583584
return nil
584585
}
@@ -621,7 +622,7 @@ func (r *ApisixRouteReconciler) filterEndpointSliceByTargetPod(ctx context.Conte
621622

622623
var (
623624
pod corev1.Pod
624-
podNN = types.NamespacedName{
625+
podNN = k8stypes.NamespacedName{
625626
Namespace: v.TargetRef.Namespace,
626627
Name: v.TargetRef.Name,
627628
}

internal/controller/httproute_controller.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
discoveryv1 "k8s.io/api/discovery/v1"
3232
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3333
"k8s.io/apimachinery/pkg/runtime"
34-
"k8s.io/apimachinery/pkg/types"
34+
k8stypes "k8s.io/apimachinery/pkg/types"
3535
"k8s.io/utils/ptr"
3636
ctrl "sigs.k8s.io/controller-runtime"
3737
"sigs.k8s.io/controller-runtime/pkg/builder"
@@ -49,6 +49,7 @@ import (
4949
"github.com/apache/apisix-ingress-controller/internal/controller/indexer"
5050
"github.com/apache/apisix-ingress-controller/internal/controller/status"
5151
"github.com/apache/apisix-ingress-controller/internal/provider"
52+
"github.com/apache/apisix-ingress-controller/internal/types"
5253
"github.com/apache/apisix-ingress-controller/internal/utils"
5354
)
5455

@@ -186,7 +187,7 @@ func (r *HTTPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
186187
var backendRefErr error
187188
if err := r.processHTTPRoute(tctx, hr); err != nil {
188189
// When encountering a backend reference error, it should not affect the acceptance status
189-
if IsSomeReasonError(err, gatewayv1.RouteReasonInvalidKind) {
190+
if types.IsSomeReasonError(err, gatewayv1.RouteReasonInvalidKind) {
190191
backendRefErr = err
191192
} else {
192193
acceptStatus.status = false
@@ -340,10 +341,10 @@ func (r *HTTPRouteReconciler) listHTTPRoutesForBackendTrafficPolicy(ctx context.
340341
}
341342
httprouteList = append(httprouteList, hrList.Items...)
342343
}
343-
var namespacedNameMap = make(map[types.NamespacedName]struct{})
344+
var namespacedNameMap = make(map[k8stypes.NamespacedName]struct{})
344345
requests := make([]reconcile.Request, 0, len(httprouteList))
345346
for _, hr := range httprouteList {
346-
key := types.NamespacedName{
347+
key := k8stypes.NamespacedName{
347348
Namespace: hr.Namespace,
348349
Name: hr.Name,
349350
}
@@ -391,12 +392,12 @@ func (r *HTTPRouteReconciler) listHTTPRouteByHTTPRoutePolicy(ctx context.Context
391392
return nil
392393
}
393394

394-
var keys = make(map[types.NamespacedName]struct{})
395+
var keys = make(map[k8stypes.NamespacedName]struct{})
395396
for _, ref := range httpRoutePolicy.Spec.TargetRefs {
396397
if ref.Kind != "HTTPRoute" {
397398
continue
398399
}
399-
key := types.NamespacedName{
400+
key := k8stypes.NamespacedName{
400401
Namespace: obj.GetNamespace(),
401402
Name: string(ref.Name),
402403
}
@@ -441,10 +442,10 @@ func (r *HTTPRouteReconciler) listHTTPRouteForGenericEvent(ctx context.Context,
441442
}
442443
}
443444

444-
func (r *HTTPRouteReconciler) processHTTPRouteBackendRefs(tctx *provider.TranslateContext, hrNN types.NamespacedName) error {
445+
func (r *HTTPRouteReconciler) processHTTPRouteBackendRefs(tctx *provider.TranslateContext, hrNN k8stypes.NamespacedName) error {
445446
var terr error
446447
for _, backend := range tctx.BackendRefs {
447-
targetNN := types.NamespacedName{
448+
targetNN := k8stypes.NamespacedName{
448449
Namespace: hrNN.Namespace,
449450
Name: string(backend.Name),
450451
}
@@ -453,7 +454,7 @@ func (r *HTTPRouteReconciler) processHTTPRouteBackendRefs(tctx *provider.Transla
453454
}
454455

455456
if backend.Kind != nil && *backend.Kind != "Service" {
456-
terr = newInvalidKindError(*backend.Kind)
457+
terr = types.NewInvalidKindError(*backend.Kind)
457458
continue
458459
}
459460

@@ -466,7 +467,7 @@ func (r *HTTPRouteReconciler) processHTTPRouteBackendRefs(tctx *provider.Transla
466467
if err := r.Get(tctx, targetNN, &service); err != nil {
467468
terr = err
468469
if client.IgnoreNotFound(err) == nil {
469-
terr = ReasonError{
470+
terr = types.ReasonError{
470471
Reason: string(gatewayv1.RouteReasonBackendNotFound),
471472
Message: fmt.Sprintf("Service %s not found", targetNN),
472473
}
@@ -490,7 +491,7 @@ func (r *HTTPRouteReconciler) processHTTPRouteBackendRefs(tctx *provider.Transla
490491
Namespace: (*gatewayv1.Namespace)(&targetNN.Namespace),
491492
},
492493
); !permitted {
493-
terr = ReasonError{
494+
terr = types.ReasonError{
494495
Reason: string(v1beta1.RouteReasonRefNotPermitted),
495496
Message: fmt.Sprintf("%s is in a different namespace than the HTTPRoute %s and no ReferenceGrant allowing reference is configured", targetNN, hrNN),
496497
}
@@ -549,15 +550,15 @@ func (r *HTTPRouteReconciler) processHTTPRoute(tctx *provider.TranslateContext,
549550
terror = err
550551
continue
551552
}
552-
tctx.PluginConfigs[types.NamespacedName{
553+
tctx.PluginConfigs[k8stypes.NamespacedName{
553554
Namespace: httpRoute.GetNamespace(),
554555
Name: string(filter.ExtensionRef.Name),
555556
}] = pluginconfig
556557
}
557558
}
558559
for _, backend := range rule.BackendRefs {
559560
if backend.Kind != nil && *backend.Kind != "Service" {
560-
terror = newInvalidKindError(*backend.Kind)
561+
terror = types.NewInvalidKindError(*backend.Kind)
561562
continue
562563
}
563564
tctx.BackendRefs = append(tctx.BackendRefs, gatewayv1.BackendRef{
@@ -659,7 +660,7 @@ func (r *HTTPRouteReconciler) listHTTPRoutesForReferenceGrant(ctx context.Contex
659660

660661
var httpRouteList gatewayv1.HTTPRouteList
661662
if err := r.List(ctx, &httpRouteList); err != nil {
662-
r.Log.Error(err, "failed to list httproutes for reference ReferenceGrant", "ReferenceGrant", types.NamespacedName{Namespace: obj.GetNamespace(), Name: obj.GetName()})
663+
r.Log.Error(err, "failed to list httproutes for reference ReferenceGrant", "ReferenceGrant", k8stypes.NamespacedName{Namespace: obj.GetNamespace(), Name: obj.GetName()})
663664
return nil
664665
}
665666

internal/controller/utils.go

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ func SetRouteConditionResolvedRefs(routeParentStatus *gatewayv1.RouteParentStatu
287287
condition.Status = metav1.ConditionFalse
288288
condition.Message = err.Error()
289289

290-
var re ReasonError
290+
var re types.ReasonError
291291
if errors.As(err, &re) {
292292
condition.Reason = re.Reason
293293
}
@@ -439,7 +439,7 @@ func SetApisixCRDConditionAccepted(status *apiv2.ApisixStatus, generation int64,
439439
condition.Reason = string(apiv2.ConditionReasonInvalidSpec)
440440
condition.Message = err.Error()
441441

442-
var re ReasonError
442+
var re types.ReasonError
443443
if errors.As(err, &re) {
444444
condition.Reason = re.Reason
445445
}
@@ -984,36 +984,6 @@ func FullTypeName(a any) string {
984984
return path.Join(path.Dir(pkgPath), name)
985985
}
986986

987-
type ReasonError struct {
988-
Reason string
989-
Message string
990-
}
991-
992-
func (e ReasonError) Error() string {
993-
return e.Message
994-
}
995-
996-
func IsSomeReasonError[Reason ~string](err error, reasons ...Reason) bool {
997-
if err == nil {
998-
return false
999-
}
1000-
var re ReasonError
1001-
if !errors.As(err, &re) {
1002-
return false
1003-
}
1004-
if len(reasons) == 0 {
1005-
return true
1006-
}
1007-
return slices.Contains(reasons, Reason(re.Reason))
1008-
}
1009-
1010-
func newInvalidKindError[Kind ~string](kind Kind) ReasonError {
1011-
return ReasonError{
1012-
Reason: string(gatewayv1.RouteReasonInvalidKind),
1013-
Message: fmt.Sprintf("Invalid kind %s, only Service is supported", kind),
1014-
}
1015-
}
1016-
1017987
// filterHostnames accepts a list of gateways and an HTTPRoute, and returns a copy of the HTTPRoute with only the hostnames that match the listener hostnames of the gateways.
1018988
// If the HTTPRoute hostnames do not intersect with the listener hostnames of the gateways, it returns an ErrNoMatchingListenerHostname error.
1019989
func filterHostnames(gateways []RouteParentRefContext, httpRoute *gatewayv1.HTTPRoute) (*gatewayv1.HTTPRoute, error) {

0 commit comments

Comments
 (0)