@@ -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"
@@ -74,41 +73,37 @@ func (r *KubeconfigReconciler) Reconcile(ctx context.Context, req ctrl.Request)
7473 }
7574
7675 var (
77- clientCertIssuer , serverCA , serverURL , serverName string
76+ rootShard * operatorv1alpha1.RootShard
77+ shard * operatorv1alpha1.Shard
78+ clientCertIssuer string
79+ serverCA string
7880 )
7981
8082 switch {
8183 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 {
84+ if err := r .Get (ctx , types.NamespacedName {Name : kc .Spec .Target .RootShardRef .Name , Namespace : req .Namespace }, rootShard ); err != nil {
8485 return ctrl.Result {}, fmt .Errorf ("failed to get RootShard: %w" , err )
8586 }
8687
87- clientCertIssuer = resources .GetRootShardCAName (& rootShard , operatorv1alpha1 .ClientCA )
88- serverCA = resources .GetRootShardCAName (& rootShard , operatorv1alpha1 .ServerCA )
89- serverURL = resources .GetRootShardBaseURL (& rootShard )
90- serverName = rootShard .Name
88+ clientCertIssuer = resources .GetRootShardCAName (rootShard , operatorv1alpha1 .ClientCA )
89+ serverCA = resources .GetRootShardCAName (rootShard , operatorv1alpha1 .ServerCA )
9190
9291 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 {
92+ if err := r .Get (ctx , types.NamespacedName {Name : kc .Spec .Target .ShardRef .Name , Namespace : req .Namespace }, shard ); err != nil {
9593 return ctrl.Result {}, fmt .Errorf ("failed to get Shard: %w" , err )
9694 }
9795
9896 ref := shard .Spec .RootShard .Reference
9997 if ref == nil || ref .Name == "" {
10098 return ctrl.Result {}, errors .New ("the Shard does not reference a (valid) RootShard" )
10199 }
102- var rootShard operatorv1alpha1.RootShard
103- if err := r .Get (ctx , types.NamespacedName {Name : ref .Name , Namespace : req .Namespace }, & rootShard ); err != nil {
100+ if err := r .Get (ctx , types.NamespacedName {Name : ref .Name , Namespace : req .Namespace }, rootShard ); err != nil {
104101 return ctrl.Result {}, fmt .Errorf ("failed to get RootShard: %w" , err )
105102 }
106103
107104 // 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
105+ clientCertIssuer = resources .GetRootShardCAName (rootShard , operatorv1alpha1 .ClientCA )
106+ serverCA = resources .GetRootShardCAName (rootShard , operatorv1alpha1 .ServerCA )
112107
113108 case kc .Spec .Target .FrontProxyRef != nil :
114109 var frontProxy operatorv1alpha1.FrontProxy
@@ -120,15 +115,12 @@ func (r *KubeconfigReconciler) Reconcile(ctx context.Context, req ctrl.Request)
120115 if ref == nil || ref .Name == "" {
121116 return ctrl.Result {}, errors .New ("the FrontProxy does not reference a (valid) RootShard" )
122117 }
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 {
118+ if err := r .Get (ctx , types.NamespacedName {Name : frontProxy .Spec .RootShard .Reference .Name , Namespace : req .Namespace }, rootShard ); err != nil {
125119 return ctrl.Result {}, fmt .Errorf ("failed to get RootShard: %w" , err )
126120 }
127121
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
122+ clientCertIssuer = resources .GetRootShardCAName (rootShard , operatorv1alpha1 .FrontProxyClientCA )
123+ serverCA = resources .GetRootShardCAName (rootShard , operatorv1alpha1 .ServerCA )
132124
133125 default :
134126 return ctrl.Result {}, fmt .Errorf ("no valid target for kubeconfig found" )
@@ -156,14 +148,12 @@ func (r *KubeconfigReconciler) Reconcile(ctx context.Context, req ctrl.Request)
156148 return ctrl.Result {RequeueAfter : time .Second * 5 }, nil
157149 }
158150
159- rootWSURL , err := url . JoinPath ( serverURL , "clusters" , "root" )
151+ reconciler , err := kubeconfig . KubeconfigSecretReconciler ( & kc , rootShard , shard , serverCASecret , clientCertSecret )
160152 if err != nil {
161153 return ctrl.Result {}, err
162154 }
163155
164- if err := k8creconciling .ReconcileSecrets (ctx , []k8creconciling.NamedSecretReconcilerFactory {
165- kubeconfig .KubeconfigSecretReconciler (& kc , serverCASecret , clientCertSecret , serverName , rootWSURL ),
166- }, req .Namespace , r .Client ); err != nil {
156+ if err := k8creconciling .ReconcileSecrets (ctx , []k8creconciling.NamedSecretReconcilerFactory {reconciler }, req .Namespace , r .Client ); err != nil {
167157 return ctrl.Result {}, err
168158 }
169159
0 commit comments