Skip to content

Commit 25dc19f

Browse files
committed
feat: admin kubeconfig with local service FQDN
Signed-off-by: Dario Tranchitella <[email protected]>
1 parent 1ccc1d1 commit 25dc19f

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

internal/resources/kubeconfig.go

+24-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package resources
66
import (
77
"context"
88
"fmt"
9+
"strings"
910

1011
corev1 "k8s.io/api/core/v1"
1112
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -126,6 +127,7 @@ func (r *KubeconfigResource) checksum(caCertificatesSecret *corev1.Secret, kubea
126127
})
127128
}
128129

130+
//nolint:gocognit
129131
func (r *KubeconfigResource) mutate(ctx context.Context, tenantControlPlane *kamajiv1alpha1.TenantControlPlane) controllerutil.MutateFn {
130132
return func() error {
131133
logger := log.FromContext(ctx, "resource", r.GetName())
@@ -186,25 +188,42 @@ func (r *KubeconfigResource) mutate(ctx context.Context, tenantControlPlane *kam
186188
v, ok := r.resource.Data[r.KubeConfigFileName]
187189
shouldCreate = len(v) == 0 || !ok
188190
}
189-
191+
//nolint:nestif
190192
if shouldCreate {
191193
crtKeyPair := kubeadm.CertificatePrivateKeyPair{
192194
Certificate: caCertificatesSecret.Data[kubeadmconstants.CACertName],
193195
PrivateKey: caCertificatesSecret.Data[kubeadmconstants.CAKeyName],
194196
}
195197

198+
if r.resource.Data == nil {
199+
r.resource.Data = map[string][]byte{}
200+
}
201+
196202
kubeconfig, kcErr := kubeadm.CreateKubeconfig(r.KubeConfigFileName, crtKeyPair, config)
197203
if kcErr != nil {
198204
logger.Error(kcErr, "cannot create a valid kubeconfig")
199205

200206
return kcErr
201207
}
202208

203-
if r.resource.Data == nil {
204-
r.resource.Data = map[string][]byte{}
205-
}
206-
207209
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+
}
208227
}
209228

210229
return nil

0 commit comments

Comments
 (0)