This repository has been archived by the owner on Jul 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #1609 - Improve Kustomize 2 parameters UI (#125)
* Issue #1609 - Improve Kustomize 2 parameters UI * Add unit tests for kustomize image parsing
- Loading branch information
Alexander Matyushentsev
authored
May 28, 2019
1 parent
71f3351
commit a49314b
Showing
10 changed files
with
1,882 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM node:9.4.0 as build | ||
FROM node:11.15.0 as build | ||
|
||
WORKDIR /src | ||
ADD ["package.json", "yarn.lock", "./"] | ||
|
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,4 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node' | ||
}; |
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
38 changes: 38 additions & 0 deletions
38
src/app/applications/components/application-parameters/kustomize-image.test.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,38 @@ | ||
import { format, parse } from './kustomize-image'; | ||
|
||
test('parse image version override', () => { | ||
const image = parse('foo/bar:v1.0.0'); | ||
|
||
expect(image.name).toBe('foo/bar'); | ||
expect(image.newTag).toBe('v1.0.0'); | ||
}); | ||
|
||
test('format image version override', () => { | ||
const formatted = format({ name: 'foo/bar', newTag: 'v1.0.0' }); | ||
expect(formatted).toBe('foo/bar:v1.0.0'); | ||
}); | ||
|
||
test('parse image name override', () => { | ||
const image = parse('foo/bar=foo/bar1:v1.0.0'); | ||
|
||
expect(image.name).toBe('foo/bar'); | ||
expect(image.newName).toBe('foo/bar1'); | ||
expect(image.newTag).toBe('v1.0.0'); | ||
}); | ||
|
||
test('format image name override', () => { | ||
const formatted = format({ name: 'foo/bar', newTag: 'v1.0.0', newName: 'foo/bar1' }); | ||
expect(formatted).toBe('foo/bar=foo/bar1:v1.0.0'); | ||
}); | ||
|
||
test('parse image digest override', () => { | ||
const image = parse('foo/bar@sha:123'); | ||
|
||
expect(image.name).toBe('foo/bar'); | ||
expect(image.digest).toBe('sha:123'); | ||
}); | ||
|
||
test('format image digest override', () => { | ||
const formatted = format({ name: 'foo/bar', digest: 'sha:123' }); | ||
expect(formatted).toBe('foo/bar@sha:123'); | ||
}); |
58 changes: 58 additions & 0 deletions
58
src/app/applications/components/application-parameters/kustomize-image.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,58 @@ | ||
const pattern = /^(.*):([a-zA-Z0-9._-]*)$/; | ||
|
||
export interface Image { | ||
name: string; | ||
newName?: string; | ||
newTag?: string; | ||
digest?: string; | ||
} | ||
|
||
function parseOverwrite(arg: string, overwriteImage: boolean): { name: string; digest?: string, tag?: string } { | ||
// match <image>@<digest> | ||
const parts = arg.split('@'); | ||
if (parts.length > 1) { | ||
return { name: parts[0], digest: parts[1]}; | ||
} | ||
|
||
// match <image>:<tag> | ||
const groups = pattern.exec(arg); | ||
if (groups && groups.length === 3) { | ||
return { name: groups[1], tag: groups[2]}; | ||
} | ||
|
||
// match <image> | ||
if (arg.length > 0 && overwriteImage) { | ||
return { name: arg }; | ||
} | ||
return { name: arg }; | ||
} | ||
|
||
export function parse(arg: string): Image { | ||
// matches if there is an image name to overwrite | ||
// <image>=<new-image><:|@><new-tag> | ||
const parts = arg.split('='); | ||
if (parts.length === 2) { | ||
const overwrite = parseOverwrite(parts[1], true); | ||
return { | ||
name: parts[0], | ||
newName: overwrite.name, | ||
newTag: overwrite.tag, | ||
digest: overwrite.digest, | ||
}; | ||
} | ||
|
||
// matches only for <tag|digest> overwrites | ||
// <image><:|@><new-tag> | ||
const p = parseOverwrite(arg, false); | ||
return {name: p.name, newTag: p.tag, digest: p.digest}; | ||
} | ||
|
||
export function format(image: Image) { | ||
const imageName = image.newName ? `${image.name}=${image.newName}` : image.name; | ||
if (image.newTag) { | ||
return `${imageName}:${image.newTag}`; | ||
} else if (image.digest) { | ||
return `${imageName}@${image.digest}`; | ||
} | ||
return imageName; | ||
} |
44 changes: 44 additions & 0 deletions
44
src/app/applications/components/application-parameters/kustomize.tsx
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,44 @@ | ||
import { Checkbox } from 'argo-ui'; | ||
import * as React from 'react'; | ||
import { FieldApi, FormField as ReactFormField } from 'react-form'; | ||
|
||
import { format, parse } from './kustomize-image'; | ||
|
||
export const ImageTagFieldEditor = ReactFormField((props: {metadata: { value: string }, fieldApi: FieldApi, className: string }) => { | ||
const { fieldApi: {getValue, setValue}} = props; | ||
const origImage = parse(props.metadata.value); | ||
const val = getValue(); | ||
const image = val ? parse(val) : { name: origImage.name }; | ||
const mustBeDigest = (image.digest || '').indexOf(':') > -1; | ||
return ( | ||
<div> | ||
<input style={{width: 'calc(50% - 1em)', marginRight: '1em'}} placeholder={origImage.name} className={props.className} value={image.newName || ''} onChange={(el) => { | ||
setValue(format({...image, newName: el.target.value})); | ||
}}/> | ||
<input style={{width: 'calc(50% - 12em)'}} className={props.className} onChange={(el) => { | ||
const forceDigest = el.target.value.indexOf(':') > -1; | ||
if (image.digest || forceDigest) { | ||
setValue(format({...image, newTag: null, digest: el.target.value})); | ||
} else { | ||
setValue(format({...image, newTag: el.target.value, digest: null})); | ||
} | ||
}} placeholder={origImage.newTag || origImage.digest} value={image.newTag || image.digest || ''}/> | ||
<div style={{width: '6em', display: 'inline-block'}}> | ||
<Checkbox checked={!!image.digest} id={`${image.name}_is-digest`} onChange={() => { | ||
const nextImg = {...image}; | ||
if (mustBeDigest) { | ||
return; | ||
} | ||
if (nextImg.digest) { | ||
nextImg.newTag = nextImg.digest; | ||
nextImg.digest = null; | ||
} else { | ||
nextImg.digest = nextImg.newTag; | ||
nextImg.newTag = null; | ||
} | ||
setValue(format(nextImg)); | ||
}}/> <label htmlFor={`${image.name}_is-digest`}> Digest?</label> | ||
</div> | ||
</div> | ||
); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,5 +17,9 @@ | |
}, | ||
"include": [ | ||
"./**/*" | ||
], | ||
"exclude": [ | ||
"node_modules", | ||
"./**/*.test.ts" | ||
] | ||
} |
Oops, something went wrong.