Skip to content

Commit

Permalink
Print the external port of the ingress controller of the current ctx
Browse files Browse the repository at this point in the history
Signed-off-by: cmoulliard <[email protected]>
  • Loading branch information
cmoulliard committed Nov 14, 2024
1 parent 0dad331 commit 7cab4bf
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions pkg/cmd/get/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/clientcmd/api"
"os"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -78,9 +79,22 @@ func list(cmd *cobra.Command, args []string) error {
fmt.Printf("URL of the kube API server: %s\n", c.Server)
fmt.Printf("TLS Verify: %t\n", c.InsecureSkipTLSVerify)
}

fmt.Println("----------------------------------------")
}

// Print the external port that users can access using the ingress nginx proxy
service := corev1.Service{}
namespacedName := types.NamespacedName{
Name: "ingress-nginx-controller",
Namespace: "ingress-nginx",
}
err = cli.Get(context.TODO(), namespacedName, &service)
if err != nil {
logger.Error(err, "failed to get the ingress service on the cluster.")
}
fmt.Printf("External Port: %d\n", findExternalHTTPSPort(service))

// Let's check what the current node reports
var nodeList corev1.NodeList
err = cli.List(context.TODO(), &nodeList)
Expand Down Expand Up @@ -167,3 +181,14 @@ func printAllocatedResources(ctx context.Context, k8sClient client.Client, nodeN

return nil
}

func findExternalHTTPSPort(service corev1.Service) int32 {
var targetPort corev1.ServicePort
for _, port := range service.Spec.Ports {
if port.Name != "" && strings.HasPrefix(port.Name, "https-") {
targetPort = port
break
}
}
return targetPort.Port
}

0 comments on commit 7cab4bf

Please sign in to comment.