|
| 1 | +/** |
| 2 | + * ShinyProxy-Operator |
| 3 | + * |
| 4 | + * Copyright (C) 2021-2023 Open Analytics |
| 5 | + * |
| 6 | + * =========================================================================== |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the Apache License as published by |
| 10 | + * The Apache Software Foundation, either version 2 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * Apache License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the Apache License |
| 19 | + * along with this program. If not, see <http://www.apache.org/licenses/> |
| 20 | + */ |
| 21 | +package eu.openanalytics.shinyproxyoperator.components |
| 22 | + |
| 23 | +import com.fasterxml.jackson.databind.ObjectMapper |
| 24 | +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory |
| 25 | +import com.fasterxml.jackson.datatype.jsr353.JSR353Module |
| 26 | +import io.fabric8.kubernetes.api.model.HTTPGetAction |
| 27 | +import io.fabric8.kubernetes.api.model.IntOrString |
| 28 | +import io.fabric8.kubernetes.api.model.PodTemplateSpec |
| 29 | +import io.fabric8.kubernetes.api.model.networking.v1.Ingress |
| 30 | +import mu.KotlinLogging |
| 31 | +import javax.json.JsonPatch |
| 32 | +import javax.json.JsonStructure |
| 33 | + |
| 34 | +class IngressPatcher { |
| 35 | + private val mapper = ObjectMapper(YAMLFactory()) |
| 36 | + private val logger = KotlinLogging.logger { } |
| 37 | + |
| 38 | + init { |
| 39 | + mapper.registerModule(JSR353Module()) |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Applies a JsonPatch to the given Ingress. |
| 44 | + */ |
| 45 | + fun patch(ingress: Ingress, patch: JsonPatch?): Ingress { |
| 46 | + if (patch == null) { |
| 47 | + return ingress |
| 48 | + } |
| 49 | + |
| 50 | + logger.debug { "Original Ingress (before applying patches): ${mapper.writeValueAsString(ingress)}" } |
| 51 | + |
| 52 | + // 1. convert PodTemplate to javax.json.JsonValue object. |
| 53 | + // This conversion does not actually convert to a string, but some internal |
| 54 | + // representation of Jackson. |
| 55 | + val podAsJsonValue: JsonStructure = mapper.convertValue(ingress, JsonStructure::class.java) |
| 56 | + // 2. apply patch |
| 57 | + val patchedPodAsJsonValue: JsonStructure = patch.apply(podAsJsonValue) |
| 58 | + // 3. convert back to a PodTemplate |
| 59 | + val patchesIngress = mapper.convertValue(patchedPodAsJsonValue, Ingress::class.java) |
| 60 | + |
| 61 | + logger.debug { "Patched Ingress (after applying patches): ${mapper.writeValueAsString(patchesIngress)}" } |
| 62 | + |
| 63 | + return patchesIngress |
| 64 | + } |
| 65 | + |
| 66 | +} |
0 commit comments