Explorar o código

Implemented custom errors for kubernetes

jnfrati %!s(int64=5) %!d(string=hai) anos
pai
achega
cbf947f211
Modificáronse 3 ficheiros con 75 adicións e 1 borrados
  1. 67 0
      internal/kubernetes/errors.go
  2. 3 0
      internal/models/cluster.go
  3. 5 1
      server/api/cluster_handler.go

+ 67 - 0
internal/kubernetes/errors.go

@@ -0,0 +1,67 @@
+package kubernetes
+
+import (
+	"fmt"
+	"net"
+	"net/url"
+	"os"
+	"syscall"
+
+	k8sErrors "k8s.io/apimachinery/pkg/api/errors"
+)
+
+func CatchK8sConnectionError(err error) error {
+	if uerr, ok := err.(*url.Error); ok {
+		if noerr, ok := uerr.Err.(*net.OpError); ok {
+			if scerr, ok := noerr.Err.(*os.SyscallError); ok {
+				if scerr.Err == syscall.ECONNREFUSED {
+					return &ErrConnection{
+						k8sErr: err,
+					}
+				}
+			}
+		}
+	}
+
+	if k8sErrors.IsTimeout(err) {
+		return &ErrConnection{
+			k8sErr: err,
+		}
+	}
+
+	if k8sErrors.IsUnauthorized(err) || k8sErrors.IsForbidden(err) {
+		return &ErrUnauthorized{
+			k8sErr: err,
+		}
+	}
+
+	return &ErrUnknown{
+		k8sErr: err,
+	}
+}
+
+type ErrUnknown struct {
+	k8sErr error
+}
+
+func (e *ErrUnknown) Error() string {
+	return fmt.Sprintf("Unknown or unhandled error: %s", e.k8sErr.Error())
+}
+
+// For ECONNREFUSED and errors.IsTimeout
+type ErrConnection struct {
+	k8sErr error
+}
+
+func (e *ErrConnection) Error() string {
+	return fmt.Sprintf("Could not connect to cluster: %s", e.k8sErr.Error())
+}
+
+// For errors.IsForbidden and errors.IsUnauthorized
+type ErrUnauthorized struct {
+	k8sErr error
+}
+
+func (e *ErrUnauthorized) Error() string {
+	return fmt.Sprintf("Unauthorized: %s", e.k8sErr.Error())
+}

+ 3 - 0
internal/models/cluster.go

@@ -116,6 +116,9 @@ type ClusterDetailedExternal struct {
 
 	// The NGINX Ingress IP to access the cluster
 	IngressIP string `json:"ingress_ip"`
+
+	// Error displayed in case couldn't get the IP
+	IngressError string `json:"ingress_error"`
 }
 
 func (c *Cluster) DetailedExternalize() *ClusterDetailedExternal {

+ 5 - 1
server/api/cluster_handler.go

@@ -100,12 +100,16 @@ func (app *App) HandleReadProjectCluster(w http.ResponseWriter, r *http.Request)
 		agent, _ = kubernetes.GetAgentOutOfClusterConfig(form.OutOfClusterConfig)
 	}
 
-	endpoint, found, _ := domain.GetNGINXIngressServiceIP(agent.Clientset)
+	endpoint, found, ingressErr := domain.GetNGINXIngressServiceIP(agent.Clientset)
 
 	if found {
 		clusterExt.IngressIP = endpoint
 	}
 
+	if !found && ingressErr != nil {
+		clusterExt.IngressError = kubernetes.CatchK8sConnectionError(ingressErr).Error()
+	}
+
 	w.WriteHeader(http.StatusOK)
 
 	if err := json.NewEncoder(w).Encode(clusterExt); err != nil {