Skip to content

Commit

Permalink
fix: setting default value for enforcement action (#267)
Browse files Browse the repository at this point in the history
* fix: setting default value for enforcement action

Signed-off-by: Jaydip Gabani <[email protected]>

* fixing tests for create schema

Signed-off-by: Jaydip Gabani <[email protected]>

* fixing unit tests

Signed-off-by: Jaydip Gabani <[email protected]>

---------

Signed-off-by: Jaydip Gabani <[email protected]>
  • Loading branch information
JaydipGabani authored Feb 14, 2024
1 parent 37bcf5f commit 930072e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
4 changes: 4 additions & 0 deletions constraint/pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,7 @@ func TestClient_AddConstraint_withDefaultParams(t *testing.T) {
"parameters": map[string]interface{}{
"foo": defaults,
},
"enforcementAction": "deny",
},
},
{
Expand Down Expand Up @@ -1136,6 +1137,7 @@ func TestClient_AddConstraint_withDefaultParams(t *testing.T) {
"foo": defaults,
"bar": defaults2,
},
"enforcementAction": "deny",
},
},
{
Expand Down Expand Up @@ -1180,6 +1182,7 @@ func TestClient_AddConstraint_withDefaultParams(t *testing.T) {
"bar": defaults2,
"fuzz": "buzz",
},
"enforcementAction": "deny",
},
},
{
Expand All @@ -1204,6 +1207,7 @@ func TestClient_AddConstraint_withDefaultParams(t *testing.T) {
"parameters": map[string]interface{}{
"foo": defaults,
},
"enforcementAction": "deny",
},
},
}
Expand Down
5 changes: 5 additions & 0 deletions constraint/pkg/client/clienttest/cts/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ func MakeConstraint(t testing.TB, kind, name string, args ...ConstraintArg) *uns
t.Fatal(err)
}

err = unstructured.SetNestedField(u.Object, "deny", "spec", "enforcementAction")
if err != nil {
t.Fatal(err)
}

for _, arg := range args {
err = arg(u)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion constraint/pkg/client/clienttest/cts/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
type PropMap map[string]apiextensions.JSONSchemaProps

func ExpectedSchema(pm PropMap) *apiextensions.JSONSchemaProps {
pm["enforcementAction"] = apiextensions.JSONSchemaProps{Type: "string"}
defaultEnforcementAction := apiextensions.JSON("deny")
pm["enforcementAction"] = apiextensions.JSONSchemaProps{Type: "string", Default: &defaultEnforcementAction}
p := Prop(
PropMap{
"metadata": Prop(PropMap{
Expand Down
3 changes: 2 additions & 1 deletion constraint/pkg/client/crds/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
// CreateSchema combines the schema of the match target and the ConstraintTemplate parameters
// to form the schema of the actual constraint resource.
func CreateSchema(templ *templates.ConstraintTemplate, target MatchSchemaProvider) *apiextensions.JSONSchemaProps {
defaultEnforcementAction := apiextensions.JSON("deny")
props := map[string]apiextensions.JSONSchemaProps{
"match": target.MatchSchema(),
"enforcementAction": {Type: "string"},
"enforcementAction": {Type: "string", Default: &defaultEnforcementAction},
}

if templ.Spec.CRD.Spec.Validation != nil && templ.Spec.CRD.Spec.Validation.OpenAPIV3Schema != nil {
Expand Down

0 comments on commit 930072e

Please sign in to comment.