diff --git a/src/odo.ts b/src/odo.ts index 88aba8151..276f72bc6 100644 --- a/src/odo.ts +++ b/src/odo.ts @@ -19,6 +19,8 @@ import * as odo from './odo/config'; import { ComponentSettings } from './odo/config'; import { GlyphChars } from './util/constants'; import { Subject } from 'rxjs'; +import open = require('open'); +import { Progress } from './util/progress'; const Collapsed = TreeItemCollapsibleState.Collapsed; @@ -552,6 +554,10 @@ export class OdoImpl implements Odo { if (clusters.length === 0) { clusters = await this.getClustersWithOc(); } + if (clusters.length > 0 && clusters[0].contextValue === ContextType.CLUSTER) { + // kick out migration + this.convertObjectsFromPreviousOdoReleases(); + } return clusters; } @@ -1075,4 +1081,53 @@ export class OdoImpl implements Odo { } return data; } + + async convertObjectsFromPreviousOdoReleases() { + const getPreviosOdoResourceNames = (resourceId) => `oc get ${resourceId} -l app.kubernetes.io/component-name -o jsonpath="{range .items[*]}{.metadata.name}{\\"\\n\\"}{end}"`; + const result1 = await this.execute(getPreviosOdoResourceNames('dc'), __dirname, false); + const dcs = result1.stdout.split('\n'); + const result2 = await this.execute(getPreviosOdoResourceNames('ServiceInstance'), __dirname, false); + const sis = result2.stdout.split('\n'); + if ((result2.stdout !== '' && sis.length > 0) || (result1.stdout !== '' && dcs.length > 0)) { + 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'); + if (choice === 'Learn more...') { + open('https://github.com/redhat-developer/vscode-openshift-tools/wiki/Migration-to-v0.0.24'); + this.subject.next(new OdoEventImpl('changed', this.getClusters()[0])); + } else if (choice === 'Update') { + const errors = []; + Progress.execFunctionWithProgress('Updating cluster resources to work with latest OpenShift Connector release', async (progress) => { + for (const resourceId of ['DeploymentConfig', 'Route', 'BuildConfig', 'ImageStream', 'Service', 'pvc', 'Secret', 'ServiceInstance']) { + progress.report({increment: 100/8, message: resourceId}); + const cmd = getPreviosOdoResourceNames(resourceId); + const result = await this.execute(cmd, __dirname, false); + const resourceNames = result.error || result.stdout === '' ? [] : result.stdout.split('\n'); + for (const resourceName of resourceNames) { + try { + const result = await this.execute(`oc get ${resourceId} ${resourceName} -o json`); + const labels = JSON.parse(result.stdout).metadata.labels; + let command = `oc label ${resourceId} ${resourceName} --overwrite app.kubernetes.io/instance=${labels['app.kubernetes.io/component-name']}`; + command = command + ` app.kubernetes.io/part-of=${labels['app.kubernetes.io/name']}`; + if (labels['app.kubernetes.io/component-type']) { + command = command + ` app.kubernetes.io/name=${labels['app.kubernetes.io/component-type']}`; + } + if (labels['app.kubernetes.io/component-version']) { + command = command + ` app.openshift.io/runtime-version=${labels['app.kubernetes.io/component-version']}`; + } + await this.execute(command); + await this.execute(`oc label ${resourceId} ${resourceName} app.kubernetes.io/component-name-`); + } catch (err) { + errors.push(err); + } + } + } + this.subject.next(new OdoEventImpl('changed', this.getClusters()[0])); + }); + if (errors.length) { + window.showErrorMessage('Not all resources were updates, please see log for details.'); + } else { + window.showInformationMessage('Cluster resources have been successfuly updated.'); + } + } + } + } } diff --git a/test/odo.test.ts b/test/odo.test.ts index 164774548..7dcdcc768 100644 --- a/test/odo.test.ts +++ b/test/odo.test.ts @@ -117,6 +117,7 @@ suite("odo", () => { setup(() => { execStub = sandbox.stub(odoCli, 'execute'); + sandbox.stub(odo.OdoImpl.prototype, 'convertObjectsFromPreviousOdoReleases'); yamlStub = sandbox.stub(jsYaml, 'safeLoad'); sandbox.stub(fs, 'readFileSync'); }); diff --git a/test/openshift/component.test.ts b/test/openshift/component.test.ts index 30b66dfa4..3cacd7f61 100644 --- a/test/openshift/component.test.ts +++ b/test/openshift/component.test.ts @@ -63,6 +63,7 @@ suite('OpenShift/Component', () => { getApps = sandbox.stub(OpenShiftItem, 'getApplicationNames').resolves([appItem]); sandbox.stub(OpenShiftItem, 'getComponentNames').resolves([componentItem]); sandbox.stub(OpenShiftItem, 'getServiceNames').resolves([serviceItem]); + sandbox.stub(OdoImpl.prototype, 'convertObjectsFromPreviousOdoReleases'); }); teardown(() => {