From e6db5a34c82f0c2f4531ed5729ad3e3fe689d98c Mon Sep 17 00:00:00 2001 From: jpgouin Date: Fri, 24 Jan 2025 18:48:31 +0000 Subject: [PATCH 1/3] fix ingress creation, use the ingress host in Kubeconfig when enabled --- pkg/controller/cluster/server/ingress.go | 3 +++ pkg/controller/controller.go | 4 ++-- pkg/controller/kubeconfig/kubeconfig.go | 13 +++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/pkg/controller/cluster/server/ingress.go b/pkg/controller/cluster/server/ingress.go index ce52696..39c5dd5 100644 --- a/pkg/controller/cluster/server/ingress.go +++ b/pkg/controller/cluster/server/ingress.go @@ -91,3 +91,6 @@ func configureIngressOptions(ingress *networkingv1.Ingress, ingressClassName str ingress.Annotations[nginxBackendProtocolAnnotation] = "HTTPS" } } +func IngressName(clusterName string) string { + return controller.SafeConcatNameWithPrefix(clusterName, "ingress") +} diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index b6f3747..357028d 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -70,8 +70,8 @@ func Addresses(ctx context.Context, client ctrlruntimeclient.Client) ([]string, } addresses := make([]string, len(nodeList.Items)) - for _, node := range nodeList.Items { - addresses = append(addresses, nodeAddress(&node)) + for i, node := range nodeList.Items { + addresses[i] = nodeAddress(&node) } return addresses, nil diff --git a/pkg/controller/kubeconfig/kubeconfig.go b/pkg/controller/kubeconfig/kubeconfig.go index 53225bc..d342aef 100644 --- a/pkg/controller/kubeconfig/kubeconfig.go +++ b/pkg/controller/kubeconfig/kubeconfig.go @@ -15,6 +15,7 @@ import ( "github.com/rancher/k3k/pkg/controller/cluster/server" "github.com/rancher/k3k/pkg/controller/cluster/server/bootstrap" v1 "k8s.io/api/core/v1" + networkingv1 "k8s.io/api/networking/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/tools/clientcmd" clientcmdapi "k8s.io/client-go/tools/clientcmd/api" @@ -73,6 +74,18 @@ func (k *KubeConfig) Extract(ctx context.Context, client client.Client, cluster nodePort := k3kService.Spec.Ports[0].NodePort url = fmt.Sprintf("https://%s:%d", hostServerIP, nodePort) } + if cluster.Spec.Expose.Ingress.Enabled { + var k3kIngress networkingv1.Ingress + nn = types.NamespacedName{ + Name: server.IngressName(cluster.Name), + Namespace: cluster.Namespace, + } + + if err := client.Get(ctx, nn, &k3kIngress); err != nil { + return nil, err + } + url = fmt.Sprintf("https://%s", k3kIngress.Spec.Rules[0].Host) + } kubeconfigData, err := kubeconfig(url, []byte(bootstrap.ServerCA.Content), adminCert, adminKey) if err != nil { return nil, err From ebdeb3aa58acca9b3167b4480cbd0f9ae7bb49bd Mon Sep 17 00:00:00 2001 From: jpgouin Date: Tue, 28 Jan 2025 14:27:50 +0000 Subject: [PATCH 2/3] fix merge --- pkg/controller/kubeconfig/kubeconfig.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/controller/kubeconfig/kubeconfig.go b/pkg/controller/kubeconfig/kubeconfig.go index 46360d4..e6d3fd1 100644 --- a/pkg/controller/kubeconfig/kubeconfig.go +++ b/pkg/controller/kubeconfig/kubeconfig.go @@ -54,7 +54,7 @@ func (k *KubeConfig) Extract(ctx context.Context, client client.Client, cluster if err != nil { return nil, err } - + url, err := getURLFromService(ctx, client, cluster, hostServerIP) if err != nil { return nil, err @@ -106,9 +106,9 @@ func getURLFromService(ctx context.Context, client client.Client, cluster *v1alp nodePort := k3kService.Spec.Ports[0].NodePort url = fmt.Sprintf("https://%s:%d", hostServerIP, nodePort) } - if cluster.Spec.Expose.Ingress.Enabled { + if cluster.Spec.Expose.Ingress.Enabled { var k3kIngress networkingv1.Ingress - ingressKey = types.NamespacedName{ + ingressKey := types.NamespacedName{ Name: server.IngressName(cluster.Name), Namespace: cluster.Namespace, } From 2019decc78e80aeabc00d20f16fec2b4e15f2ce5 Mon Sep 17 00:00:00 2001 From: jpgouin Date: Tue, 28 Jan 2025 14:34:50 +0000 Subject: [PATCH 3/3] check value of cluster.Spec.Expose.Ingress --- pkg/controller/kubeconfig/kubeconfig.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/controller/kubeconfig/kubeconfig.go b/pkg/controller/kubeconfig/kubeconfig.go index e6d3fd1..01be1a4 100644 --- a/pkg/controller/kubeconfig/kubeconfig.go +++ b/pkg/controller/kubeconfig/kubeconfig.go @@ -106,7 +106,7 @@ func getURLFromService(ctx context.Context, client client.Client, cluster *v1alp nodePort := k3kService.Spec.Ports[0].NodePort url = fmt.Sprintf("https://%s:%d", hostServerIP, nodePort) } - if cluster.Spec.Expose.Ingress.Enabled { + if cluster.Spec.Expose.Ingress != nil && cluster.Spec.Expose.Ingress.Enabled { var k3kIngress networkingv1.Ingress ingressKey := types.NamespacedName{ Name: server.IngressName(cluster.Name),