Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add additional columns to constraints #108

Merged
merged 4 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions constraint/pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,7 @@ func (c *Client) createBasicTemplateArtifacts(templ *templates.ConstraintTemplat
return nil, fmt.Errorf("failed to validate targets for template %s: %w", templ.Name, err)
}

sch, err := c.backend.crd.createSchema(templ, targetHandler)
if err != nil {
return nil, err
}
sch := c.backend.crd.createSchema(templ, targetHandler)

crd, err := c.backend.crd.createCRD(templ, sch)
if err != nil {
Expand Down
12 changes: 11 additions & 1 deletion constraint/pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,15 @@ violation[msg] {msg := "always"}`,
}, {
Name: "v1alpha1", Served: true,
}},
AdditionalPrinterColumns: []apiextensions.CustomResourceColumnDefinition{{
Name: "enforcement-action",
Type: "string",
JSONPath: ".spec.enforcementAction",
}, {
Name: "total-violations",
Type: "integer",
JSONPath: ".status.totalViolations",
}},
Conversion: &apiextensions.CustomResourceConversion{
Strategy: apiextensions.NoneConverter,
},
Expand Down Expand Up @@ -1381,7 +1390,8 @@ violation[msg] {msg := "always"}`,
}

if diff := cmp.Diff(tc.want, got,
cmpopts.IgnoreFields(apiextensions.CustomResourceDefinitionSpec{}, "Validation")); diff != "" {
cmpopts.IgnoreFields(apiextensions.CustomResourceDefinitionSpec{}, "Validation"),
cmpopts.IgnoreFields(apiextensions.CustomResourceColumnDefinition{}, "Description")); diff != "" {
t.Error(diff)
}
})
Expand Down
19 changes: 16 additions & 3 deletions constraint/pkg/client/crd_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func validateTargets(templ *templates.ConstraintTemplate) error {

// createSchema combines the schema of the match target and the ConstraintTemplate parameters
// to form the schema of the actual constraint resource.
func (h *crdHelper) createSchema(templ *templates.ConstraintTemplate, target MatchSchemaProvider) (*apiextensions.JSONSchemaProps, error) {
func (h *crdHelper) createSchema(templ *templates.ConstraintTemplate, target MatchSchemaProvider) *apiextensions.JSONSchemaProps {
props := map[string]apiextensions.JSONSchemaProps{
"match": target.MatchSchema(),
"enforcementAction": {Type: "string"},
Expand Down Expand Up @@ -77,8 +77,7 @@ func (h *crdHelper) createSchema(templ *templates.ConstraintTemplate, target Mat
},
},
}

return schema, nil
return schema
}

// crdHelper builds the scheme for handling CRDs. It is necessary to build crdHelper at runtime as
Expand Down Expand Up @@ -135,6 +134,20 @@ func (h *crdHelper) createCRD(
Served: true,
},
},
AdditionalPrinterColumns: []apiextensions.CustomResourceColumnDefinition{
{
Name: "enforcement-action",
Description: "Type of enforcement action",
JSONPath: ".spec.enforcementAction",
Type: "string",
},
{
Name: "total-violations",
Description: "Total number of violations",
JSONPath: ".status.totalViolations",
Type: "integer",
},
},
},
}

Expand Down
15 changes: 3 additions & 12 deletions constraint/pkg/client/crd_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,7 @@ func TestCreateSchema(t *testing.T) {
t.Fatalf("Could not create CRD helper: %v", err)
}
t.Run(tc.Name, func(t *testing.T) {
schema, err := h.createSchema(tc.Template, tc.Handler)
if err != nil {
t.Errorf("error = %v; want nil", err)
}
schema := h.createSchema(tc.Template, tc.Handler)
if !reflect.DeepEqual(schema, tc.ExpectedSchema) {
t.Errorf("Unexpected schema output. Diff: %v", cmp.Diff(*schema, tc.ExpectedSchema))
}
Expand Down Expand Up @@ -367,10 +364,7 @@ func TestCRDCreationAndValidation(t *testing.T) {
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
schema, err := h.createSchema(tc.Template, tc.Handler)
if err != nil {
t.Errorf("err = %v; want nil", err)
}
schema := h.createSchema(tc.Template, tc.Handler)
crd, err := h.createCRD(tc.Template, schema)
if err != nil {
t.Errorf("err = %v; want nil", err)
Expand Down Expand Up @@ -527,10 +521,7 @@ func TestCRValidation(t *testing.T) {
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
schema, err := h.createSchema(tc.Template, tc.Handler)
if err != nil {
t.Errorf("err = %v; want nil", err)
}
schema := h.createSchema(tc.Template, tc.Handler)
crd, err := h.createCRD(tc.Template, schema)
if err != nil {
t.Errorf("err = %v; want nil", err)
Expand Down