Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Removed OpenShift specific config flags from kubernetes-maven-plugin #170

Merged
merged 1 commit into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,31 @@ public class LogConfiguration implements Serializable {

public static final LogConfiguration DEFAULT = new LogConfiguration(null, null, null, null, null, null, null);

private Boolean enabled;
private boolean enabled;
private String prefix;
private String date;
private String color;
private String file;
private LogDriver driver;

@lombok.Builder
@Builder
private LogConfiguration(
Boolean enabled, String prefix, String color, String date, String file, Map<String, String> logDriverOpts, String driverName) {
this.enabled = enabled;
this.enabled = Optional.ofNullable(enabled).orElse(false);
this.prefix = prefix;
this.date = date;
this.color = color;
this.file = file;
this.driver = Optional.ofNullable(driverName).map(dn -> new LogDriver(dn, logDriverOpts)).orElse(null);
}

/**
* If explicitly enabled, or configured in any way and NOT explicitly disabled, return true.
*
* @return whether its activated or not
*/
public boolean isActivated() {
return enabled == Boolean.TRUE ||
(enabled != Boolean.FALSE && !isBlank());
return enabled || !isBlank();
}

/**
Expand All @@ -81,8 +81,6 @@ public static class LogDriver implements Serializable {

private Map<String, String> opts;

public LogDriver() {}

private LogDriver(String name, Map<String, String> opts) {
this.name = name;
this.opts = opts;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,18 +392,15 @@ private RestartPolicy extractRestartPolicy(RestartPolicy config, ValueProvider v
}

private LogConfiguration extractLogConfig(LogConfiguration config, ValueProvider valueProvider) {
LogConfiguration.LogConfigurationBuilder builder = LogConfiguration.builder()
return LogConfiguration.builder()
.color(valueProvider.getString(LOG_COLOR, config == null ? null : config.getColor()))
.date(valueProvider.getString(LOG_DATE, config == null ? null : config.getDate()))
.file(valueProvider.getString(LOG_FILE, config == null ? null : config.getFileLocation()))
.prefix(valueProvider.getString(LOG_PREFIX, config == null ? null : config.getPrefix()))
.driverName(valueProvider.getString(LOG_DRIVER_NAME, config == null || config.getDriver() == null ? null : config.getDriver().getName()))
.logDriverOpts(valueProvider.getMap(LOG_DRIVER_OPTS, config == null || config.getDriver() == null ? null : config.getDriver().getOpts()));

Boolean configEnabled = config != null ? config.getEnabled() : null;
Boolean enabled = valueProvider.getBoolean(LOG_ENABLED, configEnabled);
builder.enabled(enabled);
return builder.build();
.logDriverOpts(valueProvider.getMap(LOG_DRIVER_OPTS, config == null || config.getDriver() == null ? null : config.getDriver().getOpts()))
.enabled(valueProvider.getBoolean(LOG_ENABLED, Optional.ofNullable(config).map(LogConfiguration::isEnabled).orElse(false)))
.build();
}

private WaitConfiguration extractWaitConfig(WaitConfiguration config, ValueProvider valueProvider) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

/**
* @author roland
* @since 16.10.14
*/
public class StartOrderResolver {

Expand Down Expand Up @@ -68,7 +67,7 @@ private List<Resolvable> resolve(List<Resolvable> images) {
}

// Next passes: Those with dependencies are checked whether they already have been visited.
return secondPass.size() > 0 ? resolveRemaining(resolved) : resolved;
return secondPass.isEmpty() ? resolved : resolveRemaining(resolved);
}

private List<Resolvable> resolveRemaining(List<Resolvable> ret) {
Expand All @@ -77,11 +76,11 @@ private List<Resolvable> resolveRemaining(List<Resolvable> ret) {
try {
do {
resolveImageDependencies(ret);
} while (secondPass.size() > 0 && retries-- > 0);
} while (!secondPass.isEmpty() && retries-- > 0);
} catch (DockerAccessException | ResolveSteadyStateException e) {
error = "Cannot resolve image dependencies for start order\n" + remainingImagesDescription();
}
if (retries == 0 && secondPass.size() > 0) {
if (retries == 0 && !secondPass.isEmpty()) {
error = "Cannot resolve image dependencies after " + MAX_RESOLVE_RETRIES + " passes\n"
+ remainingImagesDescription();
}
Expand All @@ -105,7 +104,7 @@ private String remainingImagesDescription() {
ret.append("* ")
.append(config.getAlias())
.append(" depends on ")
.append(String.join(",", (String[])config.getDependencies().toArray()))
.append(String.join(",", config.getDependencies().toArray(new String[0])))
.append("\n");
}
return ret.toString();
Expand Down Expand Up @@ -156,7 +155,7 @@ private List<String> extractDependentImagesFor(Resolvable config) {
}

// Exception indicating a steady state while resolving start order of images
private static class ResolveSteadyStateException extends Throwable { }
private static class ResolveSteadyStateException extends Exception { }

public interface Resolvable {
String getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

import static org.eclipse.jkube.kit.common.util.KubernetesHelper.withSelector;
import static org.eclipse.jkube.kit.config.service.kubernetes.KubernetesClientUtil.deleteEntities;
import static org.eclipse.jkube.kit.config.service.kubernetes.KubernetesClientUtil.deleteOpenShiftEntities;
import static org.eclipse.jkube.kit.config.service.kubernetes.KubernetesClientUtil.getPodStatusDescription;
import static org.eclipse.jkube.kit.config.service.kubernetes.KubernetesClientUtil.getPodStatusMessagePostfix;
import static org.eclipse.jkube.kit.config.service.kubernetes.KubernetesClientUtil.resizeApp;
Expand Down Expand Up @@ -96,7 +97,10 @@ public void tailAppPodsLogs(final KubernetesClient kubernetes, final String name
public void run() {
if (onExitOperationLower.equals(OPERATION_UNDEPLOY)) {
log.info("Undeploying the app:");
deleteEntities(kubernetes, namespace, entities, context.getS2iBuildNameSuffix(), log);
deleteEntities(kubernetes, namespace, entities, log);
if (context.getS2iBuildNameSuffix() != null) {
deleteOpenShiftEntities(kubernetes, namespace, entities, context.getS2iBuildNameSuffix(), log);
}
} else if (onExitOperationLower.equals(OPERATION_STOP)) {
log.info("Stopping the app:");
resizeApp(kubernetes, namespace, entities, 0, log);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@
* Utility class for executing common tasks using the Kubernetes client
*
* @author nicola
* @since 09/02/17
*/
public class KubernetesClientUtil {

private KubernetesClientUtil() {}

public static void resizeApp(KubernetesClient kubernetes, String namespace, Set<HasMetadata> entities, int replicas, KitLogger log) {
for (HasMetadata entity : entities) {
String name = KubernetesHelper.getName(entity);
Expand All @@ -91,23 +92,9 @@ public static void resizeApp(KubernetesClient kubernetes, String namespace, Set<
}
}



public static void deleteEntities(KubernetesClient kubernetes, String namespace, Set<HasMetadata> entities, String s2iBuildNameSuffix, KitLogger log) {
public static void deleteEntities(KubernetesClient kubernetes, String namespace, Set<HasMetadata> entities, KitLogger log) {
List<HasMetadata> list = new ArrayList<>(entities);

// For OpenShift cluster, also delete s2i buildconfig
OpenShiftClient openshiftClient = OpenshiftHelper.asOpenShiftClient(kubernetes); if (openshiftClient != null) {
for (HasMetadata entity : list) {
if ("ImageStream".equals(KubernetesHelper.getKind(entity))) {
ImageName imageName = new ImageName(entity.getMetadata().getName());
String buildName = getS2IBuildName(imageName, s2iBuildNameSuffix);
log.info("Deleting resource BuildConfig " + namespace + "/" + buildName);
openshiftClient.buildConfigs().inNamespace(namespace).withName(buildName).delete();
}
}
}

// lets delete in reverse order
Collections.reverse(list);

Expand All @@ -117,6 +104,23 @@ public static void deleteEntities(KubernetesClient kubernetes, String namespace,
}
}

public static void deleteOpenShiftEntities(KubernetesClient kubernetes, String namespace, Set<HasMetadata> entities, String s2iBuildNameSuffix, KitLogger log) {
// For OpenShift cluster, also delete s2i buildconfig
OpenShiftClient openshiftClient = OpenshiftHelper.asOpenShiftClient(kubernetes);
if (openshiftClient == null) {
return;
}
for (HasMetadata entity : entities) {
if ("ImageStream".equals(KubernetesHelper.getKind(entity))) {
ImageName imageName = new ImageName(entity.getMetadata().getName());
String buildName = getS2IBuildName(imageName, s2iBuildNameSuffix);
log.info("Deleting resource BuildConfig %s/%s and Builds", namespace, buildName);
openshiftClient.builds().inNamespace(namespace).withLabel("buildconfig", buildName).delete();
openshiftClient.buildConfigs().inNamespace(namespace).withName(buildName).delete();
}
}
}

private static String getS2IBuildName(ImageName imageName, String s2iBuildNameSuffix) {
return imageName.getSimpleName() + s2iBuildNameSuffix;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@

/**
* @author nicola
* @since 21/02/17
*/
public class OpenshiftBuildService implements BuildService {

Expand Down Expand Up @@ -120,7 +119,7 @@ public void build(ImageConfiguration imageConfig) throws JKubeServiceException {

// Check for buildconfig / imagestream / pullSecret and create them if necessary
String openshiftPullSecret = config.getOpenshiftPullSecret();
Boolean usePullSecret = checkOrCreatePullSecret(config, client, builder, openshiftPullSecret, imageConfig);
final boolean usePullSecret = checkOrCreatePullSecret(client, builder, openshiftPullSecret, imageConfig);
if (usePullSecret) {
buildName = updateOrCreateBuildConfig(config, client, builder, imageConfig, openshiftPullSecret);
} else {
Expand Down Expand Up @@ -411,7 +410,7 @@ private List<EnvVar> checkForEnv(ImageConfiguration imageConfiguration) {
return Collections.emptyList();
}

private Boolean checkOrCreatePullSecret(BuildServiceConfig config, OpenShiftClient client, KubernetesListBuilder builder, String pullSecretName, ImageConfiguration imageConfig)
private boolean checkOrCreatePullSecret(OpenShiftClient client, KubernetesListBuilder builder, String pullSecretName, ImageConfiguration imageConfig)
throws Exception {
JKubeConfiguration configuration = jKubeServiceHub.getConfiguration();
BuildConfiguration buildConfig = imageConfig.getBuildConfiguration();
Expand Down Expand Up @@ -567,7 +566,7 @@ private Build startBuild(OpenShiftClient client, File dockerTar, String buildNam
}
}

private void waitForOpenShiftBuildToComplete(OpenShiftClient client, Build build) throws InterruptedException, IOException {
private void waitForOpenShiftBuildToComplete(OpenShiftClient client, Build build) throws IOException {
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch logTerminateLatch = new CountDownLatch(1);
final String buildName = KubernetesHelper.getName(build);
Expand Down Expand Up @@ -618,9 +617,8 @@ private void waitForOpenShiftBuildToComplete(OpenShiftClient client, Build build
* @param podName Name of the pod
* @param nAwaitTimeout Time in seconds upto which pod must be watched
* @param log Logger object
* @throws InterruptedException
*/
private void waitUntilPodIsReady(String podName, int nAwaitTimeout, final KitLogger log) throws InterruptedException {
private void waitUntilPodIsReady(String podName, int nAwaitTimeout, final KitLogger log) {
final CountDownLatch readyLatch = new CountDownLatch(1);
try (Watch watch = client.pods().withName(podName).watch(new Watcher<Pod>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
*/
public abstract class BaseGenerator implements Generator {

private static final String LABEL_SCHEMA_VERSION = "1.0";
private static final String GIT_REMOTE = "origin";

private final GeneratorContext context;
private final String name;
private final GeneratorConfig config;
Expand Down Expand Up @@ -219,8 +222,8 @@ protected void addLatestTagIfSnapshot(BuildConfiguration.BuildConfigurationBuild
}

private boolean containsBuildConfiguration(List<ImageConfiguration> configs) {
for (ImageConfiguration config : configs) {
if (config.getBuildConfiguration() != null) {
for (ImageConfiguration imageConfig : configs) {
if (imageConfig.getBuildConfiguration() != null) {
return true;
}
}
Expand All @@ -229,8 +232,6 @@ private boolean containsBuildConfiguration(List<ImageConfiguration> configs) {

protected void addSchemaLabels(BuildConfiguration.BuildConfigurationBuilder buildBuilder, PrefixedLogger log) {
final JavaProject project = getProject();
String LABEL_SCHEMA_VERSION = "1.0";
String GIT_REMOTE = "origin";
String docURL = project.getDocumentationUrl();
Map<String, String> labels = new HashMap<>();

Expand All @@ -243,7 +244,7 @@ protected void addSchemaLabels(BuildConfiguration.BuildConfigurationBuilder buil
if (project.getSite() != null) {
labels.put(BuildLabelAnnotations.URL.value(), project.getSite());
}
if (project.getOrganizationName() != null && project.getOrganizationName() != null) {
if (project.getOrganizationName() != null && !project.getOrganizationName().isEmpty()) {
labels.put(BuildLabelAnnotations.VENDOR.value(), project.getOrganizationName());
}
labels.put(BuildLabelAnnotations.VERSION.value(), project.getVersion());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
*/
package org.eclipse.jkube.maven.plugin.mojo;

public class Openshift {
import org.eclipse.jkube.kit.common.KitLogger;

public static final String DEFAULT_LOG_PREFIX = "oc:";

private Openshift() {}
public interface KitLoggerProvider {

KitLogger getKitLogger();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.maven.plugin.mojo;

import java.io.File;

import org.eclipse.jkube.kit.common.util.OpenshiftHelper;

import io.fabric8.kubernetes.client.KubernetesClient;

public interface ManifestProvider extends KitLoggerProvider{

File getKubernetesManifest();

default File getManifest(KubernetesClient kubernetesClient) {
if (OpenshiftHelper.isOpenShift(kubernetesClient)) {
getKitLogger().warn("OpenShift cluster detected, using Kubernetes manifests");
getKitLogger().warn("Switch to openshift-maven-plugin in case there are any problems");
}
return getKubernetesManifest();
}
}
Loading