Skip to content

Commit 7fa924a

Browse files
authored
Add migration for component/services deployed in active cluster (#1107)
Test if there are ServiceInstances and DeploymentConfigs created with previous odo releases. Ask for what to do when detected and update all odo created resources if requested to work with new version of odo.
1 parent 5ac6307 commit 7fa924a

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

src/odo.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import * as odo from './odo/config';
1919
import { ComponentSettings } from './odo/config';
2020
import { GlyphChars } from './util/constants';
2121
import { Subject } from 'rxjs';
22+
import open = require('open');
23+
import { Progress } from './util/progress';
2224

2325
const Collapsed = TreeItemCollapsibleState.Collapsed;
2426

@@ -552,6 +554,10 @@ export class OdoImpl implements Odo {
552554
if (clusters.length === 0) {
553555
clusters = await this.getClustersWithOc();
554556
}
557+
if (clusters.length > 0 && clusters[0].contextValue === ContextType.CLUSTER) {
558+
// kick out migration
559+
this.convertObjectsFromPreviousOdoReleases();
560+
}
555561
return clusters;
556562
}
557563

@@ -1075,4 +1081,53 @@ export class OdoImpl implements Odo {
10751081
}
10761082
return data;
10771083
}
1084+
1085+
async convertObjectsFromPreviousOdoReleases() {
1086+
const getPreviosOdoResourceNames = (resourceId) => `oc get ${resourceId} -l app.kubernetes.io/component-name -o jsonpath="{range .items[*]}{.metadata.name}{\\"\\n\\"}{end}"`;
1087+
const result1 = await this.execute(getPreviosOdoResourceNames('dc'), __dirname, false);
1088+
const dcs = result1.stdout.split('\n');
1089+
const result2 = await this.execute(getPreviosOdoResourceNames('ServiceInstance'), __dirname, false);
1090+
const sis = result2.stdout.split('\n');
1091+
if ((result2.stdout !== '' && sis.length > 0) || (result1.stdout !== '' && dcs.length > 0)) {
1092+
const choice = await window.showWarningMessage(`Some of resources in cluster must be updated to work with latest release of OpenShift Connector Extension.`, 'Learn more...', 'Update', 'Cancel');
1093+
if (choice === 'Learn more...') {
1094+
open('https://github.com/redhat-developer/vscode-openshift-tools/wiki/Migration-to-v0.0.24');
1095+
this.subject.next(new OdoEventImpl('changed', this.getClusters()[0]));
1096+
} else if (choice === 'Update') {
1097+
const errors = [];
1098+
Progress.execFunctionWithProgress('Updating cluster resources to work with latest OpenShift Connector release', async (progress) => {
1099+
for (const resourceId of ['DeploymentConfig', 'Route', 'BuildConfig', 'ImageStream', 'Service', 'pvc', 'Secret', 'ServiceInstance']) {
1100+
progress.report({increment: 100/8, message: resourceId});
1101+
const cmd = getPreviosOdoResourceNames(resourceId);
1102+
const result = await this.execute(cmd, __dirname, false);
1103+
const resourceNames = result.error || result.stdout === '' ? [] : result.stdout.split('\n');
1104+
for (const resourceName of resourceNames) {
1105+
try {
1106+
const result = await this.execute(`oc get ${resourceId} ${resourceName} -o json`);
1107+
const labels = JSON.parse(result.stdout).metadata.labels;
1108+
let command = `oc label ${resourceId} ${resourceName} --overwrite app.kubernetes.io/instance=${labels['app.kubernetes.io/component-name']}`;
1109+
command = command + ` app.kubernetes.io/part-of=${labels['app.kubernetes.io/name']}`;
1110+
if (labels['app.kubernetes.io/component-type']) {
1111+
command = command + ` app.kubernetes.io/name=${labels['app.kubernetes.io/component-type']}`;
1112+
}
1113+
if (labels['app.kubernetes.io/component-version']) {
1114+
command = command + ` app.openshift.io/runtime-version=${labels['app.kubernetes.io/component-version']}`;
1115+
}
1116+
await this.execute(command);
1117+
await this.execute(`oc label ${resourceId} ${resourceName} app.kubernetes.io/component-name-`);
1118+
} catch (err) {
1119+
errors.push(err);
1120+
}
1121+
}
1122+
}
1123+
this.subject.next(new OdoEventImpl('changed', this.getClusters()[0]));
1124+
});
1125+
if (errors.length) {
1126+
window.showErrorMessage('Not all resources were updates, please see log for details.');
1127+
} else {
1128+
window.showInformationMessage('Cluster resources have been successfuly updated.');
1129+
}
1130+
}
1131+
}
1132+
}
10781133
}

test/odo.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ suite("odo", () => {
117117

118118
setup(() => {
119119
execStub = sandbox.stub(odoCli, 'execute');
120+
sandbox.stub(odo.OdoImpl.prototype, 'convertObjectsFromPreviousOdoReleases');
120121
yamlStub = sandbox.stub(jsYaml, 'safeLoad');
121122
sandbox.stub(fs, 'readFileSync');
122123
});

test/openshift/component.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ suite('OpenShift/Component', () => {
6363
getApps = sandbox.stub(OpenShiftItem, 'getApplicationNames').resolves([appItem]);
6464
sandbox.stub(OpenShiftItem, 'getComponentNames').resolves([componentItem]);
6565
sandbox.stub(OpenShiftItem, 'getServiceNames').resolves([serviceItem]);
66+
sandbox.stub(OdoImpl.prototype, 'convertObjectsFromPreviousOdoReleases');
6667
});
6768

6869
teardown(() => {

0 commit comments

Comments
 (0)