Skip to content

Commit

Permalink
fix: handle zip locations from v1 to v2
Browse files Browse the repository at this point in the history
due to multi-host --> single-host

Change-Id: I177f4c99f430419ad25f9f7da191a97e4ed1f475
Signed-off-by: Florent Benoit <fbenoit@redhat.com>
  • Loading branch information
benoitf committed May 31, 2022
1 parent e37ee54 commit 1585329
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/converter/devfile-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<void> {
// 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:\/\/(?<name>.*?)-(?<namespace>.*?)\.(?<subdomain>.*)\/devfile-registry\/resources\/(?<resourcelink>.*)/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<void> {
// grab endpoints
Expand Down
86 changes: 86 additions & 0 deletions tests/_data/devfile-vertx-zip-v1.yaml
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions tests/converter/devfile-converter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
});
});

0 comments on commit 1585329

Please sign in to comment.