Skip to content

Commit

Permalink
feat(cluster-api): implement watch-filter arg
Browse files Browse the repository at this point in the history
  • Loading branch information
sebglon committed Jun 25, 2024
1 parent 1e56a5c commit e53c643
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 14 deletions.
5 changes: 4 additions & 1 deletion controllers/osccluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/predicates"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand All @@ -49,6 +50,7 @@ type OscClusterReconciler struct {
client.Client
Recorder record.EventRecorder
ReconcileTimeout time.Duration
WatchFilterValue string
}

//+kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=oscclusters,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -500,8 +502,9 @@ func (r *OscClusterReconciler) reconcileDelete(ctx context.Context, clusterScope
}

// SetupWithManager sets up the controller with the Manager.
func (r *OscClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *OscClusterReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&infrastructurev1beta1.OscCluster{}).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
Complete(r)
}
2 changes: 2 additions & 0 deletions controllers/oscmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type OscMachineReconciler struct {
client.Client
Recorder record.EventRecorder
ReconcileTimeout time.Duration
WatchFilterValue string
}

//+kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=oscmachines,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -393,6 +394,7 @@ func (r *OscMachineReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Ma
return errors.Errorf("failed to create mapper for Cluster to OscMachines: %+v", err)
}
err = ctrl.NewControllerManagedBy(mgr).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
For(&infrastructurev1beta1.OscMachine{}).
Watches(
&source.Kind{Type: &clusterv1.Machine{}},
Expand Down
3 changes: 3 additions & 0 deletions controllers/oscmachinetemplate_capacity_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/tools/record"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util/predicates"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
Expand All @@ -43,6 +44,7 @@ type OscMachineTemplateReconciler struct {
client.Client
Recorder record.EventRecorder
ReconcileTimeout time.Duration
WatchFilterValue string
}

// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=oscmachinetemplates,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -147,5 +149,6 @@ func (r *OscMachineTemplateReconciler) SetupWithManager(ctx context.Context, mgr
return ctrl.NewControllerManagedBy(mgr).
WithOptions(options).
For(&infrastructurev1beta1.OscMachineTemplate{}).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
Complete(r)
}
13 changes: 13 additions & 0 deletions example/cluster-machine-template-simple.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ metadata:
labels:
cni: "cluster-api-crs-cni"
ccm: "cluster-api-crs-ccm"
cluster.x-k8s.io/watch-filter: "dev"
spec:
clusterNetwork:
pods:
Expand All @@ -26,6 +27,8 @@ kind: OscCluster
metadata:
name: cluster-api
namespace: default
labels:
cluster.x-k8s.io/watch-filter: "dev"
spec:
network:
extraSecurityGroupRule: true
Expand Down Expand Up @@ -237,6 +240,8 @@ kind: MachineDeployment
metadata:
name: "cluster-api-md-0"
namespace: default
labels:
cluster.x-k8s.io/watch-filter: "dev"
spec:
clusterName: "cluster-api"
replicas: 1
Expand All @@ -263,6 +268,8 @@ kind: OscMachineTemplate
metadata:
name: "cluster-api-md-0"
namespace: default
labels:
cluster.x-k8s.io/watch-filter: "dev"
spec:
template:
spec:
Expand Down Expand Up @@ -296,6 +303,8 @@ kind: OscMachineTemplate
metadata:
name: "cluster-api-control-plane"
namespace: default
labels:
cluster.x-k8s.io/watch-filter: "dev"
spec:
template:
spec:
Expand Down Expand Up @@ -329,6 +338,8 @@ kind: KubeadmConfigTemplate
metadata:
name: "cluster-api-md-0"
namespace: default
labels:
cluster.x-k8s.io/watch-filter: "dev"
spec:
template:
spec:
Expand All @@ -355,6 +366,8 @@ kind: KubeadmControlPlane
apiVersion: controlplane.cluster.x-k8s.io/v1beta1
metadata:
name: "cluster-api-control-plane"
labels:
cluster.x-k8s.io/watch-filter: "dev"
spec:
replicas: 1
machineTemplate:
Expand Down
2 changes: 1 addition & 1 deletion helm/clusterapioutscale/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ version: 0.1.1
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "1.16.0"
appVersion: "1.17.0"
5 changes: 4 additions & 1 deletion helm/clusterapioutscale/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ spec:
- --upstream=http://127.0.0.1:8080/
- --logtostderr=true
- --v={{ .verbosity }}
{{- if .watch_filter }}
- --watch-filter {{ .watch_filter }}
{{- end}}
image: {{ .containers.imageProxy }}:{{ .containers.imageProxyTag }}
name: kube-rbac-proxy
ports:
Expand Down Expand Up @@ -80,7 +83,7 @@ spec:
secretKeyRef:
key: region
name: cluster-api-provider-outscale
optional: true
optional: true
- name: BACKOFF_DURATION
value: "{{ .backoff_duration }}"
- name: BACKOFF_FACTOR
Expand Down
1 change: 1 addition & 0 deletions helm/clusterapioutscale/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ clusterapioutscale:
# integer defining the remaining number of iterations in which the duration parameter may change
# -- Remaining number of iterations in which the duration parameter may change
backoff_steps: "20"
watch_filter: ""
# -- Additional annotions to use
pods:
annotations:
Expand Down
13 changes: 10 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"flag"
"fmt"
"os"
"time"

Expand Down Expand Up @@ -59,11 +60,14 @@ func main() {
var metricsAddr string
var enableLeaderElection bool
var probeAddr string
var watchFilterValue string
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", true,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.StringVar(&watchFilterValue, "watch-filter", "", fmt.Sprintf("Label value that the controller watches to reconcile cluster-api objects. Label key is always %s. If unspecified, the controller watches for all cluster-api objects.", clusterv1.WatchLabel))

opts := zap.Options{}
opts.BindFlags(flag.CommandLine)
flag.Parse()
Expand All @@ -88,21 +92,23 @@ func main() {
os.Exit(1)
}

ctx := ctrl.SetupSignalHandler()

if err = (&controllers.OscClusterReconciler{
Client: mgr.GetClient(),
Recorder: mgr.GetEventRecorderFor("osccluster-controller"),
ReconcileTimeout: reconcileTimeout,
}).SetupWithManager(mgr); err != nil {
WatchFilterValue: watchFilterValue,
}).SetupWithManager(ctx, mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "OscCluster")
os.Exit(1)
}

ctx := ctrl.SetupSignalHandler()

if err = (&controllers.OscMachineReconciler{
Client: mgr.GetClient(),
Recorder: mgr.GetEventRecorderFor("oscmachine-controller"),
ReconcileTimeout: reconcileTimeout,
WatchFilterValue: watchFilterValue,
}).SetupWithManager(ctx, mgr, controller.Options{}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "OscMachine")
os.Exit(1)
Expand All @@ -112,6 +118,7 @@ func main() {
Client: mgr.GetClient(),
Recorder: mgr.GetEventRecorderFor("oscmachinetemplate-controller"),
ReconcileTimeout: reconcileTimeout,
WatchFilterValue: watchFilterValue,
}).SetupWithManager(ctx, mgr, controller.Options{}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "OscMachineTemplate")
os.Exit(1)
Expand Down
12 changes: 8 additions & 4 deletions testclean/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,22 @@ package test

import (
"flag"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/outscale-dev/cluster-api-provider-outscale.git/controllers"
"golang.org/x/net/context"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
"k8s.io/klog/v2/klogr"

// "os"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"path/filepath"
"testing"
"time"

utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
"sigs.k8s.io/cluster-api/test/framework/ginkgoextensions"
Expand All @@ -36,8 +42,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/envtest"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"testing"
"time"

infrastructurev1beta1 "github.com/outscale-dev/cluster-api-provider-outscale.git/api/v1beta1"
//+kubebuilder:scaffold:imports
Expand Down Expand Up @@ -107,7 +111,7 @@ var _ = BeforeSuite(func() {
Client: k8sManager.GetClient(),
Recorder: k8sManager.GetEventRecorderFor("osc-controller"),
ReconcileTimeout: reconcileTimeout,
}).SetupWithManager(k8sManager)
}).SetupWithManager(context.Background(), k8sManager)

go func() {
defer GinkgoRecover()
Expand Down
10 changes: 6 additions & 4 deletions testenv/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,26 @@ limitations under the License.
package test

import (
"path/filepath"
"testing"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/outscale-dev/cluster-api-provider-outscale.git/controllers"
"golang.org/x/net/context"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
"k8s.io/klog/v2/klogr"
"path/filepath"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/test/framework/ginkgoextensions"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"testing"
"time"

infrastructurev1beta1 "github.com/outscale-dev/cluster-api-provider-outscale.git/api/v1beta1"
//+kubebuilder:scaffold:imports
Expand Down Expand Up @@ -106,7 +108,7 @@ var _ = BeforeSuite(func() {
Client: k8sManager.GetClient(),
Recorder: k8sManager.GetEventRecorderFor("osc-controller"),
ReconcileTimeout: reconcileTimeout,
}).SetupWithManager(k8sManager)
}).SetupWithManager(context.Background(), k8sManager)

go func() {
defer GinkgoRecover()
Expand Down

0 comments on commit e53c643

Please sign in to comment.