-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathApply.java
More file actions
258 lines (237 loc) · 10.3 KB
/
Apply.java
File metadata and controls
258 lines (237 loc) · 10.3 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
package io.kestra.plugin.kubernetes.kubectl;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import io.kestra.core.models.annotations.Example;
import io.kestra.core.models.annotations.Plugin;
import io.kestra.core.models.property.Property;
import io.kestra.core.models.tasks.RunnableTask;
import io.kestra.core.runners.RunContext;
import io.kestra.plugin.kubernetes.AbstractPod;
import io.kestra.plugin.kubernetes.models.Metadata;
import io.kestra.plugin.kubernetes.services.PodService;
import io.kestra.plugin.kubernetes.services.ResourceWaitService;
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 io.kestra.core.models.annotations.PluginProperty;
@SuperBuilder
@ToString
@EqualsAndHashCode
@Getter
@NoArgsConstructor
@Plugin(
examples = {
@Example(
title = "Apply a Kubernetes resource.",
full = true,
code = """
id: create_or_replace_deployment
namespace: company.team
tasks:
- id: apply
type: io.kestra.plugin.kubernetes.kubectl.Apply
connection:
masterUrl: "{{ secret('K8S_MASTER_URL') }}"
oauthToken: "{{ secret('K8S_TOKEN') }}"
namespace: default
spec: |-
apiVersion: apps/v1
kind: Deployment
metadata:
name: mypod
labels:
app: mypod
spec:
replicas: 1
selector:
matchLabels:
app: mypod
template:
metadata:
labels:
app: mypod
spec:
containers:
- name: app
image: nginx:stable-alpine
ports:
- containerPort: 80
"""
),
@Example(
title = "Apply a Kubernetes resource, using a namespace file.",
full = true,
code = """
id: create_or_replace_deployment
namespace: company.team
tasks:
- id: apply
type: io.kestra.plugin.kubernetes.kubectl.Apply
connection:
masterUrl: "{{ secret('K8S_MASTER_URL') }}"
oauthToken: "{{ secret('K8S_TOKEN') }}"
namespaceFiles:
enabled: true
namespace: default
spec: "{{ read('deployment.yaml') }}"
"""
),
@Example(
title = "Apply a Kubernetes custom resource definition.",
full = true,
code = """
id: k8s
namespace: company.name
tasks:
- id: apply
type: io.kestra.plugin.kubernetes.kubectl.Apply
connection:
masterUrl: "{{ secret('K8S_MASTER_URL') }}"
oauthToken: "{{ secret('K8S_TOKEN') }}"
namespace: default
spec: |-
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: shirts.stable.example.com
spec:
group: stable.example.com
scope: Namespaced
names:
plural: shirts
singular: shirt
kind: Shirt
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
apiVersion:
type: string
kind:
type: string
metadata:
type: object
spec:
type: object
x-kubernetes-preserve-unknown-fields: true # Allows any fields in spec
properties:
# You should define your actual Shirt properties here later
# For example:
# color:
# type: string
# size:
# type: string
# enum: ["S", "M", "L", "XL"]
status:
type: object
x-kubernetes-preserve-unknown-fields: true # Allows any fields in status
properties:
# Define your status properties here
# message:
# type: string
"""
),
@Example(
title = "Apply a custom resource and wait for it to become ready.",
full = true,
code = """
id: apply_and_wait_for_custom_resource
namespace: company.team
tasks:
- id: apply
type: io.kestra.plugin.kubernetes.kubectl.Apply
connection:
masterUrl: "{{ secret('K8S_MASTER_URL') }}"
oauthToken: "{{ secret('K8S_TOKEN') }}"
namespace: default
waitUntilReady: PT10M
spec: |-
apiVersion: example.com/v1
kind: MyResource
metadata:
name: my-resource
spec:
foo: bar
"""
)
}
)
@Schema(
title = "Apply Kubernetes resources with server-side apply",
description = "Applies one or more YAML/JSON manifests using server-side apply, then optionally waits until each resource is Ready (waitUntilReady, default PT0S). Namespaces must be provided for namespaced kinds."
)
public class Apply extends AbstractPod implements RunnableTask<Apply.Output> {
@NotNull
@Schema(
title = "Resource manifest",
description = "YAML or JSON manifest to apply. Can include multiple documents separated by '---'. Supports template expressions before apply."
)
@PluginProperty(group = "main")
private Property<String> spec;
@Override
public Apply.Output run(RunContext runContext) throws Exception {
var rNamespace = runContext.render(this.namespace).as(String.class).orElseThrow();
var rWaitUntilReady = runContext.render(this.waitUntilReady).as(Duration.class).orElse(Duration.ZERO);
try (var client = PodService.client(runContext, this.getConnection())) {
var resources = parseSpec(runContext.render(this.spec).as(String.class).orElseThrow());
Logger logger = runContext.logger();
logger.debug("Parsed resources: {}", resources);
List<Metadata> metadataList = new ArrayList<>();
for (var resource : resources) {
var resourceClient = client.resource(resource).inNamespace(rNamespace);
try {
var hasMetadata = resourceClient.unlock().serverSideApply();
metadataList.add(Metadata.from(hasMetadata.getMetadata()));
logger.info("Applied resource: {}", hasMetadata);
// Optionally wait for resource to become ready
if (!rWaitUntilReady.isZero()) {
var resourceMetadata = hasMetadata.getMetadata();
var resourceName = resourceMetadata.getName();
var apiVersion = hasMetadata.getApiVersion();
var kind = hasMetadata.getKind();
// Parse apiVersion to extract group and version
String group = "";
String version = apiVersion;
if (apiVersion != null && apiVersion.contains("/")) {
String[] parts = apiVersion.split("/", 2);
group = parts[0];
version = parts[1];
}
var resourceContext = new ResourceDefinitionContext.Builder()
.withGroup(group)
.withVersion(version)
.withKind(kind)
.withNamespaced(true)
.build();
logger.info("Waiting for resource '{}' to become ready (timeout: {})...", resourceName, rWaitUntilReady);
ResourceWaitService.waitForReady(client, resourceContext, rNamespace, resourceName, rWaitUntilReady, logger);
logger.info("Resource '{}' is ready", resourceName);
}
} catch (Exception exception) {
logger.error("Failed to apply resource: {}", resource, exception);
throw new Exception("Failed to apply resource: " + resource, exception);
}
}
return Output.builder()
.metadata(metadataList)
.build();
}
}
@Getter
@Builder
public static class Output implements io.kestra.core.models.tasks.Output {
@Schema(
title = "Applied resource metadata",
description = "Metadata returned by the API after server-side apply."
)
private final List<Metadata> metadata;
}
}