|
| 1 | +package envtest |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + corev1 "k8s.io/api/core/v1" |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + "k8s.io/apimachinery/pkg/types" |
| 11 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 12 | + gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 13 | + |
| 14 | + kogateway "github.com/kong/kong-operator/controller/gateway" |
| 15 | + certhelper "github.com/kong/kong-operator/ingress-controller/test/helpers/certificate" |
| 16 | + "github.com/kong/kong-operator/modules/manager/logging" |
| 17 | + managerscheme "github.com/kong/kong-operator/modules/manager/scheme" |
| 18 | + "github.com/kong/kong-operator/pkg/vars" |
| 19 | +) |
| 20 | + |
| 21 | +func TestGatewaySecretWatch_UpdatesResolvedRefsOnSecretRotation(t *testing.T) { |
| 22 | + t.Parallel() |
| 23 | + |
| 24 | + // Prepare scheme, envtest, manager and KO Gateway reconciler. |
| 25 | + scheme := managerscheme.Get() |
| 26 | + ctx := t.Context() |
| 27 | + |
| 28 | + cfg, ns := Setup(t, ctx, scheme) |
| 29 | + mgr, logs := NewManager(t, ctx, cfg, scheme) |
| 30 | + |
| 31 | + r := &kogateway.Reconciler{ |
| 32 | + Client: mgr.GetClient(), |
| 33 | + CacheSyncTimeout: 30 * time.Second, |
| 34 | + Scheme: scheme, |
| 35 | + Namespace: ns.Name, |
| 36 | + DefaultDataPlaneImage: "kong:latest", |
| 37 | + LoggingMode: logging.DevelopmentMode, |
| 38 | + } |
| 39 | + StartReconcilers(ctx, t, mgr, logs, r) |
| 40 | + |
| 41 | + c := mgr.GetClient() |
| 42 | + |
| 43 | + // Create a GatewayClass accepted by the controller. |
| 44 | + gc := &gatewayv1.GatewayClass{ |
| 45 | + ObjectMeta: metav1.ObjectMeta{Name: "gc-ko"}, |
| 46 | + Spec: gatewayv1.GatewayClassSpec{ |
| 47 | + ControllerName: gatewayv1.GatewayController(vars.ControllerName()), |
| 48 | + }, |
| 49 | + } |
| 50 | + require.NoError(t, c.Create(ctx, gc)) |
| 51 | + |
| 52 | + // Patch status Accepted=True for GatewayClass so KO Gateway controller processes Gateways. |
| 53 | + require.Eventually(t, func() bool { |
| 54 | + var cur gatewayv1.GatewayClass |
| 55 | + if err := c.Get(ctx, types.NamespacedName{Name: gc.Name}, &cur); err != nil { |
| 56 | + return false |
| 57 | + } |
| 58 | + cond := metav1.Condition{ |
| 59 | + Type: string(gatewayv1.GatewayClassConditionStatusAccepted), |
| 60 | + Status: metav1.ConditionTrue, |
| 61 | + Reason: string(gatewayv1.GatewayClassReasonAccepted), |
| 62 | + ObservedGeneration: cur.Generation, |
| 63 | + LastTransitionTime: metav1.Now(), |
| 64 | + } |
| 65 | + // Replace existing condition if present; otherwise append. |
| 66 | + updated := false |
| 67 | + for i := range cur.Status.Conditions { |
| 68 | + if cur.Status.Conditions[i].Type == cond.Type { |
| 69 | + cur.Status.Conditions[i] = cond |
| 70 | + updated = true |
| 71 | + break |
| 72 | + } |
| 73 | + } |
| 74 | + if !updated { |
| 75 | + cur.Status.Conditions = append(cur.Status.Conditions, cond) |
| 76 | + } |
| 77 | + if err := c.Status().Update(ctx, &cur); err != nil { |
| 78 | + return false |
| 79 | + } |
| 80 | + return true |
| 81 | + }, 10*time.Second, 200*time.Millisecond) |
| 82 | + |
| 83 | + // Create an initial INVALID TLS Secret referenced by the Gateway listener. |
| 84 | + secretName := "test-cert" |
| 85 | + bad := &corev1.Secret{ |
| 86 | + ObjectMeta: metav1.ObjectMeta{Namespace: ns.Name, Name: secretName}, |
| 87 | + Type: corev1.SecretTypeTLS, |
| 88 | + Data: map[string][]byte{ |
| 89 | + corev1.TLSCertKey: []byte("not-a-cert"), |
| 90 | + corev1.TLSPrivateKeyKey: []byte("not-a-key"), |
| 91 | + }, |
| 92 | + } |
| 93 | + require.NoError(t, c.Create(ctx, bad)) |
| 94 | + |
| 95 | + // Create a Gateway with a TLS listener referencing the Secret. |
| 96 | + gw := &gatewayv1.Gateway{ |
| 97 | + ObjectMeta: metav1.ObjectMeta{Namespace: ns.Name, Name: "gw"}, |
| 98 | + Spec: gatewayv1.GatewaySpec{ |
| 99 | + GatewayClassName: gatewayv1.ObjectName(gc.Name), |
| 100 | + Listeners: []gatewayv1.Listener{ |
| 101 | + { |
| 102 | + Name: "https", |
| 103 | + Port: 443, |
| 104 | + Protocol: gatewayv1.HTTPSProtocolType, |
| 105 | + TLS: &gatewayv1.GatewayTLSConfig{ |
| 106 | + Mode: gatewayTLSModePtr(gatewayv1.TLSModeTerminate), |
| 107 | + CertificateRefs: []gatewayv1.SecretObjectReference{{ |
| 108 | + Name: gatewayv1.ObjectName(secretName), |
| 109 | + }}, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + } |
| 115 | + require.NoError(t, c.Create(ctx, gw)) |
| 116 | + |
| 117 | + // Initially, the invalid Secret should result in ResolvedRefs=False (InvalidCertificateRef). |
| 118 | + require.Eventually(t, func() bool { |
| 119 | + var cur gatewayv1.Gateway |
| 120 | + if err := c.Get(ctx, types.NamespacedName{Namespace: ns.Name, Name: gw.Name}, &cur); err != nil { |
| 121 | + return false |
| 122 | + } |
| 123 | + if len(cur.Status.Listeners) == 0 { |
| 124 | + return false |
| 125 | + } |
| 126 | + for _, ls := range cur.Status.Listeners { |
| 127 | + for _, cond := range ls.Conditions { |
| 128 | + if cond.Type == string(gatewayv1.ListenerConditionResolvedRefs) && cond.Status == metav1.ConditionFalse { |
| 129 | + return true |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + return false |
| 134 | + }, 30*time.Second, 300*time.Millisecond) |
| 135 | + |
| 136 | + // Now rotate the Secret with a valid TLS certificate and key. |
| 137 | + certPEM, keyPEM := certhelper.MustGenerateCertPEMFormat() |
| 138 | + require.NoError(t, c.Patch(ctx, &corev1.Secret{ |
| 139 | + ObjectMeta: metav1.ObjectMeta{Namespace: ns.Name, Name: secretName}, |
| 140 | + Type: corev1.SecretTypeTLS, |
| 141 | + Data: map[string][]byte{ |
| 142 | + corev1.TLSCertKey: certPEM, |
| 143 | + corev1.TLSPrivateKeyKey: keyPEM, |
| 144 | + }, |
| 145 | + }, client.MergeFrom(bad))) |
| 146 | + |
| 147 | + // After rotation, the Secret watch should enqueue the Gateway and ResolvedRefs should become True. |
| 148 | + require.Eventually(t, func() bool { |
| 149 | + var cur gatewayv1.Gateway |
| 150 | + if err := c.Get(ctx, types.NamespacedName{Namespace: ns.Name, Name: gw.Name}, &cur); err != nil { |
| 151 | + return false |
| 152 | + } |
| 153 | + if len(cur.Status.Listeners) == 0 { |
| 154 | + return false |
| 155 | + } |
| 156 | + for _, ls := range cur.Status.Listeners { |
| 157 | + for _, cond := range ls.Conditions { |
| 158 | + if cond.Type == string(gatewayv1.ListenerConditionResolvedRefs) && cond.Status == metav1.ConditionTrue { |
| 159 | + return true |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + return false |
| 164 | + }, 60*time.Second, 500*time.Millisecond) |
| 165 | +} |
| 166 | + |
| 167 | +func gatewayTLSModePtr(m gatewayv1.TLSModeType) *gatewayv1.TLSModeType { return &m } |
0 commit comments