Skip to content

Commit 38558c3

Browse files
committed
add test case
1 parent 10fbf27 commit 38558c3

File tree

5 files changed

+25
-22
lines changed

5 files changed

+25
-22
lines changed

internal/controller/consumer_controller.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (r *ConsumerReconciler) listConsumersForGateway(ctx context.Context, obj cl
7575
r.Log.Error(err, "failed to list consumers")
7676
return nil
7777
}
78-
var requests []reconcile.Request
78+
requests := make([]reconcile.Request, 0, len(consumerList.Items))
7979
for _, consumer := range consumerList.Items {
8080
requests = append(requests, reconcile.Request{
8181
NamespacedName: client.ObjectKey{
@@ -100,12 +100,10 @@ func (r *ConsumerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
100100
}
101101

102102
if err := r.Provider.Delete(ctx, consumer); err != nil {
103-
r.Log.Error(err, "failed to delete consumer", "consumer", consumer)
104103
return ctrl.Result{}, err
105104
}
106105
return ctrl.Result{}, nil
107106
}
108-
r.Log.Error(err, "failed to get consumer", "consumer", consumer)
109107
return ctrl.Result{}, err
110108
}
111109

@@ -123,23 +121,20 @@ func (r *ConsumerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
123121
}
124122

125123
if err := r.updateStatus(ctx, consumer, statusErr); err != nil {
126-
r.Log.Error(err, "failed to update consumer status", "consumer", consumer)
127124
return ctrl.Result{}, err
128125
}
129126

130127
return ctrl.Result{}, nil
131128
}
132129

133130
func (r *ConsumerReconciler) processSepc(ctx context.Context, tctx *provider.TranslateContext, consumer *v1alpha1.Consumer) error {
134-
r.Log.Info("Processing consumer", "name", consumer.Name, "namespace", consumer.Namespace)
135-
136131
for _, credential := range consumer.Spec.Credentials {
137132
if credential.SecretRef == nil {
138133
continue
139134
}
140135
ns := consumer.GetNamespace()
141136
if credential.SecretRef.Namespace != nil {
142-
ns = string(*credential.SecretRef.Namespace)
137+
ns = *credential.SecretRef.Namespace
143138
}
144139
secret := corev1.Secret{}
145140
if err := r.Get(ctx, client.ObjectKey{
@@ -194,7 +189,7 @@ func (r *ConsumerReconciler) checkGatewayRef(object client.Object) bool {
194189
}
195190
ns := consumer.GetNamespace()
196191
if consumer.Spec.GatewayRef.Namespace != nil {
197-
ns = string(*consumer.Spec.GatewayRef.Namespace)
192+
ns = *consumer.Spec.GatewayRef.Namespace
198193
}
199194
gateway := &gatewayv1.Gateway{}
200195
if err := r.Get(context.TODO(), client.ObjectKey{
@@ -204,5 +199,10 @@ func (r *ConsumerReconciler) checkGatewayRef(object client.Object) bool {
204199
r.Log.Error(err, "failed to get gateway", "gateway", consumer.Spec.GatewayRef.Name)
205200
return false
206201
}
207-
return true
202+
gatewayClass := &gatewayv1.GatewayClass{}
203+
if err := r.Client.Get(context.Background(), client.ObjectKey{Name: string(gateway.Spec.GatewayClassName)}, gatewayClass); err != nil {
204+
r.Log.Error(err, "failed to get gateway class", "gateway", gateway.GetName(), "gatewayclass", gateway.Spec.GatewayClassName)
205+
return false
206+
}
207+
return matchesController(string(gatewayClass.Spec.ControllerName))
208208
}

internal/controller/indexer/indexer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func ConsumerGatewayRefIndexFunc(rawObj client.Object) []string {
6969

7070
ns := consumer.GetNamespace()
7171
if consumer.Spec.GatewayRef.Namespace != nil {
72-
ns = string(*consumer.Spec.GatewayRef.Namespace)
72+
ns = *consumer.Spec.GatewayRef.Namespace
7373
}
7474
return []string{GenIndexKey(ns, consumer.Spec.GatewayRef.Name)}
7575
}

internal/controller/status.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,20 @@ const (
1010
ConditionTypeProgressing string = "Progressing"
1111
ConditionTypeDegraded string = "Degraded"
1212

13-
ConditionReasonSynced string = "ResourceSynced"
13+
ConditionReasonSynced string = "ResourceSynced"
14+
ConditionReasonSyncAbort string = "ResourceSyncAbort"
1415
)
1516

1617
func NewCondition(observedGeneration int64, status bool, message string) metav1.Condition {
1718
condition := metav1.ConditionTrue
19+
reason := ConditionReasonSynced
1820
if !status {
1921
condition = metav1.ConditionFalse
22+
reason = ConditionReasonSyncAbort
2023
}
2124
return metav1.Condition{
2225
Type: ConditionTypeAvailable,
23-
Reason: ConditionReasonSynced,
26+
Reason: reason,
2427
Status: condition,
2528
Message: message,
2629
ObservedGeneration: observedGeneration,

internal/provider/adc/translator/plugins.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

test/e2e/crds/consumer.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,19 @@ spec:
7878
var beforeEachHTTP = func() {
7979
By("create GatewayClass")
8080
gatewayClassName := fmt.Sprintf("api7-%d", time.Now().Unix())
81-
err := s.CreateResourceFromStringWithNamespace(fmt.Sprintf(defautlGatewayClass, gatewayClassName, s.GetControllerName()), "")
81+
gatewayString := fmt.Sprintf(defautlGatewayClass, gatewayClassName, s.GetControllerName())
82+
err := s.CreateResourceFromStringWithNamespace(gatewayString, "")
8283
Expect(err).NotTo(HaveOccurred(), "creating GatewayClass")
8384
time.Sleep(5 * time.Second)
8485

8586
By("check GatewayClass condition")
8687
gcyaml, err := s.GetResourceYaml("GatewayClass", gatewayClassName)
8788
Expect(err).NotTo(HaveOccurred(), "getting GatewayClass yaml")
8889
Expect(gcyaml).To(ContainSubstring(`status: "True"`), "checking GatewayClass condition status")
89-
Expect(gcyaml).To(ContainSubstring("message: the gatewayclass has been accepted by the api7-ingress-controller"), "checking GatewayClass condition message")
90+
Expect(gcyaml).To(
91+
ContainSubstring("message: the gatewayclass has been accepted by the api7-ingress-controller"),
92+
"checking GatewayClass condition message",
93+
)
9094

9195
By("create Gateway")
9296
err = s.CreateResourceFromString(fmt.Sprintf(defautlGateway, gatewayClassName))
@@ -97,7 +101,10 @@ spec:
97101
gwyaml, err := s.GetResourceYaml("Gateway", "api7ee")
98102
Expect(err).NotTo(HaveOccurred(), "getting Gateway yaml")
99103
Expect(gwyaml).To(ContainSubstring(`status: "True"`), "checking Gateway condition status")
100-
Expect(gwyaml).To(ContainSubstring("message: the gateway has been accepted by the api7-ingress-controller"), "checking Gateway condition message")
104+
Expect(gwyaml).To(
105+
ContainSubstring("message: the gateway has been accepted by the api7-ingress-controller"),
106+
"checking Gateway condition message",
107+
)
101108

102109
s.ResourceApplied("httproute", "httpbin", defaultHTTPRoute, 1)
103110
}

0 commit comments

Comments
 (0)