Skip to content

Commit a54fca3

Browse files
committed
feat: Deploy controller that copies ClusterClasses to namespaces
1 parent fafd426 commit a54fca3

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

api/v1alpha1/constants.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,10 @@ const (
2727
ClusterAutoscalerVariableName = "clusterAutoscaler"
2828
// ServiceLoadBalancerVariableName is the Service LoadBalancer config patch variable name.
2929
ServiceLoadBalancerVariableName = "serviceLoadBalancer"
30+
31+
// NamespaceSyncLabelKey is a label that can be applied to a namespace.
32+
//
33+
// When a namespace has a label with this key, ClusterClasses and their Templates are
34+
// copied to the namespace from a source namespace. The copies are not updated or deleted.
35+
NamespaceSyncLabelKey = "caren.nutanix.com/namespace-sync"
3036
)

charts/cluster-api-runtime-extensions-nutanix/templates/role.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,26 @@ rules:
6161
- patch
6262
- update
6363
- watch
64+
- apiGroups:
65+
- bootstrap.cluster.x-k8s.io
66+
- controlplane.cluster.x-k8s.io
67+
- infrastructure.cluster.x-k8s.io
68+
resources:
69+
- '*'
70+
verbs:
71+
- create
72+
- get
73+
- list
74+
- watch
75+
- apiGroups:
76+
- cluster.x-k8s.io
77+
resources:
78+
- clusterclasses
79+
verbs:
80+
- create
81+
- get
82+
- list
83+
- watch
6484
- apiGroups:
6585
- cluster.x-k8s.io
6686
resources:

cmd/main.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,17 @@ import (
2020
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2121
crsv1 "sigs.k8s.io/cluster-api/exp/addons/api/v1beta1"
2222
ctrl "sigs.k8s.io/controller-runtime"
23+
"sigs.k8s.io/controller-runtime/pkg/client"
24+
"sigs.k8s.io/controller-runtime/pkg/controller"
2325
"sigs.k8s.io/controller-runtime/pkg/healthz"
2426
"sigs.k8s.io/controller-runtime/pkg/manager"
2527
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
2628

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

102+
namespacesyncOptions := namespacesync.Options{}
103+
98104
// Initialize and parse command line flags.
99105
logs.AddFlags(pflag.CommandLine, logs.SkipLoggingConfigurationFlags())
100106
logsv1.AddFlags(logOptions, pflag.CommandLine)
@@ -104,6 +110,7 @@ func main() {
104110
awsMetaHandlers.AddFlags(pflag.CommandLine)
105111
dockerMetaHandlers.AddFlags(pflag.CommandLine)
106112
nutanixMetaHandlers.AddFlags(pflag.CommandLine)
113+
namespacesyncOptions.AddFlags(pflag.CommandLine)
107114
pflag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)
108115
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
109116
pflag.Parse()
@@ -141,6 +148,32 @@ func main() {
141148
os.Exit(1)
142149
}
143150

151+
unstructuredCachingClient, err := client.New(mgr.GetConfig(), client.Options{
152+
HTTPClient: mgr.GetHTTPClient(),
153+
Cache: &client.CacheOptions{
154+
Reader: mgr.GetCache(),
155+
Unstructured: true,
156+
},
157+
})
158+
if err != nil {
159+
setupLog.Error(err, "unable to create unstructured caching client")
160+
os.Exit(1)
161+
}
162+
163+
if err := (&namespacesync.Reconciler{
164+
Client: mgr.GetClient(),
165+
UnstructuredCachingClient: unstructuredCachingClient,
166+
SourceClusterClassNamespace: namespacesyncOptions.SourceNamespace,
167+
TargetNamespaceFilter: namespacesync.NamespaceHasLabelKey(v1alpha1.NamespaceSyncLabelKey),
168+
}).SetupWithManager(
169+
signalCtx,
170+
mgr,
171+
controller.Options{MaxConcurrentReconciles: namespacesyncOptions.Concurrency},
172+
); err != nil {
173+
setupLog.Error(err, "unable to create controller", "controller", "namespacesync.Reconciler")
174+
os.Exit(1)
175+
}
176+
144177
if err := mgr.Start(signalCtx); err != nil {
145178
setupLog.Error(err, "unable to start controller manager")
146179
os.Exit(1)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2024 Nutanix. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package namespacesync
5+
6+
import (
7+
"github.com/spf13/pflag"
8+
corev1 "k8s.io/api/core/v1"
9+
)
10+
11+
type Options struct {
12+
Concurrency int
13+
SourceNamespace string
14+
}
15+
16+
func (o *Options) AddFlags(flags *pflag.FlagSet) {
17+
pflag.CommandLine.IntVar(
18+
&o.Concurrency,
19+
"namespacesync-concurrency",
20+
10,
21+
"Number of target namespaces to sync concurrently.",
22+
)
23+
24+
pflag.CommandLine.StringVar(
25+
&o.SourceNamespace,
26+
"namespacesync-source-namespace",
27+
corev1.NamespaceDefault, "Namespace from which ClusterClasses and Templates are copied.",
28+
)
29+
}

0 commit comments

Comments
 (0)