Repository: k3s-io/k3s
File: pkg/agent/flannel/flannel.go (Go)

Code before:
		snv6.IncrementIP()
		if _, err := fmt.Fprintf(f, "FLANNEL_IPV6_NETWORK=%s\n", nwv6); err != nil {
			cleanup()
			return err
		}
		if _, err := fmt.Fprintf(f, "FLANNEL_IPV6_SUBNET=%s\n", snv6); err != nil {
			cleanup()
			return err
		}
	}

	if _, err := fmt.Fprintf(f, "FLANNEL_MTU=%d\n", bn.MTU()); err != nil {
		cleanup()
		return err
	}
	if _, err := fmt.Fprintf(f, "FLANNEL_IPMASQ=%v\n", ipMasq); err != nil {
		cleanup()
		return err
	}
	if err := f.Sync(); err != nil {
		cleanup()
		return err
	}
	if err := f.Close(); err != nil {
		_ = os.Remove(tempFile)
		return err
	}

	// rename(2) the temporary file to the desired location so that it becomes
	// atomically visible with the contents (same directory keeps it on the same FS)
	if err := os.Rename(tempFile, path); err != nil {
		_ = os.Remove(tempFile)
		return err
	}
	return nil
}

// ReadCIDRFromSubnetFile reads the flannel subnet file and extracts the value of IPv4 network key
func ReadCIDRFromSubnetFile(path string, key string) ip.IP4Net {
	prevCIDRs := ReadCIDRsFromSubnetFile(path, key)
	if len(prevCIDRs) == 0 {
		logrus.Warningf("no subnet found for key: %s in file: %s", key, path)
		return ip.IP4Net{IP: 0, PrefixLen: 0}
	} else if len(prevCIDRs) > 1 {
		logrus.Errorf("error reading subnet: more than 1 entry found for key: %s in file %s: ", key, path)
		return ip.IP4Net{IP: 0, PrefixLen: 0}
	}
	return prevCIDRs[0]
}

func ReadCIDRsFromSubnetFile(path string, key string) []ip.IP4Net {

Review comment (suggestion):
nit: just call cleanup here, as closing the file and removing the tempfile are exactly what it does.
```suggestion
	return cleanup(nil)
```

Code after:
		snv6.IncrementIP()
		if _, err := fmt.Fprintf(f, "FLANNEL_IPV6_NETWORK=%s\n", nwv6); err != nil {
			return cleanup(err)
		}
		if _, err := fmt.Fprintf(f, "FLANNEL_IPV6_SUBNET=%s\n", snv6); err != nil {
			return cleanup(err)
		}
	}

	if _, err := fmt.Fprintf(f, "FLANNEL_MTU=%d\n", bn.MTU()); err != nil {
		return cleanup(err)
	}
	if _, err := fmt.Fprintf(f, "FLANNEL_IPMASQ=%v\n", ipMasq); err != nil {
		return cleanup(err)
	}
	if err := f.Sync(); err != nil {
		return cleanup(err)
	}
	if err := f.Close(); err != nil {
		return cleanupNoClose(err)
	}

	// rename(2) the temporary file to the desired location so that it becomes
	// atomically visible with the contents (same directory keeps it on the same FS)
	if err := os.Rename(tempFile, path); err != nil {
		_ = os.Remove(tempFile)
		return err
	}
	return nil
}

// ReadCIDRFromSubnetFile reads the flannel subnet file and extracts the value of IPv4 network key
func ReadCIDRFromSubnetFile(path string, key string) ip.IP4Net {
	prevCIDRs := ReadCIDRsFromSubnetFile(path, key)
	if len(prevCIDRs) == 0 {
		logrus.Warningf("no subnet found for key: %s in file: %s", key, path)
		return ip.IP4Net{IP: 0, PrefixLen: 0}
	} else if len(prevCIDRs) > 1 {
		logrus.Errorf("error reading subnet: more than 1 entry found for key: %s in file %s: ", key, path)
		return ip.IP4Net{IP: 0, PrefixLen: 0}
	}
	return prevCIDRs[0]
}

func ReadCIDRsFromSubnetFile(path string, key string) []ip.IP4Net {
	prevCIDRs := make([]ip.IP4Net, 0)
	if _, err := os.Stat(path); !os.IsNotExist(err) {
		prevSubnetVals, err := godotenv.Read(path)
		if err != nil {
			logrus.Errorf("Couldn't fetch previous %s from subnet file at %s: %v", key, path, err)
		} else if prevCIDRString, ok := prevSubnetVals[key]; ok {