Skip to content

Commit

Permalink
Merge pull request #42 from ykulazhenkov/pr-cidr-pool-controller
Browse files Browse the repository at this point in the history
[CIDRPool 2/x] Add controller for CIDRPool CRD
  • Loading branch information
ykulazhenkov committed Jun 11, 2024
2 parents 3ddd30f + 8324b83 commit 9531cf5
Show file tree
Hide file tree
Showing 11 changed files with 905 additions and 227 deletions.
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

0 comments on commit 9531cf5

Please sign in to comment.