Skip to content

Commit 1526bea

Browse files
authored
Merge pull request #1457 from cnotv/feature/1215-value-config-consumption
Refactor consumption of policy Helm charts
2 parents 97b0097 + f9f8b32 commit 1526bea

10 files changed

Lines changed: 732 additions & 27 deletions

File tree

pkg/kubewarden/chart/kubewarden/admission/General.vue

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { LabeledInput } from '@components/Form/LabeledInput';
1212
import { RadioGroup } from '@components/Form/Radio';
1313
1414
import { KUBEWARDEN, KUBEWARDEN_APPS } from '@kubewarden/types';
15+
import { buildModuleString } from '@kubewarden/modules/policyChart';
1516
1617
export default {
1718
name: 'General',
@@ -34,7 +35,11 @@ export default {
3435
value: {
3536
type: Object,
3637
required: true
37-
}
38+
},
39+
moduleInfo: {
40+
type: Object,
41+
default: null
42+
},
3843
},
3944
4045
components: {
@@ -82,14 +87,40 @@ export default {
8287
return {
8388
policy,
8489
initialPolicyMode: null,
85-
isNamespaceNew: false
90+
isNamespaceNew: false,
91+
92+
// OCI module override fields — populated from moduleInfo when values.yaml is present
93+
policyRegistry: '',
94+
policyRepository: '',
95+
policyTag: '',
8696
};
8797
},
8898
8999
watch: {
90100
isNamespaceNew(neu) {
91101
this.value.isNamespaceNew = neu;
92-
}
102+
},
103+
104+
moduleInfo: {
105+
immediate: true,
106+
handler(info) {
107+
if (info) {
108+
this.policyRegistry = info.registry ?? '';
109+
this.policyRepository = info.repository ?? '';
110+
this.policyTag = info.tag ?? '';
111+
}
112+
}
113+
},
114+
115+
policyRegistry() {
116+
this.syncModule();
117+
},
118+
policyRepository() {
119+
this.syncModule();
120+
},
121+
policyTag() {
122+
this.syncModule();
123+
},
93124
},
94125
95126
created() {
@@ -129,6 +160,10 @@ export default {
129160
return this.chartType === KUBEWARDEN.CLUSTER_ADMISSION_POLICY;
130161
},
131162
163+
hasValuesModule() {
164+
return this.moduleInfo?.source === 'values';
165+
},
166+
132167
modeDisabled() {
133168
// Kubewarden doesn't allow switching a policy from 'protect' to 'monitor'
134169
if (!this.isCreate) {
@@ -165,6 +200,24 @@ export default {
165200
166201
return false;
167202
}
203+
},
204+
205+
methods: {
206+
syncModule() {
207+
if (!this.hasValuesModule || !this.policy?.spec) {
208+
return;
209+
}
210+
211+
const registry = this.policyRegistry?.trim() || '';
212+
const repository = this.policyRepository?.trim() || '';
213+
const tag = this.policyTag?.trim() || '';
214+
215+
if (!repository || !tag) {
216+
return;
217+
}
218+
219+
this.policy.spec.module = buildModuleString(registry, repository, tag);
220+
}
168221
}
169222
};
170223
</script>
@@ -213,6 +266,33 @@ export default {
213266
:required="true"
214267
/>
215268
</div>
269+
<template v-if="hasValuesModule">
270+
<div class="col span-4">
271+
<LabeledInput
272+
v-model:value="policyRegistry"
273+
data-testid="kw-policy-general-registry-input"
274+
:mode="mode"
275+
:label="t('kubewarden.policies.module.registry')"
276+
:placeholder="t('kubewarden.policies.module.registryPlaceholder')"
277+
/>
278+
</div>
279+
<div class="col span-5">
280+
<LabeledInput
281+
v-model:value="policyRepository"
282+
data-testid="kw-policy-general-repository-input"
283+
:mode="mode"
284+
:label="t('kubewarden.policies.module.repository')"
285+
/>
286+
</div>
287+
<div class="col span-3">
288+
<LabeledInput
289+
v-model:value="policyTag"
290+
data-testid="kw-policy-general-tag-input"
291+
:mode="mode"
292+
:label="t('kubewarden.policies.module.tag')"
293+
/>
294+
</div>
295+
</template>
216296
</div>
217297
<div class="row mb-20">
218298
<div class="col span-6">

pkg/kubewarden/chart/kubewarden/admission/__tests__/General.spec.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ describe('component: General', () => {
4141
policyServers: () => ps,
4242
policyServerOptions: () => ['default', 'custom-ps'],
4343
isGlobal: () => true,
44+
hasValuesModule: () => false,
4445
showModeBanner: () => false,
4546
modeDisabled: () => false
4647
}
@@ -79,6 +80,7 @@ describe('component: General', () => {
7980
policyServerOptions: () => ['default', 'custom-ps'],
8081
isGlobal: () => true,
8182
isCreate: () => true,
83+
hasValuesModule: () => false,
8284
showModeBanner: () => false,
8385
modeDisabled: () => false
8486
}
@@ -89,4 +91,65 @@ describe('component: General', () => {
8991

9092
expect(radio?.props().value).toStrictEqual('protect' as string);
9193
});
94+
95+
it('should sync values-based OCI fields back into spec.module', async() => {
96+
const wrapper = shallowMount(General, {
97+
props: {
98+
targetNamespace: 'default',
99+
value: {
100+
policy: {
101+
...userGroupPolicy,
102+
spec: {
103+
...userGroupPolicy.spec,
104+
module: 'ghcr.io/kubewarden/old-policy:v1'
105+
}
106+
}
107+
},
108+
moduleInfo: {
109+
registry: 'ghcr.io',
110+
repository: 'kubewarden/pod-privileged',
111+
tag: 'v1.0.0',
112+
source: 'values'
113+
}
114+
},
115+
global: {
116+
provide: { chartType: KUBEWARDEN.CLUSTER_ADMISSION_POLICY },
117+
mocks: {
118+
$fetchState: { pending: false },
119+
$store: {
120+
getters: {
121+
currentStore: () => 'current_store',
122+
'current_store/all': jest.fn(),
123+
'i18n/t': jest.fn()
124+
},
125+
}
126+
},
127+
stubs: {
128+
NameNsDescription: { template: '<span />' },
129+
RadioGroup: { template: '<span />' },
130+
LabeledTooltip: { template: '<span />' },
131+
LabeledInput: { template: '<span />' }
132+
}
133+
},
134+
computed: {
135+
isCreate: () => true,
136+
policyServers: () => [],
137+
policyServerOptions: () => [],
138+
isGlobal: () => true,
139+
hasValuesModule: () => true,
140+
showModeBanner: () => false,
141+
modeDisabled: () => false
142+
}
143+
});
144+
145+
await wrapper.setData({
146+
policyRegistry: 'registry.internal:5000',
147+
policyRepository: 'kubewarden/pod-privileged',
148+
policyTag: 'v2.0.0'
149+
});
150+
151+
wrapper.vm.syncModule();
152+
153+
expect(wrapper.vm.policy.spec.module).toBe('registry.internal:5000/kubewarden/pod-privileged:v2.0.0');
154+
});
92155
});

pkg/kubewarden/chart/kubewarden/admission/index.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ export default {
3636
type: Object,
3737
required: true
3838
},
39+
moduleInfo: {
40+
type: Object,
41+
default: null
42+
},
3943
},
4044
4145
components: {
@@ -147,6 +151,7 @@ export default {
147151
:mode="mode"
148152
:target-namespace="targetNamespace"
149153
:is-custom="isCustom"
154+
:module-info="moduleInfo"
150155
/>
151156
</Tab>
152157

0 commit comments

Comments
 (0)