Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only reconcile ip objects with specific label #116

Merged
merged 2 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion controllers/controllers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ var _ = BeforeSuite(func() {
Expect(err).NotTo(HaveOccurred())
Expect(mgr).NotTo(BeNil())

ipReconciler, err = NewIPReconciler("")
ipReconciler, err = NewIPReconciler("", "", "")
Expect(err).NotTo(HaveOccurred())
Expect(ipReconciler.SetupWithManager(mgr)).To(Succeed())

Expand Down
35 changes: 28 additions & 7 deletions controllers/ip_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,23 @@ import (
//+kubebuilder:rbac:groups=ipam.onmetal.de,resources=ips,verbs=get;list;watch
//+kubebuilder:rbac:groups=ipam.onmetal.de,resources=ips/status,verbs=get

func NewIPReconciler(namespace string) (*IPReconciler, error) {
func NewIPReconciler(namespace string, subnetLabelName string, subnetLabelValue string) (*IPReconciler, error) {
return &IPReconciler{
namespace: namespace,
namespace: namespace,
subnetLabelName: subnetLabelName,
subnetLabelValue: subnetLabelValue,
}, nil
}

// IPReconciler reconciles a IP object.
type IPReconciler struct {
client.Client
namespace string
disabled bool
disabledMtx sync.RWMutex
macRegex *regexp.Regexp
namespace string
subnetLabelName string
subnetLabelValue string
disabled bool
disabledMtx sync.RWMutex
macRegex *regexp.Regexp
}

func (r *IPReconciler) enable() {
Expand Down Expand Up @@ -268,5 +272,22 @@ func (r *IPReconciler) SetupWithManager(mgr ctrl.Manager) error {
},
}

return ctrl.NewControllerManagedBy(mgr).For(&ipamv1alpha1.IP{}).WithEventFilter(predicate.And(predicate.GenerationChangedPredicate{}, inCorrectNamespacePredicate, notBeingDeletedPredicate, validPredicate)).WithOptions(controller.Options{MaxConcurrentReconciles: 10}).Complete(r)
hasValidLabelPredicate := predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
if r.subnetLabelName != "" {
l, ok := e.Object.GetLabels()[r.subnetLabelName]
return ok && l == r.subnetLabelValue
}
return true
},
UpdateFunc: func(e event.UpdateEvent) bool {
if r.subnetLabelName != "" {
l, ok := e.ObjectNew.GetLabels()[r.subnetLabelName]
return ok && l == r.subnetLabelValue
}
return true
},
}

return ctrl.NewControllerManagedBy(mgr).For(&ipamv1alpha1.IP{}).WithEventFilter(predicate.And(predicate.GenerationChangedPredicate{}, inCorrectNamespacePredicate, notBeingDeletedPredicate, validPredicate, hasValidLabelPredicate)).WithOptions(controller.Options{MaxConcurrentReconciles: 10}).Complete(r)
}
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ type params struct {
shutdownTimeout time.Duration
consoleServerCert string
consoleServerKey string
subnetLabelName string
subnetLabelValue string
}

func parseCmdLine() params {
Expand All @@ -91,6 +93,8 @@ func parseCmdLine() params {
pflag.Duration("shutdown-timeout", 5*time.Minute, "Wait this long before issuing an immediate shutdown, if graceful shutdown has not succeeded. See https://golang.org/pkg/time/#ParseDuration")
pflag.String("console-server-cert", "", "Use a TLS certificate for the console server. If not set, do not start the console server.")
pflag.String("console-server-key", "", "Use a TLS key for the console server. If not set, do not start the console server.")
pflag.String("subnet-label-name", "", "Label name used to filter ip.ipam objects. Not specifying it will reconcile ALL ip.ipam objects.")
pflag.String("subnet-label-value", "", "Label value used to filter ip.ipam objects.")

var help bool
pflag.BoolVarP(&help, "help", "h", false, "Show this help message.")
Expand Down Expand Up @@ -120,6 +124,8 @@ func parseCmdLine() params {
shutdownTimeout: viper.GetDuration("shutdown-timeout"),
consoleServerCert: viper.GetString("console-server-cert"),
consoleServerKey: viper.GetString("console-server-key"),
subnetLabelName: viper.GetString("subnet-label-name"),
subnetLabelValue: viper.GetString("subnet-label-value"),
}
}

Expand Down Expand Up @@ -205,7 +211,7 @@ func main() {
}

var ipReconciler *controllers.IPReconciler
ipReconciler, err = controllers.NewIPReconciler(p.namespace)
ipReconciler, err = controllers.NewIPReconciler(p.namespace, p.subnetLabelName, p.subnetLabelValue)
if err != nil {
log.Error(ctx, fmt.Errorf("cannot create controller: %w", err), "controller", "IP")
exitCode = 1
Expand Down
Loading