Переглянути джерело

Implemented subroutines to get IPs on HandleListProjectClusters

jnfrati 5 роки тому
батько
коміт
f00fd17b78
2 змінених файлів з 28 додано та 22 видалено
  1. 0 4
      internal/models/cluster.go
  2. 28 18
      server/api/cluster_handler.go

+ 0 - 4
internal/models/cluster.go

@@ -66,9 +66,6 @@ type Cluster struct {
 
 	// CertificateAuthorityData for the cluster, encrypted at rest
 	CertificateAuthorityData []byte `json:"certificate-authority-data,omitempty"`
-
-	// The ingress ip for the cluster
-	IngressIP string `json:"ingress_ip"`
 }
 
 // ClusterExternal is an external Cluster to be shared over REST
@@ -113,7 +110,6 @@ func (c *Cluster) Externalize() *ClusterExternal {
 		Server:    c.Server,
 		Service:   serv,
 		InfraID:   c.InfraID,
-		IngressIP: c.IngressIP,
 	}
 }
 

+ 28 - 18
server/api/cluster_handler.go

@@ -4,6 +4,7 @@ import (
 	"encoding/json"
 	"net/http"
 	"strconv"
+	"sync"
 
 	"github.com/go-chi/chi"
 	"github.com/porter-dev/porter/internal/forms"
@@ -109,32 +110,41 @@ func (app *App) HandleListProjectClusters(w http.ResponseWriter, r *http.Request
 
 	extClusters := make([]*models.ClusterExternal, 0)
 
+	var wg sync.WaitGroup
+
 	for _, cluster := range clusters {
-		form := &forms.K8sForm{
-			OutOfClusterConfig: &kubernetes.OutOfClusterConfig{
-				Repo:              app.Repo,
-				DigitalOceanOAuth: app.DOConf,
-				Cluster:           cluster,
-			},
-		}
+		wg.Add(1)
+		go func(cluster *models.Cluster) {
+			defer wg.Done()
+			extCluster := cluster.Externalize()
+
+			form := &forms.K8sForm{
+				OutOfClusterConfig: &kubernetes.OutOfClusterConfig{
+					Repo:              app.Repo,
+					DigitalOceanOAuth: app.DOConf,
+					Cluster:           cluster,
+				},
+			}
 
-		var agent *kubernetes.Agent
+			var agent *kubernetes.Agent
 
-		if app.ServerConf.IsTesting {
-			agent = app.TestAgents.K8sAgent
-		} else {
-			agent, _ = kubernetes.GetAgentOutOfClusterConfig(form.OutOfClusterConfig)
-		}
+			if app.ServerConf.IsTesting {
+				agent = app.TestAgents.K8sAgent
+			} else {
+				agent, _ = kubernetes.GetAgentOutOfClusterConfig(form.OutOfClusterConfig)
+			}
 
-		endpoint, found, _ := domain.GetNGINXIngressServiceIP(agent.Clientset)
+			endpoint, found, _ := domain.GetNGINXIngressServiceIP(agent.Clientset)
 
-		if found {
-			cluster.IngressIP = endpoint
-		}
+			if found {
+				extCluster.IngressIP = endpoint
+			}
 
-		extClusters = append(extClusters, cluster.Externalize())
+			extClusters = append(extClusters, extCluster)
+		}(cluster)
 	}
 
+	wg.Wait()
 	w.WriteHeader(http.StatusOK)
 
 	if err := json.NewEncoder(w).Encode(extClusters); err != nil {