-
Notifications
You must be signed in to change notification settings - Fork 1k
/
AmazonHealthIndicator.groovy
88 lines (76 loc) · 3.08 KB
/
AmazonHealthIndicator.groovy
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
/*
* Copyright 2015 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.spinnaker.clouddriver.aws.health
import com.amazonaws.AmazonClientException
import com.amazonaws.AmazonServiceException
import com.netflix.spinnaker.clouddriver.aws.security.AmazonClientProvider
import com.netflix.spinnaker.clouddriver.aws.security.NetflixAmazonCredentials
import com.netflix.spinnaker.clouddriver.security.AccountCredentialsProvider
import groovy.transform.InheritConstructors
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.actuate.health.Health
import org.springframework.boot.actuate.health.HealthIndicator
import org.springframework.http.HttpStatus
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component
import org.springframework.web.bind.annotation.ResponseStatus
import java.util.concurrent.atomic.AtomicReference
@Component
class AmazonHealthIndicator implements HealthIndicator {
private static final Logger LOG = LoggerFactory.getLogger(AmazonHealthIndicator)
@Autowired
AccountCredentialsProvider accountCredentialsProvider
@Autowired
AmazonClientProvider amazonClientProvider
private final AtomicReference<Exception> lastException = new AtomicReference<>(null)
@Override
Health health() {
def ex = lastException.get()
if (ex) {
throw ex
}
new Health.Builder().up().build()
}
@Scheduled(fixedDelay = 120000L)
void checkHealth() {
try {
Set<NetflixAmazonCredentials> amazonCredentials = accountCredentialsProvider.all.findAll {
it instanceof NetflixAmazonCredentials
} as Set<NetflixAmazonCredentials>
for (NetflixAmazonCredentials credentials in amazonCredentials) {
try {
def region = AmazonClientProvider.DEFAULT_REGION ?: credentials.getRegions().get(0).name
def ec2 = amazonClientProvider.getAmazonEC2(credentials, region, true)
if (!ec2) {
throw new AmazonClientException("Could not create Amazon client for ${credentials.name}")
}
ec2.describeAccountAttributes()
} catch (AmazonServiceException e) {
throw new AmazonUnreachableException(e)
}
}
lastException.set(null)
} catch (Exception ex) {
LOG.error "Unhealthy", ex
lastException.set(ex)
}
}
@ResponseStatus(value = HttpStatus.SERVICE_UNAVAILABLE, reason = 'Could not reach Amazon.')
@InheritConstructors
static class AmazonUnreachableException extends RuntimeException {}
}