Skip to content

Commit

Permalink
Move pool package to the pkg root
Browse files Browse the repository at this point in the history
This package is shared by components

Signed-off-by: Yury Kulazhenkov <ykulazhenkov@nvidia.com>
  • Loading branch information
ykulazhenkov committed May 4, 2023
1 parent 8e3bdf1 commit adb8c16
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 35 deletions.
2 changes: 1 addition & 1 deletion pkg/cni/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"

"github.com/Mellanox/nvidia-k8s-ipam/pkg/cni/pool"
"github.com/Mellanox/nvidia-k8s-ipam/pkg/cni/types"
"github.com/Mellanox/nvidia-k8s-ipam/pkg/pool"
"github.com/Mellanox/nvidia-k8s-ipam/pkg/version"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/cni/types/host-local.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package types
import (
"path/filepath"

"github.com/Mellanox/nvidia-k8s-ipam/pkg/cni/pool"
"github.com/Mellanox/nvidia-k8s-ipam/pkg/pool"
)

// TODO: do we want to support Routes ? DNS entires from ResolvConf as host-local CNI ?
Expand Down
2 changes: 1 addition & 1 deletion pkg/ipam-controller/allocator/allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"

"github.com/Mellanox/nvidia-k8s-ipam/pkg/cni/pool"
"github.com/Mellanox/nvidia-k8s-ipam/pkg/ip"
"github.com/Mellanox/nvidia-k8s-ipam/pkg/pool"
)

var ErrNoFreeRanges = errors.New("no free IP ranges available")
Expand Down
8 changes: 4 additions & 4 deletions pkg/ipam-controller/controllers/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ import (

corev1 "k8s.io/api/core/v1"

"github.com/Mellanox/nvidia-k8s-ipam/pkg/cni/pool"
"github.com/Mellanox/nvidia-k8s-ipam/pkg/ipam-controller/allocator"
"github.com/Mellanox/nvidia-k8s-ipam/pkg/ipam-controller/selector"
"github.com/Mellanox/nvidia-k8s-ipam/pkg/pool"
)

// NodeReconciler reconciles Node objects
Expand Down Expand Up @@ -91,7 +91,7 @@ func (r *NodeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
return ctrl.Result{}, nil
}

if err := pool.SetPools(node, expectedAlloc); err != nil {
if err := pool.SetIPBlockAnnotation(node, expectedAlloc); err != nil {
return ctrl.Result{}, err
}

Expand All @@ -108,10 +108,10 @@ func (r *NodeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.

// remove annotation from the node object in the API
func (r *NodeReconciler) cleanAnnotation(ctx context.Context, node *corev1.Node) (ctrl.Result, error) {
if !pool.AnnotationExist(node) {
if !pool.IPBlockAnnotationExists(node) {
return ctrl.Result{}, nil
}
pool.RemoveAnnotation(node)
pool.RemoveIPBlockAnnotation(node)
if err := r.Client.Update(ctx, node); err != nil {
return ctrl.Result{}, err
}
Expand Down
49 changes: 49 additions & 0 deletions pkg/pool/annotations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2023, 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 pool

import (
"encoding/json"
"fmt"

v1 "k8s.io/api/core/v1"
)

// SetIPBlockAnnotation serialize IP pools settings for the node and add this info as annotation
func SetIPBlockAnnotation(node *v1.Node, pools map[string]*IPPool) error {
annotations := node.GetAnnotations()
if annotations == nil {
annotations = map[string]string{}
}
data, err := json.Marshal(pools)
if err != nil {
return fmt.Errorf("failed to serialize pools config: %v", err)
}
annotations[ipBlocksAnnotation] = string(data)
node.SetAnnotations(annotations)
return nil
}

// IPBlockAnnotationExists returns true if ip-block annotation exist
func IPBlockAnnotationExists(node *v1.Node) bool {
_, exist := node.GetAnnotations()[ipBlocksAnnotation]
return exist
}

// RemoveIPBlockAnnotation removes annotation with ip-block from the node object
func RemoveIPBlockAnnotation(node *v1.Node) {
annotations := node.GetAnnotations()
delete(annotations, ipBlocksAnnotation)
node.SetAnnotations(annotations)
}
28 changes: 0 additions & 28 deletions pkg/cni/pool/pool.go → pkg/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,31 +77,3 @@ func (pm *ManagerImpl) GetPoolByName(name string) *IPPool {
func (pm *ManagerImpl) GetPools() map[string]*IPPool {
return pm.poolByName
}

// SetPools serialize IP pools settings for the node and add this info as annotation
func SetPools(node *v1.Node, pools map[string]*IPPool) error {
annotations := node.GetAnnotations()
if annotations == nil {
annotations = map[string]string{}
}
data, err := json.Marshal(pools)
if err != nil {
return fmt.Errorf("failed to serialize pools config: %v", err)
}
annotations[ipBlocksAnnotation] = string(data)
node.SetAnnotations(annotations)
return nil
}

// AnnotationExist returns true if ip-block annotation exist
func AnnotationExist(node *v1.Node) bool {
_, exist := node.GetAnnotations()[ipBlocksAnnotation]
return exist
}

// RemoveAnnotation removes annotation with ip-block from the node object
func RemoveAnnotation(node *v1.Node) {
annotations := node.GetAnnotations()
delete(annotations, ipBlocksAnnotation)
node.SetAnnotations(annotations)
}

0 comments on commit adb8c16

Please sign in to comment.