Skip to content

Commit 7430e63

Browse files
committed
[diagnose] Extract endpoints checks into declarative structure
Tool: gitpod/catfood.gitpod.cloud
1 parent a1b4f1d commit 7430e63

File tree

1 file changed

+66
-44
lines changed

1 file changed

+66
-44
lines changed

gitpod-network-check/cmd/checks.go

Lines changed: 66 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,69 @@ import (
2525
"k8s.io/apimachinery/pkg/util/wait"
2626
)
2727

28+
type TestsetName string
29+
30+
const (
31+
TestsetNameAwsServicesPodSubnet TestsetName = "aws-services-pod-subnet"
32+
TestSetNameAwsServicesMainSubnet TestsetName = "aws-services-main-subnet"
33+
TestSetNameHttpsHostsMainSubnet TestsetName = "https-hosts-main-subnet"
34+
)
35+
36+
type TestSet func(networkConfig *NetworkConfig) map[string]string
37+
38+
var testSets = map[TestsetName]TestSet{
39+
TestsetNameAwsServicesPodSubnet: func(networkConfig *NetworkConfig) map[string]string {
40+
return map[string]string{
41+
"SSM": fmt.Sprintf("https://ssm.%s.amazonaws.com", networkConfig.AwsRegion),
42+
"SSMmessages": fmt.Sprintf("https://ssmmessages.%s.amazonaws.com", networkConfig.AwsRegion),
43+
"Autoscaling": fmt.Sprintf("https://autoscaling.%s.amazonaws.com", networkConfig.AwsRegion),
44+
"CloudFormation": fmt.Sprintf("https://cloudformation.%s.amazonaws.com", networkConfig.AwsRegion),
45+
"EC2": fmt.Sprintf("https://ec2.%s.amazonaws.com", networkConfig.AwsRegion),
46+
"EC2messages": fmt.Sprintf("https://ec2messages.%s.amazonaws.com", networkConfig.AwsRegion),
47+
"EKS": fmt.Sprintf("https://eks.%s.amazonaws.com", networkConfig.AwsRegion),
48+
"Elastic LoadBalancing": fmt.Sprintf("https://elasticloadbalancing.%s.amazonaws.com", networkConfig.AwsRegion),
49+
"Kinesis Firehose": fmt.Sprintf("https://firehose.%s.amazonaws.com", networkConfig.AwsRegion),
50+
"KMS": fmt.Sprintf("https://kms.%s.amazonaws.com", networkConfig.AwsRegion),
51+
"CloudWatch": fmt.Sprintf("https://logs.%s.amazonaws.com", networkConfig.AwsRegion),
52+
"SecretsManager": fmt.Sprintf("https://secretsmanager.%s.amazonaws.com", networkConfig.AwsRegion),
53+
"Sts": fmt.Sprintf("https://sts.%s.amazonaws.com", networkConfig.AwsRegion),
54+
"ECR Api": fmt.Sprintf("https://api.ecr.%s.amazonaws.com", networkConfig.AwsRegion),
55+
"ECR": fmt.Sprintf("https://869456089606.dkr.ecr.%s.amazonaws.com", networkConfig.AwsRegion),
56+
}
57+
},
58+
TestSetNameAwsServicesMainSubnet: func(networkConfig *NetworkConfig) map[string]string {
59+
endpoints := map[string]string{
60+
"S3": fmt.Sprintf("https://s3.%s.amazonaws.com", networkConfig.AwsRegion),
61+
"DynamoDB": fmt.Sprintf("https://dynamodb.%s.amazonaws.com", networkConfig.AwsRegion),
62+
}
63+
if networkConfig.ApiEndpoint != "" {
64+
endpoints["ExecuteAPI"] = fmt.Sprintf("https://%s.execute-api.%s.amazonaws.com", networkConfig.ApiEndpoint, networkConfig.AwsRegion)
65+
}
66+
return endpoints
67+
},
68+
TestSetNameHttpsHostsMainSubnet: func(networkConfig *NetworkConfig) map[string]string {
69+
httpHosts := map[string]string{}
70+
for _, v := range networkConfig.HttpsHosts {
71+
host := strings.TrimSpace(v)
72+
parsedUrl, err := url.Parse(host)
73+
if err != nil {
74+
log.Warnf("🚧 Invalid Host: %s, skipping due to error: %v", host, err)
75+
continue
76+
}
77+
78+
if parsedUrl.Scheme == "" {
79+
httpHosts[host] = fmt.Sprintf("https://%s", host)
80+
} else if parsedUrl.Scheme == "https" {
81+
httpHosts[host] = parsedUrl.Host
82+
} else {
83+
log.Warnf("🚧 Unsupported scheme: %s, skipping test for %s", parsedUrl.Scheme, host)
84+
continue
85+
}
86+
}
87+
return httpHosts
88+
},
89+
}
90+
2891
var checkCommand = &cobra.Command{ // nolint:gochecknoglobals
2992
PersistentPreRunE: validateSubnets,
3093
Use: "diagnose",
@@ -110,53 +173,12 @@ var checkCommand = &cobra.Command{ // nolint:gochecknoglobals
110173
}
111174

112175
log.Infof("ℹ️ Checking if the required AWS Services can be reached from the ec2 instances in the pod subnet")
113-
serviceEndpoints := map[string]string{
114-
"SSM": fmt.Sprintf("https://ssm.%s.amazonaws.com", networkConfig.AwsRegion),
115-
"SSMmessages": fmt.Sprintf("https://ssmmessages.%s.amazonaws.com", networkConfig.AwsRegion),
116-
"Autoscaling": fmt.Sprintf("https://autoscaling.%s.amazonaws.com", networkConfig.AwsRegion),
117-
"CloudFormation": fmt.Sprintf("https://cloudformation.%s.amazonaws.com", networkConfig.AwsRegion),
118-
"EC2": fmt.Sprintf("https://ec2.%s.amazonaws.com", networkConfig.AwsRegion),
119-
"EC2messages": fmt.Sprintf("https://ec2messages.%s.amazonaws.com", networkConfig.AwsRegion),
120-
"EKS": fmt.Sprintf("https://eks.%s.amazonaws.com", networkConfig.AwsRegion),
121-
"Elastic LoadBalancing": fmt.Sprintf("https://elasticloadbalancing.%s.amazonaws.com", networkConfig.AwsRegion),
122-
"Kinesis Firehose": fmt.Sprintf("https://firehose.%s.amazonaws.com", networkConfig.AwsRegion),
123-
"KMS": fmt.Sprintf("https://kms.%s.amazonaws.com", networkConfig.AwsRegion),
124-
"CloudWatch": fmt.Sprintf("https://logs.%s.amazonaws.com", networkConfig.AwsRegion),
125-
"SecretsManager": fmt.Sprintf("https://secretsmanager.%s.amazonaws.com", networkConfig.AwsRegion),
126-
"Sts": fmt.Sprintf("https://sts.%s.amazonaws.com", networkConfig.AwsRegion),
127-
"ECR Api": fmt.Sprintf("https://api.ecr.%s.amazonaws.com", networkConfig.AwsRegion),
128-
"ECR": fmt.Sprintf("https://869456089606.dkr.ecr.%s.amazonaws.com", networkConfig.AwsRegion),
129-
}
130-
checkServicesAvailability(cmd.Context(), ssmClient, InstanceIds, serviceEndpoints)
176+
checkServicesAvailability(cmd.Context(), ssmClient, InstanceIds, testSets[TestsetNameAwsServicesPodSubnet](&networkConfig))
131177

132178
log.Infof("ℹ️ Checking if certain AWS Services can be reached from ec2 instances in the main subnet")
133-
serviceEndpointsForMain := map[string]string{
134-
"S3": fmt.Sprintf("https://s3.%s.amazonaws.com", networkConfig.AwsRegion),
135-
"DynamoDB": fmt.Sprintf("https://dynamodb.%s.amazonaws.com", networkConfig.AwsRegion),
136-
}
137-
if networkConfig.ApiEndpoint != "" {
138-
serviceEndpointsForMain["ExecuteAPI"] = fmt.Sprintf("https://%s.execute-api.%s.amazonaws.com", networkConfig.ApiEndpoint, networkConfig.AwsRegion)
139-
}
140-
checkServicesAvailability(cmd.Context(), ssmClient, mainInstanceIds, serviceEndpointsForMain)
179+
checkServicesAvailability(cmd.Context(), ssmClient, mainInstanceIds, testSets[TestSetNameAwsServicesMainSubnet](&networkConfig))
141180

142-
httpHosts := map[string]string{}
143-
for _, v := range networkConfig.HttpsHosts {
144-
host := strings.TrimSpace(v)
145-
parsedUrl, err := url.Parse(host)
146-
if err != nil {
147-
log.Warnf("🚧 Invalid Host: %s, skipping due to error: %v", host, err)
148-
continue
149-
}
150-
151-
if parsedUrl.Scheme == "" {
152-
httpHosts[host] = fmt.Sprintf("https://%s", host)
153-
} else if parsedUrl.Scheme == "https" {
154-
httpHosts[host] = parsedUrl.Host
155-
} else {
156-
log.Warnf("🚧 Unsupported scheme: %s, skipping test for %s", parsedUrl.Scheme, host)
157-
continue
158-
}
159-
}
181+
httpHosts := testSets[TestSetNameHttpsHostsMainSubnet](&networkConfig)
160182
if len(httpHosts) > 0 {
161183
log.Infof("ℹ️ Checking if hosts can be reached with HTTPS from ec2 instances in the main subnets")
162184
}

0 commit comments

Comments
 (0)