Skip to content

Commit 610747c

Browse files
authored
Improve various error messages (#1800)
1 parent 80be987 commit 610747c

File tree

15 files changed

+89
-49
lines changed

15 files changed

+89
-49
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ operator-local-dbg-gcp:
6060

6161
# configure kubectl to point to the cluster specified in dev/config/cluster-[aws|gcp].yaml
6262
kubectl-aws:
63-
@eval $$(python3 ./manager/cluster_config_env.py ./dev/config/cluster-aws.yaml) && eksctl utils write-kubeconfig --cluster="$$CORTEX_CLUSTER_NAME" --region="$$CORTEX_REGION" | grep -v "saved kubeconfig as" | grep -v "using region" | grep -v "eksctl version" || true
63+
@eval $$(python3 ./manager/cluster_config_env.py ./dev/config/cluster-aws.yaml) && eksctl utils write-kubeconfig --cluster="$$CORTEX_CLUSTER_NAME" --region="$$CORTEX_REGION" | (grep -v "saved kubeconfig as" | grep -v "using region" | grep -v "eksctl version" || true)
6464
kubectl-gcp:
65-
@eval $$(python3 ./manager/cluster_config_env.py ./dev/config/cluster-gcp.yaml) && gcloud container clusters get-credentials "$$CORTEX_CLUSTER_NAME" --zone "$$CORTEX_ZONE" --project "$$CORTEX_PROJECT" 2>&1 | grep -v "Fetching cluster" | grep -v "kubeconfig entry generated" || true
65+
@eval $$(python3 ./manager/cluster_config_env.py ./dev/config/cluster-gcp.yaml) && gcloud container clusters get-credentials "$$CORTEX_CLUSTER_NAME" --project "$$CORTEX_PROJECT" --region "$$CORTEX_ZONE" 2> /dev/stdout 1> /dev/null | (grep -v "Fetching cluster" | grep -v "kubeconfig entry generated" || true)
6666

6767
cluster-up-aws:
6868
@$(MAKE) images-all-aws

cli/cmd/cluster_gcp.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,24 @@ var _clusterGCPUpCmd = &cobra.Command{
141141
exit.Error(err)
142142
}
143143

144-
bucketName := clusterconfig.GCPBucketName(*accessConfig.ClusterName, *accessConfig.Project, *accessConfig.Zone)
144+
gkeClusterName := fmt.Sprintf("projects/%s/locations/%s/clusters/%s", *clusterConfig.Project, *clusterConfig.Zone, clusterConfig.ClusterName)
145+
bucketName := clusterconfig.GCPBucketName(clusterConfig.ClusterName, *clusterConfig.Project, *clusterConfig.Zone)
146+
147+
clusterExists, err := gcpClient.ClusterExists(gkeClusterName)
148+
if err != nil {
149+
exit.Error(err)
150+
}
151+
if clusterExists {
152+
exit.Error(ErrorGCPClusterAlreadyExists(clusterConfig.ClusterName, *clusterConfig.Zone, *clusterConfig.Project))
153+
}
154+
145155
err = gcpClient.CreateBucket(bucketName, gcp.ZoneToRegion(*accessConfig.Zone), true)
146156
if err != nil {
147157
exit.Error(err)
148158
}
149159

150160
err = createGKECluster(clusterConfig, gcpClient)
151161
if err != nil {
152-
if errors.GetKind(err) != gcp.ErrClusterAlreadyExists {
153-
gcpClient.DeleteBucket(bucketName)
154-
}
155162
exit.Error(err)
156163
}
157164

@@ -161,7 +168,6 @@ var _clusterGCPUpCmd = &cobra.Command{
161168
exit.Error(err)
162169
}
163170

164-
gkeClusterName := fmt.Sprintf("projects/%s/locations/%s/clusters/%s", *clusterConfig.Project, *clusterConfig.Zone, clusterConfig.ClusterName)
165171
operatorLoadBalancerIP, err := getGCPOperatorLoadBalancerIP(gkeClusterName, gcpClient)
166172
if err != nil {
167173
exit.Error(errors.Append(err, fmt.Sprintf("\n\nyou can attempt to resolve this issue and configure your cli environment by running `cortex cluster info --configure-env %s`", _flagClusterGCPUpEnv)))
@@ -238,6 +244,16 @@ var _clusterGCPDownCmd = &cobra.Command{
238244
}
239245

240246
gkeClusterName := fmt.Sprintf("projects/%s/locations/%s/clusters/%s", *accessConfig.Project, *accessConfig.Zone, *accessConfig.ClusterName)
247+
bucketName := clusterconfig.GCPBucketName(*accessConfig.ClusterName, *accessConfig.Project, *accessConfig.Zone)
248+
249+
clusterExists, err := gcpClient.ClusterExists(gkeClusterName)
250+
if err != nil {
251+
exit.Error(err)
252+
}
253+
if !clusterExists {
254+
gcpClient.DeleteBucket(bucketName) // silently try to delete the bucket in case it got left behind
255+
exit.Error(ErrorGCPClusterDoesntExist(*accessConfig.ClusterName, *accessConfig.Zone, *accessConfig.Project))
256+
}
241257

242258
// updating CLI env is best-effort, so ignore errors
243259
operatorLoadBalancerIP, _ := getGCPOperatorLoadBalancerIP(gkeClusterName, gcpClient)
@@ -248,7 +264,6 @@ var _clusterGCPDownCmd = &cobra.Command{
248264
prompt.YesOrExit(fmt.Sprintf("your cluster named \"%s\" in %s (zone: %s) will be spun down and all apis will be deleted, are you sure you want to continue?", *accessConfig.ClusterName, *accessConfig.Project, *accessConfig.Zone), "", "")
249265
}
250266

251-
bucketName := clusterconfig.GCPBucketName(*accessConfig.ClusterName, *accessConfig.Project, *accessConfig.Zone)
252267
fmt.Printf("○ deleting bucket %s ", bucketName)
253268
err = gcpClient.DeleteBucket(bucketName)
254269
if err != nil {
@@ -518,6 +533,10 @@ func createGKECluster(clusterConfig *clusterconfig.GCPConfig, gcpClient *gcp.Cli
518533
Cluster: &gkeClusterConfig,
519534
})
520535
if err != nil {
536+
fmt.Print("\n\n")
537+
if strings.Contains(errors.Message(err), "has no network named \"default\"") {
538+
err = errors.Append(err, "\n\nyou can specify a different network be setting the `network` field in your cluster configuration file (see https://docs.cortex.dev)")
539+
}
521540
return err
522541
}
523542

cli/cmd/errors.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ const (
7575
ErrShellCompletionNotSupported = "cli.shell_completion_not_supported"
7676
ErrNoTerminalWidth = "cli.no_terminal_width"
7777
ErrDeployFromTopLevelDir = "cli.deploy_from_top_level_dir"
78+
ErrGCPClusterAlreadyExists = "cli.gcp_cluster_already_exists"
79+
ErrGCPClusterDoesntExist = "cli.gcp_cluster_doesnt_exist"
7880
)
7981

8082
func ErrorInvalidProvider(providerStr string) error {
@@ -332,3 +334,17 @@ func ErrorDeployFromTopLevelDir(genericDirName string, providerType types.Provid
332334
Message: fmt.Sprintf("cannot deploy from your %s directory - when deploying your API, cortex sends all files in your project directory (i.e. the directory which contains cortex.yaml) to your cluster (see https://docs.cortex.dev/v/%s/); therefore it is recommended to create a subdirectory for your project files", genericDirName, consts.CortexVersionMinor),
333335
})
334336
}
337+
338+
func ErrorGCPClusterAlreadyExists(clusterName string, zone string, project string) error {
339+
return errors.WithStack(&errors.Error{
340+
Kind: ErrGCPClusterAlreadyExists,
341+
Message: fmt.Sprintf("there is already a cluster named \"%s\" in %s in the %s project", clusterName, zone, project),
342+
})
343+
}
344+
345+
func ErrorGCPClusterDoesntExist(clusterName string, zone string, project string) error {
346+
return errors.WithStack(&errors.Error{
347+
Kind: ErrGCPClusterDoesntExist,
348+
Message: fmt.Sprintf("there is no cluster named \"%s\" in %s in the %s project", clusterName, zone, project),
349+
})
350+
}

cli/cmd/version.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ var _versionCmd = &cobra.Command{
3939
Args: cobra.NoArgs,
4040
Run: func(cmd *cobra.Command, args []string) {
4141
envName, err := getEnvFromFlag(_flagVersionEnv)
42-
if err != nil {
42+
43+
if err != nil || envName == "" {
4344
telemetry.Event("cli.version")
44-
exit.Error(err)
45+
fmt.Println("cli version: " + consts.CortexVersion)
46+
return
4547
}
4648

4749
env, err := ReadOrConfigureEnv(envName)

manager/debug.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ if ! eksctl utils describe-stacks --cluster=$CORTEX_CLUSTER_NAME --region=$CORTE
2626
exit 1
2727
fi
2828

29-
eksctl utils write-kubeconfig --cluster=$CORTEX_CLUSTER_NAME --region=$CORTEX_REGION | grep -v "saved kubeconfig as" | grep -v "using region" | grep -v "eksctl version" || true
29+
eksctl utils write-kubeconfig --cluster=$CORTEX_CLUSTER_NAME --region=$CORTEX_REGION | (grep -v "saved kubeconfig as" | grep -v "using region" | grep -v "eksctl version" || true)
3030
out=$(kubectl get pods 2>&1 || true); if [[ "$out" == *"must be logged in to the server"* ]]; then echo "error: your aws iam user does not have access to this cluster; to grant access, see https://docs.cortex.dev/v/${CORTEX_VERSION_MINOR}/"; exit 1; fi
3131

3232
echo -n "gathering cluster data"

manager/debug_gcp.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ CORTEX_VERSION_MINOR=master
2121
debug_out_path="$1"
2222
mkdir -p "$(dirname "$debug_out_path")"
2323

24-
gcloud auth activate-service-account --key-file $GOOGLE_APPLICATION_CREDENTIALS > /dev/null 2>&1
25-
gcloud container clusters get-credentials $CORTEX_CLUSTER_NAME --project $CORTEX_GCP_PROJECT --region $CORTEX_GCP_ZONE > /dev/null 2>&1 # write both stderr and stdout to dev/null
24+
gcloud auth activate-service-account --key-file $GOOGLE_APPLICATION_CREDENTIALS 2> /dev/stdout 1> /dev/null | (grep -v "Activated service account credentials" || true)
25+
gcloud container clusters get-credentials $CORTEX_CLUSTER_NAME --project $CORTEX_GCP_PROJECT --region $CORTEX_GCP_ZONE 2> /dev/stdout 1> /dev/null | (grep -v "Fetching cluster" | grep -v "kubeconfig entry generated" || true)
2626
out=$(kubectl get pods 2>&1 || true); if [[ "$out" == *"must be logged in to the server"* ]]; then echo "error: your iam user does not have access to this cluster"; exit 1; fi
2727

2828
echo -n "gathering cluster data"

manager/info.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ if ! eksctl utils describe-stacks --cluster=$CORTEX_CLUSTER_NAME --region=$CORTE
3131
exit 1
3232
fi
3333

34-
eksctl utils write-kubeconfig --cluster=$CORTEX_CLUSTER_NAME --region=$CORTEX_REGION | grep -v "saved kubeconfig as" | grep -v "using region" | grep -v "eksctl version" || true
34+
eksctl utils write-kubeconfig --cluster=$CORTEX_CLUSTER_NAME --region=$CORTEX_REGION | (grep -v "saved kubeconfig as" | grep -v "using region" | grep -v "eksctl version" || true)
3535
out=$(kubectl get pods 2>&1 || true); if [[ "$out" == *"must be logged in to the server"* ]]; then echo "error: your aws iam user does not have access to this cluster; to grant access, see https://docs.cortex.dev/v/${CORTEX_VERSION_MINOR}/"; exit 1; fi
3636

3737
operator_endpoint=$(get_operator_endpoint)

manager/info_gcp.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ function get_api_load_balancer_endpoint() {
2626
kubectl -n=istio-system get service ingressgateway-apis -o json | tr -d '[:space:]' | sed 's/.*{\"ip\":\"\(.*\)\".*/\1/'
2727
}
2828

29-
gcloud auth activate-service-account --key-file $GOOGLE_APPLICATION_CREDENTIALS > /dev/null 2>&1
30-
gcloud container clusters get-credentials $CORTEX_CLUSTER_NAME --project $CORTEX_GCP_PROJECT --region $CORTEX_GCP_ZONE > /dev/null 2>&1 # write both stderr and stdout to dev/null
29+
gcloud auth activate-service-account --key-file $GOOGLE_APPLICATION_CREDENTIALS 2> /dev/stdout 1> /dev/null | (grep -v "Activated service account credentials" || true)
30+
gcloud container clusters get-credentials $CORTEX_CLUSTER_NAME --project $CORTEX_GCP_PROJECT --region $CORTEX_GCP_ZONE 2> /dev/stdout 1> /dev/null | (grep -v "Fetching cluster" | grep -v "kubeconfig entry generated" || true)
3131
out=$(kubectl get pods 2>&1 || true); if [[ "$out" == *"must be logged in to the server"* ]]; then echo "error: your iam user does not have access to this cluster"; exit 1; fi
3232

3333
operator_endpoint=$(get_operator_endpoint)

manager/install.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ function cluster_up_aws() {
9797
}
9898

9999
function cluster_up_gcp() {
100-
gcloud auth activate-service-account --key-file $GOOGLE_APPLICATION_CREDENTIALS > /dev/null 2>&1
101-
gcloud container clusters get-credentials $CORTEX_CLUSTER_NAME --project $CORTEX_GCP_PROJECT --region $CORTEX_GCP_ZONE > /dev/null 2>&1 # write both stderr and stdout to dev/null
100+
gcloud auth activate-service-account --key-file $GOOGLE_APPLICATION_CREDENTIALS 2> /dev/stdout 1> /dev/null | (grep -v "Activated service account credentials" || true)
101+
gcloud container clusters get-credentials $CORTEX_CLUSTER_NAME --project $CORTEX_GCP_PROJECT --region $CORTEX_GCP_ZONE 2> /dev/stdout 1> /dev/null | (grep -v "Fetching cluster" | grep -v "kubeconfig entry generated" || true)
102102

103103
start_pre_download_images
104104

@@ -248,7 +248,7 @@ function check_eks() {
248248
}
249249

250250
function write_kubeconfig() {
251-
eksctl utils write-kubeconfig --cluster=$CORTEX_CLUSTER_NAME --region=$CORTEX_REGION | grep -v "saved kubeconfig as" | grep -v "using region" | grep -v "eksctl version" || true
251+
eksctl utils write-kubeconfig --cluster=$CORTEX_CLUSTER_NAME --region=$CORTEX_REGION | (grep -v "saved kubeconfig as" | grep -v "using region" | grep -v "eksctl version" || true)
252252
out=$(kubectl get pods 2>&1 || true); if [[ "$out" == *"must be logged in to the server"* ]]; then echo "error: your aws iam user does not have access to this cluster; to grant access, see https://docs.cortex.dev/v/${CORTEX_VERSION_MINOR}/"; exit 1; fi
253253
}
254254

manager/refresh.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ if ! eksctl utils describe-stacks --cluster=$CORTEX_CLUSTER_NAME --region=$CORTE
2626
exit 1
2727
fi
2828

29-
eksctl utils write-kubeconfig --cluster=$CORTEX_CLUSTER_NAME --region=$CORTEX_REGION | grep -v "saved kubeconfig as" | grep -v "using region" | grep -v "eksctl version" || true
29+
eksctl utils write-kubeconfig --cluster=$CORTEX_CLUSTER_NAME --region=$CORTEX_REGION | (grep -v "saved kubeconfig as" | grep -v "using region" | grep -v "eksctl version" || true)
3030
out=$(kubectl get pods 2>&1 || true); if [[ "$out" == *"must be logged in to the server"* ]]; then echo "error: your aws iam user does not have access to this cluster; to grant access, see https://docs.cortex.dev/v/${CORTEX_VERSION_MINOR}/"; exit 1; fi
3131

3232
kubectl get -n=default configmap cluster-config -o yaml >> cluster_configmap.yaml

0 commit comments

Comments
 (0)