Skip to content

Commit 9f66c4b

Browse files
committed
feat: Optionally enable namespacesync using a flag
1 parent 2e5a2b9 commit 9f66c4b

File tree

2 files changed

+39
-25
lines changed

2 files changed

+39
-25
lines changed

cmd/main.go

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -147,30 +147,37 @@ func main() {
147147
os.Exit(1)
148148
}
149149

150-
unstructuredCachingClient, err := client.New(mgr.GetConfig(), client.Options{
151-
HTTPClient: mgr.GetHTTPClient(),
152-
Cache: &client.CacheOptions{
153-
Reader: mgr.GetCache(),
154-
Unstructured: true,
155-
},
156-
})
157-
if err != nil {
158-
setupLog.Error(err, "unable to create unstructured caching client")
159-
os.Exit(1)
160-
}
161-
162-
if err := (&namespacesync.Reconciler{
163-
Client: mgr.GetClient(),
164-
UnstructuredCachingClient: unstructuredCachingClient,
165-
SourceClusterClassNamespace: namespacesyncOptions.SourceNamespace,
166-
TargetNamespaceFilter: namespacesync.NamespaceHasLabelKey(namespacesyncOptions.TargetNamespaceLabelKey),
167-
}).SetupWithManager(
168-
signalCtx,
169-
mgr,
170-
controller.Options{MaxConcurrentReconciles: namespacesyncOptions.Concurrency},
171-
); err != nil {
172-
setupLog.Error(err, "unable to create controller", "controller", "namespacesync.Reconciler")
173-
os.Exit(1)
150+
if namespacesyncOptions.Enabled {
151+
if namespacesyncOptions.SourceNamespace == "" || namespacesyncOptions.TargetNamespaceLabelKey == "" {
152+
setupLog.Error(nil, "Namespace Sync is enabled, but source namespace and/or target namespace label key are not configured.") //nolint:lll // Usage string is wrapped in the terminal.
153+
os.Exit(1)
154+
}
155+
156+
unstructuredCachingClient, err := client.New(mgr.GetConfig(), client.Options{
157+
HTTPClient: mgr.GetHTTPClient(),
158+
Cache: &client.CacheOptions{
159+
Reader: mgr.GetCache(),
160+
Unstructured: true,
161+
},
162+
})
163+
if err != nil {
164+
setupLog.Error(err, "unable to create unstructured caching client")
165+
os.Exit(1)
166+
}
167+
168+
if err := (&namespacesync.Reconciler{
169+
Client: mgr.GetClient(),
170+
UnstructuredCachingClient: unstructuredCachingClient,
171+
SourceClusterClassNamespace: namespacesyncOptions.SourceNamespace,
172+
TargetNamespaceFilter: namespacesync.NamespaceHasLabelKey(namespacesyncOptions.TargetNamespaceLabelKey),
173+
}).SetupWithManager(
174+
signalCtx,
175+
mgr,
176+
controller.Options{MaxConcurrentReconciles: namespacesyncOptions.Concurrency},
177+
); err != nil {
178+
setupLog.Error(err, "unable to create controller", "controller", "namespacesync.Reconciler")
179+
os.Exit(1)
180+
}
174181
}
175182

176183
if err := mgr.Start(signalCtx); err != nil {

pkg/controllers/namespacesync/flags.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,23 @@ package namespacesync
55

66
import (
77
"github.com/spf13/pflag"
8-
corev1 "k8s.io/api/core/v1"
98
)
109

1110
type Options struct {
11+
Enabled bool
1212
Concurrency int
1313
SourceNamespace string
1414
TargetNamespaceLabelKey string
1515
}
1616

1717
func (o *Options) AddFlags(flags *pflag.FlagSet) {
18+
pflag.CommandLine.BoolVar(
19+
&o.Enabled,
20+
"namespacesync-enabled",
21+
false,
22+
"Enable copying of ClusterClasses and Templates from a source namespace to one or more target namespaces.",
23+
)
24+
1825
pflag.CommandLine.IntVar(
1926
&o.Concurrency,
2027
"namespacesync-concurrency",

0 commit comments

Comments
 (0)