Ensure unique veth name and myc ip per container
This commit is contained in:
parent
9bdaf13797
commit
0618b41ae2
25
main.go
25
main.go
@ -1,6 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
@ -46,8 +48,8 @@ func cmdAdd(args *skel.CmdArgs) error {
|
||||
return fmt.Errorf("failed to get Mycelium IP: %v", err)
|
||||
}
|
||||
|
||||
// Create veth pair
|
||||
hostVethName := fmt.Sprintf("veth-%s", args.ContainerID[:8])
|
||||
// Create veth pair with unique naming
|
||||
hostVethName := generateVethName(args.ContainerID)
|
||||
containerVethName := "eth0"
|
||||
|
||||
hostVeth, containerVeth, err := createVethPair(hostVethName, containerVethName)
|
||||
@ -101,7 +103,7 @@ func cmdCheck(args *skel.CmdArgs) error {
|
||||
|
||||
func cmdDel(args *skel.CmdArgs) error {
|
||||
// Clean up veth pair (host side will be automatically removed)
|
||||
hostVethName := fmt.Sprintf("veth-%s", args.ContainerID[:8])
|
||||
hostVethName := generateVethName(args.ContainerID)
|
||||
|
||||
link, err := netlink.LinkByName(hostVethName)
|
||||
if err != nil {
|
||||
@ -146,14 +148,25 @@ func getMyceliumIP(interfaceName string) (net.IP, error) {
|
||||
}
|
||||
|
||||
func generateContainerIP(myceliumPrefix net.IP, containerID string) net.IP {
|
||||
// Generate a container IP within the /64 prefix
|
||||
// Using simple approach: prefix + ::1 (could be made more sophisticated)
|
||||
// Generate a unique container IP within the /64 prefix using container ID hash
|
||||
hash := sha256.Sum256([]byte(containerID))
|
||||
|
||||
containerIP := make(net.IP, len(myceliumPrefix))
|
||||
copy(containerIP, myceliumPrefix)
|
||||
containerIP[15] = 1 // Set last byte to 1
|
||||
|
||||
// Use first 8 bytes of hash for the host part (last 64 bits)
|
||||
copy(containerIP[8:], hash[:8])
|
||||
|
||||
return containerIP
|
||||
}
|
||||
|
||||
func generateVethName(containerID string) string {
|
||||
// Generate unique but short veth name using hash of container ID
|
||||
hash := sha256.Sum256([]byte(containerID))
|
||||
shortHash := hex.EncodeToString(hash[:4]) // Use first 4 bytes for 8-char hex
|
||||
return fmt.Sprintf("veth-%s", shortHash)
|
||||
}
|
||||
|
||||
func createVethPair(hostName, containerName string) (netlink.Link, netlink.Link, error) {
|
||||
veth := &netlink.Veth{
|
||||
LinkAttrs: netlink.LinkAttrs{Name: hostName},
|
||||
|
Loading…
Reference in New Issue
Block a user