Fix race condition while updating Secrets labels in Operator

This commit is contained in:
Václav Muzikář 2022-11-04 18:15:07 +01:00 committed by Václav Muzikář
parent 1de9c201c6
commit 01f1db600d
3 changed files with 22 additions and 13 deletions

View file

@ -111,13 +111,12 @@ public class WatchedSecretsStore extends OperatorManagedResource {
Log.infof("Adding label to Secret \"%s\"", secret.getMetadata().getName());
secret = new SecretBuilder(secret)
client.secrets().inNamespace(secret.getMetadata().getNamespace()).withName(secret.getMetadata().getName())
.edit(s -> new SecretBuilder(s)
.editMetadata()
.addToLabels(Constants.KEYCLOAK_COMPONENT_LABEL, WATCHED_SECRETS_LABEL_VALUE)
.endMetadata()
.build();
client.secrets().inNamespace(secret.getMetadata().getNamespace()).withName(secret.getMetadata().getName()).patch(secret);
.build());
}
}
}
@ -194,8 +193,13 @@ public class WatchedSecretsStore extends OperatorManagedResource {
}
private static void cleanObsoleteLabelFromSecret(KubernetesClient client, Secret secret) {
secret.getMetadata().getLabels().remove(Constants.KEYCLOAK_COMPONENT_LABEL);
client.secrets().inNamespace(secret.getMetadata().getNamespace()).withName(secret.getMetadata().getName()).patch(secret);
client.secrets().inNamespace(secret.getMetadata().getNamespace()).withName(secret.getMetadata().getName())
.edit(s -> new SecretBuilder(s)
.editMetadata()
.removeFromLabels(Constants.KEYCLOAK_COMPONENT_LABEL)
.endMetadata()
.build()
);
}
public static EventSource getWatchedSecretsEventSource(KubernetesClient client, String namespace) {

View file

@ -35,6 +35,7 @@ import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestInfo;
import org.keycloak.operator.Constants;
import org.keycloak.operator.crds.v2alpha1.deployment.Keycloak;
@ -103,8 +104,11 @@ public abstract class BaseOperatorTest {
}
@BeforeEach
public void beforeEach() {
Log.info(((operatorDeployment == OperatorDeployment.remote) ? "Remote " : "Local ") + "Run Test :" + namespace);
public void beforeEach(TestInfo testInfo) {
String testClassName = testInfo.getTestClass().map(c -> c.getSimpleName() + ".").orElse("");
Log.info("\n------- STARTING: " + testClassName + testInfo.getDisplayName() + "\n"
+ "------- Namespace: " + namespace + "\n"
+ "------- Mode: " + ((operatorDeployment == OperatorDeployment.remote) ? "remote" : "local"));
}
private static void createK8sClient() {

View file

@ -24,6 +24,7 @@ import io.quarkus.test.junit.QuarkusTest;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
import org.keycloak.operator.testsuite.utils.CRAssert;
import org.keycloak.operator.controllers.KeycloakService;
@ -49,8 +50,8 @@ public class RealmImportTest extends BaseOperatorTest {
@Override
@BeforeEach
public void beforeEach() {
super.beforeEach();
public void beforeEach(TestInfo testInfo) {
super.beforeEach(testInfo);
// Recreating the database and the realm import CR to keep this test isolated
k8sclient.load(getClass().getResourceAsStream("/example-realm.yaml")).inNamespace(namespace).delete();
k8sclient.load(getClass().getResourceAsStream("/incorrect-realm.yaml")).inNamespace(namespace).delete();