Skip to content

Commit 63615c0

Browse files
authored
feat: dynamic infrastructure clusters patching (#167)
* fix: setting up logger asap for flags errors Signed-off-by: Dario Tranchitella <[email protected]> * feat: dynamic infrastructure clusters patching Signed-off-by: Dario Tranchitella <[email protected]> * chore(kustomize): dynamic infrastructure clusters patching Signed-off-by: Dario Tranchitella <[email protected]> --------- Signed-off-by: Dario Tranchitella <[email protected]>
1 parent e85f4cb commit 63615c0

6 files changed

+48
-18
lines changed

config/control-plane-components.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -13479,7 +13479,8 @@ spec:
1347913479
containers:
1348013480
- args:
1348113481
- --leader-elect
13482-
- --feature-gates=ExternalClusterReference=${CACPPK_EXTERNAL_CLUSTER_REFERENCE:=false},ExternalClusterReferenceCrossNamespace=${CACPPK_EXTERNAL_CLUSTER_REFERENCE_CROSS_NAMESPACE:=false},SkipInfraClusterPatch=${CACPPK_SKIP_INFRA_CLUSTER_PATCH:=false}
13482+
- --feature-gates=DynamicInfrastructureClusterPatch=${CACPPK_DYNAMIC_INFRASTRUCTURE_CLUSTER_PATCH:=false},ExternalClusterReference=${CACPPK_EXTERNAL_CLUSTER_REFERENCE:=false},ExternalClusterReferenceCrossNamespace=${CACPPK_EXTERNAL_CLUSTER_REFERENCE_CROSS_NAMESPACE:=false},SkipInfraClusterPatch=${CACPPK_SKIP_INFRA_CLUSTER_PATCH:=false}
13483+
- --dynamic-infrastructure-clusters=${CACPPK_INFRASTRUCTURE_CLUSTERS:= }
1348313484
command:
1348413485
- /manager
1348513486
image: docker.io/clastix/cluster-api-control-plane-provider-kamaji:v0.13.0

config/manager/manager.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ spec:
7070
- /manager
7171
args:
7272
- --leader-elect
73-
- "--feature-gates=ExternalClusterReference=${CACPPK_EXTERNAL_CLUSTER_REFERENCE:=false},ExternalClusterReferenceCrossNamespace=${CACPPK_EXTERNAL_CLUSTER_REFERENCE_CROSS_NAMESPACE:=false},SkipInfraClusterPatch=${CACPPK_SKIP_INFRA_CLUSTER_PATCH:=false}"
73+
- "--feature-gates=DynamicInfrastructureClusterPatch=${CACPPK_DYNAMIC_INFRASTRUCTURE_CLUSTER_PATCH:=false},ExternalClusterReference=${CACPPK_EXTERNAL_CLUSTER_REFERENCE:=false},ExternalClusterReferenceCrossNamespace=${CACPPK_EXTERNAL_CLUSTER_REFERENCE_CROSS_NAMESPACE:=false},SkipInfraClusterPatch=${CACPPK_SKIP_INFRA_CLUSTER_PATCH:=false}"
74+
- "--dynamic-infrastructure-clusters=${CACPPK_INFRASTRUCTURE_CLUSTERS:= }"
7475
image: controller:latest
7576
name: manager
7677
securityContext:

controllers/kamajicontrolplane_controller.go

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"k8s.io/apimachinery/pkg/api/errors"
1515
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1616
"k8s.io/apimachinery/pkg/types"
17+
"k8s.io/apimachinery/pkg/util/sets"
1718
"k8s.io/client-go/kubernetes"
1819
"k8s.io/client-go/util/retry"
1920
"k8s.io/component-base/featuregate"
@@ -40,6 +41,7 @@ type KamajiControlPlaneReconciler struct {
4041
ExternalClusterReferenceStore externalclusterreference.Store
4142
FeatureGates featuregate.FeatureGate
4243
MaxConcurrentReconciles int
44+
DynamicInfrastructureClusters sets.Set[string]
4345

4446
client client.Client
4547
}

controllers/kamajicontrolplane_controller_cluster_patch.go

+4
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ func (r *KamajiControlPlaneReconciler) patchCluster(ctx context.Context, cluster
110110
case "VSphereCluster":
111111
return r.checkOrPatchGenericCluster(ctx, cluster, endpoint, port)
112112
default:
113+
if r.DynamicInfrastructureClusters.Has(cluster.Spec.InfrastructureRef.Kind) {
114+
return r.patchGenericCluster(ctx, cluster, endpoint, port, false)
115+
}
116+
113117
return errors.New("unsupported infrastructure provider")
114118
}
115119
}

main.go

+34-16
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import (
88
"os"
99

1010
kamajiv1alpha1 "github.com/clastix/kamaji/api/v1alpha1"
11+
"github.com/pkg/errors"
1112
"github.com/spf13/pflag"
1213
"k8s.io/apimachinery/pkg/runtime"
1314
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
15+
"k8s.io/apimachinery/pkg/util/sets"
1416
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
1517
_ "k8s.io/client-go/plugin/pkg/client/auth"
1618
"k8s.io/client-go/rest"
@@ -46,10 +48,31 @@ func init() {
4648

4749
//nolint:funlen,cyclop
4850
func main() {
51+
var dynamicInfraClusters []string
52+
4953
metricsAddr, enableLeaderElection, probeAddr, maxConcurrentReconciles := "", false, "", 1
5054

5155
flagSet := pflag.NewFlagSet("kamaji-control-plane-provider", pflag.ExitOnError)
5256

57+
flagSet.StringSliceVar(&dynamicInfraClusters, "dynamic-infrastructure-clusters", nil, "When the DynamicInfrastructureClusterPatch feature flag is enabled, "+
58+
"allows specifying which Infrastructure Clusters can be dynamically patched. "+
59+
"This feature is useful for developers of custom or non public Cluster API infrastructure providers.")
60+
flagSet.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
61+
flagSet.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
62+
flagSet.BoolVar(&enableLeaderElection, "leader-elect", false,
63+
"Enable leader election for controller manager. "+
64+
"Enabling this will ensure there is only one active controller manager.")
65+
flagSet.IntVar(&maxConcurrentReconciles, "max-concurrent-reconciles", 1, "The maximum number of concurrent KamajiControlPlane reconciles which can be run")
66+
// zap logging FlagSet
67+
var goFlagSet flag.FlagSet
68+
69+
opts := zap.Options{Development: true}
70+
opts.BindFlags(&goFlagSet)
71+
72+
flagSet.AddGoFlagSet(&goFlagSet)
73+
74+
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
75+
5376
featureGate := featuregate.NewFeatureGate()
5477

5578
if err := featureGate.Add(map[featuregate.Feature]featuregate.FeatureSpec{
@@ -68,35 +91,29 @@ func main() {
6891
LockToDefault: false,
6992
PreRelease: featuregate.Alpha,
7093
},
94+
features.DynamicInfrastructureClusterPatch: {
95+
Default: false,
96+
LockToDefault: false,
97+
PreRelease: featuregate.Alpha,
98+
},
7199
}); err != nil {
72100
setupLog.Error(err, "unable to add feature gates")
73101
os.Exit(1)
74102
}
75103

76104
featureGate.AddFlag(flagSet)
77105

78-
flagSet.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
79-
flagSet.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
80-
flagSet.BoolVar(&enableLeaderElection, "leader-elect", false,
81-
"Enable leader election for controller manager. "+
82-
"Enabling this will ensure there is only one active controller manager.")
83-
flagSet.IntVar(&maxConcurrentReconciles, "max-concurrent-reconciles", 1, "The maximum number of concurrent KamajiControlPlane reconciles which can be run")
84-
// zap logging FlagSet
85-
var goFlagSet flag.FlagSet
86-
87-
opts := zap.Options{Development: true}
88-
opts.BindFlags(&goFlagSet)
89-
90-
flagSet.AddGoFlagSet(&goFlagSet)
91-
92106
if err := flagSet.Parse(os.Args[1:]); err != nil {
93107
setupLog.Error(err, "unable to parse arguments")
94108
os.Exit(1)
95109
}
96110

97-
ctx := ctrl.SetupSignalHandler()
111+
if !featureGate.Enabled(features.DynamicInfrastructureClusterPatch) && len(dynamicInfraClusters) > 0 {
112+
setupLog.Error(errors.New("cannot set dynamic infrastructure clusters when the feature flag is disabled"), "DynamicInfrastructureClusterPatch feature flag is disabled")
113+
os.Exit(1)
114+
}
98115

99-
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
116+
ctx := ctrl.SetupSignalHandler()
100117

101118
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
102119
Scheme: scheme,
@@ -126,6 +143,7 @@ func main() {
126143
ExternalClusterReferenceStore: ecrStore,
127144
FeatureGates: featureGate,
128145
MaxConcurrentReconciles: maxConcurrentReconciles,
146+
DynamicInfrastructureClusters: sets.New[string](dynamicInfraClusters...),
129147
}).SetupWithManager(ctx, mgr, triggerChannel); err != nil {
130148
setupLog.Error(err, "unable to create controller", "controller", "KamajiControlPlane")
131149
os.Exit(1)

pkg/features/features.go

+4
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@ const (
1414

1515
// SkipInfraClusterPatch bypasses patching the InfraCluster with the control-plane endpoint.
1616
SkipInfraClusterPatch = "SkipInfraClusterPatch"
17+
18+
// DynamicInfrastructureClusterPatch allows patching any generic InfraCluster with the control-plane endpoint
19+
// provided by Kamaji.
20+
DynamicInfrastructureClusterPatch = "DynamicInfrastructureClusterPatch"
1721
)

0 commit comments

Comments
 (0)