@@ -6,6 +6,7 @@ package resources
6
6
import (
7
7
"context"
8
8
"fmt"
9
+ "strings"
9
10
10
11
corev1 "k8s.io/api/core/v1"
11
12
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -126,6 +127,7 @@ func (r *KubeconfigResource) checksum(caCertificatesSecret *corev1.Secret, kubea
126
127
})
127
128
}
128
129
130
+ //nolint:gocognit
129
131
func (r * KubeconfigResource ) mutate (ctx context.Context , tenantControlPlane * kamajiv1alpha1.TenantControlPlane ) controllerutil.MutateFn {
130
132
return func () error {
131
133
logger := log .FromContext (ctx , "resource" , r .GetName ())
@@ -186,25 +188,42 @@ func (r *KubeconfigResource) mutate(ctx context.Context, tenantControlPlane *kam
186
188
v , ok := r .resource .Data [r .KubeConfigFileName ]
187
189
shouldCreate = len (v ) == 0 || ! ok
188
190
}
189
-
191
+ //nolint:nestif
190
192
if shouldCreate {
191
193
crtKeyPair := kubeadm.CertificatePrivateKeyPair {
192
194
Certificate : caCertificatesSecret .Data [kubeadmconstants .CACertName ],
193
195
PrivateKey : caCertificatesSecret .Data [kubeadmconstants .CAKeyName ],
194
196
}
195
197
198
+ if r .resource .Data == nil {
199
+ r .resource .Data = map [string ][]byte {}
200
+ }
201
+
196
202
kubeconfig , kcErr := kubeadm .CreateKubeconfig (r .KubeConfigFileName , crtKeyPair , config )
197
203
if kcErr != nil {
198
204
logger .Error (kcErr , "cannot create a valid kubeconfig" )
199
205
200
206
return kcErr
201
207
}
202
208
203
- if r .resource .Data == nil {
204
- r .resource .Data = map [string ][]byte {}
205
- }
206
-
207
209
r .resource .Data [r .KubeConfigFileName ] = kubeconfig
210
+ // Adding a kubeconfig useful for the local connections:
211
+ // especially for the admin.conf and super-admin.conf, these would use the public IP address.
212
+ // However, when running in-cluster agents, it would be beneficial having a local connection
213
+ // to avoid unnecessary hops to the LB.
214
+ if strings .Contains (r .KubeConfigFileName , "admin" ) {
215
+ key := strings .ReplaceAll (r .KubeConfigFileName , ".conf" , ".svc" )
216
+
217
+ config .InitConfiguration .ControlPlaneEndpoint = fmt .Sprintf ("%s.%s.svc:%d" , tenantControlPlane .Name , tenantControlPlane .Namespace , tenantControlPlane .Spec .NetworkProfile .Port )
218
+ kubeconfig , kcErr = kubeadm .CreateKubeconfig (r .KubeConfigFileName , crtKeyPair , config )
219
+ if kcErr != nil {
220
+ logger .Error (kcErr , "cannot create a valid kubeconfig" )
221
+
222
+ return kcErr
223
+ }
224
+
225
+ r .resource .Data [key ] = kubeconfig
226
+ }
208
227
}
209
228
210
229
return nil
0 commit comments