Skip to content

Commit 4036a73

Browse files
authored
✨ create default resources in code; block spoke delete until gone (#82)
* feat: create default resources in code; block spoke deletion until MC is gone Signed-off-by: Artur Shad Nik <arturshadnik@gmail.com> * chore: bump chart Signed-off-by: Artur Shad Nik <arturshadnik@gmail.com> --------- Signed-off-by: Artur Shad Nik <arturshadnik@gmail.com>
1 parent 3c4948d commit 4036a73

File tree

10 files changed

+271
-111
lines changed

10 files changed

+271
-111
lines changed

fleetconfig-controller/api/v1beta1/constants.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,27 @@ const (
222222
// BundleVersionDefault is the default OCM source version
223223
BundleVersionDefault = "default"
224224
)
225+
226+
// Topology resource names
227+
const (
228+
// NamespaceManagedClusterSetGlobal is the namespace for the global managed cluster set
229+
NamespaceManagedClusterSetGlobal = "managed-cluster-set-global"
230+
231+
// NamespaceManagedClusterSetDefault is the namespace for the default managed cluster set
232+
NamespaceManagedClusterSetDefault = "managed-cluster-set-default"
233+
234+
// NamespaceManagedClusterSetSpokes is the namespace for the spokes managed cluster set
235+
NamespaceManagedClusterSetSpokes = "managed-cluster-set-spokes"
236+
237+
// ManagedClusterSetGlobal is the name of the global managed cluster set
238+
ManagedClusterSetGlobal = "global"
239+
240+
// ManagedClusterSetDefault is the name of the default managed cluster set
241+
ManagedClusterSetDefault = "default"
242+
243+
// ManagedClusterSetSpokes is the name of the spokes managed cluster set
244+
ManagedClusterSetSpokes = "spokes"
245+
246+
// PlacementSpokes is the name of the spokes placement
247+
PlacementSpokes = "spokes"
248+
)

fleetconfig-controller/charts/fleetconfig-controller/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ Resource specifications for all klusterlet-managed containers.
143143

144144
### Topology Resources
145145

146-
| Name | Description | Value |
147-
| --------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------ |
148-
| `topologyResources.enabled` | Whether to create Namespaces and ManagedClusterSetBindings for the default ManagedClusterSets created when a FleetConfig is created with the DefaultClusterSet feature gate enabled. Additionally, a Namespace, ManagedClusterSet, and Placement are created for targeting all managed clusters that are not the hub running in hub-as-spoke mode. | `true` |
146+
| Name | Description | Value |
147+
| --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------ |
148+
| `topologyResources.enabled` | Whether to create Namespaces and ManagedClusterSetBindings for the default ManagedClusterSets created when a Hub is created with the DefaultClusterSet feature gate enabled. Additionally, a Namespace, ManagedClusterSet, and Placement are created for targeting all managed clusters that are not the hub running in hub-as-spoke mode. | `true` |
149149

150150
### fleetconfig-controller parameters
151151

@@ -158,7 +158,7 @@ Resource specifications for all klusterlet-managed containers.
158158
| `replicas` | fleetconfig-controller replica count | `1` |
159159
| `imageRegistry` | Image registry | `""` |
160160
| `image.repository` | Image repository | `quay.io/open-cluster-management/fleetconfig-controller` |
161-
| `image.tag` | Image tag | `v0.1.4` |
161+
| `image.tag` | Image tag | `v0.1.5` |
162162
| `image.pullPolicy` | Image pull policy | `IfNotPresent` |
163163
| `imagePullSecrets` | Image pull secrets | `[]` |
164164
| `serviceAccount.annotations` | Annotations to add to the service account | `{}` |

fleetconfig-controller/charts/fleetconfig-controller/templates/deployment.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ spec:
4444
- "--instance-type=unified"
4545
{{- end }}
4646
- "--enable-legacy-controllers={{ .Values.enableLegacyControllers | default false }}"
47+
- "--enable-topology-resources={{ .Values.topologyResources.enabled | default true }}"
4748
command:
4849
- /manager
4950
env:

fleetconfig-controller/charts/fleetconfig-controller/templates/ocm/topology-resources.yaml

Lines changed: 0 additions & 82 deletions
This file was deleted.

fleetconfig-controller/charts/fleetconfig-controller/values.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ fleetConfig:
265265

266266
## @section Topology Resources
267267

268-
## @param topologyResources.enabled Whether to create Namespaces and ManagedClusterSetBindings for the default ManagedClusterSets created when a FleetConfig is created with the DefaultClusterSet feature gate enabled. Additionally, a Namespace, ManagedClusterSet, and Placement are created for targeting all managed clusters that are not the hub running in hub-as-spoke mode.
268+
## @param topologyResources.enabled Whether to create Namespaces and ManagedClusterSetBindings for the default ManagedClusterSets created when a Hub is created with the DefaultClusterSet feature gate enabled. Additionally, a Namespace, ManagedClusterSet, and Placement are created for targeting all managed clusters that are not the hub running in hub-as-spoke mode.
269269
topologyResources:
270270
enabled: true
271271

@@ -294,7 +294,7 @@ imageRegistry: ""
294294
## @param image.pullPolicy Image pull policy
295295
image:
296296
repository: quay.io/open-cluster-management/fleetconfig-controller
297-
tag: v0.1.4
297+
tag: v0.1.5
298298
pullPolicy: IfNotPresent
299299

300300
## @param imagePullSecrets Image pull secrets

fleetconfig-controller/cmd/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import (
3636
apiv1alpha1 "github.com/open-cluster-management-io/lab/fleetconfig-controller/api/v1alpha1"
3737
apiv1beta1 "github.com/open-cluster-management-io/lab/fleetconfig-controller/api/v1beta1"
3838
"github.com/open-cluster-management-io/lab/fleetconfig-controller/cmd/manager"
39+
clusterv1beta1 "open-cluster-management.io/api/cluster/v1beta1"
40+
clusterv1beta2 "open-cluster-management.io/api/cluster/v1beta2"
3941
// +kubebuilder:scaffold:imports
4042
)
4143

@@ -49,6 +51,8 @@ func init() {
4951

5052
utilruntime.Must(apiv1alpha1.AddToScheme(scheme))
5153
utilruntime.Must(apiv1beta1.AddToScheme(scheme))
54+
utilruntime.Must(clusterv1beta1.AddToScheme(scheme))
55+
utilruntime.Must(clusterv1beta2.AddToScheme(scheme))
5256
// +kubebuilder:scaffold:scheme
5357
}
5458

@@ -70,6 +74,7 @@ func main() {
7074
flag.IntVar(&mOpts.SpokeConcurrentReconciles, "spoke-concurrent-reconciles", apiv1beta1.SpokeDefaultMaxConcurrentReconciles, fmt.Sprintf("Maximum number of Spoke resources that may be reconciled in parallel. Defaults to %d.", apiv1beta1.SpokeDefaultMaxConcurrentReconciles))
7175
flag.StringVar(&mOpts.InstanceType, "instance-type", apiv1beta1.InstanceTypeManager, fmt.Sprintf("The type of cluster that this controller instance is installed in. Defaults to %s", apiv1beta1.InstanceTypeManager))
7276
flag.BoolVar(&mOpts.EnableLegacyControllers, "enable-legacy-controllers", false, "Enable legacy FleetConfig resource and controllers")
77+
flag.BoolVar(&mOpts.EnableTopologyResources, "enable-topology-resources", true, "Enable automatic creation of topology resources (namespaces, cluster sets, bindings, and placements)")
7378

7479
zOpts := zap.Options{
7580
Development: true,

fleetconfig-controller/cmd/manager/manager.go

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package manager
33

44
import (
55
"bytes"
6+
"context"
67
"crypto/tls"
78
"fmt"
89
"os"
@@ -17,6 +18,7 @@ import (
1718
ctrl "sigs.k8s.io/controller-runtime"
1819
"sigs.k8s.io/controller-runtime/pkg/cache"
1920
"sigs.k8s.io/controller-runtime/pkg/client"
21+
"sigs.k8s.io/controller-runtime/pkg/manager"
2022
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
2123
"sigs.k8s.io/controller-runtime/pkg/webhook"
2224

@@ -42,6 +44,7 @@ type Options struct {
4244
SpokeConcurrentReconciles int
4345
InstanceType string
4446
EnableLegacyControllers bool
47+
EnableTopologyResources bool
4548
Scheme *runtime.Scheme
4649
}
4750

@@ -100,34 +103,50 @@ func ForHub(setupLog logr.Logger, opts Options) (ctrl.Manager, error) {
100103
return nil, err
101104
}
102105

103-
if err := (&controllerv1beta1.HubReconciler{
106+
if opts.EnableTopologyResources {
107+
setupLog.Info("creating topology resources")
108+
// Wait for cache to sync before setting up controllers
109+
if err = mgr.Add(manager.RunnableFunc(func(ctx context.Context) error {
110+
setupLog.Info("waiting for cache to sync")
111+
if !mgr.GetCache().WaitForCacheSync(ctx) {
112+
err = fmt.Errorf("cache sync failed")
113+
setupLog.Error(err, "failed to sync cache")
114+
return ctx.Err()
115+
}
116+
117+
setupLog.Info("cache synced successfully")
118+
if err := createTopologyResources(ctx, mgr.GetClient(), setupLog); err != nil {
119+
setupLog.Error(err, "failed to create topology resources at startup")
120+
return err
121+
}
122+
setupLog.Info("topology resources created successfully")
123+
return nil
124+
})); err != nil {
125+
return nil, err
126+
}
127+
}
128+
129+
hubReconciler := &controllerv1beta1.HubReconciler{
104130
Client: mgr.GetClient(),
105131
Log: ctrl.Log.WithName("controllers").WithName("Hub"),
106132
Scheme: mgr.GetScheme(),
107-
}).SetupWithManager(mgr); err != nil {
108-
setupLog.Error(err, "unable to create controller", "controller", "Hub")
109-
return nil, err
110133
}
111-
if err := (&controllerv1beta1.SpokeReconciler{
134+
135+
spokeReconciler := &controllerv1beta1.SpokeReconciler{
112136
Client: mgr.GetClient(),
113137
Log: ctrl.Log.WithName("controllers").WithName("Spoke"),
114138
ConcurrentReconciles: opts.SpokeConcurrentReconciles,
115139
Scheme: mgr.GetScheme(),
116140
InstanceType: opts.InstanceType,
117-
}).SetupWithManagerForHub(mgr); err != nil {
118-
setupLog.Error(err, "unable to create controller", "controller", "Spoke")
141+
}
142+
if err := hubReconciler.SetupWithManager(mgr); err != nil {
143+
setupLog.Error(err, "unable to create controller", "controller", "Hub")
119144
return nil, err
120145
}
121146

122-
if opts.EnableLegacyControllers {
123-
if err = (&controllerv1alpha1.FleetConfigReconciler{
124-
Client: mgr.GetClient(),
125-
Log: ctrl.Log.WithName("controllers").WithName("FleetConfig"),
126-
Scheme: mgr.GetScheme(),
127-
}).SetupWithManager(mgr); err != nil {
128-
setupLog.Error(err, "unable to create controller", "controller", "FleetConfig")
129-
return nil, err
130-
}
147+
if err := spokeReconciler.SetupWithManagerForHub(mgr); err != nil {
148+
setupLog.Error(err, "unable to create controller", "controller", "Spoke")
149+
return nil, err
131150
}
132151

133152
// nolint:goconst
@@ -140,13 +159,24 @@ func ForHub(setupLog logr.Logger, opts Options) (ctrl.Manager, error) {
140159
setupLog.Error(err, "unable to create webhook", "webhook", "Spoke")
141160
return nil, err
142161
}
143-
if opts.EnableLegacyControllers {
144-
if err = apiv1alpha1.SetupFleetConfigWebhookWithManager(mgr); err != nil {
145-
setupLog.Error(err, "unable to create webhook", "webhook", "FleetConfig")
146-
return nil, err
147-
}
162+
}
163+
164+
if opts.EnableLegacyControllers {
165+
fleetConfigReconciler := &controllerv1alpha1.FleetConfigReconciler{
166+
Client: mgr.GetClient(),
167+
Log: ctrl.Log.WithName("controllers").WithName("FleetConfig"),
168+
Scheme: mgr.GetScheme(),
169+
}
170+
if err = fleetConfigReconciler.SetupWithManager(mgr); err != nil {
171+
setupLog.Error(err, "unable to create controller", "controller", "FleetConfig")
172+
return nil, err
173+
}
174+
if err = apiv1alpha1.SetupFleetConfigWebhookWithManager(mgr); err != nil {
175+
setupLog.Error(err, "unable to create webhook", "webhook", "FleetConfig")
176+
return nil, err
148177
}
149178
}
179+
150180
return mgr, nil
151181
}
152182

0 commit comments

Comments
 (0)