Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
Feature 456 : Add labels from label-schema.org to the generator images (
Browse files Browse the repository at this point in the history
  • Loading branch information
Devang Gaur authored and lordofthejars committed Mar 11, 2019
1 parent 3d14903 commit f253a34
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ After this we will switch probably to real [Semantic Versioning 2.0.0](http://se
* Feature 1498: Allow users to define secrets from annotations
* Fix 1522: Remove need to initialize DockerAccess when on Openshift
* Fix 1517: update vmp groupid and vert.x version
* Feature 456: Add labels from label-schema.org to the generator images.

### 4.0.0-M2 (2018-12-14)
* Fix 10: Make VolumeConfiguration more flexible
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Copyright 2016 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version
* 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package io.fabric8.maven.core.util;

public enum BuildLabelAnnotations {
BUILD_DATE("build-date"),
NAME("name"),
DESCRIPTION("description"),
USAGE("usage"),
URL("url"),
VCS_URL("vcs-url"),
VCS_REF("vcs-ref"),
VENDOR("vendor"),
VERSION("version"),
SCHEMA_VERSION("schema-version");

private final String annotation;

BuildLabelAnnotations(String anno) {
this.annotation = "org.label-schema." + anno;
}

public String value() {
return annotation;
}

@Override
public String toString() {
return value();
}
}
83 changes: 83 additions & 0 deletions core/src/main/java/io/fabric8/maven/core/util/BuildLabelUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* Copyright 2016 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version
* 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package io.fabric8.maven.core.util;

import io.fabric8.maven.docker.config.BuildImageConfiguration;
import org.apache.maven.model.DistributionManagement;
import org.apache.maven.model.Site;
import org.apache.maven.project.MavenProject;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Repository;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

public class BuildLabelUtil {

private static String getDocumentationUrl (MavenProject project) {
while (project != null) {
DistributionManagement distributionManagement = project.getDistributionManagement();
if (distributionManagement != null) {
Site site = distributionManagement.getSite();
if (site != null) {
return site.getUrl();
}
}
project = project.getParent();
}
return null;
}

public static void addSchemaLabels (BuildImageConfiguration.Builder buildBuilder, MavenProject project, PrefixedLogger log) {
String LABEL_SCHEMA_VERSION = "1.0";
String GIT_REMOTE = "origin";
String docURL = getDocumentationUrl(project);
Map<String, String> labels = new HashMap<>();

labels.put(BuildLabelAnnotations.BUILD_DATE.value(), LocalDateTime.now().toString());
labels.put(BuildLabelAnnotations.NAME.value(), project.getName());
labels.put(BuildLabelAnnotations.DESCRIPTION.value(), project.getDescription());
if (docURL != null) {
labels.put(BuildLabelAnnotations.USAGE.value(), docURL);
}
if (project.getUrl() != null) {
labels.put(BuildLabelAnnotations.URL.value(), project.getUrl());
}
if (project.getOrganization() != null && project.getOrganization().getName() != null) {
labels.put(BuildLabelAnnotations.VENDOR.value(), project.getOrganization().getName());
}
labels.put(BuildLabelAnnotations.VERSION.value(), project.getVersion());
labels.put(BuildLabelAnnotations.SCHEMA_VERSION.value(), LABEL_SCHEMA_VERSION);

try {
Repository repository = GitUtil.getGitRepository(project.getBasedir());
String commitID = GitUtil.getGitCommitId(repository);
labels.put(BuildLabelAnnotations.VCS_REF.value(), commitID);
String gitRemoteUrl = repository.getConfig().getString("remote", GIT_REMOTE, "url");
if (gitRemoteUrl != null) {
labels.put(BuildLabelAnnotations.VCS_URL.value(), gitRemoteUrl);
} else {
log.warn("Could not detect any git remote");
}
} catch (IOException | GitAPIException | NullPointerException e) {
log.error("Cannot extract Git information: " + e, e);
} finally {
buildBuilder.labels(labels);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import org.apache.maven.plugins.assembly.model.FileSet;
import org.apache.maven.project.MavenProject;

import static io.fabric8.maven.core.util.BuildLabelUtil.addSchemaLabels;

/**
* @author roland
* @since 21/09/16
Expand Down Expand Up @@ -114,6 +116,8 @@ public List<ImageConfiguration> customize(List<ImageConfiguration> configs, bool
BuildImageConfiguration.Builder buildBuilder = null;
buildBuilder = new BuildImageConfiguration.Builder()
.ports(extractPorts());

addSchemaLabels(buildBuilder, getContext().getProject(), log);
addFrom(buildBuilder);
if (!prePackagePhase) {
// Only add assembly if not in a pre-package phase where the referenced files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import io.fabric8.maven.generator.api.support.BaseGenerator;
import org.apache.commons.lang3.StringUtils;

import static io.fabric8.maven.core.util.BuildLabelUtil.addSchemaLabels;

public class KarafGenerator extends BaseGenerator {

private static final String KARAF_MAVEN_PLUGIN_ARTIFACT_ID = "karaf-maven-plugin";
Expand All @@ -53,6 +55,7 @@ public List<ImageConfiguration> customize(List<ImageConfiguration> configs, bool
BuildImageConfiguration.Builder buildBuilder = new BuildImageConfiguration.Builder()
.ports(extractPorts())
.cmd(new Arguments(getConfig(Config.cmd)));
addSchemaLabels(buildBuilder, getContext().getProject(), log);
addFrom(buildBuilder);
if (!prePackagePhase) {
buildBuilder.assembly(createAssembly());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import io.fabric8.maven.generator.api.support.BaseGenerator;
import io.fabric8.maven.generator.webapp.handler.CustomAppServerHandler;

import static io.fabric8.maven.core.util.BuildLabelUtil.addSchemaLabels;

/**
* A generator for WAR apps
*
Expand Down Expand Up @@ -95,6 +97,8 @@ public List<ImageConfiguration> customize(List<ImageConfiguration> configs, bool
.ports(handler.exposedPorts())
.cmd(new Arguments(getDockerRunCommand(handler)))
.env(getEnv(handler));

addSchemaLabels(buildBuilder, getContext().getProject(), log);
if (!prePackagePhase) {
buildBuilder.assembly(createAssembly(handler));
}
Expand Down

0 comments on commit f253a34

Please sign in to comment.