8000 Operator Clustering support by andreaTP · Pull Request #10387 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/operator-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
- name: Test operator running in cluster
working-directory: operator
run: |
eval $(minikube -p minikube docker-env)
eval $(minikube -p minikube docker-env)
Comment thread
andreaTP marked this conversation as resolved.
Resolved
mvn clean verify \
-Dquarkus.container-image.build=true -Dquarkus.container-image.tag=test \
-Dquarkus.kubernetes.deployment-target=kubernetes \
Expand Down
16 changes: 16 additions & 0 deletions operator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,19 @@ Remove the created resources with:
```bash
kubectl delete -k <previously-used-folder>
```

### Testing

Testing allows 2 methods specified in the property `test.operator.deployment` : `local` & `remote`.

`local` : resources will be deployed to the local cluster and the operator will run out of the cluster

`remote` : same as local test but an image for the operator will be generated and deployed run inside the cluster

```bash
mvn clean verify \
-Dquarkus.container-image.build=true \
-Dquarkus.container-image.tag=test \
-Dquarkus.kubernetes.deployment-target=kubernetes \
-Dtest.operator.deployment=remote
```
5 changes: 5 additions & 0 deletions operator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@
<version>${awaitility.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
10 changes: 9 additions & 1 deletion operator/src/main/java/org/keycloak/operator/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public final class Constants {
);

public static final Map<String, String> DEFAULT_DIST_CONFIG = Map.of(
"KC_HEALTH_ENABLED", "true"
"KC_HEALTH_ENABLED","true",
"KC_CACHE", "ispn",
"KC_CACHE_STACK", "kubernetes"
);

// Init container
Expand All @@ -42,4 +44,10 @@ public final class Constants {
public static final String INIT_CONTAINER_NAME = "keycloak-extensions";
public static final String INIT_CONTAINER_EXTENSIONS_FOLDER = "/opt/extensions";
public static final String INIT_CONTAINER_EXTENSIONS_ENV_VAR = "KEYCLOAK_EXTENSIONS";

public static final Integer KEYCLOAK_SERVICE_PORT = 8080;
public static final String KEYCLOAK_SERVICE_PROTOCOL = "TCP";
public static final String KEYCLOAK_SERVICE_SUFFIX = "-service";
public static final Integer KEYCLOAK_DISCOVERY_SERVICE_PORT = 7800;
public static final String KEYCLOAK_DISCOVERY_SERVICE_SUFFIX = "-discovery";
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,10 @@ protected void setOwnerReferences(HasMetadata resource) {

resource.getMetadata().setOwnerReferences(Collections.singletonList(owner));
}

protected String getNamespace() {
return cr.getMetadata().getNamespace();
}

protected abstract String getName();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.keycloak.operator;

public interface StatusUpdater<T> {

void updateStatus(T status);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
*/
package org.keycloak.operator.v2alpha1;

import javax.inject.Inject;

import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.informers.SharedIndexInformer;
Expand All @@ -39,7 +38,7 @@
import org.keycloak.operator.v2alpha1.crds.KeycloakStatus;
import org.keycloak.operator.v2alpha1.crds.KeycloakStatusBuilder;

import java.util.Collections;
import javax.inject.Inject;
import java.util.List;
import java.util.Optional;

Expand All @@ -62,9 +61,15 @@ public List<EventSource> prepareEventSources(EventSourceContext<Keycloak> contex
.withLabels(Constants.DEFAULT_LABELS)
.runnableInformer(0);

SharedIndexInformer<Service> servicesInformer =
client.services().inNamespace(context.getConfigurationService().getClientConfiguration().getNamespace())
.withLabels(Constants.DEFAULT_LABELS)
.runnableInformer(0);

EventSource deploymentEvent = new InformerEventSource<>(deploymentInformer, Mappers.fromOwnerReference());
EventSource servicesEvent = new InformerEventSource<>(servicesInformer, Mappers.fromOwnerReference());

return List.of(deploymentEvent);
return List.of(deploymentEvent, servicesEvent);
}

@Override
Expand All @@ -82,6 +87,13 @@ public UpdateControl<Keycloak> reconcile(Keycloak kc, Context context) {
kcDeployment.updateStatus(statusBuilder);
kcDeployment.createOrUpdateReconciled();

var kcService = new KeycloakService(client, kc);
kcService.updateStatus(statusBuilder);
kcService.createOrUpdateReconciled();
var kcDiscoveryService = new KeycloakDiscoveryService(client, kc);
kcDiscoveryService.updateStatus(statusBuilder);
kcDiscoveryService.createOrUpdateReconciled();

var status = statusBuilder.build();

Log.info("--- Reconciliation finished successfully");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.ContainerBuilder;
import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.EnvVarBuilder;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.VolumeBuilder;
Expand All @@ -29,6 +30,7 @@
import org.keycloak.operator.Config;
import org.keycloak.operator.Constants;
import org.keycloak.operator.OperatorManagedResource;
import org.keycloak.operator.StatusUpdater;
import org.keycloak.operator.v2alpha1.crds.Keycloak;
import org.keycloak.operator.v2alpha1.crds.KeycloakStatusBuilder;

Expand All @@ -39,7 +41,7 @@
import java.util.Optional;
import java.util.stream.Collectors;

public class KeycloakDeployment extends OperatorManagedResource {
public class KeycloakDeployment extends OperatorManagedResource implements StatusUpdater<KeycloakStatusBuilder> {

// public static final Pattern CONFIG_SECRET_PATTERN = Pattern.compile("^\\$\\{secret:([^:]+):(.+)}$");

Expand Down Expand Up @@ -158,17 +160,9 @@ private Deployment createBaseDeployment() {

Container container = baseDeployment.getSpec().getTemplate().getSpec().getContainers().get(0);
container.setImage(Optional.ofNullable(keycloakCR.getSpec().getImage()).orElse(config.keycloak().image()));

var serverConfig = new HashMap<>(Constants.DEFAULT_DIST_CONFIG);
if (keycloakCR.getSpec().getServerConfiguration() != null) {
serverConfig.putAll(keycloakCR.getSpec().getServerConfiguration());
}

container.setImagePullPolicy(config.keycloak().imagePullPolicy());

container.setEnv(serverConfig.entrySet().stream()
.map(e -> new EnvVarBuilder().withName(e.getKey()).withValue(e.getValue()).build())
.collect(Collectors.toList()));
container.setEnv(getEnvVars());

addInitContainer(baseDeployment, keycloakCR.getSpec().getExtensions());

Expand Down Expand Up @@ -198,6 +192,20 @@ private Deployment createBaseDeployment() {
return baseDeployment;
}

private List<EnvVar> getEnvVars() {
var serverConfig = new HashMap<>(Constants.DEFAULT_DIST_CONFIG);
serverConfig.put("jgroups.dns.query", getName() + Constants.KEYCLOAK_DISCOVERY_SERVICE_SUFFIX +"." + getNamespace());
if (keycloakCR.getSpec().getServerConfiguration() != null) {
serverConfig.putAll(keycloakCR.getSpec().getServerConfiguration());
}
return serverConfig.entrySet().stream()
.map(e -> new EnvVarBuilder()
.withName(e.getKey())
.withValue(e.getValue())
.build())
.collect(Collectors.toList());
}

public void updateStatus(KeycloakStatusBuilder status) {
if (existingDeployment == null) {
status.addNotReadyMessage("No existing Deployment found, waiting for creating a new one");
Expand All @@ -223,14 +231,11 @@ public void updateStatus(KeycloakStatusBuilder status) {
// return configSecretsNames;
// }

@Override
public String getName() {
return keycloakCR.getMetadata().getName();
}

public String getNamespace() {
return keycloakCR.getMetadata().getNamespace();
}

public void rollingRestart() {
client.apps().deployments()
.inNamespace(getNamespace())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright 2021 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* 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.
*/
package org.keycloak.operator.v2alpha1;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.IntOrString;
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.ServiceBuilder;
import io.fabric8.kubernetes.api.model.ServiceSpec;
import io.fabric8.kubernetes.api.model.ServiceSpecBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.keycloak.operator.Constants;
import org.keycloak.operator.OperatorManagedResource;
import org.keycloak.operator.StatusUpdater;
import org.keycloak.operator.v2alpha1.crds.Keycloak;
import org.keycloak.operator.v2alpha1.crds.KeycloakStatusBuilder;

import java.util.Optional;

public class KeycloakDiscoveryService extends OperatorManagedResource implements StatusUpdater<KeycloakStatusBuilder> {

private Service existingService;

public KeycloakDiscoveryService(KubernetesClient client, Keycloak keycloakCR) {
super(client, keycloakCR);
this.existingService = fetchExistingService();
}

private ServiceSpec getServiceSpec() {
return new ServiceSpecBuilder()
.addNewPort()
.withPort(Constants.KEYCLOAK_DISCOVERY_SERVICE_PORT)
.endPort()
.withSelector(Constants.DEFAULT_LABELS)
.withClusterIP("None")
.build();
}

@Override
protected Optional<HasMetadata> getReconciledResource() {
var service = fetchExistingService();
if (service == null) {
service = newService();
} else {
service.setSpec(getServiceSpec());
}

return Optional.of(service);
}

private Service newService() {
Service service = new ServiceBuilder()
.withNewMetadata()
.withName(getName())
.withNamespace(getNamespace())
.endMetadata()
.withSpec(getServiceSpec())
.build();
return service;
}

private Service fetchExistingService() {
return client
.services()
.inNamespace(getNamespace())
.withName(getName())
.get();
}

public void updateStatus(KeycloakStatusBuilder status) {
if (existingService == null) {
status.addNotReadyMessage("No existing Discovery Service found, waiting for creating a new one");
return;
}
}

@Override
public String getName() {
return cr.getMetadata().getName() + Constants.KEYCLOAK_DISCOVERY_SERVICE_SUFFIX;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,11 @@ public void updateStatus(KeycloakRealmImportStatusBuilder status) {
}
}

private String getName() {
@Override
protected String getName() {
return realmCR.getMetadata().getName();
}

private String getNamespace() {
return realmCR.getMetadata().getNamespace();
}

private String getKeycloakName() { return realmCR.getSpec().getKeycloakCRName(); }

private String getRealmName() { return realmCR.getSpec().getRealm().getRealm(); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,11 @@ private Secret createSecret() {
.build();
}

private String getName() {
@Override
protected String getName() {
return realmCR.getMetadata().getName();
}

private String getNamespace() {
return realmCR.getMetadata().getNamespace();
}

private String getRealmName() { return realmCR.getSpec().getRealm().getRealm(); }

public String getSecretName() {
Expand Down
Loading
0