Skip to content

Commit ac568dd

Browse files
committed
Handle csr and token based addon registration
Signed-off-by: RokibulHasan7 <mdrokibulhasan@appscode.com>
1 parent 3e0610a commit ac568dd

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

pkg/apiserver/apiserver.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ import (
4848
"k8s.io/klog/v2"
4949
cu "kmodules.xyz/client-go/client"
5050
clustermeta "kmodules.xyz/client-go/cluster"
51+
addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
52+
addonv1beta1 "open-cluster-management.io/api/addon/v1beta1"
5153
clusterv1alpha1 "open-cluster-management.io/api/cluster/v1alpha1"
5254
ctrl "sigs.k8s.io/controller-runtime"
5355
"sigs.k8s.io/controller-runtime/pkg/cache"
@@ -70,6 +72,8 @@ func init() {
7072
utilruntime.Must(clientgoscheme.AddToScheme(Scheme))
7173
utilruntime.Must(clusterv1alpha1.Install(Scheme))
7274
utilruntime.Must(core.AddToScheme(Scheme))
75+
utilruntime.Must(addonv1alpha1.Install(Scheme))
76+
utilruntime.Must(addonv1beta1.Install(Scheme))
7377

7478
// we need to add the options to empty v1
7579
// TODO fix the server code to avoid this

pkg/manager/manager.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import (
4444
"open-cluster-management.io/api/addon/v1alpha1"
4545
ctrl "sigs.k8s.io/controller-runtime"
4646
"sigs.k8s.io/controller-runtime/pkg/cache"
47+
"sigs.k8s.io/controller-runtime/pkg/client"
4748
"sigs.k8s.io/controller-runtime/pkg/log"
4849
"sigs.k8s.io/controller-runtime/pkg/manager"
4950
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
@@ -52,11 +53,11 @@ import (
5253
//go:embed all:agent-manifests
5354
var FS embed.FS
5455

55-
func NewRegistrationOption(kubeConfig *rest.Config, addonName, agentName string) *agent.RegistrationOption {
56+
func NewRegistrationOption(restConfig *rest.Config, kc client.Client, addonName, agentName string) *agent.RegistrationOption {
5657
return &agent.RegistrationOption{
5758
CSRConfigurations: agent.KubeClientSignerConfigurations(addonName, agentName),
5859
CSRApproveCheck: agent.ApprovalAllCSRs,
59-
PermissionConfig: rbac.SetupPermission(kubeConfig, agentName),
60+
PermissionConfig: rbac.SetupPermission(restConfig, kc, agentName),
6061
AgentInstallNamespace: func(addon *v1alpha1.ManagedClusterAddOn) (string, error) {
6162
return common.AddonInstallationNamespace, nil
6263
},
@@ -140,7 +141,7 @@ func runManagerController(ctx context.Context, cfg *rest.Config, opts *ManagerOp
140141
os.Exit(1)
141142
}
142143

143-
registrationOption := NewRegistrationOption(cfg, common.AddonName, common.AgentName)
144+
registrationOption := NewRegistrationOption(cfg, hubManager.GetClient(), common.AddonName, common.AgentName)
144145

145146
addonManager, err := addonmanager.New(cfg)
146147
if err != nil {

pkg/manager/rbac/rbac.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package rbac
1919
import (
2020
"context"
2121

22+
"go.bytebuilders.dev/license-proxyserver/pkg/common"
23+
2224
rbacv1 "k8s.io/api/rbac/v1"
2325
apierrors "k8s.io/apimachinery/pkg/api/errors"
2426
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -27,12 +29,14 @@ import (
2729
"k8s.io/utils/ptr"
2830
"open-cluster-management.io/addon-framework/pkg/agent"
2931
addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
32+
addonv1beta1 "open-cluster-management.io/api/addon/v1beta1"
3033
clusterv1 "open-cluster-management.io/api/cluster/v1"
34+
"sigs.k8s.io/controller-runtime/pkg/client"
3135
)
3236

33-
func SetupPermission(kubeConfig *rest.Config, agentName string) agent.PermissionConfigFunc {
37+
func SetupPermission(restConfig *rest.Config, kc client.Client, agentName string) agent.PermissionConfigFunc {
3438
return func(cluster *clusterv1.ManagedCluster, addon *addonv1alpha1.ManagedClusterAddOn) error {
35-
nativeClient, err := kubernetes.NewForConfig(kubeConfig)
39+
nativeClient, err := kubernetes.NewForConfig(restConfig)
3640
if err != nil {
3741
return err
3842
}
@@ -81,12 +85,28 @@ func SetupPermission(kubeConfig *rest.Config, agentName string) agent.Permission
8185
},
8286
Subjects: []rbacv1.Subject{
8387
{
84-
Kind: rbacv1.UserKind,
85-
Name: agentUser,
88+
Kind: "ServiceAccount",
89+
Name: common.AddonName + "-agent",
8690
},
8791
},
8892
}
8993

94+
managedClusterAddon := &addonv1beta1.ManagedClusterAddOn{}
95+
if err := kc.Get(context.TODO(), client.ObjectKey{Namespace: namespace, Name: addon.Name}, managedClusterAddon); err != nil {
96+
return err
97+
}
98+
99+
for _, reg := range managedClusterAddon.Status.Registrations {
100+
if reg.Type == addonv1beta1.KubeClient && reg.KubeClient.Driver == "csr" {
101+
roleBinding.Subjects = []rbacv1.Subject{
102+
{
103+
Kind: "User",
104+
Name: agentUser,
105+
},
106+
}
107+
}
108+
}
109+
90110
_, err = nativeClient.RbacV1().Roles(cluster.Name).Get(context.TODO(), role.Name, metav1.GetOptions{})
91111
switch {
92112
case apierrors.IsNotFound(err):

0 commit comments

Comments
 (0)