@@ -25,6 +25,7 @@ import (
2525 "path/filepath"
2626 "strings"
2727 "sync"
28+ "time"
2829
2930 "go.bytebuilders.dev/license-proxyserver/pkg/common"
3031 "go.bytebuilders.dev/license-proxyserver/pkg/storage"
@@ -46,6 +47,8 @@ import (
4647 "sigs.k8s.io/controller-runtime/pkg/reconcile"
4748)
4849
50+ const ttl = storage .LicenseAcquisitionBuffer + storage .MinRemainingLife
51+
4952type LicenseAcquirer struct {
5053 client.Client
5154 BaseURL string
@@ -88,10 +91,7 @@ func (r *LicenseAcquirer) Reconcile(ctx context.Context, request reconcile.Reque
8891 }
8992 }
9093 if cid != "" && len (features ) > 0 {
91- err = r .reconcile (ctx , managedCluster .Name , cid , features )
92- if err != nil {
93- return reconcile.Result {}, err
94- }
94+ return r .reconcile (managedCluster .Name , cid , features )
9595 }
9696
9797 return reconcile.Result {}, nil
@@ -111,12 +111,12 @@ func (r *LicenseAcquirer) getLicenseRegistry(cid string) (*storage.LicenseRegist
111111 if err != nil {
112112 return nil , err
113113 }
114- reg = storage .NewLicenseRegistry (dir , nil )
114+ reg = storage .NewLicenseRegistry (dir , ttl , nil )
115115 r .LicenseCache [cid ] = reg
116116 return reg , nil
117117}
118118
119- func (r * LicenseAcquirer ) reconcile (ctx context. Context , clusterName , cid string , features []string ) error {
119+ func (r * LicenseAcquirer ) reconcile (clusterName , cid string , features []string ) (reconcile. Result , error ) {
120120 sec := core.Secret {
121121 ObjectMeta : metav1.ObjectMeta {
122122 Name : common .LicenseSecret ,
@@ -130,20 +130,21 @@ func (r *LicenseAcquirer) reconcile(ctx context.Context, clusterName, cid string
130130 } else if apierrors .IsNotFound (err ) {
131131 sec .Data = map [string ][]byte {}
132132 } else {
133- return err
133+ return reconcile. Result {}, err
134134 }
135135
136136 var errList []error
137+ var earliestExpired time.Time
137138
138139 reg , err := r .getLicenseRegistry (cid )
139140 if err != nil {
140- return err
141+ return reconcile. Result {}, err
141142 }
142143 for _ , feature := range features {
143144 l , found := reg .LicenseForFeature (feature )
144145 if ! found {
145146 var c * v1alpha1.Contract
146- l , c , err = r .getNewLicense (ctx , cid , []string {feature })
147+ l , c , err = r .getNewLicense (cid , []string {feature })
147148 if err == nil {
148149 reg .Add (l , c )
149150 } else {
@@ -156,6 +157,9 @@ func (r *LicenseAcquirer) reconcile(ctx context.Context, clusterName, cid string
156157 }
157158 if l != nil && l .Status == v1alpha1 .LicenseActive {
158159 sec .Data [l .PlanName ] = l .Data
160+ if earliestExpired .IsZero () || earliestExpired .After (l .NotAfter .Time ) {
161+ earliestExpired = l .NotAfter .Time
162+ }
159163 }
160164 }
161165
@@ -165,12 +169,15 @@ func (r *LicenseAcquirer) reconcile(ctx context.Context, clusterName, cid string
165169 errList = append (errList , r .Create (context .TODO (), & sec ))
166170 }
167171
168- return utilerrors .NewAggregate (errList )
172+ if ! earliestExpired .IsZero () {
173+ return reconcile.Result {
174+ RequeueAfter : time .Until (earliestExpired .Add (- ttl )),
175+ }, utilerrors .NewAggregate (errList )
176+ }
177+ return reconcile.Result {}, utilerrors .NewAggregate (errList )
169178}
170179
171- func (r * LicenseAcquirer ) getNewLicense (ctx context.Context , cid string , features []string ) (* v1alpha1.License , * v1alpha1.Contract , error ) {
172- logger := log .FromContext (ctx )
173-
180+ func (r * LicenseAcquirer ) getNewLicense (cid string , features []string ) (* v1alpha1.License , * v1alpha1.Contract , error ) {
174181 lc , err := pc .NewClient (r .BaseURL , r .Token , cid , r .CaCert , r .InsecureSkipTLSVerify , fmt .Sprintf ("license-proxyserver-manager/%s" , v .Version .Version ))
175182 if err != nil {
176183 return nil , nil , err
@@ -180,7 +187,6 @@ func (r *LicenseAcquirer) getNewLicense(ctx context.Context, cid string, feature
180187 if err != nil {
181188 return nil , nil , err
182189 }
183- logger .Info ("acquired new license" , "cid" , cid , "features" , strings .Join (features , "," ))
184190
185191 caData , err := info .LoadLicenseCA ()
186192 if err != nil {
@@ -200,5 +206,12 @@ func (r *LicenseAcquirer) getNewLicense(ctx context.Context, cid string, feature
200206 return nil , nil , err
201207 }
202208
209+ klog .InfoS ("acquired new license" ,
210+ "cluster" , cid ,
211+ "id" , l .ID ,
212+ "product" , l .ProductLine ,
213+ "plan" , l .PlanName ,
214+ "expiry" , l .NotAfter .UTC ().Format (time .RFC822 ),
215+ )
203216 return & l , con , nil
204217}
0 commit comments