@@ -20,14 +20,14 @@ 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"
2726 certmanagermetav1 "github.com/cert-manager/cert-manager/pkg/apis/meta/v1"
2827 k8creconciling "k8c.io/reconciler/pkg/reconciling"
2928
3029 corev1 "k8s.io/api/core/v1"
30+ apierrors "k8s.io/apimachinery/pkg/api/errors"
3131 "k8s.io/apimachinery/pkg/runtime"
3232 "k8s.io/apimachinery/pkg/types"
3333 ctrl "sigs.k8s.io/controller-runtime"
@@ -70,45 +70,46 @@ func (r *KubeconfigReconciler) Reconcile(ctx context.Context, req ctrl.Request)
7070
7171 var kc operatorv1alpha1.Kubeconfig
7272 if err := r .Get (ctx , req .NamespacedName , & kc ); err != nil {
73+ // object has been deleted.
74+ if apierrors .IsNotFound (err ) {
75+ return ctrl.Result {}, nil
76+ }
7377 return ctrl.Result {}, err
7478 }
7579
80+ rootShard := & operatorv1alpha1.RootShard {}
81+ shard := & operatorv1alpha1.Shard {}
82+
7683 var (
77- clientCertIssuer , serverCA , serverURL , serverName string
84+ clientCertIssuer string
85+ serverCA string
7886 )
7987
8088 switch {
8189 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 {
90+ if err := r .Get (ctx , types.NamespacedName {Name : kc .Spec .Target .RootShardRef .Name , Namespace : req .Namespace }, rootShard ); err != nil {
8491 return ctrl.Result {}, fmt .Errorf ("failed to get RootShard: %w" , err )
8592 }
8693
87- clientCertIssuer = resources .GetRootShardCAName (& rootShard , operatorv1alpha1 .ClientCA )
88- serverCA = resources .GetRootShardCAName (& rootShard , operatorv1alpha1 .ServerCA )
89- serverURL = resources .GetRootShardBaseURL (& rootShard )
90- serverName = rootShard .Name
94+ clientCertIssuer = resources .GetRootShardCAName (rootShard , operatorv1alpha1 .ClientCA )
95+ serverCA = resources .GetRootShardCAName (rootShard , operatorv1alpha1 .ServerCA )
9196
9297 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 {
98+ if err := r .Get (ctx , types.NamespacedName {Name : kc .Spec .Target .ShardRef .Name , Namespace : req .Namespace }, shard ); err != nil {
9599 return ctrl.Result {}, fmt .Errorf ("failed to get Shard: %w" , err )
96100 }
97101
98102 ref := shard .Spec .RootShard .Reference
99103 if ref == nil || ref .Name == "" {
100104 return ctrl.Result {}, errors .New ("the Shard does not reference a (valid) RootShard" )
101105 }
102- var rootShard operatorv1alpha1.RootShard
103- if err := r .Get (ctx , types.NamespacedName {Name : ref .Name , Namespace : req .Namespace }, & rootShard ); err != nil {
106+ if err := r .Get (ctx , types.NamespacedName {Name : ref .Name , Namespace : req .Namespace }, rootShard ); err != nil {
104107 return ctrl.Result {}, fmt .Errorf ("failed to get RootShard: %w" , err )
105108 }
106109
107110 // 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
111+ clientCertIssuer = resources .GetRootShardCAName (rootShard , operatorv1alpha1 .ClientCA )
112+ serverCA = resources .GetRootShardCAName (rootShard , operatorv1alpha1 .ServerCA )
112113
113114 case kc .Spec .Target .FrontProxyRef != nil :
114115 var frontProxy operatorv1alpha1.FrontProxy
@@ -120,15 +121,12 @@ func (r *KubeconfigReconciler) Reconcile(ctx context.Context, req ctrl.Request)
120121 if ref == nil || ref .Name == "" {
121122 return ctrl.Result {}, errors .New ("the FrontProxy does not reference a (valid) RootShard" )
122123 }
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 {
124+ if err := r .Get (ctx , types.NamespacedName {Name : frontProxy .Spec .RootShard .Reference .Name , Namespace : req .Namespace }, rootShard ); err != nil {
125125 return ctrl.Result {}, fmt .Errorf ("failed to get RootShard: %w" , err )
126126 }
127127
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
128+ clientCertIssuer = resources .GetRootShardCAName (rootShard , operatorv1alpha1 .FrontProxyClientCA )
129+ serverCA = resources .GetRootShardCAName (rootShard , operatorv1alpha1 .ServerCA )
132130
133131 default :
134132 return ctrl.Result {}, fmt .Errorf ("no valid target for kubeconfig found" )
@@ -156,14 +154,12 @@ func (r *KubeconfigReconciler) Reconcile(ctx context.Context, req ctrl.Request)
156154 return ctrl.Result {RequeueAfter : time .Second * 5 }, nil
157155 }
158156
159- rootWSURL , err := url . JoinPath ( serverURL , "clusters" , "root" )
157+ reconciler , err := kubeconfig . KubeconfigSecretReconciler ( & kc , rootShard , shard , serverCASecret , clientCertSecret )
160158 if err != nil {
161159 return ctrl.Result {}, err
162160 }
163161
164- if err := k8creconciling .ReconcileSecrets (ctx , []k8creconciling.NamedSecretReconcilerFactory {
165- kubeconfig .KubeconfigSecretReconciler (& kc , serverCASecret , clientCertSecret , serverName , rootWSURL ),
166- }, req .Namespace , r .Client ); err != nil {
162+ if err := k8creconciling .ReconcileSecrets (ctx , []k8creconciling.NamedSecretReconcilerFactory {reconciler }, req .Namespace , r .Client ); err != nil {
167163 return ctrl.Result {}, err
168164 }
169165
0 commit comments