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

Stub mesh configuration resource controller #3302

Merged
merged 1 commit into from
Dec 7, 2023
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
2 changes: 2 additions & 0 deletions charts/consul/templates/connect-inject-clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ rules:
resources:
- gatewayclassconfigs
- gatewayclasses
- meshconfigurations
- grpcroutes
- httproutes
- meshgateways
Expand All @@ -116,6 +117,7 @@ rules:
resources:
- gatewayclassconfigs/status
- gatewayclasses/status
- meshconfigurations/status
- grpcroutes/status
- httproutes/status
- meshgateways/status
Expand Down
4 changes: 3 additions & 1 deletion control-plane/api/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
package common

import (
mapset "github.com/deckarep/golang-set"
"time"

mapset "github.com/deckarep/golang-set"
)

const (
Expand Down Expand Up @@ -35,6 +36,7 @@ const (
MeshGateway string = "meshgateway"
GatewayClass string = "gatewayclass"
GatewayClassConfig string = "gatewayclassconfig"
MeshConfiguration string = "meshconfiguration"

Global string = "global"
Mesh string = "mesh"
Expand Down
12 changes: 6 additions & 6 deletions control-plane/api/mesh/v2beta1/mesh_configuration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ type MeshConfigurationList struct {
Items []*MeshConfiguration `json:"items"`
}

func (in *MeshConfiguration) ResourceID(namespace, partition string) *pbresource.ID {
func (in *MeshConfiguration) ResourceID(_, partition string) *pbresource.ID {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why use a blank identifier here as opposed to just removing the namespace parameter as it's no longer required?

return &pbresource.ID{
Name: in.Name,
Type: pbmesh.MeshConfigurationType,
Tenancy: &pbresource.Tenancy{
// we don't pass a namespace here because MeshConfiguration is partition-scoped
Partition: partition,
Namespace: namespace,

// Because we are explicitly defining NS/partition, this will not default and must be explicit.
// At a future point, this will move out of the Tenancy block.
Expand All @@ -65,9 +65,9 @@ func (in *MeshConfiguration) ResourceID(namespace, partition string) *pbresource
}
}

func (in *MeshConfiguration) Resource(namespace, partition string) *pbresource.Resource {
func (in *MeshConfiguration) Resource(_, partition string) *pbresource.Resource {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same question as above

Copy link
Contributor Author

@andrewstucki andrewstucki Dec 6, 2023

Choose a reason for hiding this comment

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

These are both to implement the ConsulResource interface found here -- it's what allows us to leverage the shared controller code:

ResourceID(namespace, partition string) *pbresource.ID
Resource(namespace, partition string) *pbresource.Resource

So I need to keep the arguments even though they're unused since the resource is purely partition scoped (it doesn't have a namespace). It's a much less invasive change on the shared controller -- alternatively we'd need to make the shared code aware of resources that are partition-scoped and treat them differently.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the explanation

return &pbresource.Resource{
Id: in.ResourceID(namespace, partition),
Id: in.ResourceID("", partition),
Data: inject.ToProtoAny(&in.Spec),
Metadata: meshConfigMeta(),
}
Expand All @@ -91,9 +91,9 @@ func (in *MeshConfiguration) Finalizers() []string {
return in.ObjectMeta.Finalizers
}

func (in *MeshConfiguration) MatchesConsul(candidate *pbresource.Resource, namespace, partition string) bool {
func (in *MeshConfiguration) MatchesConsul(candidate *pbresource.Resource, _, partition string) bool {
return cmp.Equal(
in.Resource(namespace, partition),
in.Resource("", partition),
candidate,
protocmp.IgnoreFields(&pbresource.Resource{}, "status", "generation", "version"),
protocmp.IgnoreFields(&pbresource.ID{}, "uid"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package controllersv2

import (
"context"

"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

meshv2beta1 "github.com/hashicorp/consul-k8s/control-plane/api/mesh/v2beta1"
)

// MeshConfigurationController reconciles a MeshConfiguration object.
type MeshConfigurationController struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
Controller *ConsulResourceController
}

// +kubebuilder:rbac:groups=mesh.consul.hashicorp.com,resources=meshconfiguration,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=mesh.consul.hashicorp.com,resources=meshconfiguration/status,verbs=get;update;patch

func (r *MeshConfigurationController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
return r.Controller.ReconcileEntry(ctx, r, req, &meshv2beta1.MeshConfiguration{})
}

func (r *MeshConfigurationController) Logger(name types.NamespacedName) logr.Logger {
return r.Log.WithValues("request", name)
}

func (r *MeshConfigurationController) UpdateStatus(ctx context.Context, obj client.Object, opts ...client.SubResourceUpdateOption) error {
return r.Status().Update(ctx, obj, opts...)
}

func (r *MeshConfigurationController) SetupWithManager(mgr ctrl.Manager) error {
return setupWithManager(mgr, &meshv2beta1.MeshConfiguration{}, r)
}
20 changes: 20 additions & 0 deletions control-plane/config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,26 @@ rules:
- get
- patch
- update
- apiGroups:
- mesh.consul.hashicorp.com
resources:
- meshconfiguration
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- mesh.consul.hashicorp.com
resources:
- meshconfiguration/status
verbs:
- get
- patch
- update
- apiGroups:
- mesh.consul.hashicorp.com
resources:
Expand Down
18 changes: 18 additions & 0 deletions control-plane/subcommand/inject-connect/v2controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package connectinject

import (
"context"

"github.com/hashicorp/consul-k8s/control-plane/gateways"
"github.com/hashicorp/consul-server-connection-manager/discovery"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -132,6 +133,7 @@ func (c *Command) configureV2Controllers(ctx context.Context, mgr manager.Manage
ConsulServerConnMgr: watcher,
ConsulTenancyConfig: consulTenancyConfig,
}

if err := (&controllersv2.TrafficPermissionsController{
Controller: consulResourceController,
Client: mgr.GetClient(),
Expand All @@ -141,6 +143,7 @@ func (c *Command) configureV2Controllers(ctx context.Context, mgr manager.Manage
setupLog.Error(err, "unable to create controller", "controller", common.TrafficPermissions)
return err
}

if err := (&controllersv2.GRPCRouteController{
Controller: consulResourceController,
Client: mgr.GetClient(),
Expand All @@ -150,6 +153,7 @@ func (c *Command) configureV2Controllers(ctx context.Context, mgr manager.Manage
setupLog.Error(err, "unable to create controller", "controller", common.GRPCRoute)
return err
}

if err := (&controllersv2.HTTPRouteController{
Controller: consulResourceController,
Client: mgr.GetClient(),
Expand All @@ -159,6 +163,7 @@ func (c *Command) configureV2Controllers(ctx context.Context, mgr manager.Manage
setupLog.Error(err, "unable to create controller", "controller", common.HTTPRoute)
return err
}

if err := (&controllersv2.TCPRouteController{
Controller: consulResourceController,
Client: mgr.GetClient(),
Expand All @@ -168,6 +173,7 @@ func (c *Command) configureV2Controllers(ctx context.Context, mgr manager.Manage
setupLog.Error(err, "unable to create controller", "controller", common.TCPRoute)
return err
}

if err := (&controllersv2.ProxyConfigurationController{
Controller: consulResourceController,
Client: mgr.GetClient(),
Expand All @@ -177,6 +183,17 @@ func (c *Command) configureV2Controllers(ctx context.Context, mgr manager.Manage
setupLog.Error(err, "unable to create controller", "controller", common.ProxyConfiguration)
return err
}

if err := (&controllersv2.MeshConfigurationController{
Controller: consulResourceController,
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controller").WithName(common.MeshConfiguration),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", common.MeshConfiguration)
return err
}

if err := (&controllersv2.MeshGatewayController{
Controller: consulResourceController,
Client: mgr.GetClient(),
Expand Down Expand Up @@ -215,6 +232,7 @@ func (c *Command) configureV2Controllers(ctx context.Context, mgr manager.Manage
setupLog.Error(err, "unable to create controller", "controller", common.GatewayClassConfig)
return err
}

if err := (&controllersv2.GatewayClassController{
Controller: consulResourceController,
Client: mgr.GetClient(),
Expand Down