Skip to content

Commit

Permalink
NET-6722 Rename MeshConfigController to ConsulResourceController (#3283)
Browse files Browse the repository at this point in the history
* Refactor MeshConfigController to ConsulResouceController

* Rename controller file

* Rename MeshConfig to ConsulResource

* Rename Controller to ResourceController

* Update some references to meshConfig

* Fixup some comments

* Change MeshConfigController to ConsulResourceController in all implementations

* Change MeshConfigController to ConsulResourceController in instances

* Fix references to MeshConfigController in some tests

* Rename mockMeshConfig to mockConsulResource

* Rename meshConfigReconciler to consulResourceReconciler

* Rename ConsulResourceController to Controller

* Fix calls to validate

* Use "Controller" instead of "Reconciler"

* Empty commit to retrigger tests
  • Loading branch information
Thomas Eckert authored and sarahalsmiller committed Jan 5, 2024
1 parent 5d77006 commit e1ad061
Show file tree
Hide file tree
Showing 20 changed files with 284 additions and 284 deletions.
10 changes: 5 additions & 5 deletions control-plane/api/auth/v2beta1/trafficpermissions_webhook.go
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 @@ -44,17 +44,17 @@ func (v *TrafficPermissionsWebhook) Handle(ctx context.Context, req admission.Re
return admission.Errored(http.StatusBadRequest, err)
}

return common.ValidateMeshConfig(ctx, req, v.Logger, v, &resource, v.ConsulTenancyConfig)
return common.ValidateConsulResource(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

0 comments on commit e1ad061

Please sign in to comment.