Skip to content

Commit

Permalink
Update subnet labels if not expected
Browse files Browse the repository at this point in the history
  • Loading branch information
STARRY-S committed Jul 23, 2024
1 parent 8fb4baa commit 0a31328
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions pkg/controller/flatnetworksubnet/flatnetworksubnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"net"
"reflect"
"slices"
"time"

Expand Down Expand Up @@ -242,6 +243,41 @@ func (h *handler) onSubnetUpdate(subnet *flv1.FlatNetworkSubnet) (*flv1.FlatNetw
// Sync this subnet in every 10 minutes.
defer h.subnetEnqueueAfter(subnet.Namespace, subnet.Name, time.Minute*10)

// Update subnet labels.
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
result, err := h.subnetCache.Get(subnet.Namespace, subnet.Name)
if err != nil {
logrus.WithFields(fieldsSubnet(subnet)).
Errorf("failed to get subnet from cache: %v", err)
return err
}
result = result.DeepCopy()
if result.Labels == nil {
result.Labels = make(map[string]string)
}
result.Labels["master"] = result.Spec.Master
result.Labels["vlan"] = fmt.Sprintf("%v", result.Spec.VLAN)
result.Labels["mode"] = result.Spec.Mode
result.Labels["flatMode"] = result.Spec.FlatMode
if reflect.DeepEqual(result.Labels, subnet.Labels) {
// Skip if already updated
return nil
}

result, err = h.subnetClient.Update(result)
if err != nil {
return err
}
logrus.WithFields(fieldsSubnet(subnet)).
Infof("update subnet label %q: %v",
subnet.Name, utils.Print(result.Labels))
subnet = result
return nil
})
if err != nil {
return subnet, fmt.Errorf("failed to update label and gateway of subnet: %w", err)
}

// List IPs using this subnet.
ips, err := h.ipCache.List("", labels.SelectorFromSet(labels.Set{
"subnet": subnet.Name,
Expand Down

0 comments on commit 0a31328

Please sign in to comment.