use of io.fabric8.maven.docker.util.ImageName in project fabric8-maven-plugin by fabric8io.
the class BaseGenerator method addFrom.
/**
* Add the base image either from configuration or from a given selector
*
* @param builder for the build image configuration to add the from to.
*/
protected void addFrom(BuildImageConfiguration.Builder builder) {
String fromMode = getConfigWithSystemFallbackAndDefault(Config.fromMode, "fabric8.generator.fromMode", getFromModeDefault(context.getMode()));
String from = getConfigWithSystemFallbackAndDefault(Config.from, "fabric8.generator.from", null);
if ("docker".equalsIgnoreCase(fromMode)) {
String fromImage = from;
if (fromImage == null) {
fromImage = fromSelector != null ? fromSelector.getFrom() : null;
}
builder.from(fromImage);
log.info("Using Docker image %s as base / builder", fromImage);
} else if ("istag".equalsIgnoreCase(fromMode)) {
Map<String, String> fromExt = new HashMap<>();
if (from != null) {
ImageName iName = new ImageName(from);
// user/project is considered to be the namespace
String tag = iName.getTag();
if (StringUtils.isBlank(tag)) {
tag = "latest";
}
fromExt.put(OpenShiftBuildStrategy.SourceStrategy.name.key(), iName.getSimpleName() + ":" + tag);
if (iName.getUser() != null) {
fromExt.put(OpenShiftBuildStrategy.SourceStrategy.namespace.key(), iName.getUser());
}
fromExt.put(OpenShiftBuildStrategy.SourceStrategy.kind.key(), "ImageStreamTag");
} else {
fromExt = fromSelector != null ? fromSelector.getImageStreamTagFromExt() : null;
}
if (fromExt != null) {
String namespace = fromExt.get(OpenShiftBuildStrategy.SourceStrategy.namespace.key());
if (namespace != null) {
log.info("Using ImageStreamTag '%s' from namespace '%s' as builder image", fromExt.get(OpenShiftBuildStrategy.SourceStrategy.name.key()), namespace);
} else {
log.info("Using ImageStreamTag '%s' as builder image", fromExt.get(OpenShiftBuildStrategy.SourceStrategy.name.key()));
}
builder.fromExt(fromExt);
}
} else {
throw new IllegalArgumentException(String.format("Invalid 'fromMode' in generator configuration for '%s'", getName()));
}
}
use of io.fabric8.maven.docker.util.ImageName in project fabric8-maven-plugin by fabric8io.
the class DeploymentOpenShiftConverter method convert.
@Override
public HasMetadata convert(HasMetadata item, boolean trimImageInContainerSpec, boolean enableAutomaticTrigger) {
Deployment resource = (Deployment) item;
DeploymentConfigBuilder builder = new DeploymentConfigBuilder();
builder.withMetadata(resource.getMetadata());
DeploymentSpec spec = resource.getSpec();
if (spec != null) {
DeploymentConfigFluent.SpecNested<DeploymentConfigBuilder> specBuilder = builder.withNewSpec();
Integer replicas = spec.getReplicas();
if (replicas != null) {
specBuilder.withReplicas(replicas);
}
Integer revisionHistoryLimit = spec.getRevisionHistoryLimit();
if (revisionHistoryLimit != null) {
specBuilder.withRevisionHistoryLimit(revisionHistoryLimit);
}
LabelSelector selector = spec.getSelector();
if (selector != null) {
Map<String, String> matchLabels = selector.getMatchLabels();
if (matchLabels != null && !matchLabels.isEmpty()) {
specBuilder.withSelector(matchLabels);
}
}
Map<String, String> containerToImageMap = new HashMap<>();
PodTemplateSpec template = spec.getTemplate();
if (template != null) {
specBuilder.withTemplate(template);
PodSpec podSpec = template.getSpec();
notNull(podSpec, "No PodSpec for PodTemplate:" + template);
List<Container> containers = podSpec.getContainers();
notNull(podSpec, "No containers for PodTemplate.spec: " + template);
for (Container container : containers) {
validateContainer(container);
containerToImageMap.put(container.getName(), container.getImage());
}
}
DeploymentStrategy strategy = spec.getStrategy();
String strategyType = null;
if (strategy != null) {
strategyType = strategy.getType();
}
if (openshiftDeployTimeoutSeconds != null && openshiftDeployTimeoutSeconds > 0) {
if (Strings.isNullOrBlank(strategyType) || "Rolling".equals(strategyType)) {
specBuilder.withNewStrategy().withType("Rolling").withNewRollingParams().withTimeoutSeconds(openshiftDeployTimeoutSeconds).endRollingParams().endStrategy();
} else if ("Recreate".equals(strategyType)) {
specBuilder.withNewStrategy().withType("Recreate").withNewRecreateParams().withTimeoutSeconds(openshiftDeployTimeoutSeconds).endRecreateParams().endStrategy();
} else {
specBuilder.withNewStrategy().withType(strategyType).endStrategy();
}
} else if (Strings.isNotBlank(strategyType)) {
// TODO is there any values we can copy across?
specBuilder.withNewStrategy().withType(strategyType).endStrategy();
}
// lets add a default trigger so that its triggered when we change its config
if (enableAutomaticTrigger) {
specBuilder.addNewTrigger().withType("ConfigChange").endTrigger();
}
// add a new image change trigger for the build stream
if (containerToImageMap.size() != 0) {
if (mode.equals(PlatformMode.openshift)) {
for (Map.Entry<String, String> entry : containerToImageMap.entrySet()) {
String containerName = entry.getKey();
ImageName image = new ImageName(entry.getValue());
String tag = image.getTag() != null ? image.getTag() : "latest";
specBuilder.addNewTrigger().withType("ImageChange").withNewImageChangeParams().withAutomatic(enableAutomaticTrigger).withNewFrom().withKind("ImageStreamTag").withName(image.getSimpleName() + ":" + tag).withNamespace(image.getUser()).endFrom().withContainerNames(containerName).endImageChangeParams().endTrigger();
}
}
if (trimImageInContainerSpec) {
/*
* In Openshift 3.7, update to container image is automatically triggering redeployments
* and those subsequent rollouts lead to RC complaining about a missing image reference.
*
* See this : https://github.com/openshift/origin/issues/18406#issuecomment-364090247
*
* this the time it gets fixed. Do this:
* Since we're using ImageTrigger here, set container image to " ". If there is any config
* change never set to image else than " "; so doing oc apply/rollouts won't be creating
* re-deployments again and again.
*
*/
List<Container> containers = template.getSpec().getContainers();
for (Integer nIndex = 0; nIndex < containers.size(); nIndex++) {
containers.get(nIndex).setImage(" ");
}
template.getSpec().setContainers(containers);
specBuilder.withTemplate(template);
}
}
specBuilder.endSpec();
}
return builder.build();
}
use of io.fabric8.maven.docker.util.ImageName in project fabric8-maven-plugin by fabric8io.
the class DockerImageWatcher method buildImage.
protected void buildImage(ImageConfiguration imageConfig) throws DockerAccessException, MojoExecutionException {
String imageName = imageConfig.getName();
// lets regenerate the label
try {
String imagePrefix = getImagePrefix(imageName);
imageName = imagePrefix + "%t";
ImageNameFormatter formatter = new ImageNameFormatter(getContext().getProject(), new Date());
imageName = formatter.format(imageName);
imageConfig.setName(imageName);
log.info("New image name: " + imageConfig.getName());
} catch (Exception e) {
log.error("Caught: " + e, e);
}
}
use of io.fabric8.maven.docker.util.ImageName in project fabric8-maven-plugin by fabric8io.
the class DockerImageWatcher method updateImageName.
private void updateImageName(KubernetesClient kubernetes, String namespace, HasMetadata entity, String imagePrefix, String imageName) {
String name = KubernetesHelper.getName(entity);
if (entity instanceof Deployment) {
Deployment resource = (Deployment) entity;
DeploymentSpec spec = resource.getSpec();
if (spec != null) {
if (updateImageName(entity, spec.getTemplate(), imagePrefix, imageName)) {
kubernetes.extensions().deployments().inNamespace(namespace).withName(name).replace(resource);
}
}
} else if (entity instanceof ReplicaSet) {
ReplicaSet resource = (ReplicaSet) entity;
ReplicaSetSpec spec = resource.getSpec();
if (spec != null) {
if (updateImageName(entity, spec.getTemplate(), imagePrefix, imageName)) {
kubernetes.extensions().replicaSets().inNamespace(namespace).withName(name).replace(resource);
}
}
} else if (entity instanceof ReplicationController) {
ReplicationController resource = (ReplicationController) entity;
ReplicationControllerSpec spec = resource.getSpec();
if (spec != null) {
if (updateImageName(entity, spec.getTemplate(), imagePrefix, imageName)) {
kubernetes.replicationControllers().inNamespace(namespace).withName(name).replace(resource);
}
}
} else if (entity instanceof DeploymentConfig) {
DeploymentConfig resource = (DeploymentConfig) entity;
DeploymentConfigSpec spec = resource.getSpec();
if (spec != null) {
if (updateImageName(entity, spec.getTemplate(), imagePrefix, imageName)) {
OpenShiftClient openshiftClient = new Controller(kubernetes).getOpenShiftClientOrNull();
if (openshiftClient == null) {
log.warn("Ignoring DeploymentConfig %s as not connected to an OpenShift cluster", name);
}
openshiftClient.deploymentConfigs().inNamespace(namespace).withName(name).replace(resource);
}
}
}
}
use of io.fabric8.maven.docker.util.ImageName in project fabric8 by fabric8io.
the class SessionListener method hasRegistry.
/**
* Checks to see if there's a registry name already provided in the image name
*
* Code influenced from <a href="https://github.com/rhuss/docker-maven-plugin/blob/master/src/main/java/org/jolokia/docker/maven/util/ImageName.java">docker-maven-plugin</a>
* @param imageName
* @return true if the image name contains a registry
*/
public static boolean hasRegistry(String imageName) {
if (imageName == null) {
throw new NullPointerException("Image name must not be null");
}
Pattern tagPattern = Pattern.compile("^(.+?)(?::([^:/]+))?$");
Matcher matcher = tagPattern.matcher(imageName);
if (!matcher.matches()) {
throw new IllegalArgumentException(imageName + " is not a proper image name ([registry/][repo][:port]");
}
String rest = matcher.group(1);
String[] parts = rest.split("\\s*/\\s*");
String part = parts[0];
return part.contains(".") || part.contains(":");
}
Aggregations