Files
teleport/integrations/operator/controllers/resources/user_controller.go
T
Hugo Shaka 4dfc23442f integrations/operator: use unstructured objects for all controllers (#36142)
This commit prepares everything for us to switch to a single reconciler for all resources. Part of https://github.com/gravitational/teleport/issues/21416

This commit does the following changes:
- setup controllers for the unstructured objects instead of the structured ones (like `Role` was doing). This is due to the bad behaviour of `utils.Strings` of serializing as a single string if the list contains a single element.
- use a single scheme for everything instead of multiple ones. We suffered from drifts between main and tests in the past.
- fetch the GVK for the unstructure objects from the scheme. This operation can fail and now a bunch of builder/init functions can return errors
- Introduce a new `Reconciler` interface to hide the controller internals
- unify how controllers are started in main and tests. This is not directly related to the PR goal (preparing the ground for a unified controller) but I was drawn into this when I made the constructors return errors.
2024-01-17 15:43:26 +00:00

80 lines
2.6 KiB
Go

/*
* Teleport
* Copyright (C) 2023 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"
resourcesv2 "github.com/gravitational/teleport/integrations/operator/apis/resources/v2"
)
// userClient implements TeleportResourceClient and offers CRUD methods needed to reconcile users
type userClient struct {
teleportClient *client.Client
}
// Get gets the Teleport user of a given name
func (r userClient) Get(ctx context.Context, name string) (types.User, error) {
user, err := r.teleportClient.GetUser(ctx, name, false /* with secrets*/)
return user, trace.Wrap(err)
}
// Create creates a Teleport user
func (r userClient) Create(ctx context.Context, user types.User) error {
_, err := r.teleportClient.CreateUser(ctx, user)
return trace.Wrap(err)
}
// Update updates a Teleport user
func (r userClient) Update(ctx context.Context, user types.User) error {
_, err := r.teleportClient.UpdateUser(ctx, user)
return trace.Wrap(err)
}
// Delete deletes a Teleport user
func (r userClient) Delete(ctx context.Context, name string) error {
return trace.Wrap(r.teleportClient.DeleteUser(ctx, name))
}
// MutateExisting ensures the spec.createdBy property is persisted
func (r userClient) MutateExisting(newUser, existingUser types.User) {
if existingUser != nil {
newUser.SetCreatedBy(existingUser.GetCreatedBy())
}
}
// NewUserReconciler instantiates a new Kubernetes controller reconciling user resources
func NewUserReconciler(client kclient.Client, tClient *client.Client) (Reconciler, error) {
userClient := &userClient{
teleportClient: tClient,
}
resourceReconciler, err := NewTeleportResourceReconciler[types.User, *resourcesv2.TeleportUser](
client,
userClient,
)
return resourceReconciler, trace.Wrap(err, "building teleport resource reconciler")
}