Skip to content

Commit

Permalink
[descriptors] Enable to force a content-type for PATCH method in virt…
Browse files Browse the repository at this point in the history
…ual attribute $bundlebeePatchContentType to override default strategic merge patch one without using force or update mode
  • Loading branch information
rmannibucau committed Aug 25, 2023
1 parent ec6b63f commit cc6a471
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.yupiik.bundlebee.core.lang.ConfigHolder;
import io.yupiik.bundlebee.core.qualifier.BundleBee;
import io.yupiik.bundlebee.core.yaml.Yaml2JsonConverter;
import lombok.Data;
import lombok.extern.java.Log;
import org.eclipse.microprofile.config.inject.ConfigProperty;

Expand Down Expand Up @@ -118,8 +119,9 @@ public class KubeClient implements ConfigHolder {
@Description("" +
"Enables to tolerate custom attributes in the descriptors. " +
"Typically used to drop `/$schema` attribute which enables a nice completion in editors. " +
"Values are `|` delimited and are either a JSON-Pointer (wrapped in a remove JSON-Patch) or directly a JSON-Patch.")
@ConfigProperty(name = "bundlebee.kube.implicitlyDroppedAttributes", defaultValue = "/$schema|/$bundlebeeIgnoredLintingRules")
"Values are `|` delimited and are either a JSON-Pointer (wrapped in a remove JSON-Patch) or directly a JSON-Patch. " +
"Using `none` ignores this processing.")
@ConfigProperty(name = "bundlebee.kube.implicitlyDroppedAttributes", defaultValue = "/$schema|/$bundlebeeIgnoredLintingRules|/$bundlebeePatchContentType")
private String implicitlyDroppedAttributes;

@Inject
Expand Down Expand Up @@ -149,6 +151,12 @@ public class KubeClient implements ConfigHolder {
@ConfigProperty(name = "bundlebee.kube.customMetadataInjectionPoint", defaultValue = "labels")
private String customMetadataInjectionPoint;

@Inject
@Description("Default header value for `PATCH` requests. It uses strategic merge patch algorithm but in some cases you just want to use `application/json` or (better) `application/merge-patch+json`. " +
"Note that this value can be overriden per descriptor using `$bundlebee_patch_content_type` root attribute.")
@ConfigProperty(name = "bundlebee.kube.patchContentType", defaultValue = "application/strategic-merge-patch+json")
private String patchContentType;

@Inject
@BundleBee
private ScheduledExecutorService scheduledExecutorService;
Expand All @@ -174,16 +182,18 @@ private void init() {
resourceMapping = Map.of();
}

this.implicitlyDrops = Stream.of(implicitlyDroppedAttributes.split("\\|"))
.map(it -> jsonProvider.createPatch(
it.startsWith("[") ?
jsonb.fromJson(it, JsonArray.class) :
jsonBuilderFactory.createArrayBuilder()
.add(jsonBuilderFactory.createObjectBuilder()
.add("op", JsonPatch.Operation.REMOVE.operationName())
.add("path", it))
.build()))
.collect(toList());
this.implicitlyDrops = "implicitlyDroppedAttributes".equals("none") ?
List.of() :
Stream.of(implicitlyDroppedAttributes.split("\\|"))
.map(it -> jsonProvider.createPatch(
it.startsWith("[") ?
jsonb.fromJson(it, JsonArray.class) :
jsonBuilderFactory.createArrayBuilder()
.add(jsonBuilderFactory.createObjectBuilder()
.add("op", JsonPatch.Operation.REMOVE.operationName())
.add("path", it))
.build()))
.collect(toList());
}

// for backward compatibility
Expand Down Expand Up @@ -259,7 +269,7 @@ private CompletionStage<?> doExists(final AtomicBoolean result, final JsonObject

public CompletionStage<?> apply(final String descriptorContent, final String ext,
final Map<String, String> customLabels) {
return forDescriptor("Applying", descriptorContent, ext, json -> doApply(json, customLabels));
return forDescriptorWithOriginal("Applying", descriptorContent, ext, item -> doApply(item.getRaw(), item.getPrepared(), customLabels));
}

public CompletionStage<?> delete(final String descriptorContent, final String ext, final int gracePeriod) {
Expand All @@ -268,6 +278,11 @@ public CompletionStage<?> delete(final String descriptorContent, final String ex

public <T> CompletionStage<List<T>> forDescriptor(final String prefixLog, final String descriptorContent, final String ext,
final Function<JsonObject, CompletionStage<T>> descHandler) {
return forDescriptorWithOriginal(prefixLog, descriptorContent, ext, item -> descHandler.apply(item.getPrepared()));
}

public <T> CompletionStage<List<T>> forDescriptorWithOriginal(final String prefixLog, final String descriptorContent, final String ext,
final Function<DescriptorItem, CompletionStage<T>> descHandler) {
if (api.isVerbose()) {
log.info(() -> prefixLog + " descriptor\n" + descriptorContent);
}
Expand All @@ -280,14 +295,15 @@ public <T> CompletionStage<List<T>> forDescriptor(final String prefixLog, final
return all(
json.asJsonArray().stream()
.map(JsonValue::asJsonObject)
.map(this::sanitizeJson)
.map(it -> new DescriptorItem(it, sanitizeJson(it)))
.map(descHandler)
.collect(toList()),
toList(),
true);
case OBJECT:
final var jsonObject = json.asJsonObject();
return descHandler
.apply(sanitizeJson(json.asJsonObject()))
.apply(new DescriptorItem(jsonObject, sanitizeJson(jsonObject)))
.thenApply(List::of);
default:
throw new IllegalArgumentException("Unsupported json type for apply: " + json);
Expand Down Expand Up @@ -359,7 +375,7 @@ private CompletionStage<?> doDelete(final JsonObject desc, final int gracePeriod
});
}

private CompletionStage<?> doApply(final JsonObject rawDesc, final Map<String, String> customLabels) {
private CompletionStage<?> doApply(final JsonObject originalDontUseDesc, final JsonObject rawDesc, final Map<String, String> customLabels) {
// apply logic is a "create or replace" one
// so first thing we have to do is to test if the resource exists, and if not create it
// for that we will need to extract the resource "kind" and "name" (id):
Expand All @@ -381,7 +397,7 @@ private CompletionStage<?> doApply(final JsonObject rawDesc, final Map<String, S
final var desc = customLabels.isEmpty() ? rawDesc : injectMetadata(rawDesc, customLabels);
final var kindLowerCased = desc.getString("kind").toLowerCase(ROOT) + 's';
return apiPreloader.ensureResourceSpec(desc, kindLowerCased)
.thenCompose(ignored -> doApply(rawDesc, desc, kindLowerCased, 1));
.thenCompose(ignored -> doApply(originalDontUseDesc, desc, kindLowerCased, 1));
}

private CompletionStage<HttpResponse<String>> doApply(final JsonObject rawDesc, final JsonObject preparedDesc,
Expand All @@ -396,7 +412,7 @@ private CompletionStage<HttpResponse<String>> doApply(final JsonObject rawDesc,
final var baseUri = toBaseUri(preparedDesc, kindLowerCased, namespace);

if (api.isVerbose()) {
log.info(() -> "Will apply descriptor " + rawDesc + " on " + baseUri);
log.info(() -> "Will apply descriptor " + preparedDesc + " on " + baseUri);
}

return api.execute(HttpRequest.newBuilder()
Expand Down Expand Up @@ -427,7 +443,7 @@ private CompletionStage<HttpResponse<String>> doApply(final JsonObject rawDesc,

final var desc = filterForApply("@" + kindLowerCased + "/" + namespace + '/' + name, preparedDesc, kindLowerCased);
if (obj == null || !kindsToSkipUpdateIfPossible.contains(kind) || needsUpdate(obj, desc)) {
return doUpdate(desc, name, fieldManager, baseUri)
return doUpdate(rawDesc, desc, name, fieldManager, baseUri)
.thenCompose(response -> {
if (api.isVerbose()) {
log.info(response::toString);
Expand All @@ -438,7 +454,7 @@ private CompletionStage<HttpResponse<String>> doApply(final JsonObject rawDesc,
response.body();
if (response.statusCode() == 422) { // try to get then update to forward the existing id
return injectResourceVersionInDescriptor(desc, name, baseUri, errorMessage)
.thenCompose(descWithResourceVersion -> doUpdate(descWithResourceVersion, name, fieldManager, baseUri)
.thenCompose(descWithResourceVersion -> doUpdate(rawDesc, descWithResourceVersion, name, fieldManager, baseUri)
.thenApply(recoverResponse -> {
if (api.isVerbose()) {
log.info(recoverResponse::toString);
Expand Down Expand Up @@ -564,7 +580,8 @@ private CompletionStage<JsonObject> injectResourceVersionInDescriptor(final Json
});
}

private CompletableFuture<HttpResponse<String>> doUpdate(final JsonObject desc,
private CompletableFuture<HttpResponse<String>> doUpdate(final JsonObject raw,
final JsonObject desc,
final String name,
final String fieldManager,
final String baseUri) {
Expand Down Expand Up @@ -593,10 +610,11 @@ private CompletableFuture<HttpResponse<String>> doUpdate(final JsonObject desc,
baseUri + "/" + name + fieldManager))
.toCompletableFuture();
}

return api.execute(
HttpRequest.newBuilder()
.method("PATCH", HttpRequest.BodyPublishers.ofString(desc.toString()))
.header("Content-Type", "application/strategic-merge-patch+json")
.header("Content-Type", raw.getString("$bundlebeePatchContentType", patchContentType))
.header("Accept", "application/json"),
baseUri + "/" + name + fieldManager)
.toCompletableFuture();
Expand Down Expand Up @@ -791,4 +809,10 @@ private Stream<Map.Entry<String, JsonValue>> mergeLabels(final Map.Entry<String,
JsonObjectBuilder::addAll,
JsonObjectBuilder::build))));
}

@Data
public static class DescriptorItem {
private final JsonObject raw;
private final JsonObject prepared;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ class ApplyCommandTest {
@HttpApiInject
private HttpApiHandler<?> handler;

@Test
void patchCustomContentType(final CommandExecutor executor, final TestInfo info) {
final var spyingResponseLocator = newSpyingHandler(info);
handler.setResponseLocator(spyingResponseLocator);

final var logs = executor.wrap(handler, INFO, () -> new BundleBee().launch(
"apply", "--alveolus", "customContentType", "--injectBundleBeeMetadata", "false", "--injectTimestamp", "false"));
assertEquals("Deploying 'customContentType'\nApplying 's0' (kind=services) for namespace 'default'\n", logs);

assertEquals(1, spyingResponseLocator.requests.size());
assertEquals("custom/json", spyingResponseLocator.requests.get(0).headers().get("Content-Type"));
}

@Test
void includeIfPatch(final CommandExecutor executor, final TestInfo info) {
final var spyingResponseLocator = newSpyingHandler(info);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void yaml2jsonCommand(final CommandExecutor executor, @TempDir Path work) {
"--bundlebee.yaml2json.input", "src/test/resources/bundlebee",
"--bundlebee.yaml2json.output", work.toAbsolutePath().toString()));
assertAll(
() -> assertTrue(logs.contains("Found 5 files to convert")),
() -> assertTrue(logs.contains("Found 6 files to convert")),
() -> assertTrue(Files.exists(work.resolve("kubernetes/ApplyCommandTest.d0.json"))),
() -> assertTrue(Files.exists(work.resolve("kubernetes/ApplyCommandTest.d1.json"))),
() -> assertTrue(Files.exists(work.resolve("kubernetes/ApplyCommandTest.d2.json"))),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#
# Copyright (c) 2021-2023 - Yupiik SAS - https://www.yupiik.com
# 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.
#

$bundlebeePatchContentType: "custom/json"
apiVersion: v1
kind: Service
metadata:
name: s0
labels:
app: s-test
spec:
type: NodePort
ports:
- port: 1234
targetPort: 1234
selector:
app: s-test
8 changes: 8 additions & 0 deletions bundlebee-core/src/test/resources/bundlebee/manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
{
"alveoli": [
{
"name": "customContentType",
"descriptors": [
{
"name": "customContentType"
}
]
},
{
"name": "ApplyCommandTest.includeIfPatch",
"descriptors": [
Expand Down

0 comments on commit cc6a471

Please sign in to comment.