@@ -20,7 +20,6 @@ import (
2020 "context"
2121 "errors"
2222 "fmt"
23- "net/url"
2423 "time"
2524
2625 certmanagerv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
@@ -73,42 +72,39 @@ func (r *KubeconfigReconciler) Reconcile(ctx context.Context, req ctrl.Request)
7372 return ctrl.Result {}, err
7473 }
7574
75+ rootShard := & operatorv1alpha1.RootShard {}
76+ shard := & operatorv1alpha1.Shard {}
77+
7678 var (
77- clientCertIssuer , serverCA , serverURL , serverName string
79+ clientCertIssuer string
80+ serverCA string
7881 )
7982
8083 switch {
8184 case kc .Spec .Target .RootShardRef != nil :
82- var rootShard operatorv1alpha1.RootShard
83- if err := r .Get (ctx , types.NamespacedName {Name : kc .Spec .Target .RootShardRef .Name , Namespace : req .Namespace }, & rootShard ); err != nil {
85+ if err := r .Get (ctx , types.NamespacedName {Name : kc .Spec .Target .RootShardRef .Name , Namespace : req .Namespace }, rootShard ); err != nil {
8486 return ctrl.Result {}, fmt .Errorf ("failed to get RootShard: %w" , err )
8587 }
8688
87- clientCertIssuer = resources .GetRootShardCAName (& rootShard , operatorv1alpha1 .ClientCA )
88- serverCA = resources .GetRootShardCAName (& rootShard , operatorv1alpha1 .ServerCA )
89- serverURL = resources .GetRootShardBaseURL (& rootShard )
90- serverName = rootShard .Name
89+ clientCertIssuer = resources .GetRootShardCAName (rootShard , operatorv1alpha1 .ClientCA )
90+ serverCA = resources .GetRootShardCAName (rootShard , operatorv1alpha1 .ServerCA )
9191
9292 case kc .Spec .Target .ShardRef != nil :
93- var shard operatorv1alpha1.Shard
94- if err := r .Get (ctx , types.NamespacedName {Name : kc .Spec .Target .ShardRef .Name , Namespace : req .Namespace }, & shard ); err != nil {
93+ if err := r .Get (ctx , types.NamespacedName {Name : kc .Spec .Target .ShardRef .Name , Namespace : req .Namespace }, shard ); err != nil {
9594 return ctrl.Result {}, fmt .Errorf ("failed to get Shard: %w" , err )
9695 }
9796
9897 ref := shard .Spec .RootShard .Reference
9998 if ref == nil || ref .Name == "" {
10099 return ctrl.Result {}, errors .New ("the Shard does not reference a (valid) RootShard" )
101100 }
102- var rootShard operatorv1alpha1.RootShard
103- if err := r .Get (ctx , types.NamespacedName {Name : ref .Name , Namespace : req .Namespace }, & rootShard ); err != nil {
101+ if err := r .Get (ctx , types.NamespacedName {Name : ref .Name , Namespace : req .Namespace }, rootShard ); err != nil {
104102 return ctrl.Result {}, fmt .Errorf ("failed to get RootShard: %w" , err )
105103 }
106104
107105 // The client CA is shared among all shards and owned by the root shard.
108- clientCertIssuer = resources .GetRootShardCAName (& rootShard , operatorv1alpha1 .ClientCA )
109- serverCA = resources .GetRootShardCAName (& rootShard , operatorv1alpha1 .ServerCA )
110- serverURL = resources .GetShardBaseURL (& shard )
111- serverName = shard .Name
106+ clientCertIssuer = resources .GetRootShardCAName (rootShard , operatorv1alpha1 .ClientCA )
107+ serverCA = resources .GetRootShardCAName (rootShard , operatorv1alpha1 .ServerCA )
112108
113109 case kc .Spec .Target .FrontProxyRef != nil :
114110 var frontProxy operatorv1alpha1.FrontProxy
@@ -120,15 +116,12 @@ func (r *KubeconfigReconciler) Reconcile(ctx context.Context, req ctrl.Request)
120116 if ref == nil || ref .Name == "" {
121117 return ctrl.Result {}, errors .New ("the FrontProxy does not reference a (valid) RootShard" )
122118 }
123- var rootShard operatorv1alpha1.RootShard
124- if err := r .Get (ctx , types.NamespacedName {Name : frontProxy .Spec .RootShard .Reference .Name , Namespace : req .Namespace }, & rootShard ); err != nil {
119+ if err := r .Get (ctx , types.NamespacedName {Name : frontProxy .Spec .RootShard .Reference .Name , Namespace : req .Namespace }, rootShard ); err != nil {
125120 return ctrl.Result {}, fmt .Errorf ("failed to get RootShard: %w" , err )
126121 }
127122
128- clientCertIssuer = resources .GetRootShardCAName (& rootShard , operatorv1alpha1 .FrontProxyClientCA )
129- serverCA = resources .GetRootShardCAName (& rootShard , operatorv1alpha1 .ServerCA )
130- serverURL = fmt .Sprintf ("https://%s:6443" , rootShard .Spec .External .Hostname )
131- serverName = rootShard .Spec .External .Hostname
123+ clientCertIssuer = resources .GetRootShardCAName (rootShard , operatorv1alpha1 .FrontProxyClientCA )
124+ serverCA = resources .GetRootShardCAName (rootShard , operatorv1alpha1 .ServerCA )
132125
133126 default :
134127 return ctrl.Result {}, fmt .Errorf ("no valid target for kubeconfig found" )
@@ -156,14 +149,12 @@ func (r *KubeconfigReconciler) Reconcile(ctx context.Context, req ctrl.Request)
156149 return ctrl.Result {RequeueAfter : time .Second * 5 }, nil
157150 }
158151
159- rootWSURL , err := url . JoinPath ( serverURL , "clusters" , "root" )
152+ reconciler , err := kubeconfig . KubeconfigSecretReconciler ( & kc , rootShard , shard , serverCASecret , clientCertSecret )
160153 if err != nil {
161154 return ctrl.Result {}, err
162155 }
163156
164- if err := k8creconciling .ReconcileSecrets (ctx , []k8creconciling.NamedSecretReconcilerFactory {
165- kubeconfig .KubeconfigSecretReconciler (& kc , serverCASecret , clientCertSecret , serverName , rootWSURL ),
166- }, req .Namespace , r .Client ); err != nil {
157+ if err := k8creconciling .ReconcileSecrets (ctx , []k8creconciling.NamedSecretReconcilerFactory {reconciler }, req .Namespace , r .Client ); err != nil {
167158 return ctrl.Result {}, err
168159 }
169160
0 commit comments