feat(operator): add support for custom labels in ingress (#41627)

Signed-off-by: AvivGuiser <avivguiser@gmail.com>
This commit is contained in:
AvivGuiser 2025-08-06 10:09:42 +03:00 committed by GitHub
parent 5731cdf673
commit 9feca65665
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 31 additions and 4 deletions

View File

@ -115,6 +115,7 @@ public class KeycloakIngressDependentResource extends CRUDKubernetesDependentRes
.withNewMetadata()
.withName(getName(keycloak))
.withNamespace(keycloak.getMetadata().getNamespace())
.addToLabels(optionalSpec.map(IngressSpec::getLabels).orElse(null))
.addToLabels(Utils.allInstanceLabels(keycloak))
.addToAnnotations(annotations)
.endMetadata()

View File

@ -22,6 +22,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import io.sundr.builder.annotations.Buildable;
import java.util.LinkedHashMap;
import java.util.Map;
@JsonInclude(JsonInclude.Include.NON_NULL)
@ -41,6 +42,10 @@ public class IngressSpec {
@JsonPropertyDescription("A secret containing the TLS configuration for re-encrypt or TLS termination scenarios. Reference: https://kubernetes.io/docs/concepts/configuration/secret/#tls-secrets.")
private String tlsSecret;
@JsonProperty("labels")
@JsonPropertyDescription("Additional labels to be appended to the Ingress object")
Map<String, String> labels = new LinkedHashMap<String, String>();
public boolean isIngressEnabled() {
return ingressEnabled;
}
@ -72,4 +77,10 @@ public class IngressSpec {
public void setTlsSecret(String tlsSecret) {
this.tlsSecret = tlsSecret;
}
public Map<String, String> getLabels() {
return labels;
}
public void setLabels(Map<String, String> labels) {
this.labels = labels;
}
}

View File

@ -61,18 +61,22 @@ public class IngressLogicTest {
}
public static MockKeycloakIngress build(Boolean defaultIngressEnabled, boolean ingressExists, boolean ingressSpecDefined, boolean tlsConfigured) {
return build(defaultIngressEnabled, ingressExists, ingressSpecDefined, tlsConfigured, null, null);
return build(defaultIngressEnabled, ingressExists, ingressSpecDefined, tlsConfigured, null,null, null);
}
public static MockKeycloakIngress build(String hostname) {
return build(true, false, true, true, null, hostname);
return build(true, false, true, true, null,null, hostname);
}
public static MockKeycloakIngress build(Boolean defaultIngressEnabled, boolean ingressExists, boolean ingressSpecDefined, boolean tlsConfigured, Map<String, String> annotations) {
return build(defaultIngressEnabled, ingressExists, ingressSpecDefined, tlsConfigured, annotations, null);
return build(defaultIngressEnabled, ingressExists, ingressSpecDefined, tlsConfigured, annotations,null, null);
}
public static MockKeycloakIngress build(Boolean defaultIngressEnabled, boolean ingressExists, boolean ingressSpecDefined, boolean tlsConfigured, Map<String, String> annotations, String hostname) {
public static MockKeycloakIngress build(Boolean defaultIngressEnabled, boolean ingressExists, boolean ingressSpecDefined,Map<String, String> labels) {
return build(defaultIngressEnabled, ingressExists, ingressSpecDefined, true, null, labels, null);
}
public static MockKeycloakIngress build(Boolean defaultIngressEnabled, boolean ingressExists, boolean ingressSpecDefined, boolean tlsConfigured, Map<String, String> annotations, Map<String, String> labels,String hostname) {
IngressSpec ingressSpec = null;
if (ingressSpecDefined) {
ingressSpec = new IngressSpec();
@ -82,6 +86,9 @@ public class IngressLogicTest {
if (annotations != null) {
ingressSpec.setAnnotations(annotations);
}
if (labels != null) {
ingressSpec.setLabels(labels);
}
}
MockKeycloakIngress mock = new MockKeycloakIngress(tlsConfigured, ingressSpec, hostname);
if (ingressExists) {
@ -183,6 +190,14 @@ public class IngressLogicTest {
assertEquals("value", reconciled.get().getMetadata().getAnnotations().get("custom"));
assertFalse(reconciled.get().getMetadata().getAnnotations().containsKey(EXISTING_ANNOTATION_KEY));
}
@Test
public void testCustomLabels() {
var kc = MockKeycloakIngress.build(null, false, true, Map.of("custom", "value"));
Optional<Ingress> reconciled = kc.getReconciledResource();
assertTrue(reconciled.isPresent());
assertFalse(kc.deleted());
assertEquals("value", reconciled.get().getMetadata().getLabels().get("custom"));
}
@Test
public void testRemoveCustomAnnotation() {