Skip to content

Commit

Permalink
feat: add version info when the extension starts
Browse files Browse the repository at this point in the history
  • Loading branch information
metacosm committed Jul 30, 2021
1 parent 8de20b5 commit 7ae4cc7
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import io.javaoperatorsdk.operator.api.Controller;
import io.javaoperatorsdk.operator.api.ResourceController;
import io.javaoperatorsdk.operator.api.config.ConfigurationService;
import io.javaoperatorsdk.operator.api.config.Utils;
import io.quarkiverse.operatorsdk.runtime.*;
import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.ObserverRegistrationPhaseBuildItem;
Expand Down Expand Up @@ -113,7 +112,6 @@ ConfigurationServiceBuildItem createConfigurationServiceAndOperator(
BuildProducer<ForceNonWeakReflectiveClassBuildItem> forcedReflectionClasses,
LiveReloadBuildItem liveReload) {

final var version = Utils.loadFromProperties();
final CRDConfiguration crdConfig = buildTimeConfiguration.crd;
final boolean validateCustomResources = ConfigurationUtils.shouldValidateCustomResources(
buildTimeConfiguration.checkCRDAndValidateLocalModel, buildTimeConfiguration.crd.validate, log);
Expand Down Expand Up @@ -141,14 +139,12 @@ ConfigurationServiceBuildItem createConfigurationServiceAndOperator(
// if the app doesn't provide a main class, add the StartupListener
if (index.getAllKnownImplementors(DotName.createSimple(QuarkusApplication.class.getName())).isEmpty()) {
additionalBeans.produce(AdditionalBeanBuildItem.builder()
.addBeanClass(StartupListener.class).setDefaultScope(DotName.createSimple(Singleton.class.getName())).setUnremovable()
.addBeanClass(StartupListener.class).setDefaultScope(DotName.createSimple(Singleton.class.getName()))
.setUnremovable()
.build());
}

return new ConfigurationServiceBuildItem(
new Version(version.getSdkVersion(), version.getCommit(), version.getBuiltTime()),
controllerConfigs,
crdInfo);
return new ConfigurationServiceBuildItem(Version.loadFromProperties(), controllerConfigs, crdInfo);
}

/**
Expand Down
25 changes: 25 additions & 0 deletions runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,31 @@
</execution>
</executions>
</plugin>
<plugin>
<!-- Used to generate the version / commit information -->
<groupId>io.github.git-commit-id</groupId>
<artifactId>git-commit-id-maven-plugin</artifactId>
<version>5.0.0</version>
<executions>
<execution>
<id>get-the-git-infos</id>
<goals>
<goal>revision</goal>
</goals>
<phase>initialize</phase>
</execution>
</executions>
<configuration>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<generateGitPropertiesFilename>${project.build.outputDirectory}/extension-version.properties</generateGitPropertiesFilename>
<includeOnlyProperties>
<includeOnlyProperty>^git.build.(time|version)$</includeOnlyProperty>
<includeOnlyProperty>^git.commit.id.(abbrev|full)$</includeOnlyProperty>
<includeOnlyProperty>git.branch</includeOnlyProperty>
</includeOnlyProperties>
<commitIdGenerationMode>full</commitIdGenerationMode>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,31 @@

import javax.enterprise.event.Observes;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.javaoperatorsdk.operator.Operator;
import io.quarkus.runtime.StartupEvent;

public class StartupListener {
private static final Logger log = LoggerFactory.getLogger(StartupListener.class);
private final Operator operator;
private final QuarkusConfigurationService configurationService;

public StartupListener(Operator operator) {
public StartupListener(Operator operator, QuarkusConfigurationService configurationService) {
this.operator = operator;
this.configurationService = configurationService;
}

public void onStartup(@Observes StartupEvent event) {
if (configurationService.getVersion() instanceof Version) {
final var version = ((Version) configurationService.getVersion());
final var branch = !version.getExtensionBranch().equals(Version.UNKNOWN)
? " on branch: " + version.getExtensionBranch()
: "";
log.info("Quakus Java Operator SDK extension {} (commit: {}{})", version.getExtensionVersion(),
version.getExtensionCommit(), branch);
}
operator.start();
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,78 @@
package io.quarkiverse.operatorsdk.runtime;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Date;
import java.util.Properties;

import org.jboss.logging.Logger;

import io.javaoperatorsdk.operator.api.config.Utils;
import io.quarkus.runtime.annotations.RecordableConstructor;

/**
* Re-publish with a recordable constructor so that quarkus can do its thing with it!
*/
public class Version extends io.javaoperatorsdk.operator.api.config.Version {
private static final Logger log = Logger.getLogger(Version.class.getName());
public static final String UNKNOWN = "unknown";

private final String extensionVersion;
private final String extensionBranch;
private final String extensionCommit;
private final Date extensionBuildTime;

@RecordableConstructor
public Version(String sdkVersion, String commit, Date builtTime) {
@RecordableConstructor // constructor needs to be recordable for the class to be passed around by Quarkus
public Version(String sdkVersion, String commit, Date builtTime, String extensionVersion, String extensionCommit,
String extensionBranch, Date extensionBuildTime) {
super(sdkVersion, commit, builtTime);
this.extensionVersion = extensionVersion;
this.extensionBranch = extensionBranch;
this.extensionCommit = extensionCommit;
this.extensionBuildTime = extensionBuildTime;
}

public String getExtensionVersion() {
return extensionVersion;
}

public String getExtensionBranch() {
return extensionBranch;
}

public String getExtensionCommit() {
return extensionCommit;
}

public Date getExtensionBuildTime() {
return extensionBuildTime;
}

public static Version loadFromProperties() {
final var sdkVersion = Utils.loadFromProperties();
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("extension-version.properties");
Properties properties = new Properties();
if (is != null) {
try {
properties.load(is);
} catch (IOException e) {
log.warnf("Couldn't load extension version information: {0}", e.getMessage());
}
} else {
log.warn("Couldn't find version.properties file. Default version information will be used.");
}

Date builtTime;
try {
builtTime = (new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")).parse(properties.getProperty("git.build.time"));
} catch (ParseException var4) {
builtTime = Date.from(Instant.EPOCH);
}

return new Version(sdkVersion.getSdkVersion(), sdkVersion.getCommit(), sdkVersion.getBuiltTime(),
properties.getProperty("git.build.version", UNKNOWN),
properties.getProperty("git.commit.id.abbrev", UNKNOWN),
properties.getProperty("git.branch", UNKNOWN),
builtTime);
}
}

0 comments on commit 7ae4cc7

Please sign in to comment.