Skip to content

Commit

Permalink
fix deprecations and code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Björn Wenzel committed Jan 31, 2023
1 parent f2d1b5f commit 008c7b4
Show file tree
Hide file tree
Showing 24 changed files with 440 additions and 421 deletions.
10 changes: 7 additions & 3 deletions src/main/java/de/koudingspawn/vault/Constants.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package de.koudingspawn.vault;

public class Constants {
public static String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm'Z'";
public static String COMPARE_ANNOTATION = "/compare";
public static String LAST_UPDATE_ANNOTATION = "/lastUpdated";

private Constants() {
}

public static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm'Z'";
public static final String COMPARE_ANNOTATION = "/compare";
public static final String LAST_UPDATE_ANNOTATION = "/lastUpdated";
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
import io.fabric8.kubernetes.api.model.admission.v1.AdmissionResponse;
import io.fabric8.kubernetes.api.model.admission.v1.AdmissionReview;
import io.fabric8.kubernetes.api.model.admission.v1.AdmissionReviewBuilder;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.utils.ApiVersionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -16,8 +12,6 @@
@RequestMapping("/validation/vault-crd")
public class AdmissionReviewRestService {

private static final Logger log = LoggerFactory.getLogger(AdmissionReviewRestService.class);

private final AdmissionReviewService admissionReviewService;

public AdmissionReviewRestService(AdmissionReviewService admissionReviewService) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/de/koudingspawn/vault/crd/Vault.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public boolean modifyHandlerEquals(Object o) {

HashMap<String, String> annotations = new HashMap<>(getMetadata().getAnnotations());
annotations.remove("kubectl.kubernetes.io/last-applied-configuration");
if (!Objects.equals(vaultAnnotations, annotations)) return false;
return Objects.equals(vaultAnnotations, annotations);
}


Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/koudingspawn/vault/crd/VaultList.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package de.koudingspawn.vault.crd;

import io.fabric8.kubernetes.client.CustomResourceList;
import io.fabric8.kubernetes.api.model.DefaultKubernetesResourceList;

public class VaultList extends CustomResourceList<Vault> {
public class VaultList extends DefaultKubernetesResourceList<Vault> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@Service
public class ChangeAdjustmentService {

private static final Logger log = LoggerFactory.getLogger(KubernetesService.class);
private static final Logger log = LoggerFactory.getLogger(ChangeAdjustmentService.class);

private final KubernetesClient client;

Expand All @@ -24,15 +24,13 @@ public void handle(Vault resource) {
VaultChangeAdjustmentCallback changeAdjustmentCallback = resource.getSpec().getChangeAdjustmentCallback();
if (changeAdjustmentCallback != null && changeAdjustmentCallback.getType() != null && changeAdjustmentCallback.getName() != null) {
switch (changeAdjustmentCallback.getType().toLowerCase()) {
case "deployment":
rotateDeployment(resource.getMetadata().getNamespace(), changeAdjustmentCallback.getName());
break;
case "statefulset":
rotateStatefulSet(resource.getMetadata().getNamespace(), changeAdjustmentCallback.getName());
break;
default:
log.info("Currently a change adjustment is only supported for type deployment. Resource {} in namespace {} has type {}",
resource.getMetadata().getName(), resource.getMetadata().getNamespace(), changeAdjustmentCallback.getType());
case "deployment" ->
rotateDeployment(resource.getMetadata().getNamespace(), changeAdjustmentCallback.getName());
case "statefulset" ->
rotateStatefulSet(resource.getMetadata().getNamespace(), changeAdjustmentCallback.getName());
default ->
log.info("Currently a change adjustment is only supported for type deployment. Resource {} in namespace {} has type {}",
resource.getMetadata().getName(), resource.getMetadata().getNamespace(), changeAdjustmentCallback.getType());
}
} else {
log.warn("Change adjustment callback for resource {} in namespace {} is invalid!", resource.getMetadata().getName(), resource.getMetadata().getNamespace());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import de.koudingspawn.vault.crd.Vault;
import de.koudingspawn.vault.crd.VaultList;
import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.*;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.internal.KubernetesDeserializer;
Expand All @@ -27,13 +24,15 @@ public class KubernetesConnection {
@Profile("development")
public KubernetesClient testClient() {
Config config = new ConfigBuilder().withMasterUrl("http://localhost:8001").withWatchReconnectLimit(5).build();
return new DefaultKubernetesClient(config);
return new KubernetesClientBuilder()
.withConfig(config)
.build();
}

@Bean
@Profile("!development")
public KubernetesClient client() {
return new DefaultKubernetesClient();
return new KubernetesClientBuilder().build();
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void createSecret(Vault resource, VaultSecret vaultSecret) {
Secret secret = newSecretInstance(resource, vaultSecret);

secretCache.invalidate(secret.getMetadata().getNamespace(), secret.getMetadata().getName());
client.secrets().inNamespace(resource.getMetadata().getNamespace()).create(secret);
client.secrets().inNamespace(resource.getMetadata().getNamespace()).resource(secret).create();

log.info("Created secret for vault resource {} in namespace {}", secret.getMetadata().getName(), secret.getMetadata().getNamespace());
}
Expand All @@ -86,7 +86,7 @@ void modifySecret(Vault resource, VaultSecret vaultSecret) {
secret.setData(vaultSecret.getData());

secretCache.invalidate(resource.getMetadata().getNamespace(), resource.getMetadata().getName());
secretResource.createOrReplace(secret);
client.secrets().inNamespace(resource.getMetadata().getNamespace()).resource(secret).createOrReplace();

log.info("Modified secret {} in namespace {}", resource.getMetadata().getName(), resource.getMetadata().getNamespace());
}
Expand Down Expand Up @@ -145,9 +145,7 @@ public boolean hasBrokenOwnerReference(Vault resource) {

if (secret.getMetadata() != null && secret.getMetadata().getOwnerReferences() != null && secret.getMetadata().getOwnerReferences().size() == 1) {
OwnerReference ownerReference = secret.getMetadata().getOwnerReferences().get(0);
if (ownerReference.getApiVersion().equals(crdName + "/v1")) {
return true;
}
return ownerReference.getApiVersion().equals(crdName + "/v1");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class SecretCache {

private static final Logger log = LoggerFactory.getLogger(SecretCache.class);

private Cache<String, Secret> secretResourceCache = Caffeine.newBuilder().build();
private final Cache<String, Secret> secretResourceCache = Caffeine.newBuilder().build();
private final KubernetesClient client;

public SecretCache(KubernetesClient client, boolean watch) {
Expand All @@ -28,23 +28,28 @@ public SecretCache(KubernetesClient client, boolean watch) {
public void watcher() {
client.secrets().inAnyNamespace().withLabel("vault.koudingspawn.de=vault").inform(
new ResourceEventHandler<>() {

private String cacheKey(String namespace, String name) {
return "%s/%s".formatted(namespace, name);
}

@Override
public void onAdd(Secret obj) {
String key = String.format("%s/%s", obj.getMetadata().getNamespace(), obj.getMetadata().getName());
String key = cacheKey(obj.getMetadata().getNamespace(), obj.getMetadata().getName());
log.debug("Received create secret for {}", key);
secretResourceCache.put(key, obj);
}

@Override
public void onUpdate(Secret oldObj, Secret newObj) {
String key = String.format("%s/%s", newObj.getMetadata().getNamespace(), newObj.getMetadata().getName());
String key = cacheKey(newObj.getMetadata().getNamespace(), newObj.getMetadata().getName());
log.debug("Received update for secret {}", key);
secretResourceCache.put(key, newObj);
}

@Override
public void onDelete(Secret obj, boolean deletedFinalStateUnknown) {
String key = String.format("%s/%s", obj.getMetadata().getNamespace(), obj.getMetadata().getName());
String key = cacheKey(obj.getMetadata().getNamespace(), obj.getMetadata().getName());
log.debug("Invalidate secret cache for {} after delete", key);
secretResourceCache.invalidate(key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void storeNewEvent(EventType type, String message, Vault resource) {
.build();

try {
client.v1().events().inNamespace(resource.getMetadata().getNamespace()).create(evt);
client.v1().events().resource(evt).create();
} catch (Exception ex) {
log.error("Failed to store event for {} in namespace {} next to resource with error",
resource.getMetadata().getName(), resource.getMetadata().getNamespace(), ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public enum EventType {
private final String type;
private final String reason;

private EventType(String type, String reason) {
EventType(String type, String reason) {
this.type = type;
this.reason = reason;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package de.koudingspawn.vault.kubernetes.scheduler;

import de.koudingspawn.vault.crd.Vault;
import de.koudingspawn.vault.crd.VaultList;
import de.koudingspawn.vault.kubernetes.EventHandler;
import de.koudingspawn.vault.kubernetes.event.EventNotification;
import de.koudingspawn.vault.vault.communication.SecretNotAccessibleException;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,6 @@ private static PrivateKey readPkcs1PrivateKey(byte[] pkcs1Bytes) throws GeneralS
return readPkcs8PrivateKey(pkcs8bytes);
}

public static String sanitizeJson(String json) {
return json.replace("\n", "")
.replace("\r", "")
.replace("\t", "");
}

private static byte[] join(byte[] byteArray1, byte[] byteArray2) {
byte[] bytes = new byte[byteArray1.length + byteArray2.length];
System.arraycopy(byteArray1, 0, bytes, 0, byteArray1.length);
Expand Down
Loading

0 comments on commit 008c7b4

Please sign in to comment.