github.com/telepresenceio/telepresence/v2@v2.20.0-pro.6.0.20240517030216-236ea954e789/pkg/client/cli/intercept/result.go (about)

     1  package intercept
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/telepresenceio/telepresence/rpc/v2/common"
     9  	"github.com/telepresenceio/telepresence/rpc/v2/connector"
    10  	"github.com/telepresenceio/telepresence/rpc/v2/manager"
    11  	"github.com/telepresenceio/telepresence/v2/pkg/errcat"
    12  )
    13  
    14  func Result(r *connector.InterceptResult, err error) error {
    15  	if r == nil || err != nil {
    16  		return err
    17  	}
    18  	msg := ""
    19  	errCat := errcat.Unknown
    20  	switch r.Error {
    21  	case common.InterceptError_UNSPECIFIED:
    22  		return nil
    23  	case common.InterceptError_INTERNAL:
    24  		msg = r.ErrorText
    25  	case common.InterceptError_NO_CONNECTION:
    26  		msg = "Local network is not connected to the cluster"
    27  	case common.InterceptError_NO_TRAFFIC_MANAGER:
    28  		msg = "Intercept unavailable: no traffic manager"
    29  	case common.InterceptError_TRAFFIC_MANAGER_CONNECTING:
    30  		msg = "Connecting to traffic manager..."
    31  	case common.InterceptError_TRAFFIC_MANAGER_ERROR:
    32  		msg = r.ErrorText
    33  	case common.InterceptError_ALREADY_EXISTS:
    34  		msg = fmt.Sprintf("Intercept with name %q already exists", r.ErrorText)
    35  	case common.InterceptError_NAMESPACE_AMBIGUITY:
    36  		nss := strings.Split(r.ErrorText, ",")
    37  		msg = fmt.Sprintf(
    38  			"Cannot create an intercept in namespace %q. A workstation cannot have simultaneous intercepts in different namespaces. Leave all intercepts in namespace %q first.",
    39  			nss[1], nss[0])
    40  	case common.InterceptError_LOCAL_TARGET_IN_USE:
    41  		spec := r.InterceptInfo.Spec
    42  		msg = fmt.Sprintf("Port %s:%d is already in use by intercept %s",
    43  			spec.TargetHost, spec.TargetPort, spec.Name)
    44  	case common.InterceptError_NO_ACCEPTABLE_WORKLOAD:
    45  		msg = fmt.Sprintf("No interceptable deployment, replicaset, or statefulset matching %s found", r.ErrorText)
    46  	case common.InterceptError_AMBIGUOUS_MATCH:
    47  		var matches []manager.AgentInfo
    48  		err := json.Unmarshal([]byte(r.ErrorText), &matches)
    49  		if err != nil {
    50  			msg = fmt.Sprintf("Unable to unmarshal JSON: %v", err)
    51  			break
    52  		}
    53  		st := &strings.Builder{}
    54  		fmt.Fprintf(st, "Found more than one possible match:")
    55  		for idx := range matches {
    56  			match := &matches[idx]
    57  			fmt.Fprintf(st, "\n%4d: %s.%s", idx+1, match.Name, match.Namespace)
    58  		}
    59  		msg = st.String()
    60  	case common.InterceptError_FAILED_TO_ESTABLISH:
    61  		msg = fmt.Sprintf("Failed to establish intercept: %s", r.ErrorText)
    62  	case common.InterceptError_UNSUPPORTED_WORKLOAD:
    63  		msg = fmt.Sprintf("Unsupported workload type: %s", r.ErrorText)
    64  	case common.InterceptError_NOT_FOUND:
    65  		msg = fmt.Sprintf("Intercept named %q not found", r.ErrorText)
    66  	case common.InterceptError_MOUNT_POINT_BUSY:
    67  		msg = fmt.Sprintf("Mount point already in use by intercept %q", r.ErrorText)
    68  	case common.InterceptError_MISCONFIGURED_WORKLOAD:
    69  		msg = r.ErrorText
    70  	case common.InterceptError_UNKNOWN_FLAG:
    71  		msg = fmt.Sprintf("Unknown flag: %s", r.ErrorText)
    72  	default:
    73  		msg = fmt.Sprintf("Unknown error code %d", r.Error)
    74  	}
    75  	if r.ErrorCategory > 0 {
    76  		errCat = errcat.Category(r.ErrorCategory)
    77  	}
    78  
    79  	if id := r.GetInterceptInfo().GetId(); id != "" {
    80  		msg = fmt.Sprintf("%s: id = %q", msg, id)
    81  	}
    82  	return errCat.Newf(msg)
    83  }