forked from gravitational/teleport
/
rolevX_controller.go
104 lines (86 loc) · 3.57 KB
/
rolevX_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* 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"
)
// 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) (Reconciler, error) {
roleClient := &roleClient{
teleportClient: tClient,
}
resourceReconciler, err := NewTeleportResourceReconciler[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) (Reconciler, error) {
roleClient := &roleClient{
teleportClient: tClient,
}
resourceReconciler, err := NewTeleportResourceReconciler[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) (Reconciler, error) {
roleClient := &roleClient{
teleportClient: tClient,
}
resourceReconciler, err := NewTeleportResourceReconciler[types.Role, *resourcesv1.TeleportRoleV7](
client,
roleClient,
)
return resourceReconciler, trace.Wrap(err, "building teleport resource reconciler")
}