@@ -18,8 +18,10 @@ package controllers
1818
1919import (
2020 "context"
21+ "crypto/tls"
2122 "fmt"
2223 "net"
24+ "time"
2325
2426 rhsysenggithubiov1beta1 "github.com/RHsyseng/cluster-relocation-operator/api/v1beta1"
2527 reconcileACM "github.com/RHsyseng/cluster-relocation-operator/internal/acm"
@@ -31,6 +33,7 @@ import (
3133 reconcilePullSecret "github.com/RHsyseng/cluster-relocation-operator/internal/pullSecret"
3234 registryCert "github.com/RHsyseng/cluster-relocation-operator/internal/registryCert"
3335 reconcileSSH "github.com/RHsyseng/cluster-relocation-operator/internal/ssh"
36+ "github.com/RHsyseng/cluster-relocation-operator/internal/util"
3437 agentv1 "github.com/stolostron/klusterlet-addon-controller/pkg/apis/agent/v1"
3538 clusterv1 "open-cluster-management.io/api/cluster/v1"
3639 operatorapiv1 "open-cluster-management.io/api/operator/v1"
@@ -75,6 +78,7 @@ const relocationFinalizer = "relocationfinalizer"
7578//+kubebuilder:rbac:groups="",resources=secrets,verbs=watch;list
7679//+kubebuilder:rbac:groups="",resources=configmaps,verbs=watch;list
7780//+kubebuilder:rbac:groups=config.openshift.io,resources=clusterversions,verbs=get;watch;list
81+ //+kubebuilder:rbac:groups=config.openshift.io,resources=dnses,verbs=get;watch;list
7882//+kubebuilder:rbac:groups=config.openshift.io,resources=imagedigestmirrorsets,verbs=watch;list
7983//+kubebuilder:rbac:groups=operators.coreos.com,resources=catalogsources,verbs=watch;list
8084//+kubebuilder:rbac:groups=machineconfiguration.openshift.io,resources=machineconfigs,verbs=watch;list
@@ -181,24 +185,6 @@ func (r *ClusterRelocationReconciler) Reconcile(ctx context.Context, req ctrl.Re
181185 return ctrl.Result {}, err
182186 }
183187
184- // Applies a new certificate and domain alias to the API server
185- if err := reconcileAPI .Reconcile (ctx , r .Client , r .Scheme , relocation , logger ); err != nil {
186- r .setFailedStatus (relocation , rhsysenggithubiov1beta1 .APIReconciliationFailedReason , err .Error ())
187- return ctrl.Result {}, err
188- }
189-
190- // Applies a new certificate and domain alias to the Apps ingressesed
191- if err := reconcileIngress .Reconcile (ctx , r .Client , r .Scheme , relocation , logger ); err != nil {
192- r .setFailedStatus (relocation , rhsysenggithubiov1beta1 .IngressReconciliationFailedReason , err .Error ())
193- return ctrl.Result {}, err
194- }
195-
196- // Apply a new cluster-wide pull secret
197- if err := reconcilePullSecret .Reconcile (ctx , r .Client , r .Scheme , relocation , logger ); err != nil {
198- r .setFailedStatus (relocation , rhsysenggithubiov1beta1 .PullSecretReconciliationFailedReason , err .Error ())
199- return ctrl.Result {}, err
200- }
201-
202188 // Applies a SSH key for the 'core' user
203189 if err := reconcileSSH .Reconcile (ctx , r .Client , r .Scheme , relocation , logger ); err != nil {
204190 r .setFailedStatus (relocation , rhsysenggithubiov1beta1 .SSHReconciliationFailedReason , err .Error ())
@@ -217,12 +203,40 @@ func (r *ClusterRelocationReconciler) Reconcile(ctx context.Context, req ctrl.Re
217203 return ctrl.Result {}, err
218204 }
219205
206+ // Apply a new cluster-wide pull secret
207+ if err := reconcilePullSecret .Reconcile (ctx , r .Client , r .Scheme , relocation , logger ); err != nil {
208+ r .setFailedStatus (relocation , rhsysenggithubiov1beta1 .PullSecretReconciliationFailedReason , err .Error ())
209+ return ctrl.Result {}, err
210+ }
211+
220212 // Applies new catalog sources
221213 if err := reconcileCatalog .Reconcile (ctx , r .Client , r .Scheme , relocation , logger ); err != nil {
222214 r .setFailedStatus (relocation , rhsysenggithubiov1beta1 .CatalogReconciliationFailedReason , err .Error ())
223215 return ctrl.Result {}, err
224216 }
225217
218+ // Applies a new certificate and domain alias to the Ingress
219+ if err := reconcileIngress .Reconcile (ctx , r .Client , r .Scheme , relocation , logger ); err != nil {
220+ r .setFailedStatus (relocation , rhsysenggithubiov1beta1 .IngressReconciliationFailedReason , err .Error ())
221+ return ctrl.Result {}, err
222+ }
223+
224+ // Applies a new certificate and domain alias to the API server
225+ if err := reconcileAPI .Reconcile (ctx , r .Client , r .Scheme , relocation , logger ); err != nil {
226+ r .setFailedStatus (relocation , rhsysenggithubiov1beta1 .APIReconciliationFailedReason , err .Error ())
227+ return ctrl.Result {}, err
228+ }
229+
230+ if err := r .verifyDomain (ctx , relocation .Spec .Domain , logger ); err != nil {
231+ r .setFailedStatus (relocation , rhsysenggithubiov1beta1 .InProgressReconciliationFailedReason , err .Error ())
232+ return ctrl.Result {}, err
233+ }
234+
235+ if err := reconcileIngress .ResetRoutes (ctx , r .Client , fmt .Sprintf ("apps.%s" , relocation .Spec .Domain ), logger ); err != nil {
236+ r .setFailedStatus (relocation , rhsysenggithubiov1beta1 .InProgressReconciliationFailedReason , err .Error ())
237+ return ctrl.Result {}, err
238+ }
239+
226240 // Registers to ACM
227241 if err := reconcileACM .Reconcile (ctx , r .Client , r .Scheme , relocation , logger ); err != nil {
228242 r .setFailedStatus (relocation , rhsysenggithubiov1beta1 .ACMReconciliationFailedReason , err .Error ())
@@ -328,12 +342,68 @@ func (r *ClusterRelocationReconciler) finalizeRelocation(ctx context.Context, lo
328342 if err := reconcileAPI .Cleanup (ctx , r .Client , logger ); err != nil {
329343 return err
330344 }
345+
346+ clusterDNS := & configv1.DNS {}
347+ if err := r .Client .Get (ctx , types.NamespacedName {Name : "cluster" }, clusterDNS ); err != nil {
348+ return err
349+ }
350+ if err := r .verifyDomain (ctx , clusterDNS .Spec .BaseDomain , logger ); err != nil {
351+ return err
352+ }
353+
354+ if err := reconcileIngress .ResetRoutes (ctx , r .Client , fmt .Sprintf ("apps.%s" , clusterDNS .Spec .BaseDomain ), logger ); err != nil {
355+ return err
356+ }
331357 }
332358
333359 logger .Info ("Successfully finalized ClusterRelocation" )
334360 return nil
335361}
336362
363+ func (r * ClusterRelocationReconciler ) verifyDomain (ctx context.Context , domainName string , logger logr.Logger ) error {
364+ urls := []map [string ]string {
365+ {
366+ "type" : "ingress" ,
367+ "url" : fmt .Sprintf ("test.apps.%s:443" , domainName ),
368+ "commonName" : fmt .Sprintf ("*.apps.%s" , domainName ),
369+ },
370+ {
371+ "type" : "kube-apiserver" ,
372+ "url" : fmt .Sprintf ("api.%s:6443" , domainName ),
373+ "commonName" : fmt .Sprintf ("api.%s" , domainName ),
374+ },
375+ }
376+
377+ for _ , v := range urls {
378+ updated := false
379+ for {
380+ conn , err := tls .Dial ("tcp" , v ["url" ], & tls.Config {InsecureSkipVerify : true })
381+ if err != nil {
382+ return err
383+ }
384+ certs := conn .ConnectionState ().PeerCertificates
385+ conn .Close ()
386+ for _ , cert := range certs {
387+ if cert .Subject .CommonName == v ["commonName" ] {
388+ updated = true
389+ }
390+ }
391+ if updated {
392+ // ensure that ClusterOperator has settled
393+ if err := util .WaitForCO (ctx , r .Client , logger , v ["type" ]); err != nil {
394+ return err
395+ }
396+ break
397+ } else {
398+ logger .Info (fmt .Sprintf ("Waiting for %s to update" , v ["type" ]))
399+ time .Sleep (time .Second * 10 )
400+ }
401+ }
402+ }
403+
404+ return nil
405+ }
406+
337407func (r * ClusterRelocationReconciler ) installSchemes () error {
338408 if err := configv1 .Install (r .Scheme ); err != nil { // Add config.openshift.io/v1 to the scheme
339409 return err
0 commit comments