Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Release 1.30] Fixed hns clean only in case of reboot #6538

Merged
merged 1 commit into from
Aug 12, 2024
Merged
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
51 changes: 49 additions & 2 deletions pkg/windows/calico.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/rancher/rke2/pkg/logging"
"github.com/sirupsen/logrus"
opv1 "github.com/tigera/operator/api/v1"
"golang.org/x/sys/windows"
authv1 "k8s.io/api/authentication/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -302,8 +303,14 @@ func (c *Calico) Start(ctx context.Context) error {

// generateCalicoNetworks creates the overlay networks for internode networking
func (c *Calico) generateCalicoNetworks() error {
if err := deleteAllNetworks(); err != nil {
return errors.Wrapf(err, "failed to delete all networks before bootstrapping calico")
nodeRebooted, err := c.isNodeRebooted()
if err != nil {
return errors.Wrapf(err, "failed to check last node reboot time")
}
if nodeRebooted {
if err = deleteAllNetworks(); err != nil {
return errors.Wrapf(err, "failed to delete all networks before bootstrapping calico")
}
}

// There are four ways to select the vxlan interface. In order of priority:
Expand Down Expand Up @@ -528,3 +535,43 @@ func (c *Calico) ReserveSourceVip(ctx context.Context) (string, error) {

return vip, nil
}

//Get latest stored reboot
func (c *Calico) getStoredLastBootTime() (string, error) {
lastRebootPath := filepath.Join(c.CNICfg.ConfigPath, "lastBootTime.txt")
lastStoredBoot, err := os.ReadFile(lastRebootPath)
if err != nil {
if os.IsNotExist(err) {
return "", nil
} else {
return "", err
}
}
return string(lastStoredBoot), nil
}

//Set last boot time on the registry
func (c *Calico) setStoredLastBootTime(lastBootTime string) error {
lastRebootPath := filepath.Join(c.CNICfg.ConfigPath, "lastBootTime.txt")
err := os.WriteFile(lastRebootPath, []byte(lastBootTime), 0644)
if err != nil {
return err
}
return nil
}

// Check if the node was rebooted
func (c *Calico) isNodeRebooted() (bool, error) {
tickCountSinceBoot := windows.DurationSinceBoot()
bootTime := time.Now().Add(-tickCountSinceBoot)
lastReboot := bootTime.Format(time.UnixDate)
prevLastReboot, err := c.getStoredLastBootTime()
if err != nil {
return true, err
}
if lastReboot == prevLastReboot {
return false, nil
}
err = c.setStoredLastBootTime(lastReboot)
return true, err
}
6 changes: 6 additions & 0 deletions pkg/windows/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ var (
// createHnsNetwork creates the network that will connect nodes and returns its managementIP
func createHnsNetwork(backend string, networkAdapter string) (string, error) {
var network hcsshim.HNSNetwork
// Check if the interface already exists
hcsnetwork, err := hcsshim.GetHNSNetworkByName(CalicoHnsNetworkName)
if err == nil {
return hcsnetwork.ManagementIP, nil
}

if backend == "vxlan" {
// Ignoring the return because both true and false without an error represent that the firewall rule was created or already exists
if _, err := wapi.FirewallRuleAdd("OverlayTraffic4789UDP", "Overlay network traffic UDP", "", "4789", wapi.NET_FW_IP_PROTOCOL_UDP, wapi.NET_FW_PROFILE2_ALL); err != nil {
Expand Down