@@ -10,6 +10,7 @@ import (
1010 apierrors "k8s.io/apimachinery/pkg/api/errors"
1111 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1212 "k8s.io/apimachinery/pkg/runtime"
13+ "k8s.io/apimachinery/pkg/runtime/schema"
1314 "sigs.k8s.io/controller-runtime/pkg/client"
1415 "sigs.k8s.io/controller-runtime/pkg/client/fake"
1516
@@ -52,6 +53,29 @@ func newRegistrar(t *testing.T, now time.Time, objs ...client.Object) (*RelayReg
5253 return r , c
5354}
5455
56+ type deleteLeaseOnUpdateClient struct {
57+ client.Client
58+ deleteOnNextUpdate bool
59+ }
60+
61+ func (c * deleteLeaseOnUpdateClient ) Update (ctx context.Context , obj client.Object , opts ... client.UpdateOption ) error {
62+ if _ , ok := obj .(* apoxycoordv1.Lease ); ok && c .deleteOnNextUpdate {
63+ c .deleteOnNextUpdate = false
64+ lease := & apoxycoordv1.Lease {ObjectMeta : metav1.ObjectMeta {
65+ Namespace : obj .GetNamespace (),
66+ Name : obj .GetName (),
67+ }}
68+ if err := c .Client .Delete (ctx , lease ); err != nil && ! apierrors .IsNotFound (err ) {
69+ return err
70+ }
71+ return apierrors .NewNotFound (schema.GroupResource {
72+ Group : apoxycoordv1 .GroupName ,
73+ Resource : "leases" ,
74+ }, obj .GetName ())
75+ }
76+ return c .Client .Update (ctx , obj , opts ... )
77+ }
78+
5579func TestRelayRegistrarEnsureRelay (t * testing.T ) {
5680 ctx := context .Background ()
5781 now := time .Unix (1_700_000_000 , 0 )
@@ -110,6 +134,52 @@ func TestRelayRegistrarRenewLease(t *testing.T) {
110134 })
111135}
112136
137+ func TestRelayRegistrarRecoversAfterRestoredObjectsDisappear (t * testing.T ) {
138+ ctx := context .Background ()
139+ now := time .Unix (1_700_000_000 , 0 )
140+ lease := & apoxycoordv1.Lease {
141+ ObjectMeta : metav1.ObjectMeta {Namespace : DefaultLeaseNamespace , Name : LeaseName ("r0" )},
142+ }
143+ _ , base := newRegistrar (t , now , lease )
144+ leaseClient := & deleteLeaseOnUpdateClient {Client : base , deleteOnNextUpdate : true }
145+ r := NewRelayRegistrar (leaseClient , base , stubRelay {name : "r0" }, []string {"1.2.3.4:6081" }, nil )
146+ r .now = func () time.Time { return now }
147+
148+ require .NoError (t , r .renewLease (ctx ))
149+
150+ var gotRelay vpcv1alpha1.Relay
151+ require .NoError (t , base .Get (ctx , client.ObjectKey {Name : "r0" }, & gotRelay ))
152+ var gotLease apoxycoordv1.Lease
153+ require .NoError (t , base .Get (ctx , client.ObjectKey {
154+ Namespace : DefaultLeaseNamespace ,
155+ Name : LeaseName ("r0" ),
156+ }, & gotLease ))
157+ require .Equal (t , now .Unix (), gotLease .Spec .RenewTime .Unix ())
158+ }
159+
160+ func TestRelayRegistrarRecreatesRelayDuringRenewal (t * testing.T ) {
161+ r , c := newRegistrar (t , time .Unix (1_700_000_000 , 0 ))
162+ r .renewInterval = 10 * time .Millisecond
163+ ctx , cancel := context .WithCancel (context .Background ())
164+ done := make (chan error , 1 )
165+ go func () {
166+ done <- r .Start (ctx )
167+ }()
168+
169+ require .Eventually (t , func () bool {
170+ return c .Get (context .Background (), client.ObjectKey {Name : "r0" }, & vpcv1alpha1.Relay {}) == nil
171+ }, time .Second , 10 * time .Millisecond )
172+ require .NoError (t , c .Delete (context .Background (), & vpcv1alpha1.Relay {
173+ ObjectMeta : metav1.ObjectMeta {Name : "r0" },
174+ }))
175+ require .Eventually (t , func () bool {
176+ return c .Get (context .Background (), client.ObjectKey {Name : "r0" }, & vpcv1alpha1.Relay {}) == nil
177+ }, time .Second , 10 * time .Millisecond )
178+
179+ cancel ()
180+ require .ErrorIs (t , <- done , context .Canceled )
181+ }
182+
113183func TestRelayRegistrarSubSecondLeaseDuration (t * testing.T ) {
114184 ctx := context .Background ()
115185 r , c := newRegistrar (t , time .Unix (1_700_000_000 , 0 ))
0 commit comments