37 lines
840 B
Go

package util
import (
"fmt"
"github.com/stackanetes/kubernetes-entrypoint/util/env"
"net"
"os"
"strings"
)
func GetIp() (ip string, err error) {
var iface string
if iface = os.Getenv("INTERFACE_NAME"); iface == "" {
return "", fmt.Errorf("Environment variable INTERFACE_NAME not set")
}
i, err := net.InterfaceByName(iface)
if err != nil {
return "", fmt.Errorf("Cannot get iface: %v", err)
}
address, err := i.Addrs()
if err != nil || len(address) == 0 {
return "", fmt.Errorf("Cannot get ip: %v", err)
}
//Take first element to get rid of subnet
ip = strings.Split(address[0].String(), "/")[0]
return
}
func ContainsSeparator(envString string, kind string) bool {
if strings.Contains(envString, env.Separator) {
fmt.Errorf("%s doesn't accept namespace: %s", kind, envString)
return true
}
return false
}