Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Commit

Permalink
WorkflowConfigBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Norberg committed May 1, 2017
1 parent 33522c9 commit d74f860
Show file tree
Hide file tree
Showing 17 changed files with 241 additions and 265 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,22 @@ public static void tearDownClass() throws Exception {
public void setUp() throws Exception {
storage.storeWorkflow(Workflow.create(
BACKFILL_1.workflowId().componentId(), URI.create("http://example.com"),
WorkflowConfiguration.create(BACKFILL_1.workflowId().id(), Schedule.HOURS,
empty(), empty(), empty(), empty(),
empty(), empty(), Collections.emptyList())));
WorkflowConfiguration.builder()
.id(BACKFILL_1.workflowId().id())
.schedule(Schedule.HOURS)
.build()));
storage.storeWorkflow(Workflow.create(
BACKFILL_2.workflowId().componentId(), URI.create("http://example.com"),
WorkflowConfiguration.create(BACKFILL_2.workflowId().id(), Schedule.HOURS,
empty(), empty(), empty(), empty(),
empty(), empty(), Collections.emptyList())));
WorkflowConfiguration.builder()
.id(BACKFILL_2.workflowId().id())
.schedule(Schedule.HOURS)
.build()));
storage.storeWorkflow(Workflow.create(
BACKFILL_3.workflowId().componentId(), URI.create("http://example.com"),
WorkflowConfiguration.create(BACKFILL_3.workflowId().id(), Schedule.HOURS,
empty(), empty(), empty(), empty(),
empty(), empty(), Collections.emptyList())));
WorkflowConfiguration.builder()
.id(BACKFILL_3.workflowId().id())
.schedule(Schedule.HOURS)
.build()));
storage.storeBackfill(BACKFILL_1);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
import static com.spotify.styx.api.JsonMatchers.assertNoJson;
import static com.spotify.styx.model.SequenceEvent.create;
import static com.spotify.styx.model.WorkflowState.patchDockerImage;
import static java.util.Collections.emptyList;
import static java.util.Optional.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -85,9 +83,10 @@ public WorkflowResourceTest(Api.Version version) {
}

private static final WorkflowConfiguration WORKFLOW_CONFIGURATION =
WorkflowConfiguration
.create("bar", Schedule.DAYS, empty(), empty(), empty(), empty(), empty(), empty(),
emptyList());
WorkflowConfiguration.builder()
.id("bar")
.schedule(Schedule.DAYS)
.build();

private static final Workflow WORKFLOW =
Workflow.create("foo", URI.create("/hejhej"), WORKFLOW_CONFIGURATION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import static com.spotify.apollo.test.unit.StatusTypeMatchers.belongsToFamily;
import static com.spotify.styx.api.JsonMatchers.assertJson;
import static com.spotify.styx.api.JsonMatchers.assertNoJson;
import static java.util.Optional.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -71,7 +70,6 @@
import java.net.URI;
import java.time.Duration;
import java.time.Instant;
import java.util.Collections;
import okio.ByteString;
import org.apache.hadoop.hbase.client.Connection;
import org.junit.After;
Expand Down Expand Up @@ -152,19 +150,22 @@ public static void tearDownClass() throws Exception {
public void setUp() throws Exception {
storage.storeWorkflow(Workflow.create(
BACKFILL_1.workflowId().componentId(), URI.create("http://example.com"),
WorkflowConfiguration.create(BACKFILL_1.workflowId().id(), Schedule.HOURS,
empty(), empty(), empty(), empty(),
empty(), empty(), Collections.emptyList())));
WorkflowConfiguration.builder()
.id(BACKFILL_1.workflowId().id())
.schedule(Schedule.HOURS)
.build()));
storage.storeWorkflow(Workflow.create(
BACKFILL_2.workflowId().componentId(), URI.create("http://example.com"),
WorkflowConfiguration.create(BACKFILL_2.workflowId().id(), Schedule.HOURS,
empty(), empty(), empty(), empty(),
empty(), empty(), Collections.emptyList())));
WorkflowConfiguration.builder()
.id(BACKFILL_2.workflowId().id())
.schedule(Schedule.HOURS)
.build()));
storage.storeWorkflow(Workflow.create(
BACKFILL_3.workflowId().componentId(), URI.create("http://example.com"),
WorkflowConfiguration.create(BACKFILL_3.workflowId().id(), Schedule.HOURS,
empty(), empty(), empty(), empty(),
empty(), empty(), Collections.emptyList())));
WorkflowConfiguration.builder()
.id(BACKFILL_3.workflowId().id())
.schedule(Schedule.HOURS)
.build()));
storage.storeBackfill(BACKFILL_1);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
import static com.spotify.styx.api.JsonMatchers.assertNoJson;
import static com.spotify.styx.model.SequenceEvent.create;
import static com.spotify.styx.model.WorkflowState.patchDockerImage;
import static java.util.Collections.emptyList;
import static java.util.Optional.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -88,9 +86,10 @@ public WorkflowResourceTest(Api.Version version) {
}

private static final WorkflowConfiguration WORKFLOW_CONFIGURATION =
WorkflowConfiguration
.create("bar", Schedule.DAYS, empty(), empty(), empty(), empty(), empty(), empty(),
emptyList());
WorkflowConfiguration.builder()
.id("bar")
.schedule(Schedule.DAYS)
.build();

private static final Workflow WORKFLOW =
Workflow.create("foo", URI.create("/hejhej"), WORKFLOW_CONFIGURATION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,29 @@

package com.spotify.styx.model;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.auto.value.AutoValue;
import com.spotify.styx.model.Schedule.WellKnown;
import com.spotify.styx.util.TimeUtil;
import io.norberg.automatter.AutoMatter;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
* A specification of a scheduled workflow
*/
@AutoValue
@JsonIgnoreProperties(ignoreUnknown = true)
public abstract class WorkflowConfiguration {
@AutoMatter
public interface WorkflowConfiguration {

@JsonProperty
public abstract String id();
String id();

@JsonProperty
public abstract Schedule schedule();
Schedule schedule();

@JsonProperty
public abstract Optional<String> offset();
Optional<String> offset();

@JsonProperty
public abstract Optional<String> dockerImage();
Optional<String> dockerImage();

@JsonProperty
public abstract Optional<List<String>> dockerArgs();
Optional<List<String>> dockerArgs();

/**
* Toggles behavior to reliably report exit status from the Docker container, via
Expand All @@ -60,44 +51,30 @@ public abstract class WorkflowConfiguration {
* <p>Ideally this should be unneeded, but mere exitCode is known to sometimes spuriously
* return 0 when in fact the container has been killed. See https://github.com/kubernetes/kubernetes/issues/41516
*/
@JsonProperty
public abstract boolean dockerTerminationLogging();

@JsonProperty
public abstract Optional<Secret> secret();

@JsonProperty
public abstract Optional<String> serviceAccount();

@JsonProperty
public abstract List<String> resources();

@JsonCreator
public static WorkflowConfiguration create(
@JsonProperty("id") String id,
@JsonProperty("schedule") Schedule schedule,
@JsonProperty("offset") Optional<String> offset,
@JsonProperty("docker_image") Optional<String> dockerImage,
@JsonProperty("docker_args") Optional<List<String>> dockerArgs,
@JsonProperty("docker_termination_logging") Optional<Boolean> dockerTerminationLogging,
@JsonProperty("secret") Optional<Secret> secret,
@JsonProperty("service_account") Optional<String> serviceAccount,
@JsonProperty("resources") List<String> resources) {

return new AutoValue_WorkflowConfiguration(
id, schedule, offset, dockerImage, dockerArgs,
dockerTerminationLogging.orElse(false), secret, serviceAccount,
resources == null ? Collections.emptyList() : resources);
}
boolean dockerTerminationLogging();

Optional<Secret> secret();

Optional<String> serviceAccount();

List<String> resources();

public Instant addOffset(Instant next) {
default Instant addOffset(Instant next) {
final String offset = offset().orElseGet(this::defaultOffset);

return TimeUtil.addOffset(next.atZone(ZoneOffset.UTC), offset).toInstant();
}

private String defaultOffset() {
switch (schedule().wellKnown()) {
default String defaultOffset() {
return defaultOffset(schedule());
}

default String defaultOffset(Schedule schedule) {
return defaultOffset(schedule.wellKnown());
}

default String defaultOffset(WellKnown schedule) {
switch (schedule) {
case HOURLY:
return "PT1H";
case DAILY:
Expand All @@ -114,22 +91,28 @@ private String defaultOffset() {
}
}

@AutoValue
@JsonIgnoreProperties(ignoreUnknown = true)
public abstract static class Secret {
static WorkflowConfigurationBuilder builder() {
return new WorkflowConfigurationBuilder();
}

@AutoMatter
interface Secret {

@JsonProperty
public abstract String name();
String name();

@JsonProperty
public abstract String mountPath();
String mountPath();

@JsonCreator
public static Secret create(
@JsonProperty("name") String name,
@JsonProperty("mount_path") String mountPath) {
return new AutoValue_WorkflowConfiguration_Secret(name, mountPath);
static Secret create(
String name,
String mountPath) {
return builder()
.name(name)
.mountPath(mountPath)
.build();
}
}

static SecretBuilder builder() {
return new SecretBuilder();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,16 @@ public static WorkflowConfiguration create(
resources == null ? Collections.emptyList() : resources);
}

public static com.spotify.styx.model.WorkflowConfiguration create(WorkflowConfiguration workflowConfiguration) {
return com.spotify.styx.model.WorkflowConfiguration
.create(workflowConfiguration.id(), workflowConfiguration.partitioning(), Optional.empty(),
workflowConfiguration.dockerImage(), workflowConfiguration.dockerArgs(),
Optional.of(workflowConfiguration.dockerTerminationLogging()),
Secret.create(workflowConfiguration.secret()),
Optional.empty(),
workflowConfiguration.resources());
public static com.spotify.styx.model.WorkflowConfiguration create(
WorkflowConfiguration workflowConfiguration) {
return com.spotify.styx.model.WorkflowConfiguration.builder()
.id(workflowConfiguration.id())
.schedule(workflowConfiguration.partitioning())
.dockerImage(workflowConfiguration.dockerImage())
.dockerArgs(workflowConfiguration.dockerArgs())
.dockerTerminationLogging(workflowConfiguration.dockerTerminationLogging())
.secret(workflowConfiguration.secret().map(Secret::create))
.build();
}

public static WorkflowConfiguration create(
Expand Down Expand Up @@ -118,10 +120,15 @@ public static Secret create(
return new AutoValue_WorkflowConfiguration_Secret(name, mountPath);
}

public static com.spotify.styx.model.WorkflowConfiguration.Secret create(
Secret secret) {
return com.spotify.styx.model.WorkflowConfiguration.Secret.create(
secret.name(), secret.mountPath());
}

public static Optional<com.spotify.styx.model.WorkflowConfiguration.Secret> create(
Optional<Secret> secret) {
return secret.map(
s -> com.spotify.styx.model.WorkflowConfiguration.Secret.create(s.name(), s.mountPath()));
return secret.map(Secret::create);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
Expand Down Expand Up @@ -56,6 +57,7 @@ private Json() {
.setPropertyNamingStrategy(SNAKE_CASE)
.enable(ACCEPT_SINGLE_VALUE_AS_ARRAY)
.disable(WRITE_DATES_AS_TIMESTAMPS)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.registerModule(ADT_MODULE)
.registerModule(new JavaTimeModule())
.registerModule(new Jdk8Module())
Expand Down
Loading

0 comments on commit d74f860

Please sign in to comment.