Skip to content

Commit

Permalink
reconciler/tenancy: replicate LogicalClusters for WorkspaceType
Browse files Browse the repository at this point in the history
  • Loading branch information
p0lyn0mial authored and sttts committed Jan 27, 2023
1 parent b68b3db commit 641ac0d
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
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 replicatelogicalcluster

import (
"fmt"

kcpcache "github.com/kcp-dev/apimachinery/v2/pkg/cache"
"github.com/kcp-dev/logicalcluster/v3"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/cache"

corev1alpha1 "github.com/kcp-dev/kcp/pkg/apis/core/v1alpha1"
"github.com/kcp-dev/kcp/pkg/apis/tenancy"
tenancyv1alpha1 "github.com/kcp-dev/kcp/pkg/apis/tenancy/v1alpha1"
kcpclientset "github.com/kcp-dev/kcp/pkg/client/clientset/versioned/cluster"
corev1alpha1informers "github.com/kcp-dev/kcp/pkg/client/informers/externalversions/core/v1alpha1"
tenancyv1alpha1informers "github.com/kcp-dev/kcp/pkg/client/informers/externalversions/tenancy/v1alpha1"
"github.com/kcp-dev/kcp/pkg/reconciler/cache/labellogicalcluster"
"github.com/kcp-dev/kcp/pkg/reconciler/cache/replication"
)

const (
ControllerName = "kcp-tenancy-replicate-logicalcluster"
)

// NewController returns a new controller for labelling LogicalClusters that should be replicated.

func NewController(
kcpClusterClient kcpclientset.ClusterInterface,
logicalClusterInformer corev1alpha1informers.LogicalClusterClusterInformer,
workspaceTypeInformer tenancyv1alpha1informers.WorkspaceTypeClusterInformer,
) labellogicalcluster.Controller {
logicalClusterLister := logicalClusterInformer.Lister()
workspaceTypeIndexer := workspaceTypeInformer.Informer().GetIndexer()

c := labellogicalcluster.NewController(
ControllerName,
tenancy.GroupName,
func(cluster *corev1alpha1.LogicalCluster) bool {
// If there are any WorkspaceTypes for this logical cluster, then the LogicalCluster object should be replicated.
keys, err := workspaceTypeIndexer.IndexKeys(kcpcache.ClusterIndexName, kcpcache.ClusterIndexKey(logicalcluster.From(cluster)))
if err != nil {
runtime.HandleError(fmt.Errorf("failed to list WorkspaceTypes: %v", err))
return false
}
return len(keys) > 0
},
kcpClusterClient,
logicalClusterInformer,
)

// enqueue the logical cluster every time a Workspace changes
enqueueWorkspaceType := func(obj interface{}) {
if tombstone, ok := obj.(cache.DeletedFinalStateUnknown); ok {
obj = tombstone.Obj
}

workspaceType, ok := obj.(*tenancyv1alpha1.WorkspaceType)
if !ok {
runtime.HandleError(fmt.Errorf("unexpected object type: %T", obj))
return
}

cluster, err := logicalClusterLister.Cluster(logicalcluster.From(workspaceType)).Get(corev1alpha1.LogicalClusterName)
if err != nil && !apierrors.IsNotFound(err) {
runtime.HandleError(fmt.Errorf("failed to get logical cluster: %v", err))
return
} else if !apierrors.IsNotFound(err) {
return
}

c.EnqueueLogicalCluster(cluster, "reason", "WorkspaceType changed", "workspacetype", workspaceType.Name)
}

workspaceTypeInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{
FilterFunc: replication.IsNoSystemClusterName,
Handler: cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
enqueueWorkspaceType(obj)
},
DeleteFunc: func(obj interface{}) {
enqueueWorkspaceType(obj)
},
},
})

return c
}
28 changes: 28 additions & 0 deletions pkg/server/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"
"fmt"
tenancyreplicatelogicalcluster "github.com/kcp-dev/kcp/pkg/reconciler/tenancy/replicatelogicalcluster"
_ "net/http/pprof"
"os"
"time"
Expand Down Expand Up @@ -1067,6 +1068,33 @@ func (s *Server) installApisReplicateLogicalClusterControllers(ctx context.Conte
})
}

func (s *Server) installTenancyReplicateLogicalClusterControllers(ctx context.Context, config *rest.Config, server *genericapiserver.GenericAPIServer) error {
config = rest.CopyConfig(config)
config = rest.AddUserAgent(config, tenancyreplicatelogicalcluster.ControllerName)
kcpClusterClient, err := kcpclientset.NewForConfig(config)
if err != nil {
return err
}

c := tenancyreplicatelogicalcluster.NewController(
kcpClusterClient,
s.KcpSharedInformerFactory.Core().V1alpha1().LogicalClusters(),
s.KcpSharedInformerFactory.Tenancy().V1alpha1().WorkspaceTypes(),
)

return server.AddPostStartHook(postStartHookName(tenancyreplicatelogicalcluster.ControllerName), func(hookContext genericapiserver.PostStartHookContext) error {
logger := klog.FromContext(ctx).WithValues("postStartHook", postStartHookName(tenancyreplicatelogicalcluster.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) installCoreReplicateClusterRoleBindingControllers(ctx context.Context, config *rest.Config, server *genericapiserver.GenericAPIServer) error {
config = rest.CopyConfig(config)
config = rest.AddUserAgent(config, corereplicateclusterrolebinding.ControllerName)
Expand Down
6 changes: 6 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,12 @@ func (s *Server) Run(ctx context.Context) error {
}
}

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

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

0 comments on commit 641ac0d

Please sign in to comment.