-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
217 additions
and
45 deletions.
There are no files selected for viewing
14 changes: 0 additions & 14 deletions
14
...m-x/services/telegram/src/main/java/software/tnb/telegram/resource/TelegramContainer.java
This file was deleted.
Oops, something went wrong.
47 changes: 47 additions & 0 deletions
47
...x/services/telegram/src/main/java/software/tnb/telegram/resource/local/LocalTelegram.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package software.tnb.telegram.resource.local; | ||
|
||
import software.tnb.common.deployment.Deployable; | ||
import software.tnb.telegram.service.Telegram; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.google.auto.service.AutoService; | ||
|
||
import java.io.IOException; | ||
|
||
@AutoService(LocalTelegram.class) | ||
public class LocalTelegram extends Telegram implements Deployable { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(LocalTelegram.class); | ||
private TelegramClientContainer container; | ||
|
||
@Override | ||
public String execInContainer(String... commands) { | ||
try { | ||
return container.execInContainer(commands).getStdout(); | ||
} catch (IOException | InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void deploy() { | ||
LOG.debug("Starting Telegram client container"); | ||
container = new TelegramClientContainer(image(), getEnv()); | ||
container.start(); | ||
} | ||
|
||
@Override | ||
public void undeploy() { | ||
container.stop(); | ||
} | ||
|
||
@Override | ||
public void openResources() { | ||
} | ||
|
||
@Override | ||
public void closeResources() { | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
.../telegram/src/main/java/software/tnb/telegram/resource/local/TelegramClientContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package software.tnb.telegram.resource.local; | ||
|
||
import org.testcontainers.containers.GenericContainer; | ||
|
||
import java.util.Map; | ||
|
||
public class TelegramClientContainer extends GenericContainer<TelegramClientContainer> { | ||
|
||
public TelegramClientContainer(String image, Map<String, String> env) { | ||
super(image); | ||
withEnv(env); | ||
} | ||
|
||
} |
133 changes: 133 additions & 0 deletions
133
...es/telegram/src/main/java/software/tnb/telegram/resource/openshift/OpenshiftTelegram.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package software.tnb.telegram.resource.openshift; | ||
|
||
import software.tnb.common.config.OpenshiftConfiguration; | ||
import software.tnb.common.deployment.OpenshiftDeployable; | ||
import software.tnb.common.deployment.WithName; | ||
import software.tnb.common.openshift.OpenshiftClient; | ||
import software.tnb.telegram.service.Telegram; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.google.auto.service.AutoService; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import cz.xtf.core.openshift.OpenShiftWaiters; | ||
import cz.xtf.core.openshift.helpers.ResourceFunctions; | ||
import io.fabric8.kubernetes.api.model.EnvVar; | ||
import io.fabric8.kubernetes.api.model.ObjectReferenceBuilder; | ||
import io.fabric8.kubernetes.api.model.Pod; | ||
import io.fabric8.openshift.api.model.DeploymentConfigBuilder; | ||
import io.fabric8.openshift.api.model.DeploymentTriggerImageChangeParams; | ||
import io.fabric8.openshift.api.model.ImageStream; | ||
import io.fabric8.openshift.api.model.ImageStreamBuilder; | ||
|
||
@AutoService(OpenshiftTelegram.class) | ||
public class OpenshiftTelegram extends Telegram implements OpenshiftDeployable, WithName { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(OpenshiftTelegram.class); | ||
|
||
@Override | ||
public void undeploy() { | ||
LOG.info("Undeploying Telegram client"); | ||
OpenshiftClient.get().deploymentConfigs().withName(name()).delete(); | ||
OpenShiftWaiters.get(OpenshiftClient.get(), () -> false).areNoPodsPresent(OpenshiftConfiguration.openshiftDeploymentLabel(), name()) | ||
.timeout(120_000).waitFor(); | ||
OpenshiftClient.get().imageStreams().withLabel(OpenshiftConfiguration.openshiftDeploymentLabel(), name()).delete(); | ||
} | ||
|
||
@Override | ||
public void openResources() { | ||
} | ||
|
||
@Override | ||
public void closeResources() { | ||
} | ||
|
||
@Override | ||
public void create() { | ||
LOG.info("Deploying Telegram client"); | ||
|
||
ImageStream imageStream = new ImageStreamBuilder() | ||
.withNewMetadata() | ||
.withName(name()) | ||
.endMetadata() | ||
.withNewSpec() | ||
.addNewTag() | ||
.withName("latest") | ||
.withFrom(new ObjectReferenceBuilder().withKind("DockerImage").withName(image()).build()) | ||
.endTag() | ||
.endSpec() | ||
.build(); | ||
|
||
OpenshiftClient.get().imageStreams().createOrReplace(imageStream); | ||
|
||
LOG.debug("Creating deployment {}", name()); | ||
OpenshiftClient.get().deploymentConfigs().createOrReplace( | ||
new DeploymentConfigBuilder() | ||
.editOrNewMetadata() | ||
.withName(name()) | ||
.addToLabels(OpenshiftConfiguration.openshiftDeploymentLabel(), name()) | ||
.endMetadata() | ||
.editOrNewSpec() | ||
.addToSelector(OpenshiftConfiguration.openshiftDeploymentLabel(), name()) | ||
.withReplicas(1) | ||
.editOrNewTemplate() | ||
.editOrNewMetadata() | ||
.addToLabels(OpenshiftConfiguration.openshiftDeploymentLabel(), name()) | ||
.endMetadata() | ||
.editOrNewSpec() | ||
.addNewContainer() | ||
.withName(name()) | ||
.withImagePullPolicy("IfNotPresent") | ||
.addAllToEnv(getEnv().entrySet().stream().map(e -> new EnvVar(e.getKey(), e.getValue(), null)) | ||
.collect(Collectors.toList())) | ||
.endContainer() | ||
.endSpec() | ||
.endTemplate() | ||
.addNewTrigger() | ||
.withType("ConfigChange") | ||
.endTrigger() | ||
.addNewTrigger() | ||
.withType("ImageChange") | ||
.withImageChangeParams( | ||
new DeploymentTriggerImageChangeParams(true, Arrays.asList(name()), | ||
new ObjectReferenceBuilder().withKind("ImageStreamTag").withName(name() + ":latest").build() | ||
, null) | ||
) | ||
.endTrigger() | ||
.endSpec() | ||
.build() | ||
); | ||
} | ||
|
||
@Override | ||
public boolean isReady() { | ||
List<Pod> pods = OpenshiftClient.get().getLabeledPods(OpenshiftConfiguration.openshiftDeploymentLabel(), name()); | ||
return ResourceFunctions.areExactlyNPodsReady(1).apply(pods); | ||
} | ||
|
||
@Override | ||
public boolean isDeployed() { | ||
return OpenshiftClient.get().getLabeledPods(OpenshiftConfiguration.openshiftDeploymentLabel(), name()).size() != 0; | ||
} | ||
|
||
@Override | ||
public String execInContainer(String... commands) { | ||
ByteArrayOutputStream baosOutput = new ByteArrayOutputStream(); | ||
|
||
OpenshiftClient.get().pods().withName(OpenshiftClient.get().getAnyPod(name()).getMetadata().getName()) | ||
.writingOutput(baosOutput).exec(commands); | ||
|
||
return baosOutput.toString(); | ||
} | ||
|
||
@Override | ||
public String name() { | ||
return "telegram-client"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters