Skip to content

Commit

Permalink
Refactor Pool-manager tests (#448)
Browse files Browse the repository at this point in the history
* pool-manager/pool_test: Give meaningful names to const MACs

Signed-off-by: Ram Lavi <[email protected]>

* pkg/pool: Use multus consts

The networks and network-status annotations consts are set on multus
repo, and controlled by it. Using their consts to keep consistency.
For example, the deprecated 'k8s.v1.cni.cncf.io/networks-status'
annotation is replaced by the new one 'k8s.v1.cni.cncf.io/network-status'

Signed-off-by: Ram Lavi <[email protected]>

* pool_test: Refactor closure functions be be regular functions

There is no good reason to use these functions as closure functions.
Moving them to be regular functions, thus reducing code complexity.

Signed-off-by: Ram Lavi <[email protected]>

* pool_test: Change checkMacPoolMapEntries to check for deleted MACs

Currently not all unit tests check macpool size after doing changes in
the macpool map (allocate, update or delete MACs entries)
Moving the check inside checkMacPoolMapEntries so that all tests check
this, removing the now redundant checks.
Also refactoring use of "errors.New(fmt.Sprintf(...))" into
"fmt.Errorf(...)"

Signed-off-by: Ram Lavi <[email protected]>

* pool: Reformat pod and network struct definitions

using formatted view instead of long lines improves readability

Signed-off-by: Ram Lavi <[email protected]>

---------

Signed-off-by: Ram Lavi <[email protected]>
  • Loading branch information
RamLavi authored Jan 6, 2025
1 parent 2cf12d0 commit 26eb443
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 132 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.22.0

require (
github.com/go-logr/logr v1.2.4
github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.1.2-0.20221215110210-ad3f3381681f
github.com/onsi/ginkgo/v2 v2.9.4
github.com/onsi/gomega v1.27.6
github.com/pkg/errors v0.9.1
Expand Down Expand Up @@ -52,7 +53,6 @@ require (
github.com/imdario/mergo v0.3.15 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.1.2-0.20221215110210-ad3f3381681f // indirect
github.com/kubernetes-csi/external-snapshotter/client/v4 v4.2.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2 // indirect
Expand Down
10 changes: 6 additions & 4 deletions pkg/pool-manager/pod_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

networkv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
)

const tempPodName = "tempPodName"
Expand All @@ -40,7 +42,7 @@ func (p *PoolManager) AllocatePodMac(pod *corev1.Pod, isNotDryRun bool) error {
"macmap", p.macPoolMap,
"currentMac", p.currentMac.String())

networkValue, ok := pod.Annotations[NetworksAnnotation]
networkValue, ok := pod.Annotations[networkv1.NetworkAttachmentAnnot]
if !ok {
return nil
}
Expand All @@ -49,7 +51,7 @@ func (p *PoolManager) AllocatePodMac(pod *corev1.Pod, isNotDryRun bool) error {
// we want to connect the allocated mac from the webhook to a pod object in the podToMacPoolMap
// run it before multus added the status annotation
// this mean the pod is not ready
if _, ok := pod.Annotations[networksStatusAnnotation]; ok {
if _, ok := pod.Annotations[networkv1.NetworkStatusAnnot]; ok {
return nil
}

Expand Down Expand Up @@ -97,7 +99,7 @@ func (p *PoolManager) AllocatePodMac(pod *corev1.Pod, isNotDryRun bool) error {
if err != nil {
return err
}
pod.Annotations[NetworksAnnotation] = string(networkListJson)
pod.Annotations[networkv1.NetworkAttachmentAnnot] = string(networkListJson)

return nil
}
Expand Down Expand Up @@ -238,7 +240,7 @@ func (p *PoolManager) initPodMap() error {
continue
}

networkValue, ok := pod.Annotations[NetworksAnnotation]
networkValue, ok := pod.Annotations[networkv1.NetworkAttachmentAnnot]
if !ok {
continue
}
Expand Down
2 changes: 0 additions & 2 deletions pkg/pool-manager/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ const (
RangeStartEnv = "RANGE_START"
RangeEndEnv = "RANGE_END"
RuntimeObjectFinalizerName = "k8s.v1.cni.cncf.io/kubeMacPool"
NetworksAnnotation = "k8s.v1.cni.cncf.io/networks"
networksStatusAnnotation = "k8s.v1.cni.cncf.io/networks-status"
TransactionTimestampAnnotation = "kubemacpool.io/transaction-timestamp"
mutatingWebhookConfigName = "kubemacpool-mutator"
virtualMachnesWebhookName = "mutatevirtualmachines.kubemacpool.io"
Expand Down
Loading

0 comments on commit 26eb443

Please sign in to comment.