forked from giantswarm/aws-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
111 lines (90 loc) · 2.6 KB
/
service.go
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package healthz
import (
"context"
"fmt"
"time"
"github.com/aws/aws-sdk-go/service/iam"
healthzservice "github.com/giantswarm/microendpoint/service/healthz"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
awsutil "github.com/giantswarm/aws-operator/client/aws"
)
const (
// AWSRegion is required even though the IAM API is global.
AWSRegion string = "eu-central-1"
// Description describes which functionality this health check implements.
Description = "Ensure AWS API availability."
// Name is the identifier of the health check. This can be used for emitting
// metrics.
Name = "aws"
// SuccessMessage is the message returned in case the health check did not
// fail.
SuccessMessage = "all good"
// Timeout is the time being waited until timing out health check, which
// renders its result unsuccessful.
Timeout = 5 * time.Second
)
// Config represents the configuration used to create a healthz service.
type Config struct {
Logger micrologger.Logger
AwsConfig awsutil.Config
}
// New creates a new configured healthz service.
func New(config Config) (*Service, error) {
if config.Logger == nil {
return nil, microerror.Maskf(invalidConfigError, "logger must not be empty")
}
var emptyAwsConfig awsutil.Config
if config.AwsConfig == emptyAwsConfig {
return nil, microerror.Maskf(invalidConfigError, "config.AwsConfig must not be empty")
}
newService := &Service{
logger: config.Logger,
awsConfig: config.AwsConfig,
}
return newService, nil
}
// Service implements the healthz service interface.
type Service struct {
logger micrologger.Logger
awsConfig awsutil.Config
}
// GetHealthz implements the health check for AWS. It does this by calling the
// IAM API to get the current user. This checks that we can connect to the API
// and the credentials are correct.
func (s *Service) GetHealthz(ctx context.Context) (healthzservice.Response, error) {
failed := false
message := SuccessMessage
{
ch := make(chan string, 1)
go func() {
// Set the region for the API client.
s.awsConfig.Region = AWSRegion
clients := awsutil.NewClients(s.awsConfig)
// Get the current user.
_, err := clients.IAM.GetUser(&iam.GetUserInput{})
if err != nil {
ch <- err.Error()
return
}
ch <- ""
}()
select {
case m := <-ch:
if m != "" {
failed = true
message = m
}
case <-time.After(Timeout):
failed = true
message = fmt.Sprintf("timed out after %s", Timeout)
}
}
response := healthzservice.Response{
Description: Description,
Failed: failed,
Message: message,
Name: Name,
}
return response, nil
}