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

fix CT name validation #27

Merged
merged 1 commit into from
Jul 2, 2019
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
3 changes: 3 additions & 0 deletions constraint/pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ func (c *client) CreateCRD(ctx context.Context, templ *v1alpha1.ConstraintTempla
if templ.ObjectMeta.Name != templ.Spec.CRD.Spec.Names.Plural {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am split on requiring the plural name == the lower-cased kind (which should equal the singular name).

On the one hand it's an odd requirement. On the other hand it plays well with our convention of appending a version to the name.

One way versioning might change is by giving users control over the group, allowing them to store the version there.

My impulse is to keep what's here b/c tighter requirements can always be loosened. Thoughts?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep what's here b/c tighter requirements can always be loosened

Agreed.

return nil, fmt.Errorf("Template's name %s is not equal to the CRD's plural name: %s", templ.ObjectMeta.Name, templ.Spec.CRD.Spec.Names.Plural)
}
if templ.ObjectMeta.Name != strings.ToLower(templ.Spec.CRD.Spec.Names.Kind) {
return nil, fmt.Errorf("Template's name %s is not equal to the lowercase of CRD's Kind: %s", templ.ObjectMeta.Name, strings.ToLower(templ.Spec.CRD.Spec.Names.Kind))
}

var src string
var target TargetHandler
Expand Down
36 changes: 18 additions & 18 deletions constraint/pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func TestAddTemplate(t *testing.T) {
{
Name: "Good Template",
Handler: &badHandler{Name: "h1", HasLib: true},
Template: createTemplate(name("fakes"), crdNames("Fake", "fakes"), targets("h1")),
Template: createTemplate(name("fakes"), crdNames("Fakes", "fakes"), targets("h1")),
ErrorExpected: false,
},
{
Expand Down Expand Up @@ -365,7 +365,7 @@ func TestAddTemplate(t *testing.T) {
}

func TestRemoveTemplate(t *testing.T) {
badRegoTempl := createTemplate(name("fake"), crdNames("Fake", "fakes"), targets("h1"))
badRegoTempl := createTemplate(name("fake"), crdNames("Fakes", "fakes"), targets("h1"))
badRegoTempl.Spec.Targets[0].Rego = "asd{"
tc := []struct {
Name string
Expand All @@ -376,13 +376,13 @@ func TestRemoveTemplate(t *testing.T) {
{
Name: "Good Template",
Handler: &badHandler{Name: "h1", HasLib: true},
Template: createTemplate(name("fake"), crdNames("Fake", "fakes"), targets("h1")),
Template: createTemplate(name("fake"), crdNames("Fakes", "fakes"), targets("h1")),
ErrorExpected: false,
},
{
Name: "Unknown Target",
Handler: &badHandler{Name: "h1", HasLib: true},
Template: createTemplate(name("fake"), crdNames("Fake", "fakes"), targets("h2")),
Template: createTemplate(name("fake"), crdNames("Fakes", "fakes"), targets("h2")),
ErrorExpected: true,
},
{
Expand Down Expand Up @@ -435,11 +435,11 @@ func TestAddConstraint(t *testing.T) {
}{
{
Name: "Good Constraint",
Constraint: newConstraint("Foo", "foo", nil),
Constraint: newConstraint("Foos", "foo", nil),
},
{
Name: "No Name",
Constraint: newConstraint("Foo", "", nil),
Constraint: newConstraint("Foos", "", nil),
ErrorExpected: true,
},
{
Expand Down Expand Up @@ -467,7 +467,7 @@ func TestAddConstraint(t *testing.T) {
t.Fatal(err)
}
if !tt.OmitTemplate {
tmpl := createTemplate(name("foos"), crdNames("Foo", "foos"), targets("h1"))
tmpl := createTemplate(name("foos"), crdNames("Foos", "foos"), targets("h1"))
_, err := c.AddTemplate(context.Background(), tmpl)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -498,19 +498,19 @@ func TestAddConstraint(t *testing.T) {

func TestRemoveConstraint(t *testing.T) {
tc := []struct {
Name string
Constraint *unstructured.Unstructured
OmitTemplate bool
ErrorExpected bool
Name string
Constraint *unstructured.Unstructured
OmitTemplate bool
ErrorExpected bool
ExpectedErrorType string
}{
{
Name: "Good Constraint",
Constraint: newConstraint("Foo", "foo", nil),
Constraint: newConstraint("Foos", "foo", nil),
},
{
Name: "No Name",
Constraint: newConstraint("Foo", "", nil),
Constraint: newConstraint("Foos", "", nil),
ErrorExpected: true,
},
{
Expand All @@ -525,10 +525,10 @@ func TestRemoveConstraint(t *testing.T) {
ErrorExpected: true,
},
{
Name: "Unrecognized Constraint",
Constraint: newConstraint("Bar", "bar", nil),
OmitTemplate: true,
ErrorExpected: true,
Name: "Unrecognized Constraint",
Constraint: newConstraint("Bar", "bar", nil),
OmitTemplate: true,
ErrorExpected: true,
ExpectedErrorType: "*client.UnrecognizedConstraintError",
},
}
Expand All @@ -545,7 +545,7 @@ func TestRemoveConstraint(t *testing.T) {
t.Fatal(err)
}
if !tt.OmitTemplate {
tmpl := createTemplate(name("foos"), crdNames("Foo", "foos"), targets("h1"))
tmpl := createTemplate(name("foos"), crdNames("Foos", "foos"), targets("h1"))
_, err := c.AddTemplate(context.Background(), tmpl)
if err != nil {
t.Fatal(err)
Expand Down
4 changes: 2 additions & 2 deletions constraint/pkg/client/e2e_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ var ctx = context.Background()

func newConstraintTemplate(name, rego string) *v1alpha1.ConstraintTemplate {
return &v1alpha1.ConstraintTemplate{
ObjectMeta: metav1.ObjectMeta{Name: strings.ToLower(name) + "s"},
ObjectMeta: metav1.ObjectMeta{Name: strings.ToLower(name)},
Spec: v1alpha1.ConstraintTemplateSpec{
CRD: v1alpha1.CRD{
Spec: v1alpha1.CRDSpec{
Names: apiextensionsv1beta1.CustomResourceDefinitionNames{
Kind: name,
Plural: strings.ToLower(name) + "s",
Plural: strings.ToLower(name),
},
Validation: &v1alpha1.Validation{
OpenAPIV3Schema: &apiextensionsv1beta1.JSONSchemaProps{
Expand Down