Skip to content

Commit 5368a3b

Browse files
authored
fix: anyObject was not fully plumbed to compiler, streamline plumbing to fix (#437)
Signed-off-by: Max Smythe <[email protected]>
1 parent 2843c77 commit 5368a3b

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

constraint/pkg/client/drivers/k8scel/transform/cel_snippets.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,11 @@ func BindParamsV1Beta1() admissionregistrationv1beta1.Variable {
159159
}
160160
}
161161

162-
func BindParamsCEL() []cel.NamedExpressionAccessor {
162+
func BindParamsCEL() cel.NamedExpressionAccessor {
163163
v := BindParamsV1Beta1()
164-
return []cel.NamedExpressionAccessor{
165-
&validatingadmissionpolicy.Variable{
166-
Name: v.Name,
167-
Expression: v.Expression,
168-
},
164+
return &validatingadmissionpolicy.Variable{
165+
Name: v.Name,
166+
Expression: v.Expression,
169167
}
170168
}
171169

@@ -176,13 +174,11 @@ func BindObjectV1Beta1() admissionregistrationv1beta1.Variable {
176174
}
177175
}
178176

179-
func BindObjectCEL() []cel.NamedExpressionAccessor {
177+
func BindObjectCEL() cel.NamedExpressionAccessor {
180178
v := BindObjectV1Beta1()
181-
return []cel.NamedExpressionAccessor{
182-
&validatingadmissionpolicy.Variable{
183-
Name: v.Name,
184-
Expression: v.Expression,
185-
},
179+
return &validatingadmissionpolicy.Variable{
180+
Name: v.Name,
181+
Expression: v.Expression,
186182
}
187183
}
188184

@@ -196,7 +192,15 @@ func AllMatchersV1Beta1() []admissionregistrationv1beta1.MatchCondition {
196192
}
197193

198194
func AllVariablesCEL() []cel.NamedExpressionAccessor {
199-
return BindParamsCEL()
195+
vars := AllVariablesV1Beta1()
196+
xform := make([]cel.NamedExpressionAccessor, len(vars))
197+
for i := range vars {
198+
xform[i] = &validatingadmissionpolicy.Variable{
199+
Name: vars[i].Name,
200+
Expression: vars[i].Expression,
201+
}
202+
}
203+
return xform
200204
}
201205

202206
func AllVariablesV1Beta1() []admissionregistrationv1beta1.Variable {

constraint/pkg/client/drivers/k8scel/transform/cel_snippets_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ func TestMatchKinds(t *testing.T) {
258258
t.Fatal(err)
259259
}
260260
celOpts := cel.OptionalVariableDeclarations{HasParams: true}
261-
filterCompiler.CompileAndStoreVariables(BindParamsCEL(), celOpts, environment.StoredExpressions)
261+
filterCompiler.CompileAndStoreVariables(AllVariablesCEL(), celOpts, environment.StoredExpressions)
262262
matcher := matchconditions.NewMatcher(filterCompiler.Compile(MatchKindsCEL(), celOpts, environment.StoredExpressions), ptr.To[v1.FailurePolicyType](v1.Fail), "matchTest", "kind", test.name)
263263

264264
obj := &unstructured.Unstructured{}
@@ -463,7 +463,7 @@ func TestMatchNameGlob(t *testing.T) {
463463
t.Fatal(err)
464464
}
465465
celOpts := cel.OptionalVariableDeclarations{HasParams: true}
466-
filterCompiler.CompileAndStoreVariables(BindParamsCEL(), celOpts, environment.StoredExpressions)
466+
filterCompiler.CompileAndStoreVariables(AllVariablesCEL(), celOpts, environment.StoredExpressions)
467467
matcher := matchconditions.NewMatcher(filterCompiler.Compile(MatchNameGlobCEL(), celOpts, environment.StoredExpressions), ptr.To[v1.FailurePolicyType](v1.Fail), "matchTest", "name", test.name)
468468

469469
constraint := &unstructured.Unstructured{Object: map[string]interface{}{}}
@@ -633,7 +633,7 @@ func TestMatchNamespacesGlob(t *testing.T) {
633633
t.Fatal(err)
634634
}
635635
celOpts := cel.OptionalVariableDeclarations{HasParams: true}
636-
filterCompiler.CompileAndStoreVariables(BindParamsCEL(), celOpts, environment.StoredExpressions)
636+
filterCompiler.CompileAndStoreVariables(AllVariablesCEL(), celOpts, environment.StoredExpressions)
637637
matcher := matchconditions.NewMatcher(filterCompiler.Compile(MatchNamespacesGlobCEL(), celOpts, environment.StoredExpressions), ptr.To[v1.FailurePolicyType](v1.Fail), "matchTest", "name", test.name)
638638

639639
versionedAttributes, err := RequestToVersionedAttributes(subTest.request)
@@ -807,7 +807,7 @@ func TestMatchExcludedNamespacesGlob(t *testing.T) {
807807
t.Fatal(err)
808808
}
809809
celOpts := cel.OptionalVariableDeclarations{HasParams: true}
810-
filterCompiler.CompileAndStoreVariables(BindParamsCEL(), celOpts, environment.StoredExpressions)
810+
filterCompiler.CompileAndStoreVariables(AllVariablesCEL(), celOpts, environment.StoredExpressions)
811811
matcher := matchconditions.NewMatcher(filterCompiler.Compile(MatchExcludedNamespacesGlobCEL(), celOpts, environment.StoredExpressions), ptr.To[v1.FailurePolicyType](v1.Fail), "matchTest", "name", test.name)
812812

813813
versionedAttributes, err := RequestToVersionedAttributes(subTest.request)
@@ -871,7 +871,7 @@ func TestParamsBinding(t *testing.T) {
871871
t.Fatal(err)
872872
}
873873
celOpts := cel.OptionalVariableDeclarations{HasParams: true}
874-
filterCompiler.CompileAndStoreVariables(BindParamsCEL(), celOpts, environment.StoredExpressions)
874+
filterCompiler.CompileAndStoreVariables(AllVariablesCEL(), celOpts, environment.StoredExpressions)
875875
matcher := matchconditions.NewMatcher(
876876
filterCompiler.Compile(
877877
[]cel.ExpressionAccessor{
@@ -952,7 +952,7 @@ func TestObjectBinding(t *testing.T) {
952952
t.Fatal(err)
953953
}
954954
celOpts := cel.OptionalVariableDeclarations{HasParams: true}
955-
filterCompiler.CompileAndStoreVariables(BindObjectCEL(), celOpts, environment.StoredExpressions)
955+
filterCompiler.CompileAndStoreVariables(AllVariablesCEL(), celOpts, environment.StoredExpressions)
956956
matcher := matchconditions.NewMatcher(
957957
filterCompiler.Compile(
958958
[]cel.ExpressionAccessor{

0 commit comments

Comments
 (0)