-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check Pipeline Operator CSV version to use matching Tekton api
- Loading branch information
Showing
9 changed files
with
201 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
...d/packages/dev-console/src/components/pipelines/utils/__tests__/pipeline-operator.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { k8sList } from '@console/internal/module/k8s'; | ||
import { ClusterServiceVersionKind } from '@console/operator-lifecycle-manager'; | ||
import { getPipelineOperatorVersion } from '../pipeline-operator'; | ||
|
||
jest.mock('@console/internal/module/k8s', () => ({ | ||
k8sList: jest.fn(), | ||
})); | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
const k8sListMock = k8sList as jest.Mock; | ||
|
||
describe('getPipelineOperatorVersion', () => { | ||
it('should fetch the ClusterServiceVersion from the api', async () => { | ||
const csvs = [ | ||
{ | ||
metadata: { name: 'openshift-pipelines-operator.v1.0.1' }, | ||
spec: { version: '1.0.1' }, | ||
status: { phase: 'Succeeded' }, | ||
} as ClusterServiceVersionKind, | ||
]; | ||
k8sListMock.mockReturnValueOnce(Promise.resolve(csvs)); | ||
|
||
const version = await getPipelineOperatorVersion('unit-test'); | ||
|
||
expect(version.raw).toBe('1.0.1'); | ||
expect(version.major).toBe(1); | ||
expect(version.minor).toBe(0); | ||
expect(version.patch).toBe(1); | ||
expect(k8sList).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should return the active ClusterServiceVersion if multiple returns', async () => { | ||
const csvs = [ | ||
{ | ||
metadata: { name: 'openshift-pipelines-operator.v1.0.1' }, | ||
spec: { version: '1.0.1' }, | ||
status: { phase: 'Succeeded' }, | ||
} as ClusterServiceVersionKind, | ||
{ | ||
metadata: { name: 'openshift-pipelines-operator.v1.1.1' }, | ||
spec: { version: '1.1.1' }, | ||
status: { phase: 'Pending' }, | ||
} as ClusterServiceVersionKind, | ||
]; | ||
k8sListMock.mockReturnValueOnce(Promise.resolve(csvs)); | ||
|
||
const version = await getPipelineOperatorVersion('unit-test'); | ||
|
||
expect(version.raw).toBe('1.0.1'); | ||
expect(version.major).toBe(1); | ||
expect(version.minor).toBe(0); | ||
expect(version.patch).toBe(1); | ||
expect(k8sList).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should fetch the latest (highest) ClusterServiceVersion from the api', async () => { | ||
const csvs = [ | ||
{ | ||
metadata: { name: 'openshift-pipelines-operator.v1.0.1' }, | ||
spec: { version: '1.0.1' }, | ||
status: { phase: 'Succeeded' }, | ||
} as ClusterServiceVersionKind, | ||
{ | ||
metadata: { name: 'openshift-pipelines-operator.v10.11.12' }, | ||
spec: { version: '10.11.12' }, | ||
status: { phase: 'Succeeded' }, | ||
} as ClusterServiceVersionKind, | ||
{ | ||
metadata: { name: 'openshift-pipelines-operator.v1.1.1' }, | ||
spec: { version: '1.1.1' }, | ||
status: { phase: 'Succeeded' }, | ||
} as ClusterServiceVersionKind, | ||
]; | ||
k8sListMock.mockReturnValueOnce(Promise.resolve(csvs)); | ||
|
||
const version = await getPipelineOperatorVersion('unit-test'); | ||
|
||
expect(version.raw).toBe('10.11.12'); | ||
expect(version.major).toBe(10); | ||
expect(version.minor).toBe(11); | ||
expect(version.patch).toBe(12); | ||
expect(k8sList).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should return null if there is no ClusterServiceVersion available', async () => { | ||
k8sListMock.mockReturnValueOnce(Promise.resolve([])); | ||
await expect(getPipelineOperatorVersion('unit-test')).resolves.toBe(null); | ||
expect(k8sList).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should return null if there is no pipeline operator at all', async () => { | ||
const csvs = [ | ||
{ | ||
metadata: { name: 'another-operator' }, | ||
spec: { version: '1.0.0' }, | ||
status: { phase: 'Succeeded' }, | ||
} as ClusterServiceVersionKind, | ||
]; | ||
k8sListMock.mockReturnValueOnce(Promise.resolve(csvs)); | ||
await expect(getPipelineOperatorVersion('unit-test')).resolves.toBe(null); | ||
expect(k8sList).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should return null if there is no matching ClusterServiceVersion available', async () => { | ||
const csvs = [ | ||
{ | ||
metadata: { name: 'another-operator' }, | ||
spec: { version: '1.0.0' }, | ||
status: { phase: 'Succeeded' }, | ||
} as ClusterServiceVersionKind, | ||
{ | ||
metadata: { name: 'openshift-pipelines-operator.v1.1.1' }, | ||
spec: { version: '1.1.1' }, | ||
status: { phase: 'Installing' }, | ||
} as ClusterServiceVersionKind, | ||
]; | ||
k8sListMock.mockReturnValueOnce(Promise.resolve(csvs)); | ||
await expect(getPipelineOperatorVersion('unit-test')).resolves.toBe(null); | ||
expect(k8sList).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
25 changes: 25 additions & 0 deletions
25
frontend/packages/dev-console/src/components/pipelines/utils/pipeline-operator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { compare, parse, SemVer } from 'semver'; | ||
import { | ||
ClusterServiceVersionKind, | ||
ClusterServiceVersionModel, | ||
ClusterServiceVersionPhase, | ||
} from '@console/operator-lifecycle-manager'; | ||
import { k8sList } from '@console/internal/module/k8s'; | ||
|
||
export const getPipelineOperatorVersion = async (namespace: string): Promise<SemVer | null> => { | ||
const allCSVs: ClusterServiceVersionKind[] = await k8sList(ClusterServiceVersionModel, { | ||
ns: namespace, | ||
}); | ||
const matchingCSVs = allCSVs.filter( | ||
(csv) => | ||
csv.metadata?.name?.startsWith('openshift-pipelines-operator') && | ||
csv.status?.phase === ClusterServiceVersionPhase.CSVPhaseSucceeded, | ||
); | ||
const versions = matchingCSVs.map((csv) => parse(csv.spec.version)).filter(Boolean); | ||
// Orders from small (oldest) to highest (newest) version | ||
versions.sort(compare); | ||
if (versions.length > 0) { | ||
return versions[versions.length - 1]; | ||
} | ||
return null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters