-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathEksResource.java
159 lines (134 loc) · 5.3 KB
/
EksResource.java
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.contrib.aws.resource;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.semconv.ResourceAttributes;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* A factory for a {@link Resource} which provides information about the current ECS container if
* running on AWS EKS.
*/
public final class EksResource {
private static final Logger logger = Logger.getLogger(EksResource.class.getName());
private static final JsonFactory JSON_FACTORY = new JsonFactory();
static final String K8S_SVC_URL = "https://kubernetes.default.svc";
static final String AUTH_CONFIGMAP_PATH = "/api/v1/namespaces/kube-system/configmaps/aws-auth";
static final String CW_CONFIGMAP_PATH =
"/api/v1/namespaces/amazon-cloudwatch/configmaps/cluster-info";
private static final String K8S_TOKEN_PATH =
"/var/run/secrets/kubernetes.io/serviceaccount/token";
private static final String K8S_CERT_PATH =
"/var/run/secrets/kubernetes.io/serviceaccount/ca.crt";
private static final Resource INSTANCE = buildResource();
/**
* Returns a factory for a {@link Resource} which provides information about the current ECS
* container if running on AWS EKS.
*/
public static Resource get() {
return INSTANCE;
}
private static Resource buildResource() {
return buildResource(new JdkHttpClient(), new DockerHelper(), K8S_TOKEN_PATH, K8S_CERT_PATH);
}
// Visible for testing
static Resource buildResource(
JdkHttpClient httpClient,
DockerHelper dockerHelper,
String k8sTokenPath,
String k8sKeystorePath) {
if (!isEks(k8sTokenPath, k8sKeystorePath, httpClient)) {
return Resource.empty();
}
AttributesBuilder attrBuilders = Attributes.builder();
attrBuilders.put(ResourceAttributes.CLOUD_PROVIDER, ResourceAttributes.CloudProviderValues.AWS);
attrBuilders.put(
ResourceAttributes.CLOUD_PLATFORM, ResourceAttributes.CloudPlatformValues.AWS_EKS);
String clusterName = getClusterName(httpClient);
if (clusterName != null && !clusterName.isEmpty()) {
attrBuilders.put(ResourceAttributes.K8S_CLUSTER_NAME, clusterName);
}
String containerId = dockerHelper.getContainerId();
if (containerId != null && !containerId.isEmpty()) {
attrBuilders.put(ResourceAttributes.CONTAINER_ID, containerId);
}
return Resource.create(attrBuilders.build(), ResourceAttributes.SCHEMA_URL);
}
private static boolean isEks(
String k8sTokenPath, String k8sKeystorePath, JdkHttpClient httpClient) {
if (!isK8s(k8sTokenPath, k8sKeystorePath)) {
logger.log(Level.FINE, "Not running on k8s.");
return false;
}
Map<String, String> requestProperties = new HashMap<>();
requestProperties.put("Authorization", getK8sCredHeader());
String awsAuth =
httpClient.fetchString(
"GET", K8S_SVC_URL + AUTH_CONFIGMAP_PATH, requestProperties, K8S_CERT_PATH);
return awsAuth != null && !awsAuth.isEmpty();
}
private static boolean isK8s(String k8sTokenPath, String k8sKeystorePath) {
File k8sTokeyFile = new File(k8sTokenPath);
File k8sKeystoreFile = new File(k8sKeystorePath);
return k8sTokeyFile.exists() && k8sKeystoreFile.exists();
}
private static String getClusterName(JdkHttpClient httpClient) {
Map<String, String> requestProperties = new HashMap<>();
requestProperties.put("Authorization", getK8sCredHeader());
String json =
httpClient.fetchString(
"GET", K8S_SVC_URL + CW_CONFIGMAP_PATH, requestProperties, K8S_CERT_PATH);
try (JsonParser parser = JSON_FACTORY.createParser(json)) {
parser.nextToken();
if (!parser.isExpectedStartObjectToken()) {
throw new IOException("Invalid JSON:" + json);
}
while (parser.nextToken() != JsonToken.END_OBJECT) {
parser.nextToken();
if (!parser.getCurrentName().equals("data")) {
parser.skipChildren();
continue;
}
if (!parser.isExpectedStartObjectToken()) {
throw new IOException("Invalid JSON:" + json);
}
while (parser.nextToken() != JsonToken.END_OBJECT) {
String value = parser.nextTextValue();
if (!parser.getCurrentName().equals("cluster.name")) {
parser.skipChildren();
continue;
}
return value;
}
}
} catch (IOException e) {
logger.log(Level.WARNING, "Can't get cluster name on EKS.", e);
}
return "";
}
private static String getK8sCredHeader() {
try {
String content =
new String(Files.readAllBytes(Paths.get(K8S_TOKEN_PATH)), StandardCharsets.UTF_8);
return "Bearer " + content;
} catch (IOException e) {
logger.log(Level.WARNING, "Unable to load K8s client token.", e);
}
return "";
}
private EksResource() {}
}