From 2199d7f45183350e9963d3bcc8f41746da5f3d59 Mon Sep 17 00:00:00 2001 From: Sam Gleske <875669+samrocketman@users.noreply.github.com> Date: Thu, 14 Mar 2024 04:46:01 -0400 Subject: [PATCH] Support for IPv6 clusters (#370) * Support for IPv6 clusters No idea if this works. This is literally my first Go code (my "hello world" but IPv6 attempt). Hopefully fixes https://github.com/k8sgpt-ai/k8sgpt-operator/issues/369 closes #369 Signed-off-by: Sam Gleske * Apply code review feedback * Return err instead of nil * Defer connection closing Signed-off-by: JuHyung Son Signed-off-by: Sam Gleske Co-authored-by: JuHyung Son --------- Signed-off-by: Sam Gleske Co-authored-by: JuHyung Son --- pkg/client/client.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index bd490907..6def142a 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -48,6 +48,8 @@ func NewClient(address string) (*Client, error) { func GenerateAddress(ctx context.Context, cli client.Client, k8sgptConfig *v1alpha1.K8sGPT) (string, error) { var address string + var ip net.IP + if os.Getenv("LOCAL_MODE") != "" { address = "localhost:8080" } else { @@ -56,9 +58,14 @@ func GenerateAddress(ctx context.Context, cli client.Client, k8sgptConfig *v1alp err := cli.Get(ctx, client.ObjectKey{Namespace: k8sgptConfig.Namespace, Name: k8sgptConfig.Name}, svc) if err != nil { - return "", nil + return "", err + } + ip = net.ParseIP(svc.Spec.ClusterIP) + if ip.To4() != nil { + address = fmt.Sprintf("%s:%d", svc.Spec.ClusterIP, svc.Spec.Ports[0].Port) + } else { + address = fmt.Sprintf("[%s]:%d", svc.Spec.ClusterIP, svc.Spec.Ports[0].Port) } - address = fmt.Sprintf("%s:%d", svc.Spec.ClusterIP, svc.Spec.Ports[0].Port) } fmt.Printf("Creating new client for %s\n", address) @@ -67,6 +74,7 @@ func GenerateAddress(ctx context.Context, cli client.Client, k8sgptConfig *v1alp if err != nil { return "", err } + defer conn.Close() fmt.Printf("Connection established between %s and localhost with time out of %d seconds.\n", address, int64(1)) fmt.Printf("Remote Address : %s \n", conn.RemoteAddr().String())