diff options
6 files changed, 85 insertions, 7 deletions
diff --git a/config-model-api/src/main/java/com/yahoo/config/application/api/xml/DeploymentSpecXmlReader.java b/config-model-api/src/main/java/com/yahoo/config/application/api/xml/DeploymentSpecXmlReader.java index 050fba1cd2f..1915aa66d4c 100644 --- a/config-model-api/src/main/java/com/yahoo/config/application/api/xml/DeploymentSpecXmlReader.java +++ b/config-model-api/src/main/java/com/yahoo/config/application/api/xml/DeploymentSpecXmlReader.java @@ -49,7 +49,6 @@ public class DeploymentSpecXmlReader { private static final String deploymentTag = "deployment"; private static final String instanceTag = "instance"; - private static final String majorVersionTag = "major-version"; private static final String testTag = "test"; private static final String stagingTag = "staging"; private static final String upgradeTag = "upgrade"; @@ -67,6 +66,7 @@ public class DeploymentSpecXmlReader { private static final String athenzServiceAttribute = "athenz-service"; private static final String athenzDomainAttribute = "athenz-domain"; private static final String testerFlavorAttribute = "tester-flavor"; + private static final String majorVersionAttribute = "major-version"; private final boolean validate; private final Clock clock; @@ -133,7 +133,7 @@ public class DeploymentSpecXmlReader { } return new DeploymentSpec(steps, - optionalIntegerAttribute(majorVersionTag, root), + optionalIntegerAttribute(majorVersionAttribute, root), stringAttribute(athenzDomainAttribute, root).map(AthenzDomain::from), stringAttribute(athenzServiceAttribute, root).map(AthenzService::from), applicationEndpoints, diff --git a/hosted-api/src/main/java/ai/vespa/hosted/api/ControllerHttpClient.java b/hosted-api/src/main/java/ai/vespa/hosted/api/ControllerHttpClient.java index 3189a2c8e92..2860b8878b7 100644 --- a/hosted-api/src/main/java/ai/vespa/hosted/api/ControllerHttpClient.java +++ b/hosted-api/src/main/java/ai/vespa/hosted/api/ControllerHttpClient.java @@ -41,6 +41,7 @@ import java.util.HashSet; import java.util.List; import java.util.Objects; import java.util.Optional; +import java.util.OptionalInt; import java.util.OptionalLong; import java.util.Set; import java.util.concurrent.Callable; @@ -152,10 +153,13 @@ public abstract class ControllerHttpClient { return ZoneId.from(environment.value(), rootObject.field("name").asString()); } - /** Returns the Vespa version to compile against, for a hosted Vespa application. This is its lowest runtime version. */ - public String compileVersion(ApplicationId id) { - return toInspector(send(request(HttpRequest.newBuilder(compileVersionPath(id.tenant(), id.application())) - .timeout(Duration.ofSeconds(20)), + /** Returns the Vespa version to compile against, for a hosted Vespa application */ + public String compileVersion(ApplicationId id, OptionalInt allowMajor) { + URI url = compileVersionPath(id.tenant(), id.application()); + if (allowMajor.isPresent()) { + url = withQuery(url, "allowMajor", Integer.toString(allowMajor.getAsInt())); + } + return toInspector(send(request(HttpRequest.newBuilder(url).timeout(Duration.ofSeconds(20)), GET))) .field("compileVersion").asString(); } diff --git a/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/CompileVersionMojo.java b/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/CompileVersionMojo.java index eff1acfeb06..707da31b5ef 100644 --- a/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/CompileVersionMojo.java +++ b/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/CompileVersionMojo.java @@ -1,14 +1,18 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package ai.vespa.hosted.plugin; +import com.yahoo.text.XML; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; +import org.w3c.dom.Element; import java.io.IOException; import java.io.UncheckedIOException; import java.nio.file.Files; +import java.nio.file.NoSuchFileException; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.OptionalInt; /** * Finds the Vespa version to compile against, for a hosted Vespa application. @@ -24,11 +28,41 @@ public class CompileVersionMojo extends AbstractVespaMojo { @Override protected void doExecute() throws IOException { Path output = Paths.get(outputFile).toAbsolutePath(); - String compileVersion = controller.compileVersion(id); + OptionalInt allowMajor = majorVersion(Paths.get("src/main/application/deployment.xml")); + String compileVersion = controller.compileVersion(id, allowMajor); + if (allowMajor.isPresent()) { + getLog().info("Allowing major version " + allowMajor.getAsInt() + "."); + } getLog().info("Vespa version to compile against is '" + compileVersion + "'."); getLog().info("Writing compile version to '" + output + "'."); Files.createDirectories(output.getParent()); Files.writeString(output, compileVersion); } + /** Returns the major version declared in given deploymentXml, if any */ + static OptionalInt majorVersion(Path deploymentXml) { + try { + String xml = Files.readString(deploymentXml); + Element deploymentTag = XML.getDocument(xml).getDocumentElement(); + if (deploymentTag == null) return OptionalInt.empty(); + String allowMajor = deploymentTag.getAttribute("major-version"); + if (allowMajor.isEmpty()) return OptionalInt.empty(); + return OptionalInt.of(parseMajor(allowMajor)); + } catch (NoSuchFileException ignored) { + return OptionalInt.empty(); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + + private static int parseMajor(String s) { + try { + int major = Integer.parseInt(s); + if (major < 1) throw new IllegalArgumentException("Major version must be positive, got " + major); + return major; + } catch (NumberFormatException e) { + throw new IllegalArgumentException("Invalid major version '" + s + "'", e); + } + } + } diff --git a/vespa-maven-plugin/src/test/java/ai/vespa/hosted/plugin/CompileVersionMojoTest.java b/vespa-maven-plugin/src/test/java/ai/vespa/hosted/plugin/CompileVersionMojoTest.java new file mode 100644 index 00000000000..ebb91934470 --- /dev/null +++ b/vespa-maven-plugin/src/test/java/ai/vespa/hosted/plugin/CompileVersionMojoTest.java @@ -0,0 +1,28 @@ +package ai.vespa.hosted.plugin; + +import org.junit.jupiter.api.Test; + +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.OptionalInt; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * @author mpolden + */ +class CompileVersionMojoTest { + + @Test + public void allow_major() { + assertMajorVersion(OptionalInt.empty(), Paths.get("non-existent-deployment.xml")); + assertMajorVersion(OptionalInt.empty(), Paths.get("src/test/resources/deployment.xml")); + assertMajorVersion(OptionalInt.of(8), Paths.get("src/test/resources/deployment-with-major.xml")); + } + + private void assertMajorVersion(OptionalInt expected, Path deploymentXml) { + OptionalInt allowMajor = CompileVersionMojo.majorVersion(deploymentXml); + assertEquals(expected, allowMajor); + } + +} diff --git a/vespa-maven-plugin/src/test/resources/deployment-with-major.xml b/vespa-maven-plugin/src/test/resources/deployment-with-major.xml new file mode 100644 index 00000000000..5ffc3e81fcc --- /dev/null +++ b/vespa-maven-plugin/src/test/resources/deployment-with-major.xml @@ -0,0 +1,6 @@ +<!-- Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. --> +<deployment version='1.0' major-version='8'> + <prod> + <region>us-east</region> + </prod> +</deployment> diff --git a/vespa-maven-plugin/src/test/resources/deployment.xml b/vespa-maven-plugin/src/test/resources/deployment.xml new file mode 100644 index 00000000000..3d63224cf13 --- /dev/null +++ b/vespa-maven-plugin/src/test/resources/deployment.xml @@ -0,0 +1,6 @@ +<!-- Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. --> +<deployment version='1.0'> + <prod> + <region>us-east</region> + </prod> +</deployment> |