Skip to content

Commit

Permalink
feat: Deploy controller that copies ClusterClasses to namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
dlipovetsky committed Jun 11, 2024
1 parent 7d065bf commit e6a6f3a
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
6 changes: 6 additions & 0 deletions api/v1alpha1/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ const (
ClusterAutoscalerVariableName = "clusterAutoscaler"
// ServiceLoadBalancerVariableName is the Service LoadBalancer config patch variable name.
ServiceLoadBalancerVariableName = "serviceLoadBalancer"

// NamespaceSyncLabelKey is a label that can be applied to a namespace.
//
// When a namespace has a label with this key, ClusterClasses and their Templates are
// copied to the namespace from a source namespace. The copies are not updated or deleted.
NamespaceSyncLabelKey = "caren.nutanix.com/namespace-sync"
)
20 changes: 20 additions & 0 deletions charts/cluster-api-runtime-extensions-nutanix/templates/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ rules:
- patch
- update
- watch
- apiGroups:
- bootstrap.cluster.x-k8s.io
- controlplane.cluster.x-k8s.io
- infrastructure.cluster.x-k8s.io
resources:
- '*'
verbs:
- create
- get
- list
- watch
- apiGroups:
- cluster.x-k8s.io
resources:
- clusterclasses
verbs:
- create
- get
- list
- watch
- apiGroups:
- cluster.x-k8s.io
resources:
Expand Down
33 changes: 33 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ import (
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
crsv1 "sigs.k8s.io/cluster-api/exp/addons/api/v1beta1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/manager"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"

caaphv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/external/sigs.k8s.io/cluster-api-addon-provider-helm/api/v1alpha1"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/server"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/controllers/namespacesync"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/docker"
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic"
Expand Down Expand Up @@ -95,6 +99,8 @@ func main() {
// It allows to specify configuration under a single variable.
genericMetaHandlers := generic.New()

namespacesyncOptions := namespacesync.Options{}

// Initialize and parse command line flags.
logs.AddFlags(pflag.CommandLine, logs.SkipLoggingConfigurationFlags())
logsv1.AddFlags(logOptions, pflag.CommandLine)
Expand All @@ -104,6 +110,7 @@ func main() {
awsMetaHandlers.AddFlags(pflag.CommandLine)
dockerMetaHandlers.AddFlags(pflag.CommandLine)
nutanixMetaHandlers.AddFlags(pflag.CommandLine)
namespacesyncOptions.AddFlags(pflag.CommandLine)
pflag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
Expand Down Expand Up @@ -141,6 +148,32 @@ func main() {
os.Exit(1)
}

unstructuredCachingClient, err := client.New(mgr.GetConfig(), client.Options{
HTTPClient: mgr.GetHTTPClient(),
Cache: &client.CacheOptions{
Reader: mgr.GetCache(),
Unstructured: true,
},
})
if err != nil {
setupLog.Error(err, "unable to create unstructured caching client")
os.Exit(1)
}

if err := (&namespacesync.Reconciler{
Client: mgr.GetClient(),
UnstructuredCachingClient: unstructuredCachingClient,
SourceClusterClassNamespace: namespacesyncOptions.SourceNamespace,
TargetNamespaceFilter: namespacesync.NamespaceHasLabelKey(v1alpha1.NamespaceSyncLabelKey),
}).SetupWithManager(
signalCtx,
mgr,
controller.Options{MaxConcurrentReconciles: namespacesyncOptions.Concurrency},
); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "namespacesync.Reconciler")
os.Exit(1)
}

if err := mgr.Start(signalCtx); err != nil {
setupLog.Error(err, "unable to start controller manager")
os.Exit(1)
Expand Down
26 changes: 26 additions & 0 deletions pkg/controllers/namespacesync/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package namespacesync

import (
"github.com/spf13/pflag"
corev1 "k8s.io/api/core/v1"
)

type Options struct {
Concurrency int
SourceNamespace string
}

func (o *Options) AddFlags(flags *pflag.FlagSet) {
pflag.CommandLine.IntVar(
&o.Concurrency,
"namespacesync-concurrency",
10,
"Number of target namespaces to sync concurrently.",
)

pflag.CommandLine.StringVar(
&o.SourceNamespace,
"namespacesync-source-namespace",
corev1.NamespaceDefault, "Namespace from which ClusterClasses and Templates are copied.",
)
}

0 comments on commit e6a6f3a

Please sign in to comment.