Skip to content

Commit

Permalink
Support for IPv6 clusters (#370)
Browse files Browse the repository at this point in the history
* Support for IPv6 clusters

No idea if this works.  This is literally my first Go code (my "hello world" but IPv6 attempt).

Hopefully fixes #369

closes #369

Signed-off-by: Sam Gleske <sam.mxracer@gmail.com>

* Apply code review feedback

* Return err instead of nil
* Defer connection closing

Signed-off-by: JuHyung Son <sonju0427@gmail.com>
Signed-off-by: Sam Gleske <sam.mxracer@gmail.com>

Co-authored-by: JuHyung Son <sonju0427@gmail.com>

---------

Signed-off-by: Sam Gleske <sam.mxracer@gmail.com>
Co-authored-by: JuHyung Son <sonju0427@gmail.com>
  • Loading branch information
samrocketman and JuHyung-Son committed Mar 14, 2024
1 parent e88a92a commit 2199d7f
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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())
Expand Down

0 comments on commit 2199d7f

Please sign in to comment.