Skip to content

Commit

Permalink
reconciler/tenancy: add replicate controller for workspacetypes
Browse files Browse the repository at this point in the history
  • Loading branch information
sttts committed Jan 13, 2023
1 parent b8abffb commit de413ee
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Copyright 2023 The KCP Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package replicateclusterrole

import (
kcprbacinformers "github.com/kcp-dev/client-go/informers/rbac/v1"
kcpkubernetesclientset "github.com/kcp-dev/client-go/kubernetes"

rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/util/sets"

"github.com/kcp-dev/kcp/pkg/apis/tenancy"
"github.com/kcp-dev/kcp/pkg/reconciler/cache/labelclusterroles"
)

const (
ControllerName = "kcp-apiexport-replicate-clusterrole"
)

// NewController returns a new controller for labelling ClusterRole that should be replicated.
func NewController(
kubeClusterClient kcpkubernetesclientset.ClusterInterface,
clusterRoleInformer kcprbacinformers.ClusterRoleClusterInformer,
clusterRoleBindingInformer kcprbacinformers.ClusterRoleBindingClusterInformer,
) (labelclusterroles.Controller, error) {
return labelclusterroles.NewController(
ControllerName,
tenancy.GroupName,
HasUseRule,
func(crb *rbacv1.ClusterRoleBinding) bool { return false },
kubeClusterClient,
clusterRoleInformer,
clusterRoleBindingInformer,
)
}

func HasUseRule(cr *rbacv1.ClusterRole) bool {
for _, rule := range cr.Rules {
apiGroups := sets.NewString(rule.APIGroups...)
if !apiGroups.Has(tenancy.GroupName) && !apiGroups.Has("*") {
continue
}
resources := sets.NewString(rule.Resources...)
verbs := sets.NewString(rule.Verbs...)
if (resources.Has("workspacetypes") || resources.Has("*")) && (verbs.Has("use") || verbs.Has("*")) {
return true
}
}
return false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2023 The KCP Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package replicateclusterrolebinding

import (
kcprbacinformers "github.com/kcp-dev/client-go/informers/rbac/v1"
kcpkubernetesclientset "github.com/kcp-dev/client-go/kubernetes"

rbacv1 "k8s.io/api/rbac/v1"

"github.com/kcp-dev/kcp/pkg/apis/tenancy"
"github.com/kcp-dev/kcp/pkg/reconciler/cache/labelclusterrolebindings"
replicateclusterrole "github.com/kcp-dev/kcp/pkg/reconciler/tenancy/replicateclusterrole"
)

const (
ControllerName = "kcp-apis-replicate-clusterrolebinding"
)

// NewController returns a new controller for labelling ClusterRoleBinding that should be replicated.
func NewController(
kubeClusterClient kcpkubernetesclientset.ClusterInterface,
clusterRoleBindingInformer kcprbacinformers.ClusterRoleBindingClusterInformer,
clusterRoleInformer kcprbacinformers.ClusterRoleClusterInformer,
) (labelclusterrolebindings.Controller, error) {
return labelclusterrolebindings.NewController(
ControllerName,
tenancy.GroupName,
replicateclusterrole.HasUseRule,
func(crb *rbacv1.ClusterRoleBinding) bool { return false },
kubeClusterClient,
clusterRoleBindingInformer,
clusterRoleInformer,
)
}
62 changes: 62 additions & 0 deletions pkg/server/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ import (
"github.com/kcp-dev/kcp/pkg/reconciler/tenancy/bootstrap"
"github.com/kcp-dev/kcp/pkg/reconciler/tenancy/initialization"
tenancylogicalcluster "github.com/kcp-dev/kcp/pkg/reconciler/tenancy/logicalcluster"
tenancyreplicateclusterrole "github.com/kcp-dev/kcp/pkg/reconciler/tenancy/replicateclusterrole"
tenancyreplicateclusterrolebinding "github.com/kcp-dev/kcp/pkg/reconciler/tenancy/replicateclusterrolebinding"
"github.com/kcp-dev/kcp/pkg/reconciler/tenancy/workspace"
"github.com/kcp-dev/kcp/pkg/reconciler/tenancy/workspacetype"
workloadsapiexport "github.com/kcp-dev/kcp/pkg/reconciler/workload/apiexport"
Expand Down Expand Up @@ -991,6 +993,66 @@ func (s *Server) installApisReplicateClusterRoleBindingControllers(ctx context.C
})
}

func (s *Server) installTenancyReplicateClusterRoleControllers(ctx context.Context, config *rest.Config, server *genericapiserver.GenericAPIServer) error {
config = rest.CopyConfig(config)
config = rest.AddUserAgent(config, tenancyreplicateclusterrole.ControllerName)
kubeClusterClient, err := kcpkubernetesclientset.NewForConfig(config)
if err != nil {
return err
}

c, err := tenancyreplicateclusterrole.NewController(
kubeClusterClient,
s.KubeSharedInformerFactory.Rbac().V1().ClusterRoles(),
s.KubeSharedInformerFactory.Rbac().V1().ClusterRoleBindings(),
)
if err != nil {
return err
}

return server.AddPostStartHook(postStartHookName(tenancyreplicateclusterrole.ControllerName), func(hookContext genericapiserver.PostStartHookContext) error {
logger := klog.FromContext(ctx).WithValues("postStartHook", postStartHookName(tenancyreplicateclusterrole.ControllerName))
if err := s.waitForSync(hookContext.StopCh); err != nil {
logger.Error(err, "failed to finish post-start-hook")
return nil // don't klog.Fatal. This only happens when context is cancelled.
}

go c.Start(goContext(hookContext), 2)

return nil
})
}

func (s *Server) installTenancyReplicateClusterRoleBindingControllers(ctx context.Context, config *rest.Config, server *genericapiserver.GenericAPIServer) error {
config = rest.CopyConfig(config)
config = rest.AddUserAgent(config, tenancyreplicateclusterrolebinding.ControllerName)
kubeClusterClient, err := kcpkubernetesclientset.NewForConfig(config)
if err != nil {
return err
}

c, err := tenancyreplicateclusterrolebinding.NewController(
kubeClusterClient,
s.KubeSharedInformerFactory.Rbac().V1().ClusterRoleBindings(),
s.KubeSharedInformerFactory.Rbac().V1().ClusterRoles(),
)
if err != nil {
return err
}

return server.AddPostStartHook(postStartHookName(tenancyreplicateclusterrolebinding.ControllerName), func(hookContext genericapiserver.PostStartHookContext) error {
logger := klog.FromContext(ctx).WithValues("postStartHook", postStartHookName(tenancyreplicateclusterrolebinding.ControllerName))
if err := s.waitForSync(hookContext.StopCh); err != nil {
logger.Error(err, "failed to finish post-start-hook")
return nil // don't klog.Fatal. This only happens when context is cancelled.
}

go c.Start(goContext(hookContext), 2)

return nil
})
}

func (s *Server) installAPIExportEndpointSliceController(ctx context.Context, config *rest.Config, server *genericapiserver.GenericAPIServer) error {
config = rest.CopyConfig(config)
config = rest.AddUserAgent(config, apiexportendpointslice.ControllerName)
Expand Down
11 changes: 11 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,17 @@ func (s *Server) Run(ctx context.Context) error {
}
}

if s.Options.Controllers.EnableAll || enabled.Has("tenancyreplicateclusterrole") {
if err := s.installTenancyReplicateClusterRoleControllers(ctx, controllerConfig, delegationChainHead); err != nil {
return err
}
}
if s.Options.Controllers.EnableAll || enabled.Has("tenancyreplicationclusterrolebinding") {
if err := s.installTenancyReplicateClusterRoleBindingControllers(ctx, controllerConfig, delegationChainHead); err != nil {
return err
}
}

if s.Options.Controllers.EnableAll || enabled.Has("apiexportendpointslice") {
if err := s.installAPIExportEndpointSliceController(ctx, controllerConfig, delegationChainHead); err != nil {
return err
Expand Down

0 comments on commit de413ee

Please sign in to comment.