Skip to content

Commit a38f729

Browse files
committed
fix: r
Signed-off-by: ashing <[email protected]>
1 parent 79638ff commit a38f729

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

internal/controller/ingress_controller.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (r *IngressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
7575
ingress.Name = req.Name
7676

7777
ingress.TypeMeta = metav1.TypeMeta{
78-
Kind: "Ingress",
78+
Kind: KindIngress,
7979
APIVersion: networkingv1.SchemeGroupVersion.String(),
8080
}
8181

@@ -84,8 +84,9 @@ func (r *IngressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
8484
return ctrl.Result{}, err
8585
}
8686
r.Log.Info("deleted ingress resources", "ingress", ingress.Name)
87+
return ctrl.Result{}, nil
8788
}
88-
return ctrl.Result{}, client.IgnoreNotFound(err)
89+
return ctrl.Result{}, err
8990
}
9091

9192
r.Log.Info("reconciling ingress", "ingress", ingress.Name)
@@ -128,11 +129,7 @@ func (r *IngressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
128129

129130
// checkIngressClass check if the ingress uses the ingress class that we control
130131
func (r *IngressReconciler) checkIngressClass(obj client.Object) bool {
131-
ingress, ok := obj.(*networkingv1.Ingress)
132-
if !ok {
133-
r.Log.Error(fmt.Errorf("unexpected object type"), "failed to convert object to Ingress")
134-
return false
135-
}
132+
ingress := obj.(*networkingv1.Ingress)
136133

137134
if ingress.Spec.IngressClassName == nil {
138135
// handle the case where IngressClassName is not specified
@@ -145,10 +142,8 @@ func (r *IngressReconciler) checkIngressClass(obj client.Object) bool {
145142

146143
// find the ingress class that is marked as default
147144
for _, ic := range ingressClassList.Items {
148-
if ic.Annotations["ingressclass.kubernetes.io/is-default-class"] == "true" {
149-
if matchesController(ic.Spec.Controller) {
150-
return true
151-
}
145+
if IsDefaultIngressClass(&ic) && matchesController(ic.Spec.Controller) {
146+
return true
152147
}
153148
}
154149

@@ -162,8 +157,8 @@ func (r *IngressReconciler) checkIngressClass(obj client.Object) bool {
162157
}
163158

164159
// if it does not match, check if the ingress class is controlled by us
165-
ingressClass := &networkingv1.IngressClass{}
166-
if err := r.Client.Get(context.Background(), client.ObjectKey{Name: *ingress.Spec.IngressClassName}, ingressClass); err != nil {
160+
ingressClass := networkingv1.IngressClass{}
161+
if err := r.Client.Get(context.Background(), client.ObjectKey{Name: *ingress.Spec.IngressClassName}, &ingressClass); err != nil {
167162
r.Log.Error(err, "failed to get ingress class", "ingress", ingress.GetName(), "ingressclass", *ingress.Spec.IngressClassName)
168163
return false
169164
}
@@ -279,7 +274,6 @@ func (r *IngressReconciler) processTLS(ctx context.Context, tctx *provider.Trans
279274
Name: tls.SecretName,
280275
}, &secret); err != nil {
281276
log.Error(err, "failed to get secret", "namespace", ingress.Namespace, "name", tls.SecretName)
282-
// todo: set the ingress condition to false
283277
return err
284278
}
285279

internal/controller/utils.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/api7/api7-ingress-controller/internal/controller/config"
99
"github.com/samber/lo"
1010
corev1 "k8s.io/api/core/v1"
11+
networkingv1 "k8s.io/api/networking/v1"
1112
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1213
"k8s.io/apimachinery/pkg/labels"
1314
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -19,8 +20,19 @@ const (
1920
KindGateway = "Gateway"
2021
KindHTTPRoute = "HTTPRoute"
2122
KindGatewayClass = "GatewayClass"
23+
KindIngress = "Ingress"
2224
)
2325

26+
const defaultIngressClassAnnotation = "ingressclass.kubernetes.io/is-default-class"
27+
28+
// IsDefaultIngressClass returns whether an IngressClass is the default IngressClass.
29+
func IsDefaultIngressClass(obj client.Object) bool {
30+
if ingressClass, ok := obj.(*networkingv1.IngressClass); ok {
31+
return ingressClass.Annotations[defaultIngressClassAnnotation] == "true"
32+
}
33+
return false
34+
}
35+
2436
func acceptedMessage(kind string) string {
2537
return fmt.Sprintf("the %s has been accepted by the api7-ingress-controller", kind)
2638
}

0 commit comments

Comments
 (0)