Skip to content
Open
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
2 changes: 1 addition & 1 deletion api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions controllers/staticroute/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,15 @@ func (m routeManagerMock) Run(chan struct{}) error {
return nil
}

func newFakeClient(route *staticroutev1.StaticRoute) client.Client {
func newFakeClient(route *staticroutev1.StaticRoute, nodes *corev1.NodeList) client.Client {
s := runtime.NewScheme()
s.AddKnownTypes(staticroutev1.GroupVersion, route)
nodes := &corev1.NodeList{}
s.AddKnownTypes(corev1.SchemeGroupVersion, nodes)
s.AddKnownTypes(corev1.SchemeGroupVersion, &corev1.NodeList{})
s.AddKnownTypes(corev1.SchemeGroupVersion, &corev1.Node{})
return fake.NewClientBuilder().
WithScheme(s).
WithStatusSubresource(route).
WithRuntimeObjects([]runtime.Object{route}...).
WithRuntimeObjects([]runtime.Object{nodes, route}...).
Build()
}

Expand Down Expand Up @@ -185,7 +185,7 @@ func newStaticRouteWithValues(withSpec, withStatus bool) *staticroutev1.StaticRo
if withStatus {
route.Status = staticroutev1.StaticRouteStatus{
NodeStatus: []staticroutev1.StaticRouteNodeStatus{
staticroutev1.StaticRouteNodeStatus{
{
Hostname: "hostname",
State: staticroutev1.StaticRouteSpec{
Subnet: "10.0.0.1/16",
Expand Down
58 changes: 58 additions & 0 deletions controllers/staticroute/staticroute_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,18 @@ func reconcileImpl(params reconcileImplParams) (res *reconcile.Result, err error

rw := routeWrapper{instance: instance}

// Cleanup is only needed when the StaticRoute is being deleted.
// Do not list all Nodes during normal reconciliation.
if !instance.DeletionTimestamp.IsZero() {
Comment thread
FilatovM marked this conversation as resolved.
Resolved
if err = removeStaleNodeStatuses(params, instance, reqLogger); err != nil {
return nodeGetError, err
}

if len(instance.Status.NodeStatus) == 0 {
return deleteOperation(params, &rw, reqLogger)
}
}

defer func() {
if !reportStatus {
return
Expand Down Expand Up @@ -440,3 +452,49 @@ func convertToOperator(operator metav1.LabelSelectorOperator) (selection.Operato
return selection.Equals, errors.New("unable to convert operator")
}
}

// removeStaleNodeStatuses removes status entries for nodes that no longer exist
func removeStaleNodeStatuses(
Comment thread
FilatovM marked this conversation as resolved.
Resolved
params reconcileImplParams,
instance *staticroutev1.StaticRoute,
logger types.Logger,
) error {
nodes := &corev1.NodeList{}
if err := params.client.List(context.Background(), nodes); err != nil {
logger.Error(err, "Unable to list Kubernetes nodes")
return err
}

existingNodes := make(map[string]struct{}, len(nodes.Items))
for i := range nodes.Items {
node := &nodes.Items[i]

existingNodes[node.Name] = struct{}{}

if hostname := node.Labels[HostNameLabel]; hostname != "" {
existingNodes[hostname] = struct{}{}
}
}

originalLength := len(instance.Status.NodeStatus)
updatedStatus := make([]staticroutev1.StaticRouteNodeStatus, 0, originalLength)
for _, status := range instance.Status.NodeStatus {
if _, exists := existingNodes[status.Hostname]; exists {
updatedStatus = append(updatedStatus, status)
continue
}
logger.Info("Removing stale node status", "hostname", status.Hostname)
}

if len(updatedStatus) == originalLength {
return nil
}

instance.Status.NodeStatus = updatedStatus
if err := params.client.Status().Update(context.Background(), instance); err != nil {
logger.Error(err, "Unable to update StaticRoute status after stale-node cleanup")
return err
}

return nil
}
Loading