Skip to content

Commit

Permalink
Stub mesh configuration resource controller (#3302)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Stucki authored and jmurret committed Dec 8, 2023
1 parent 634be2c commit 278d981
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 7 deletions.
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 {
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 {
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 @@ -445,6 +445,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

0 comments on commit 278d981

Please sign in to comment.