8000 #9730 Clustering support by jonathanvila · Pull Request #10249 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Closed
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
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
```
4 changes: 4 additions & 0 deletions operator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@
<version>${awaitility.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ public final class Constants {
);

public static final Map<String, String> DEFAULT_DIST_CONFIG = Map.of(
"KC_METRICS_ENABLED", "true"
"KC_METRICS_ENABLED","true",
"JGROUPS_DISCOVERY_PROTOCOL", "dns.DNS_PING",
"CACHE_OWNERS_COUNT", "2",
"CACHE_OWNERS_AUTH_SESSIONS_COUNT","2"
);
public static final Integer KEYCLOAK_SERVICE_PORT = 8080;
public static final String KEYCLOAK_SERVICE_PROTOCOL = "TCP";
public static final Integer KEYCLOAK_DISCOVERY_SERVICE_PORT = 8080;
}
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();
}
8000
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*/
package org.keycloak.operator.v2alpha1;

import javax.inject.Inject;

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 +37,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 Down Expand Up @@ -82,6 +80,9 @@ public UpdateControl<Keycloak> reconcile(Keycloak kc, Context context) {
kcDeployment.updateStatus(statusBuilder);
kcDeployment.createOrUpdateReconciled();

new KeycloakService(client, kc).createOrUpdateReconciled();
new KeycloakDiscoveryService(client, kc).createOrUpdateReconciled();
Comment on lines +83 to +84
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably reflect this in the CR's status. I'd probably call addNotReadyMessage if a service is not found, similarly to:

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


var status = statusBuilder.build();

Log.info("--- Reconciliation finished successfully");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.keycloak.operator.v2alpha1;

import io.fabric8.kubernetes.api.model.Container;
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.apps.Deployment;
Expand All @@ -31,6 +32,7 @@

import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -99,17 +101,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());

// Set<String> configSecretsNames = new HashSet<>();
// List<EnvVar> configEnvVars = serverConfig.entrySet().stream()
Expand Down Expand Up @@ -137,6 +131,20 @@ private Deployment createBaseDeployment() {
return baseDeployment;
}

private List<EnvVar> getEnvVars() {
var serverConfig = new HashMap<>(Constants.DEFAULT_DIST_CONFIG);
serverConfig.put("JGROUPS_DISCOVERY_PROPERTIES", "dns_query=" + getName() + "-discovery." + 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 @@ -162,14 +170,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,82 @@
/*
* 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.v2alpha1.crds.Keycloak;

import java.util.Optional;

public class KeycloakDiscoveryService extends OperatorManagedResource {

public KeycloakDiscoveryService(KubernetesClient client, Keycloak keycloakCR) {
super(client, keycloakCR);
}

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

@Override
protected Optional<HasMetadata> getReconciledResource() {
return Optional.of(fetchExistingService()
.map(a -> {
a.setSpec(getServiceSpec());
return a;
})
.orElse(newService()));
}

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

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

@Override
protected String getName() {
return cr.getMetadata().getName() + "-discovery";
}
}
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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.v2alpha1.crds.Keycloak;

import java.util.Optional;

public class KeycloakService extends OperatorManagedResource {

public KeycloakService(KubernetesClient client, Keycloak keycloakCR) {
super(client, keycloakCR);
}

private ServiceSpec getServiceSpec() {
return new ServiceSpecBuilder()
.withSelector(Constants.DEFAULT_LABELS)
.addNewPort()
.withName(getName())
.withTargetPort(new IntOrString(Constants.KEYCLOAK_SERVICE_PORT))
.withPort(Constants.KEYCLOAK_SERVICE_PORT)
.withProtocol(Constants.KEYCLOAK_SERVICE_PROTOCOL)
.endPort()
.build();
}

@Override
protected Optional<HasMetadata> getReconciledResource() {
return Optional.of(fetchExistingService()
.map(a -> {
a.setSpec(getServiceSpec());
return a;
})
.orElse(newService()));
}

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

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

public String getName() {
return cr.getMetadata().getName()+"-service";
}
}
4 changes: 2 additions & 2 deletions operator/src/main/resources/base-keycloak-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ spec:
app: ""
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
maxSurge: 1
maxUnavailable: 1
type: RollingUpdate
template:
metadata:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ private static void createCRDs() throws FileNotFoundException {
k8sclient.load(new FileInputStream(TARGET_KUBERNETES_GENERATED_YML_FOLDER + "keycloakrealmimports.keycloak.org-v1.yml")).createOrReplace();
}

private static void cleanCRDs() throws FileNotFoundException {
Log.info("Cleaning CRDs ");
k8sclient.load(new FileInputStream(TARGET_KUBERNETES_GENERATED_YML_FOLDER + "keycloaks.keycloak.org-v1.yml")).delete();
k8sclient.load(new FileInputStream(TARGET_KUBERNETES_GENERATED_YML_FOLDER + "keycloakrealmimports.keycloak.org-v1.yml")).delete();
}

private static void registerReconcilers() {
Log.info("Registering reconcilers for operator : " + operator + " [" + operatorDeployment + "]");

Expand Down Expand Up @@ -186,6 +192,8 @@ public static void after() throws FileNotFoundException {
cleanRBACresourcesAndOperatorDeployment();
}

cleanCRDs();

Log.info("Deleting namespace : " + namespace);
assertThat(k8sclient.namespaces().withName(namespace).delete()).isTrue();
k8sclient.close();
Expand Down
Loading
0