diff --git a/src/converter/devfile-converter.ts b/src/converter/devfile-converter.ts index da75af6..6096763 100644 --- a/src/converter/devfile-converter.ts +++ b/src/converter/devfile-converter.ts @@ -687,6 +687,9 @@ export class DevfileConverter { await this.fixInvalidVolumeName(devfileV2); await this.processVolumesFromDevfileV2(devfileV2); + // fix zip project location (multi-host --> single-host) + await this.fixProjectsZipLocations(devfileV2); + let content = JSON.stringify(devfileV2); // update devfile v1 constants @@ -695,6 +698,26 @@ export class DevfileConverter { return JSON.parse(content); } + // if some endpoints have excactly the same value, remove duplicates + async fixProjectsZipLocations(devfileV2: Devfile): Promise { + // get projects + devfileV2.projects.forEach(project => { + if (project.zip && project.zip.location) { + const location = project.zip.location; + // if matching a pattern, then update the location + const regex = + /https:\/\/(?.*?)-(?.*?)\.(?.*)\/devfile-registry\/resources\/(?.*)/gm; + let m: RegExpExecArray = regex.exec(location); + if (m !== null) { + const namespace = m.groups.namespace; + const resourcelink = m.groups.resourcelink; + const newLocation = `http://devfile-registry.${namespace}.svc:8080/resources/${resourcelink}`; + project.zip.location = newLocation; + } + } + }); + } + // if some endpoints have excactly the same value, remove duplicates async fixDuplicatedEndpoints(devfileV2: Devfile): Promise { // grab endpoints diff --git a/tests/_data/devfile-vertx-zip-v1.yaml b/tests/_data/devfile-vertx-zip-v1.yaml new file mode 100644 index 0000000..40d285e --- /dev/null +++ b/tests/_data/devfile-vertx-zip-v1.yaml @@ -0,0 +1,86 @@ +apiVersion: 1.0.0 +metadata: + generateName: vertx-http-booster- +projects: + - name: vertx-http-booster + source: + type: zip + location: 'https://codeready-codeready-workspaces-operator.apps.sandbox-m2.ll9k.p1.openshiftapps.com/devfile-registry/resources/vertx-http-booster-vertx-http-booster-master.zip' +components: + - id: redhat/java11/latest + type: chePlugin + preferences: + java.server.launchMode: Standard + - id: redhat/dependency-analytics/latest + type: chePlugin + - mountSources: true + endpoints: + - name: 8080-tcp + port: 8080 + type: dockerimage + memoryLimit: 512Mi + volumes: + - name: m2 + containerPath: /home/jboss/.m2 + alias: maven + image: registry.redhat.io/codeready-workspaces/plugin-java11-rhel8@sha256:79b59596870382e968850bc241ac41a3345a9079f28251808e35479f92bd3b86 + env: + - value: -XX:MaxRAMPercentage=50.0 -XX:+UseParallelGC -XX:MinHeapFreeRatio=10 + -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 + -Dsun.zip.disableMemoryMapping=true -Xms20m -Djava.security.egd=file:/dev/./urandom + -Duser.home=/home/jboss + name: JAVA_OPTS + - value: $(JAVA_OPTS) + name: MAVEN_OPTS +commands: + - name: 1. Build + actions: + - workdir: ${CHE_PROJECTS_ROOT}/vertx-http-booster + type: exec + command: mvn -DskipTests clean install + component: maven + - name: 2. Run + actions: + - workdir: ${CHE_PROJECTS_ROOT}/vertx-http-booster + type: exec + command: mvn -Dvertx.disableDnsResolver=true vertx:run + component: maven + - name: 3. Run in debug mode + actions: + - workdir: ${CHE_PROJECTS_ROOT}/vertx-http-booster + type: exec + command: mvn -DskipTests vertx:debug + component: maven + - name: 4. Run tests + actions: + - workdir: ${CHE_PROJECTS_ROOT}/vertx-http-booster + type: exec + command: mvn -DskipTests verify + component: maven + - name: 5. Log into deployment cluster + actions: + - workdir: ${CHE_PROJECTS_ROOT}/vertx-http-booster + type: exec + command: 'echo + + echo "Before you can deploy this application to an openshift cluster," + + echo "you must run ''oc login ...'' in the maven terminal." + + echo + + ' + component: maven + - name: 6. Deploy to OpenShift + actions: + - workdir: ${CHE_PROJECTS_ROOT}/vertx-http-booster + type: exec + command: mvn fabric8:deploy -Popenshift -DskipTests -Dvertx.disableDnsResolver=true + component: maven + - name: Debug remote java application + actions: + - referenceContent: "{\n \"version\": \"0.2.0\",\n \"configurations\": [\n \ + \ {\n \"type\": \"java\",\n \"name\": \"Debug (Attach) - Remote\"\ + ,\n \"request\": \"attach\",\n \"hostName\": \"localhost\",\n \ + \ \"port\": 5005\n }]\n }\n" + type: vscode-launch \ No newline at end of file diff --git a/tests/converter/devfile-converter.spec.ts b/tests/converter/devfile-converter.spec.ts index 51380ba..bae1a8f 100644 --- a/tests/converter/devfile-converter.spec.ts +++ b/tests/converter/devfile-converter.spec.ts @@ -677,4 +677,24 @@ describe('Test Devfile converter', () => { // the endpoint name is invalid and then it should have been fixed on the conversion expect(validationResult.valid).toBeTruthy(); }); + + // check that zip location is updated as part of the multi-host --> single-host conversion + test('convert v1 -> v2 devfile-vertx-zip-v1.yaml', async () => { + const devfileYamlPath = path.resolve(__dirname, '..', '_data', 'devfile-vertx-zip-v1.yaml'); + const devfileContent = await fs.readFile(devfileYamlPath, 'utf-8'); + const devfileV1 = jsYaml.load(devfileContent); + const convertedDevfileV2 = await devfileConverter.devfileV1toDevfileV2(devfileV1); + var v = new Validator(); + const validationResult = v.validate(convertedDevfileV2, schemaV2_2_0); + // the project name is invalid and then it should have been fixed on the conversion + expect(validationResult.valid).toBeTruthy(); + + // grab locations + const locations = convertedDevfileV2?.projects?.map(project => project.zip.location); + expect(locations.length).toBe(1); + const location = locations[0]; + expect(location).toBe( + 'http://devfile-registry.codeready-workspaces-operator.svc:8080/resources/vertx-http-booster-vertx-http-booster-master.zip' + ); + }); });