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

NET-6722 Rename MeshConfigController to ConsulResourceController #3283

Merged
merged 15 commits into from
Nov 30, 2023
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type TrafficPermissionsWebhook struct {
client.Client
}

var _ common.MeshConfigLister = &TrafficPermissionsWebhook{}
var _ common.ConsulResourceLister = &TrafficPermissionsWebhook{}

// NOTE: The path value in the below line is the path to the webhook.
// If it is updated, run code-gen, update subcommand/inject-connect/command.go
Expand All @@ -47,14 +47,14 @@ func (v *TrafficPermissionsWebhook) Handle(ctx context.Context, req admission.Re
return common.ValidateMeshConfig(ctx, req, v.Logger, v, &resource, v.ConsulTenancyConfig)
}

func (v *TrafficPermissionsWebhook) List(ctx context.Context) ([]common.MeshConfig, error) {
func (v *TrafficPermissionsWebhook) List(ctx context.Context) ([]common.ConsulResource, error) {
var resourceList TrafficPermissionsList
if err := v.Client.List(ctx, &resourceList); err != nil {
return nil, err
}
var entries []common.MeshConfig
var entries []common.ConsulResource
for _, item := range resourceList.Items {
entries = append(entries, common.MeshConfig(item))
entries = append(entries, common.ConsulResource(item))
}
return entries, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
)

type MeshConfig interface {
type ConsulResource interface {
ResourceID(namespace, partition string) *pbresource.ID
Resource(namespace, partition string) *pbresource.Resource

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,26 @@ import (
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

// MeshConfigLister is implemented by CRD-specific webhooks.
type MeshConfigLister interface {
// ConsulResourceLister is implemented by CRD-specific webhooks.
type ConsulResourceLister interface {
// List returns all resources of this type across all namespaces in a
// Kubernetes cluster.
List(ctx context.Context) ([]MeshConfig, error)
List(ctx context.Context) ([]ConsulResource, error)
}

// ValidateMeshConfig validates a MeshConfig. It is a generic method that
// ValidateConsulResource validates a Consul Resource. It is a generic method that
// can be used by all CRD-specific validators.
// Callers should pass themselves as validator and kind should be the custom
// resource name, e.g. "TrafficPermissions".
func ValidateMeshConfig(
func ValidateConsulResource(
ctx context.Context,
req admission.Request,
logger logr.Logger,
meshConfigLister MeshConfigLister,
meshConfig MeshConfig,
resourceLister ConsulResourceLister,
resource ConsulResource,
tenancy ConsulTenancyConfig) admission.Response {

defaultingPatches, err := MeshConfigDefaultingPatches(meshConfig, tenancy)
defaultingPatches, err := ConsulResourceDefaultingPatches(resource, tenancy)
if err != nil {
return admission.Errored(http.StatusInternalServerError, err)
}
Expand All @@ -45,36 +45,36 @@ func ValidateMeshConfig(
// are running Consul enterprise with namespace mirroring.
singleConsulDestNS := !(tenancy.EnableConsulNamespaces && tenancy.EnableNSMirroring)
if req.Operation == admissionv1.Create && singleConsulDestNS {
logger.Info("validate create", "name", meshConfig.KubernetesName())
logger.Info("validate create", "name", resource.KubernetesName())

list, err := meshConfigLister.List(ctx)
list, err := resourceLister.List(ctx)
if err != nil {
return admission.Errored(http.StatusInternalServerError, err)
}
for _, item := range list {
if item.KubernetesName() == meshConfig.KubernetesName() {
if item.KubernetesName() == resource.KubernetesName() {
return admission.Errored(http.StatusBadRequest,
fmt.Errorf("%s resource with name %q is already defined – all %s resources must have unique names across namespaces",
meshConfig.KubeKind(),
meshConfig.KubernetesName(),
meshConfig.KubeKind()))
resource.KubeKind(),
resource.KubernetesName(),
resource.KubeKind()))
}
}
}
if err := meshConfig.Validate(tenancy); err != nil {
if err := resource.Validate(tenancy); err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
return admission.Patched(fmt.Sprintf("valid %s request", meshConfig.KubeKind()), defaultingPatches...)
return admission.Patched(fmt.Sprintf("valid %s request", resource.KubeKind()), defaultingPatches...)
}

// MeshConfigDefaultingPatches returns the patches needed to set fields to their defaults.
func MeshConfigDefaultingPatches(meshConfig MeshConfig, tenancy ConsulTenancyConfig) ([]jsonpatch.Operation, error) {
beforeDefaulting, err := json.Marshal(meshConfig)
// ConsulResourceDefaultingPatches returns the patches needed to set fields to their defaults.
func ConsulResourceDefaultingPatches(resource ConsulResource, tenancy ConsulTenancyConfig) ([]jsonpatch.Operation, error) {
beforeDefaulting, err := json.Marshal(resource)
if err != nil {
return nil, fmt.Errorf("marshalling input: %s", err)
}
meshConfig.DefaultNamespaceFields(tenancy)
afterDefaulting, err := json.Marshal(meshConfig)
resource.DefaultNamespaceFields(tenancy)
afterDefaulting, err := json.Marshal(resource)
if err != nil {
return nil, fmt.Errorf("marshalling after defaulting: %s", err)
}
Expand Down
Loading