mirror of
https://github.com/gravitational/teleport.git
synced 2026-11-03 07:40:28 +08:00
* Refactor operator resource reconciler to support resource153 + add label propagation I initially just wanted to add label propagation (which is required for OpenSSH and OpenSSHEICE servers). However I ran into an issue: not all Teleport resources implement the `ResourceWithLabels` interface. Basically we have 4 cases: - resources implementing `ResourceWithLabels` (e.g. User, Role, ...) - resources implementing `ResourceWithOrigin` but not `ResourceWithLabels` (e.g. GitHub connector, ProvisionToken, ...) - resources that have been tweaked to implement only the operator `TeleportResource` interface (`LoginRule`) - resources implementing `Resource153` To support all those resoucres, we can either: - edit each resource so they expose the same methods (e.g. ProvisionToken exposes `GetLabels` instead of `GetStaticLabels`) - wrap each ressource in a wrapper that implements the operator TeleportResource interface - add multiple reconcilers, one for each resource kind I don't think it's sane for the operator to force changes on Teleport resources. Especially as those are known to be non consistent. I also don't want the operator to maintain a copy of each resource, this is not viable on the long term. For those reasons I chose the 3rd approach and built 3 reconcilers. Then I deducplicated the code to end up with a generic reconciler that can be tuned by passing an adapter. The adapter is an empty struct that has all the methods to extract the information the operator requires from the resource. The resulting PR is large and heavily uses generics but the amount of logical changes is quite low (the only diff is that we now copy labels). I can walk you through the PR if needed. With this PR, the operator supports properly any resource implementing `types.Resource153`, this will be useful to add `TeleportBotV1` support. * Update integrations/operator/controllers/reconcilers/base.go Co-authored-by: Noah Stride <noah.stride@goteleport.com> * use structured logging --------- Co-authored-by: Noah Stride <noah.stride@goteleport.com>
107 lines
3.8 KiB
Go
107 lines
3.8 KiB
Go
/*
|
|
* Teleport
|
|
* Copyright (C) 2024 Gravitational, Inc.
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
package resources
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gravitational/trace"
|
|
kclient "sigs.k8s.io/controller-runtime/pkg/client"
|
|
|
|
"github.com/gravitational/teleport/api/client"
|
|
"github.com/gravitational/teleport/api/types"
|
|
resourcesv1 "github.com/gravitational/teleport/integrations/operator/apis/resources/v1"
|
|
resourcesv5 "github.com/gravitational/teleport/integrations/operator/apis/resources/v5"
|
|
"github.com/gravitational/teleport/integrations/operator/controllers"
|
|
"github.com/gravitational/teleport/integrations/operator/controllers/reconcilers"
|
|
)
|
|
|
|
// roleClient implements TeleportResourceClient and offers CRUD methods needed to reconcile roles
|
|
// Currently the same client is used by all role versions. If we need to treat
|
|
// them differently at some point, for example by introducing Mutate or MutateExisting
|
|
// functions, we can always split the client into separate clients.
|
|
type roleClient struct {
|
|
teleportClient *client.Client
|
|
}
|
|
|
|
// Get gets the Teleport role of a given name
|
|
func (r roleClient) Get(ctx context.Context, name string) (types.Role, error) {
|
|
role, err := r.teleportClient.GetRole(ctx, name)
|
|
return role, trace.Wrap(err)
|
|
}
|
|
|
|
// Create creates a Teleport role
|
|
func (r roleClient) Create(ctx context.Context, role types.Role) error {
|
|
_, err := r.teleportClient.UpsertRole(ctx, role)
|
|
return trace.Wrap(err)
|
|
}
|
|
|
|
// Update updates a Teleport role
|
|
func (r roleClient) Update(ctx context.Context, role types.Role) error {
|
|
_, err := r.teleportClient.UpsertRole(ctx, role)
|
|
return trace.Wrap(err)
|
|
}
|
|
|
|
// Delete deletes a Teleport role
|
|
func (r roleClient) Delete(ctx context.Context, name string) error {
|
|
return trace.Wrap(r.teleportClient.DeleteRole(ctx, name))
|
|
}
|
|
|
|
// NewRoleReconciler instantiates a new Kubernetes controller reconciling legacy role v5 resources
|
|
func NewRoleReconciler(client kclient.Client, tClient *client.Client) (controllers.Reconciler, error) {
|
|
roleClient := &roleClient{
|
|
teleportClient: tClient,
|
|
}
|
|
|
|
resourceReconciler, err := reconcilers.NewTeleportResourceWithLabelsReconciler[types.Role, *resourcesv5.TeleportRole](
|
|
client,
|
|
roleClient,
|
|
)
|
|
|
|
return resourceReconciler, trace.Wrap(err, "building teleport resource reconciler")
|
|
}
|
|
|
|
// NewRoleV6Reconciler instantiates a new Kubernetes controller reconciling role v6 resources
|
|
func NewRoleV6Reconciler(client kclient.Client, tClient *client.Client) (controllers.Reconciler, error) {
|
|
roleClient := &roleClient{
|
|
teleportClient: tClient,
|
|
}
|
|
|
|
resourceReconciler, err := reconcilers.NewTeleportResourceWithLabelsReconciler[types.Role, *resourcesv1.TeleportRoleV6](
|
|
client,
|
|
roleClient,
|
|
)
|
|
|
|
return resourceReconciler, trace.Wrap(err, "building teleport resource reconciler")
|
|
}
|
|
|
|
// NewRoleV7Reconciler instantiates a new Kubernetes controller reconciling role v7 resources
|
|
func NewRoleV7Reconciler(client kclient.Client, tClient *client.Client) (controllers.Reconciler, error) {
|
|
roleClient := &roleClient{
|
|
teleportClient: tClient,
|
|
}
|
|
|
|
resourceReconciler, err := reconcilers.NewTeleportResourceWithLabelsReconciler[types.Role, *resourcesv1.TeleportRoleV7](
|
|
client,
|
|
roleClient,
|
|
)
|
|
|
|
return resourceReconciler, trace.Wrap(err, "building teleport resource reconciler")
|
|
}
|