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 namespace proto and registration #18848

Merged
merged 13 commits into from
Sep 20, 2023
2 changes: 2 additions & 0 deletions agent/consul/type_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/consul/internal/mesh"
"github.com/hashicorp/consul/internal/resource"
"github.com/hashicorp/consul/internal/resource/demo"
"github.com/hashicorp/consul/internal/tenancy"
)

// NewTypeRegistry returns a registry populated with all supported resource
Expand All @@ -25,6 +26,7 @@ func NewTypeRegistry() resource.Registry {
mesh.RegisterTypes(registry)
catalog.RegisterTypes(registry)
auth.RegisterTypes(registry)
tenancy.RegisterTypes(registry)

return registry
}
28 changes: 28 additions & 0 deletions internal/tenancy/exports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package tenancy

import (
"github.com/hashicorp/consul/internal/resource"
"github.com/hashicorp/consul/internal/tenancy/internal/types"
)

var (
// API Group Information

APIGroup = types.GroupName
VersionV1Alpha1 = types.VersionV1Alpha1
CurrentVersion = types.CurrentVersion

// Resource Kind Names.

NamespaceKind = types.NamespaceKind
NamespaceV1Alpha1Type = types.NamespaceV1Alpha1Type
)

// RegisterTypes adds all resource types within the "catalog" API group
dhiaayachi marked this conversation as resolved.
Show resolved Hide resolved
// to the given type registry
func RegisterTypes(r resource.Registry) {
types.Register(r)
}
11 changes: 11 additions & 0 deletions internal/tenancy/internal/types/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package types

import "errors"

var (
errInvalidName = errors.New("invalid namespace name provided")
errOwnerNonEmpty = errors.New("namespace should not have an owner")
)
61 changes: 61 additions & 0 deletions internal/tenancy/internal/types/namespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package types

import (
"github.com/go-openapi/strfmt"
"github.com/hashicorp/consul/internal/resource"
"github.com/hashicorp/consul/proto-public/pbresource"
tenancyv1alpha1 "github.com/hashicorp/consul/proto-public/pbtenancy/v1alpha1"
analogue marked this conversation as resolved.
Show resolved Hide resolved
"strings"
)

const (
NamespaceKind = "Namespace"
)

var (
NamespaceV1Alpha1Type = &pbresource.Type{
Group: GroupName,
GroupVersion: VersionV1Alpha1,
Kind: NamespaceKind,
}

TrafficPermissionsType = NamespaceV1Alpha1Type
dhiaayachi marked this conversation as resolved.
Show resolved Hide resolved
)

func RegisterNamespace(r resource.Registry) {
r.Register(resource.Registration{
Type: NamespaceV1Alpha1Type,
Proto: &tenancyv1alpha1.Namespace{},
Scope: resource.ScopePartition,
Validate: ValidateNamespace,
Mutate: MutateNamespace,
})
}

func MutateNamespace(res *pbresource.Resource) error {
res.Id.Name = strings.ToLower(res.Id.Name)
return nil
}

func ValidateNamespace(res *pbresource.Resource) error {
var ns tenancyv1alpha1.Namespace

if err := res.Data.UnmarshalTo(&ns); err != nil {
return resource.NewErrDataParse(&ns, err)
}
if res.Owner != nil {
return errInvalidName
Copy link
Collaborator

Choose a reason for hiding this comment

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

I imagine we'll make this errors more detailed in follow on tickets assuming the V1 implementation is also as detailed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I added an appropriate error message

}

if res.Id.Name == resource.DefaultNamespacedTenancy().Namespace {
dhiaayachi marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should only fail if the partition is also default.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That actually bring a question 🤔
how we will create the default/default on cluster bootstrapping?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I imagine it should be part of the system startup, but I'm not familiar enough with snapshot restoration after startup and if that will cause issues if an existing resource named "default" is already in the state store under a different UID.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I made the change, we will revisit later when we get to creating the defaults.

return errInvalidName
}

if ok := strfmt.IsHostname(res.Id.Name); !ok {
return errInvalidName
}
return nil
}
98 changes: 98 additions & 0 deletions internal/tenancy/internal/types/namespace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package types

import (
"github.com/hashicorp/consul/internal/resource"
pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v1alpha1"
"github.com/hashicorp/consul/proto-public/pbresource"
tenancyv1alpha1 "github.com/hashicorp/consul/proto-public/pbtenancy/v1alpha1"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/known/anypb"
"testing"
)

func createNamespaceResource(t *testing.T, data protoreflect.ProtoMessage) *pbresource.Resource {
res := &pbresource.Resource{
Id: &pbresource.ID{
Type: NamespaceV1Alpha1Type,
Tenancy: &pbresource.Tenancy{
Partition: "default",
Namespace: "",
PeerName: "local",
},
dhiaayachi marked this conversation as resolved.
Show resolved Hide resolved
Name: "ns1234",
},
}

var err error
res.Data, err = anypb.New(data)
require.NoError(t, err)
return res
}

func validNamespace() *tenancyv1alpha1.Namespace {
return &tenancyv1alpha1.Namespace{
Description: "description from user",
}
}

func TestValidateNamespace_Ok(t *testing.T) {
res := createNamespaceResource(t, validNamespace())

err := ValidateNamespace(res)
require.NoError(t, err)
}

func TestValidateNamespace_defaultNamespace(t *testing.T) {
res := createNamespaceResource(t, validNamespace())
res.Id.Name = resource.DefaultNamespacedTenancy().Namespace
dhiaayachi marked this conversation as resolved.
Show resolved Hide resolved

err := ValidateNamespace(res)
require.Error(t, err)
require.ErrorAs(t, err, &errInvalidName)
}

func TestValidateNamespace_InvalidName(t *testing.T) {
res := createNamespaceResource(t, validNamespace())
res.Id.Name = "-invalid"

err := ValidateNamespace(res)
require.Error(t, err)
require.ErrorAs(t, err, &errInvalidName)
}

func TestValidateNamespace_ParseError(t *testing.T) {
// Any type other than the Workload type would work
dhiaayachi marked this conversation as resolved.
Show resolved Hide resolved
// to cause the error we are expecting
data := &pbcatalog.IP{Address: "198.18.0.1"}

res := createNamespaceResource(t, data)

err := ValidateNamespace(res)
require.Error(t, err)
require.ErrorAs(t, err, &resource.ErrDataParse{})
}

func TestMutateNamespace(t *testing.T) {
tests := []struct {
name string
namespaceName string
expectedName string
}{
{"lower", "lower", "lower"},
{"mixed", "MiXeD", "mixed"},
{"upper", "UPPER", "upper"},
Copy link
Collaborator

Choose a reason for hiding this comment

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

Corner case: what if it is prefixed/suffixed with spaces? Does V1 fix those cases?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think that would be catched as part of the fqdn check, but I will add few to make sure

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added the tests

}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res := &pbresource.Resource{Id: &pbresource.ID{Name: tt.namespaceName}}
if err := MutateNamespace(res); err != nil {
t.Errorf("MutateNamespace() error = %v", err)
}
require.Equal(t, res.Id.Name, tt.expectedName)
})
}
}
18 changes: 18 additions & 0 deletions internal/tenancy/internal/types/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package types

import (
"github.com/hashicorp/consul/internal/resource"
)

const (
GroupName = "tenancy"
VersionV1Alpha1 = "v1alpha1"
CurrentVersion = VersionV1Alpha1
)

func Register(r resource.Registry) {
RegisterNamespace(r)
}
38 changes: 38 additions & 0 deletions proto-public/pbtenancy/v1alpha1/namespace.pb.binary.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading