-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathGet.java
More file actions
429 lines (383 loc) · 16.7 KB
/
Get.java
File metadata and controls
429 lines (383 loc) · 16.7 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package io.kestra.plugin.kubernetes.kubectl;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URI;
import java.time.Duration;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import io.kestra.core.models.annotations.Example;
import io.kestra.core.models.annotations.Metric;
import io.kestra.core.models.annotations.Plugin;
import io.kestra.core.models.executions.metrics.Counter;
import io.kestra.core.models.property.Property;
import io.kestra.core.models.tasks.RunnableTask;
import io.kestra.core.models.tasks.common.FetchType;
import io.kestra.core.runners.RunContext;
import io.kestra.core.serializers.FileSerde;
import io.kestra.plugin.kubernetes.AbstractPod;
import io.kestra.plugin.kubernetes.models.Metadata;
import io.kestra.plugin.kubernetes.models.ResourceStatus;
import io.kestra.plugin.kubernetes.services.PodService;
import io.kestra.plugin.kubernetes.services.ResourceWaitService;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.dsl.base.ResourceDefinitionContext;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.*;
import lombok.experimental.SuperBuilder;
import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxSink;
import static io.kestra.core.models.tasks.common.FetchType.NONE;
import io.kestra.core.models.annotations.PluginProperty;
@SuperBuilder
@ToString
@EqualsAndHashCode
@Getter
@NoArgsConstructor
@Plugin(
examples = {
@Example(
title = "Get all pods from Kubernetes with a service account and log the status of the pods.",
full = true,
code = """
id: get_all_pods
namespace: company.team
tasks:
- id: get
type: io.kestra.plugin.kubernetes.kubectl.Get
connection:
masterUrl: "{{ secret('K8S_MASTER_URL') }}"
oauthToken: "{{ secret('K8S_TOKEN') }}"
namespace: default
resourceType: pods
fetchType: FETCH
- id: log
type: io.kestra.plugin.core.log.Log
message: "{{ outputs.get.statusItems }}"
"""
),
@Example(
title = "Get one deployment named 'my-deployment' from Kubernetes with a service account and log the status of the pod.",
full = true,
code = """
id: get_one_deployment
namespace: company.team
tasks:
- id: get
type: io.kestra.plugin.kubernetes.kubectl.Get
connection:
masterUrl: "{{ secret('K8S_MASTER_URL') }}"
oauthToken: "{{ secret('K8S_TOKEN') }}"
trustCerts: true
namespace: default
resourceType: deployments
resourcesNames:
- my-deployment
fetchType: FETCH_ONE
- id: log_status
type: io.kestra.plugin.core.log.Log
message: "{{ outputs.get.statusItem }}"
"""
),
@Example(
title = "Get two deployments named my-deployment and my-deployment-2 from Kubernetes and store them in the internal storage.",
full = true,
code = """
id: get_two_deployments
namespace: company.team
tasks:
- id: get
type: io.kestra.plugin.kubernetes.kubectl.Get
connection:
masterUrl: "{{ secret('K8S_MASTER_URL') }}"
oauthToken: "{{ secret('K8S_TOKEN') }}"
namespace: default
resourceType: deployments
resourcesNames:
- my-deployment
- my-deployment-2
fetchType: STORE
"""
),
@Example(
title = "Get one custom resource named Shirt from Kubernetes.",
full = true,
code = """
id: get_one_custom_resource
namespace: company.team
tasks:
- id: get
type: io.kestra.plugin.kubernetes.kubectl.Get
connection:
masterUrl: "{{ secret('K8S_MASTER_URL') }}"
oauthToken: "{{ secret('K8S_TOKEN') }}"
namespace: default
resourceType: shirts # could be Shirt
apiGroup: stable.example.com
apiVersion: v1
fetchType: FETCH_ONE
"""
),
@Example(
title = "Get a custom resource and wait for it to become ready.",
full = true,
code = """
id: get_and_wait_for_custom_resource
namespace: company.team
tasks:
- id: get
type: io.kestra.plugin.kubernetes.kubectl.Get
connection:
masterUrl: "{{ secret('K8S_MASTER_URL') }}"
oauthToken: "{{ secret('K8S_TOKEN') }}"
namespace: default
resourceType: myresource
apiGroup: example.com
apiVersion: v1
resourcesNames:
- my-resource
fetchType: FETCH_ONE
waitUntilReady: PT10M
"""
),
},
metrics = {
@Metric(
name = "fetch.size",
type = Counter.TYPE,
unit = "records",
description = "The number of rows fetch."
),
}
)
@Schema(
title = "Fetch Kubernetes resources with optional storage",
description = "Reads resources of a given kind in a namespace, optionally filtering by name. Supports waiting for readiness, returning data to the flow, or storing results in internal storage depending on fetchType."
)
public class Get extends AbstractPod implements RunnableTask<Get.Output> {
@Schema(
title = "Resource kind",
description = "Kubernetes kind (e.g., Pod, Deployment, Service). Case-insensitive."
)
@NotNull
@PluginProperty(group = "main")
private Property<String> resourceType;
@Schema(
title = "Resource names",
description = "Optional list of names to fetch. When empty, all resources of the kind in the namespace are returned."
)
@PluginProperty(group = "source")
private Property<List<String>> resourcesNames;
@Schema(
title = "API group",
description = "Group for the resource kind (empty for core resources)."
)
@PluginProperty(group = "advanced")
private Property<String> apiGroup;
@Schema(
title = "API version",
description = "Version for the resource kind. Defaults to v1 when omitted."
)
@PluginProperty(group = "advanced")
private Property<String> apiVersion;
@Schema(
title = "Fetch behavior",
description = "Determines the output: NONE returns only metrics; FETCH returns lists; FETCH_ONE returns a single item; STORE writes to internal storage and returns URI."
)
@NotNull
@Builder.Default
@PluginProperty(group = "processing")
protected Property<FetchType> fetchType = Property.ofValue(NONE);
@Override
public Output run(RunContext runContext) throws Exception {
var rNamespace = runContext.render(this.namespace).as(String.class)
.orElseThrow(() -> new IllegalArgumentException("namespace must be provided and rendered."));
var rResourceType = runContext.render(this.resourceType).as(String.class)
.orElseThrow(() -> new IllegalArgumentException("resourceType must be provided and rendered."));
var rResourcesNames = runContext.render(this.resourcesNames).asList(String.class);
var rApiGroup = runContext.render(this.apiGroup).as(String.class).orElse("");
var rApiVersion = runContext.render(this.apiVersion).as(String.class).orElse("v1");
var rFetchType = runContext.render(this.fetchType).as(FetchType.class).orElse(NONE);
var rWaitUntilReady = runContext.render(this.waitUntilReady).as(Duration.class).orElse(Duration.ZERO);
List<Metadata> metadataList = new ArrayList<>();
List<ResourceStatus> statusList = new ArrayList<>();
Logger logger = runContext.logger();
try (var client = PodService.client(runContext, this.getConnection())) {
var resourceDefinitionContext = new ResourceDefinitionContext.Builder()
// See: https://kubernetes.io/docs/reference/using-api/api-concepts/#resource-uris
.withGroup(rApiGroup)
.withVersion(rApiVersion)
.withKind(rResourceType)
.withNamespaced(true) // Assuming resources are namespaced as we take namespace input
.build();
if (rResourcesNames.isEmpty()) {
logger.debug("Fetching all resources of kind '{}' in namespace '{}'", rResourceType, rNamespace);
var resources = client.genericKubernetesResources(resourceDefinitionContext)
.inNamespace(rNamespace)
.list()
.getItems();
for (var resource : resources) {
if (resource != null && resource.getMetadata() != null) {
metadataList.add(Metadata.from(resource.getMetadata()));
statusList.add(ResourceStatus.from(resource));
}
}
logger.info("Fetched {} resource(s) of kind '{}' in namespace '{}'", metadataList.size(), rResourceType, rNamespace);
} else {
rResourcesNames.forEach(name ->
{
logger.debug(
"Fetching resource of kind '{}' with name '{}' in namespace '{}'",
rResourceType, name, rNamespace
);
var resource = client.genericKubernetesResources(resourceDefinitionContext)
.inNamespace(rNamespace)
.withName(name)
.get();
if (resource != null && resource.getMetadata() != null) {
// Optionally wait for resource to become ready
if (!rWaitUntilReady.isZero()) {
runContext.logger().info("Waiting for resource '{}' to become ready (timeout: {})...", name, rWaitUntilReady);
resource = ResourceWaitService.waitForReady(
client,
resourceDefinitionContext,
rNamespace,
name,
rWaitUntilReady,
runContext.logger()
);
runContext.logger().info("Resource '{}' is ready", name);
}
metadataList.add(Metadata.from(resource.getMetadata()));
statusList.add(ResourceStatus.from(resource));
logger.info(
"Fetched resource of kind '{}' with name '{}' in namespace '{}'",
rResourceType, name, rNamespace
);
} else {
logger.warn(
"Resource of kind '{}' with name '{}' not found in namespace '{}'",
rResourceType, name, rNamespace
);
}
}
);
}
} catch (KubernetesClientException e) {
logger.error("Kubernetes API error while fetching kind '{}' in namespace '{}': {}", rResourceType, rNamespace, e.getMessage(), e);
throw new Exception("Failed to interact with Kubernetes API: " + e.getMessage(), e);
} catch (IllegalArgumentException e) {
logger.error("Configuration error: {}", e.getMessage(), e);
throw e;
}
Output output;
int fetchedItemsCount = metadataList.size();
switch (rFetchType) {
case NONE:
output = Output.builder().build();
runContext.metric(Counter.of("fetch.size", 0, "fetch", "false", "store", "false"));
break;
case FETCH:
output = Output.builder()
.metadataItems(metadataList)
.statusItems(statusList)
.size(fetchedItemsCount)
.build();
runContext.metric(Counter.of("fetch.size", fetchedItemsCount, "fetch", "true", "store", "false"));
break;
case FETCH_ONE:
output = Output.builder()
.metadataItem(metadataList.isEmpty() ? null : metadataList.getFirst())
.statusItem(statusList.isEmpty() ? null : statusList.getFirst())
.size(fetchedItemsCount)
.build();
runContext.metric(Counter.of("fetch.size", fetchedItemsCount, "fetch", "true", "store", "false"));
break;
case STORE:
var result = storeResult(metadataList, statusList, runContext);
int storedItemsCount = result.getValue().intValue();
output = Output.builder()
.uri(result.getKey())
.size(storedItemsCount)
.build();
runContext.metric(Counter.of("fetch.size", storedItemsCount, "fetch", "false", "store", "true"));
break;
default:
throw new IllegalStateException("Unexpected fetchType value: " + fetchType);
}
return output;
}
@Builder
public record ResourceInfo(Metadata metadata, ResourceStatus status) {
}
@Getter
@Builder
public static class Output implements io.kestra.core.models.tasks.Output {
@Schema(
title = "Metadata list",
description = "Only available when `fetchType` is set to `FETCH`."
)
private final List<Metadata> metadataItems;
@Schema(
title = "Single metadata",
description = "Only available when `fetchType` is set to `FETCH_ONE`."
)
private final Metadata metadataItem;
@Schema(
title = "Status list",
description = "Only available when `fetchType` is set to `FETCH`."
)
private final List<ResourceStatus> statusItems;
@Schema(
title = "Single status",
description = "Only available when `fetchType` is set to `FETCH_ONE`."
)
private final ResourceStatus statusItem;
@Schema(
title = "Stored result URI",
description = "Only available when `fetchType` is set to `STORE`."
)
private final URI uri;
@Schema(
title = "Resource count"
)
private Integer size;
}
private Map.Entry<URI, Long> storeResult(List<Metadata> metadataList, List<ResourceStatus> statusList, RunContext runContext) throws IOException {
var tempFile = runContext.workingDir().createTempFile(".ion").toFile();
// Combine metadata and status into ResourceInfo objects
List<ResourceInfo> resourceInfoList = new ArrayList<>();
for (int i = 0; i < metadataList.size(); i++) {
resourceInfoList.add(
ResourceInfo.builder()
.metadata(metadataList.get(i))
.status(i < statusList.size() ? statusList.get(i) : null)
.build()
);
}
try (
var output = new BufferedWriter(new FileWriter(tempFile), FileSerde.BUFFER_SIZE)
) {
var flowable = Flux
.create(
s ->
{
resourceInfoList.forEach(s::next);
s.complete();
},
FluxSink.OverflowStrategy.BUFFER
);
var count = FileSerde.writeAll(output, flowable);
var lineCount = count.block();
output.flush();
return new AbstractMap.SimpleEntry<>(
runContext.storage().putFile(tempFile),
lineCount
);
}
}
}