Skip to content

Commit

Permalink
use WHEREABOUTS_NAMESPACE for nodeslicepools like it does for ippools
Browse files Browse the repository at this point in the history
  • Loading branch information
ivelichkovich committed Jun 18, 2024
1 parent fdb06ee commit 48298e7
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 16 deletions.
9 changes: 9 additions & 0 deletions cmd/nodeslicecontroller/node_slice_controller.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"errors"
"flag"
"os"
"time"

nadclient "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/client/clientset/versioned"
Expand Down Expand Up @@ -55,6 +57,12 @@ func main() {
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}

whereaboutsNamespace := os.Getenv("WHEREABOUTS_NAMESPACE")
if whereaboutsNamespace == "" {
logger.Error(errors.New("env var for WHEREABOUTS_NAMESPACE not set"), "unable to discover namespace")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}

kubeInformerFactory := kubeinformers.NewSharedInformerFactory(kubeClient, time.Second*30)
whereaboutsInformerFactory := informers.NewSharedInformerFactory(whereaboutsClient, time.Second*30)
nadInformerFactory := nadinformers.NewSharedInformerFactory(nadClient, time.Second*30)
Expand All @@ -68,6 +76,7 @@ func main() {
whereaboutsInformerFactory.Whereabouts().V1alpha1().NodeSlicePools(),
nadInformerFactory.K8sCniCncfIo().V1().NetworkAttachmentDefinitions(),
false,
whereaboutsNamespace,
)

// notice that there is no need to run Start methods in a separate goroutine. (i.e. go kubeInformerFactory.Start(ctx.done())
Expand Down
4 changes: 1 addition & 3 deletions e2e/e2e_node_slice/e2e_node_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
var _ = Describe("Whereabouts node slice functionality", func() {
Context("Test setup", func() {
const (
testNamespace = "default"
testNamespace = "kube-system"
ipv4TestRange = "10.0.0.0/8"
sliceSize = "/20" // tests will depend on subnets being > node count of test environment
testNetworkName = "net1"
Expand Down Expand Up @@ -125,7 +125,6 @@ var _ = Describe("Whereabouts node slice functionality", func() {
},
NodeSliceSize: sliceSize,
NetworkName: testNetworkName,
Namespace: testNamespace,
}, ipPoolNamespace)
Expect(err).NotTo(HaveOccurred())

Expand Down Expand Up @@ -228,7 +227,6 @@ var _ = Describe("Whereabouts node slice functionality", func() {
},
NodeSliceSize: sliceSize,
NetworkName: testNetworkName,
Namespace: testNamespace,
}, ipPoolNamespace)
Expect(err).NotTo(HaveOccurred())
})
Expand Down
3 changes: 1 addition & 2 deletions e2e/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ func MacvlanNetworkWithNodeSlice(networkName, namespaceName, ipRange, poolName,
"leader_lease_duration": 1500,
"leader_renew_deadline": 1000,
"leader_retry_period": 500,
"namespace": "%s",
"range": "%s",
"log_level": "debug",
"log_file": "/tmp/wb",
Expand All @@ -197,7 +196,7 @@ func MacvlanNetworkWithNodeSlice(networkName, namespaceName, ipRange, poolName,
}
}
]
}`, namespaceName, ipRange, poolName, sliceSize)
}`, ipRange, poolName, sliceSize)
return GenerateNetAttachDefSpec(networkName, namespaceName, macvlanConfig)
}

Expand Down
18 changes: 12 additions & 6 deletions pkg/node-controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ type Controller struct {

//For testing, sort nodes before assigning to get consistent return values
sortResults bool

// whereabouts namespace set from WHEREABOUTS_NAMESPACE env var, should match what's in the daemonset
// this is where the IPPools and NodeSlicePools will be created
whereaboutsNamespace string
}

// NewController returns a new sample controller
Expand All @@ -89,6 +93,7 @@ func NewController(
nodeSlicePoolInformer whereaboutsInformers.NodeSlicePoolInformer,
nadInformer nadinformers.NetworkAttachmentDefinitionInformer,
sortResults bool,
whereaboutsNamespace string,
) *Controller {
logger := klog.FromContext(ctx)

Expand Down Expand Up @@ -119,6 +124,7 @@ func NewController(
workqueue: workqueue.NewRateLimitingQueue(ratelimiter),
recorder: recorder,
sortResults: sortResults,
whereaboutsNamespace: whereaboutsNamespace,
}

logger.Info("Setting up event handlers")
Expand Down Expand Up @@ -328,7 +334,7 @@ func (c *Controller) syncHandler(ctx context.Context, key string) error {
if hasOwnerRef(nodeSlice, name) {
if len(nodeSlice.OwnerReferences) == 1 {
//this is the last NAD owning this so delete
err = c.whereaboutsclientset.WhereaboutsV1alpha1().NodeSlicePools(namespace).Delete(ctx, name, metav1.DeleteOptions{})
err = c.whereaboutsclientset.WhereaboutsV1alpha1().NodeSlicePools(c.whereaboutsNamespace).Delete(ctx, name, metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
return err
}
Expand All @@ -355,7 +361,7 @@ func (c *Controller) syncHandler(ctx context.Context, key string) error {
logger.Info("About to update node slices for network-attachment-definition",
"network-attachment-definition", klog.KRef(namespace, name))

currentNodeSlicePool, err := c.nodeSlicePoolLister.NodeSlicePools(namespace).Get(getSliceName(ipamConf))
currentNodeSlicePool, err := c.nodeSlicePoolLister.NodeSlicePools(c.whereaboutsNamespace).Get(getSliceName(ipamConf))
if err != nil {
logger.Info("node slice pool does not exist, creating")
if !errors.IsNotFound(err) {
Expand All @@ -369,7 +375,7 @@ func (c *Controller) syncHandler(ctx context.Context, key string) error {
},
ObjectMeta: metav1.ObjectMeta{
Name: getSliceName(ipamConf),
Namespace: namespace,
Namespace: c.whereaboutsNamespace,
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(nad, cncfV1.SchemeGroupVersion.WithKind("NetworkAttachmentDefinition")),
},
Expand Down Expand Up @@ -406,7 +412,7 @@ func (c *Controller) syncHandler(ctx context.Context, key string) error {
Allocations: allocations,
}
logger.Info(fmt.Sprintf("final allocations: %v", allocations))
_, err = c.whereaboutsclientset.WhereaboutsV1alpha1().NodeSlicePools(namespace).Create(ctx, nodeslice, metav1.CreateOptions{})
_, err = c.whereaboutsclientset.WhereaboutsV1alpha1().NodeSlicePools(c.whereaboutsNamespace).Create(ctx, nodeslice, metav1.CreateOptions{})
if err != nil {
return err
}
Expand Down Expand Up @@ -449,7 +455,7 @@ func (c *Controller) syncHandler(ctx context.Context, key string) error {
nodeslice.Status = v1alpha1.NodeSlicePoolStatus{
Allocations: allocations,
}
_, err = c.whereaboutsclientset.WhereaboutsV1alpha1().NodeSlicePools(namespace).Update(ctx, nodeslice, metav1.UpdateOptions{})
_, err = c.whereaboutsclientset.WhereaboutsV1alpha1().NodeSlicePools(c.whereaboutsNamespace).Update(ctx, nodeslice, metav1.UpdateOptions{})
if err != nil {
return err
}
Expand All @@ -467,7 +473,7 @@ func (c *Controller) syncHandler(ctx context.Context, key string) error {
removeUnusedNodes(allocations, nodes)
nodeslice.Status.Allocations = allocations

_, err = c.whereaboutsclientset.WhereaboutsV1alpha1().NodeSlicePools(namespace).Update(context.TODO(), nodeslice, metav1.UpdateOptions{})
_, err = c.whereaboutsclientset.WhereaboutsV1alpha1().NodeSlicePools(c.whereaboutsNamespace).Update(context.TODO(), nodeslice, metav1.UpdateOptions{})
if err != nil {
logger.Info(fmt.Sprintf("Error updating NSP with no changes: %v", err))
return err
Expand Down
3 changes: 2 additions & 1 deletion pkg/node-controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ func (f *fixture) newController(ctx context.Context) (*Controller, informers.Sha
kubeInformerFactory.Core().V1().Nodes(),
whereaboutsInformerFactory.Whereabouts().V1alpha1().NodeSlicePools(),
nadInformerFactory.K8sCniCncfIo().V1().NetworkAttachmentDefinitions(),
true)
true,
metav1.NamespaceDefault)

//TODO: add sync for IP Pool or remove IP pool if not used
c.nadSynced = alwaysReady
Expand Down
4 changes: 2 additions & 2 deletions pkg/storage/kubernetes/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,9 +512,9 @@ func IPManagement(ctx context.Context, mode int, ipamConf whereaboutstypes.IPAMC

func GetNodeSlicePoolRange(ctx context.Context, ipam *KubernetesIPAM, nodeName string) (string, error) {
logging.Debugf("ipam namespace is %v", ipam.namespace)
nodeSlice, err := ipam.client.WhereaboutsV1alpha1().NodeSlicePools(ipam.Config.Namespace).Get(ctx, getNodeSliceName(ipam), metav1.GetOptions{})
nodeSlice, err := ipam.client.WhereaboutsV1alpha1().NodeSlicePools(ipam.namespace).Get(ctx, getNodeSliceName(ipam), metav1.GetOptions{})
if err != nil {
logging.Errorf("error getting node slice %s/%s %v", ipam.Config.Namespace, getNodeSliceName(ipam), err)
logging.Errorf("error getting node slice %s/%s %v", ipam.namespace, getNodeSliceName(ipam), err)
return "", err
}
for _, allocation := range nodeSlice.Status.Allocations {
Expand Down
2 changes: 0 additions & 2 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ type IPAMConfig struct {
DNS cnitypes.DNS `json:"dns"`
Range string `json:"range"`
NodeSliceSize string `json:"node_slice_size"`
Namespace string `json:"namespace"` //TODO: best way to get namespace of the NAD?
RangeStart net.IP `json:"range_start,omitempty"`
RangeEnd net.IP `json:"range_end,omitempty"`
GatewayStr string `json:"gateway"`
Expand Down Expand Up @@ -130,7 +129,6 @@ func (ic *IPAMConfig) UnmarshalJSON(data []byte) error {
OmitRanges: ipamConfigAlias.OmitRanges,
DNS: ipamConfigAlias.DNS,
Range: ipamConfigAlias.Range,
Namespace: ipamConfigAlias.Namespace,
RangeStart: backwardsCompatibleIPAddress(ipamConfigAlias.RangeStart),
RangeEnd: backwardsCompatibleIPAddress(ipamConfigAlias.RangeEnd),
NodeSliceSize: ipamConfigAlias.NodeSliceSize,
Expand Down

0 comments on commit 48298e7

Please sign in to comment.