Skip to content

Commit

Permalink
feat: refactoring digest
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejpetras committed May 23, 2024
1 parent a4198fa commit edf8583
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@ app:
watchNamespaces: "JOSDK_WATCH_CURRENT"
----

ACES or comma separated list of namespaces
watchNamespaces: "JOSDK_WATCH_CURRENT"
----
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.tkit.onecx.product.store.slot.operator;
package org.tkit.onecx.data.orchestrator.operator;

import io.fabric8.kubernetes.api.model.Namespaced;
import io.fabric8.kubernetes.client.CustomResource;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.tkit.onecx.product.store.slot.operator;
package org.tkit.onecx.data.orchestrator.operator;

import io.quarkus.runtime.annotations.ConfigDocFilename;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.quarkus.runtime.annotations.StaticInitSafe;
import io.quarkus.runtime.annotations.*;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;
import io.smallrye.config.WithName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
package org.tkit.onecx.product.store.slot.operator;
package org.tkit.onecx.data.orchestrator.operator;

import static io.javaoperatorsdk.operator.api.reconciler.Constants.WATCH_CURRENT_NAMESPACE;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import jakarta.inject.Inject;
import jakarta.ws.rs.WebApplicationException;
import jakarta.xml.bind.DatatypeConverter;

import org.eclipse.microprofile.config.ConfigProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tkit.onecx.product.store.slot.operator.client.DataService;
import org.tkit.onecx.data.orchestrator.operator.client.DataService;

import io.javaoperatorsdk.operator.api.reconciler.*;
import io.javaoperatorsdk.operator.processing.event.source.filter.OnAddFilter;
import io.javaoperatorsdk.operator.processing.event.source.filter.OnUpdateFilter;
import io.smallrye.config.SmallRyeConfig;

@ControllerConfiguration(name = "data", namespaces = WATCH_CURRENT_NAMESPACE, onAddFilter = DataController.SlotAddFilter.class, onUpdateFilter = DataController.SlotUpdateFilter.class)
public class DataController implements Reconciler<Data>, ErrorStatusHandler<Data> {

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

private static final CheckSum CHECK_SUM = new CheckSum();

@Inject
DataService service;

Expand Down Expand Up @@ -65,7 +70,7 @@ public ErrorStatusUpdateControl<Data> updateErrorStatus(Data slot,
private void updateStatusPojo(Data data, int responseCode) {
var result = new DataStatus();
result.setResponseCode(responseCode);
result.setChecksum(CHECK_SUM.createCheckSum(data.getSpec().getData()));
result.setChecksum(createCheckSum(data.getSpec().getData()));
var status = switch (responseCode) {
case 201:
yield DataStatus.Status.CREATED;
Expand Down Expand Up @@ -108,8 +113,30 @@ public boolean accept(Data newResource, Data oldResource) {
if (newResource.getSpec().getData() == null || newResource.getSpec().getData().isEmpty()) {
return false;
}
return oldResource.getStatus().getChecksum().equals(CHECK_SUM.createCheckSum(newResource.getSpec().getData()));
return oldResource.getStatus().getChecksum()
.equals(createCheckSum(newResource.getSpec().getData()));
}
}

static MessageDigest createDigest(String name) {
try {
return MessageDigest.getInstance(name);
} catch (NoSuchAlgorithmException e) {
throw new CheckSumException(e);
}
}

static String createCheckSum(String data) {
var config = ConfigProvider.getConfig().unwrap(SmallRyeConfig.class).getConfigMapping(DataConfig.class);
var digest = createDigest(config.digest());
byte[] hash = digest.digest(data.getBytes(StandardCharsets.UTF_8));
return DatatypeConverter.printHexBinary(hash).toUpperCase();
}

static class CheckSumException extends RuntimeException {

public CheckSumException(Throwable e) {
super("Error create check-sum from the data content", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.tkit.onecx.product.store.slot.operator;
package org.tkit.onecx.data.orchestrator.operator;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.tkit.onecx.product.store.slot.operator;
package org.tkit.onecx.data.orchestrator.operator;

import com.fasterxml.jackson.annotation.JsonProperty;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.tkit.onecx.product.store.slot.operator.client;
package org.tkit.onecx.data.orchestrator.operator.client;

import jakarta.ws.rs.POST;
import jakarta.ws.rs.Produces;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.tkit.onecx.product.store.slot.operator.client;
package org.tkit.onecx.data.orchestrator.operator.client;

import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.tkit.onecx.product.store.slot.operator.client;
package org.tkit.onecx.data.orchestrator.operator.client;

import java.net.URI;

Expand All @@ -9,8 +9,8 @@
import org.jboss.resteasy.reactive.client.api.QuarkusRestClientProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tkit.onecx.product.store.slot.operator.Data;
import org.tkit.onecx.product.store.slot.operator.DataSpec;
import org.tkit.onecx.data.orchestrator.operator.Data;
import org.tkit.onecx.data.orchestrator.operator.DataSpec;

@ApplicationScoped
public class DataService {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.tkit.onecx.data.orchestrator.operator;

public abstract class AbstractTest {
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package org.tkit.onecx.product.store.slot.operator;
package org.tkit.onecx.data.orchestrator.operator;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.tkit.onecx.data.orchestrator.operator.DataController.createDigest;

import jakarta.inject.Inject;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.tkit.onecx.product.store.slot.operator.client.DataService;
import org.tkit.onecx.data.orchestrator.operator.client.DataService;

import io.javaoperatorsdk.operator.api.reconciler.UpdateControl;
import io.quarkus.test.InjectMock;
Expand All @@ -28,6 +30,11 @@ void beforeAll() {
Mockito.when(dataService.updateData(any())).thenReturn(404);
}

@Test
void wrongAlgoDigest() {
Assertions.assertThrows(DataController.CheckSumException.class, () -> createDigest("WRONG_NAME"));
}

@Test
void testWrongResponse() throws Exception {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.tkit.onecx.product.store.slot.operator;
package org.tkit.onecx.data.orchestrator.operator;

import static java.util.concurrent.TimeUnit.SECONDS;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit edf8583

Please sign in to comment.