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

[CIDRPool 2/x] Add controller for CIDRPool CRD #42

Merged
merged 2 commits into from
Jun 11, 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
61 changes: 61 additions & 0 deletions api/v1alpha1/cidrpool_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2024, NVIDIA CORPORATION & AFFILIATES
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logPkg "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

var cidrPoolLogger = logPkg.Log.WithName("CIDRPool-validator")

// SetupWebhookWithManager registers webhook handler in the manager
func (r *CIDRPool) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}

var _ webhook.Validator = &CIDRPool{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *CIDRPool) ValidateCreate() (admission.Warnings, error) {
cidrPoolLogger.V(1).Info("validate create", "name", r.Name)
return r.validate()
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *CIDRPool) ValidateUpdate(_ runtime.Object) (admission.Warnings, error) {
cidrPoolLogger.V(1).Info("validate update", "name", r.Name)
return r.validate()
}

func (r *CIDRPool) validate() (admission.Warnings, error) {
errList := r.Validate()
if len(errList) == 0 {
cidrPoolLogger.V(1).Info("validation succeed")
return nil, nil
}
err := errList.ToAggregate()
cidrPoolLogger.V(1).Info("validation failed", "reason", err.Error())
return nil, err
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *CIDRPool) ValidateDelete() (admission.Warnings, error) {
return nil, nil
}
36 changes: 26 additions & 10 deletions cmd/ipam-controller/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ import (
ipamv1alpha1 "github.com/Mellanox/nvidia-k8s-ipam/api/v1alpha1"
"github.com/Mellanox/nvidia-k8s-ipam/cmd/ipam-controller/app/options"
"github.com/Mellanox/nvidia-k8s-ipam/pkg/common"
poolctrl "github.com/Mellanox/nvidia-k8s-ipam/pkg/ipam-controller/controllers/ippool"
cidrpoolctrl "github.com/Mellanox/nvidia-k8s-ipam/pkg/ipam-controller/controllers/cidrpool"
ippoolctrl "github.com/Mellanox/nvidia-k8s-ipam/pkg/ipam-controller/controllers/ippool"
nodectrl "github.com/Mellanox/nvidia-k8s-ipam/pkg/ipam-controller/controllers/node"
"github.com/Mellanox/nvidia-k8s-ipam/pkg/ipam-controller/migrator"
"github.com/Mellanox/nvidia-k8s-ipam/pkg/version"
Expand Down Expand Up @@ -150,14 +151,16 @@ func RunController(ctx context.Context, config *rest.Config, opts *options.Optio
os.Exit(1)
}

nodeEventCH := make(chan event.GenericEvent, 1)
ipPoolNodeEventCH := make(chan event.GenericEvent, 1)
cidrPoolNodeEventCH := make(chan event.GenericEvent, 1)

if err = (&nodectrl.NodeReconciler{
NodeEventCh: nodeEventCH,
MigrationCh: migrationChan,
PoolsNamespace: opts.IPPoolsNamespace,
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
IPPoolNodeEventCH: ipPoolNodeEventCH,
CIDRPoolNodeEventCH: cidrPoolNodeEventCH,
MigrationCh: migrationChan,
PoolsNamespace: opts.IPPoolsNamespace,
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
logger.Error(err, "unable to create controller", "controller", "Node")
return err
Expand All @@ -168,10 +171,13 @@ func RunController(ctx context.Context, config *rest.Config, opts *options.Optio
logger.Error(err, "unable to create webhook", "webhook", "IPPool")
os.Exit(1)
}
if err = (&ipamv1alpha1.CIDRPool{}).SetupWebhookWithManager(mgr); err != nil {
logger.Error(err, "unable to create webhook", "webhook", "CIDRPool")
os.Exit(1)
}
}

if err = (&poolctrl.IPPoolReconciler{
NodeEventCh: nodeEventCH,
if err = (&ippoolctrl.IPPoolReconciler{
NodeEventCh: ipPoolNodeEventCH,
PoolsNamespace: opts.IPPoolsNamespace,
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Expand All @@ -181,6 +187,16 @@ func RunController(ctx context.Context, config *rest.Config, opts *options.Optio
return err
}

if err = (&cidrpoolctrl.CIDRPoolReconciler{
NodeEventCh: cidrPoolNodeEventCH,
PoolsNamespace: opts.IPPoolsNamespace,
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
logger.Error(err, "unable to create controller", "controller", "CIDRPool")
return err
}

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
logger.Error(err, "unable to set up health check")
return err
Expand Down
1 change: 0 additions & 1 deletion cmd/ipam-controller/app/app_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (

const (
TestNamespace = "test-ns"
TestConfigMapName = "test-config"
)

var (
Expand Down
Loading
Loading