-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcluster_describer.go
More file actions
80 lines (63 loc) · 2.15 KB
/
Copy pathcluster_describer.go
File metadata and controls
80 lines (63 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package kafka
import (
"context"
"fmt"
"time"
"github.com/aws/aws-sdk-go-v2/aws/arn"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/kafka"
"github.com/aws/aws-sdk-go-v2/service/kafka/types"
)
const (
clusterConfigTimeout = 5 * time.Second
clusterDescriptionTimeout = 2 * time.Second
)
type clusterDescriber interface {
DescribeClusterV2(ctx context.Context, input *kafka.DescribeClusterV2Input, optFns ...func(*kafka.Options)) (*kafka.DescribeClusterV2Output, error)
}
func newClusterDescriber(clusterArn *string) (clusterDescriber, error) {
ctx, cancel := context.WithTimeout(context.Background(), clusterConfigTimeout)
defer cancel()
cfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
return nil, fmt.Errorf("loading config: %w", err)
}
client := kafka.NewFromConfig(cfg)
// Ensure the client is properly configured.
if _, err = retrieveClusterState(client, clusterArn); err != nil {
return nil, fmt.Errorf("retrieving cluster state: %w", err)
}
return client, nil
}
// Verifies whether the Kafka cluster is available or not.
// False positive healthcheck errors are being ignored during maintenance windows.
func verifyHealthErrorSeverity(healthErr error, describer clusterDescriber, clusterArn *string) error {
state, stateErr := retrieveClusterState(describer, clusterArn)
if stateErr != nil {
return fmt.Errorf("cluster status is unknown: %w", stateErr)
}
if isMaintenanceState(state) {
return nil
}
return healthErr
}
func retrieveClusterState(describer clusterDescriber, clusterArn *string) (types.ClusterState, error) {
parsedARN, err := arn.Parse(*clusterArn)
if err != nil {
return "", fmt.Errorf("error parsing cluster ARN: %w", err)
}
ctx, cancel := context.WithTimeout(context.Background(), clusterDescriptionTimeout)
defer cancel()
cluster, err := describer.DescribeClusterV2(ctx, &kafka.DescribeClusterV2Input{
ClusterArn: clusterArn,
}, func(opt *kafka.Options) {
opt.Region = parsedARN.Region
})
if err != nil {
return "", err
}
return cluster.ClusterInfo.State, nil
}
func isMaintenanceState(state types.ClusterState) bool {
return state == types.ClusterStateMaintenance
}