forked from rancher/dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rancher#10318 from ly5156/secret-resource-type
fix(steve/actions): fix missing type attribute of secret resource
- Loading branch information
Showing
8 changed files
with
263 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import Secret from '@shell/models/secret'; | ||
|
||
describe('class Secret', () => { | ||
it('should contains the type attribute if cleanForDownload', async() => { | ||
const secret = new Secret({}); | ||
const yaml = `apiVersion: v1 | ||
kind: Secret | ||
metadata: | ||
name: my-secret | ||
type: Opaque | ||
`; | ||
const cleanYaml = await secret.cleanForDownload(yaml); | ||
|
||
expect(cleanYaml).toBe(yaml); | ||
}); | ||
|
||
it('should remove id, links and actions keys if cleanForDownload', async() => { | ||
const secret = new Secret({}); | ||
const expectedYamlStr = `apiVersion: v1 | ||
kind: Secret | ||
metadata: | ||
name: my-secret | ||
namespace: default | ||
type: Opaque | ||
`; | ||
const part = `id: test_id | ||
links: | ||
view: https://example.com | ||
actions: | ||
remove: https://example.com`; | ||
const yaml = `${ expectedYamlStr } | ||
${ part }`; | ||
const cleanYaml = await secret.cleanForDownload(yaml); | ||
|
||
expect(cleanYaml).toBe(expectedYamlStr); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import { steveCleanForDownload } from '@shell/plugins/steve/resource-utils'; | ||
|
||
describe('steve: ressource-utils', () => { | ||
it('should do nothing if the yaml is not passed', () => { | ||
const r = steveCleanForDownload(); | ||
|
||
expect(r).toBeUndefined(); | ||
}); | ||
it('should remove all default rootKeys', () => { | ||
const expectedYamlStr = `apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: my-configmap | ||
`; | ||
const yamlStr = ` | ||
id: test_id | ||
links: | ||
view: https://example.com2 | ||
type: test_type | ||
actions: | ||
remove: https://example.com | ||
${ expectedYamlStr } | ||
`; | ||
const cleanedYamlStr = steveCleanForDownload(yamlStr); | ||
|
||
expect(cleanedYamlStr).toBe(expectedYamlStr); | ||
}); | ||
it('should remove all the specified root keys', () => { | ||
const part = `apiVersion: v1 | ||
kind: Secret | ||
metadata: | ||
name: my-secret`; | ||
|
||
const rootKeyToYamlStringMap = { | ||
id: 'id: test_id', | ||
links: `links: | ||
view: https://example.com`, | ||
actions: `actions: | ||
remove: https://example.com`, | ||
type: 'type: Opaque' | ||
}; | ||
|
||
const entries = Object.entries(rootKeyToYamlStringMap); | ||
const yamlStr = `${ part } | ||
${ entries.map(([_, str]) => str).join('\n') }`; | ||
|
||
entries.forEach(([key, str]) => { | ||
const expectedYamlStr = `${ part } | ||
${ entries.filter(([k]) => k !== key).map(([_, str]) => str).join('\n') }\n`; | ||
const cleanedYamlStr = steveCleanForDownload(yamlStr, { rootKeys: [key] }); | ||
|
||
expect(cleanedYamlStr).toBe(expectedYamlStr); | ||
}); | ||
}); | ||
it('should remove all default metadata keys', () => { | ||
const expectedYamlStr = `apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: my-configmap | ||
`; | ||
const yamlStr = `apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: my-configmap | ||
fields: | ||
- kube-root-ca.crt | ||
- 1 | ||
- 7d23h | ||
relationships: | ||
- rel: 'owner' | ||
state: 'active' | ||
`; | ||
const cleanedYamlStr = steveCleanForDownload(yamlStr); | ||
|
||
expect(cleanedYamlStr).toBe(expectedYamlStr); | ||
}); | ||
|
||
it('should remove all the specified metadata keys', () => { | ||
const part = `apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: my-configmap`; | ||
|
||
const metadataKeyToYamlStringMap = { | ||
fields: | ||
` fields: | ||
- kube-root-ca.crt | ||
- 1 | ||
- 7d23h`, | ||
relationships: | ||
` relationships: | ||
- rel: owner`, | ||
state: ` state: active` | ||
}; | ||
|
||
const entries = Object.entries(metadataKeyToYamlStringMap); | ||
const yamlStr = `${ part } | ||
${ entries.map(([_, str]) => str).join('\n') }`; | ||
|
||
entries.forEach(([key, str]) => { | ||
const expectedYamlStr = `${ part } | ||
${ entries.filter(([k]) => k !== key).map(([_, str]) => str).join('\n') }\n`; | ||
const cleanedYamlStr = steveCleanForDownload(yamlStr, { metadataKeys: [key] }); | ||
|
||
expect(cleanedYamlStr).toBe(expectedYamlStr); | ||
}); | ||
}); | ||
it('should remove all defalut condition keys', () => { | ||
const expectedYamlStr = `apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: my-configmap | ||
status: | ||
conditions: | ||
- {} | ||
- {} | ||
- message: message | ||
`; | ||
const yamlStr = `apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: my-configmap | ||
status: | ||
conditions: | ||
- error: 'error' | ||
- transitioning: false | ||
- message: message | ||
`; | ||
const cleanedYamlStr = steveCleanForDownload(yamlStr); | ||
|
||
expect(cleanedYamlStr).toBe(expectedYamlStr); | ||
}); | ||
it('should remove all the specified condition keys', () => { | ||
const part = `apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: my-configmap | ||
status: | ||
conditions: | ||
- message: message`; | ||
|
||
const conditionKeyToYamlStringMap = { | ||
error: ' - error: error', | ||
transitioning: ' - transitioning: false' | ||
}; | ||
|
||
const entries = Object.entries(conditionKeyToYamlStringMap); | ||
const yamlStr = `${ part } | ||
${ entries.map(([_, str]) => str).join('\n') }`; | ||
|
||
entries.forEach(([key, str]) => { | ||
const expectedYamlStr = `${ part } | ||
${ entries.map(([k, str]) => k === key ? ' - {}' : str).join('\n') }\n`; | ||
const cleanedYamlStr = steveCleanForDownload(yamlStr, { conditionKeys: [key] }); | ||
|
||
expect(cleanedYamlStr).toBe(expectedYamlStr); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { dropKeys } from '@shell/utils/object'; | ||
import jsyaml from 'js-yaml'; | ||
|
||
export function steveCleanForDownload(yaml: string, keys?: { | ||
rootKeys?: string[], | ||
metadataKeys?: string[], | ||
conditionKeys?: string[] | ||
}): string | undefined { | ||
if (!yaml) { | ||
return; | ||
} | ||
|
||
const { | ||
rootKeys = [ | ||
'id', | ||
'links', | ||
'type', | ||
'actions' | ||
], | ||
metadataKeys = [ | ||
'fields', | ||
'relationships', | ||
'state', | ||
], | ||
conditionKeys = [ | ||
'error', | ||
'transitioning', | ||
] | ||
} = keys || {}; | ||
|
||
const obj: any = jsyaml.load(yaml); | ||
|
||
dropKeys(obj, rootKeys); | ||
dropKeys(obj?.metadata, metadataKeys); | ||
(obj?.status?.conditions || []).forEach((condition: any) => dropKeys(condition, conditionKeys)); | ||
|
||
return jsyaml.dump(obj); | ||
} |
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