|
| 1 | +// Package aboutme provides information to a pod about itself. |
| 2 | +// |
| 3 | +// Typical usage is to let the Pod auto-detect information about itself: |
| 4 | +// |
| 5 | +// my, err := aboutme.FromEnv() |
| 6 | +// if err != nil { |
| 7 | +// // Error connecting to tke k8s API server |
| 8 | +// } |
| 9 | +// |
| 10 | +// fmt.Printf("My Pod Name is %s", my.Name) |
| 11 | +package aboutme |
| 12 | + |
| 13 | +import ( |
| 14 | + "errors" |
| 15 | + "net" |
| 16 | + "os" |
| 17 | + "strings" |
| 18 | + |
| 19 | + "github.com/deis/deis/pkg/k8s" |
| 20 | + |
| 21 | + "k8s.io/kubernetes/pkg/api" |
| 22 | + "k8s.io/kubernetes/pkg/client/unversioned" |
| 23 | + "k8s.io/kubernetes/pkg/labels" |
| 24 | +) |
| 25 | + |
| 26 | +type Me struct { |
| 27 | + ApiServer, Name string |
| 28 | + IP, NodeIP, Namespace, SelfLink, UID string |
| 29 | + Labels map[string]string |
| 30 | + Annotations map[string]string |
| 31 | + |
| 32 | + c *unversioned.Client |
| 33 | +} |
| 34 | + |
| 35 | +// FromEnv uses the environment to create a new Me. |
| 36 | +// |
| 37 | +// To use this, a client MUST be running inside of a Pod environment. It uses |
| 38 | +// a combination of environment variables and file paths to determine |
| 39 | +// information about the cluster. |
| 40 | +func FromEnv() (*Me, error) { |
| 41 | + |
| 42 | + host := os.Getenv("KUBERNETES_SERVICE_HOST") |
| 43 | + port := os.Getenv("KUBERNETES_SERVICE_PORT") |
| 44 | + name := os.Getenv("HOSTNAME") |
| 45 | + |
| 46 | + // FIXME: Better way? Probably scanning secrets for |
| 47 | + // an SSL cert would help? |
| 48 | + proto := "https" |
| 49 | + |
| 50 | + url := proto + "://" + host + ":" + port |
| 51 | + |
| 52 | + me := &Me{ |
| 53 | + ApiServer: url, |
| 54 | + Name: name, |
| 55 | + |
| 56 | + // FIXME: This is a chicken-and-egg problem. We need the namespace |
| 57 | + // to get pod info, and we can only get info from the pod. |
| 58 | + Namespace: "default", |
| 59 | + } |
| 60 | + |
| 61 | + client, err := k8s.PodClient() |
| 62 | + if err != nil { |
| 63 | + return me, err |
| 64 | + } |
| 65 | + me.c = client |
| 66 | + |
| 67 | + me.init() |
| 68 | + |
| 69 | + return me, nil |
| 70 | +} |
| 71 | + |
| 72 | +// Client returns an initialized Kubernetes API client. |
| 73 | +func (me *Me) Client() *unversioned.Client { |
| 74 | + return me.c |
| 75 | +} |
| 76 | + |
| 77 | +// ShuntEnv puts the Me object into the environment. |
| 78 | +// |
| 79 | +// The properties of Me are placed into the environment according to the |
| 80 | +// following rules: |
| 81 | +// |
| 82 | +// - In general, all variables are prefaced with MY_ (MY_IP, MY_NAMESPACE) |
| 83 | +// - Labels become MY_LABEL_[NAME]=[value] |
| 84 | +// - Annotations become MY_ANNOTATION_[NAME] = [value] |
| 85 | +func (me *Me) ShuntEnv() { |
| 86 | + env := map[string]string{ |
| 87 | + "MY_APISERVER": me.ApiServer, |
| 88 | + "MY_NAME": me.Name, |
| 89 | + "MY_IP": me.IP, |
| 90 | + "MY_NODEIP": me.NodeIP, |
| 91 | + "MY_NAMESPACE": me.Namespace, |
| 92 | + "MY_SELFLINK": me.SelfLink, |
| 93 | + "MY_UID": me.UID, |
| 94 | + } |
| 95 | + for k, v := range env { |
| 96 | + os.Setenv(k, v) |
| 97 | + } |
| 98 | + var name string |
| 99 | + for k, v := range me.Labels { |
| 100 | + name = "MY_LABEL_" + strings.ToUpper(k) |
| 101 | + os.Setenv(name, v) |
| 102 | + } |
| 103 | + for k, v := range me.Annotations { |
| 104 | + name = "MY_ANNOTATION_" + strings.ToUpper(k) |
| 105 | + os.Setenv(name, v) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func (me *Me) init() error { |
| 110 | + p, n, err := me.findPodInNamespaces() |
| 111 | + if err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + |
| 115 | + me.Namespace = n |
| 116 | + me.IP = p.Status.PodIP |
| 117 | + me.NodeIP = p.Status.HostIP |
| 118 | + me.SelfLink = p.SelfLink |
| 119 | + me.UID = string(p.UID) |
| 120 | + me.Labels = p.Labels |
| 121 | + me.Annotations = me.Annotations |
| 122 | + |
| 123 | + return nil |
| 124 | +} |
| 125 | + |
| 126 | +// findPodInNamespaces searches relevant namespaces for this pod. |
| 127 | +// |
| 128 | +// It returns a PodInterface for working with the pod, a namespace name as a |
| 129 | +// string, and an error if something goes wrong. |
| 130 | +// |
| 131 | +// The search pattern is to look for namespaces that have the "deis" name in |
| 132 | +// their labels, and then to fall back to default. We don't look at all |
| 133 | +// namespaces. |
| 134 | +func (me *Me) findPodInNamespaces() (*api.Pod, string, error) { |
| 135 | + // Get the deis namespace. If it does not exist, get the default namespce. |
| 136 | + s, err := labels.Parse("name=deis") |
| 137 | + if err == nil { |
| 138 | + ns, err := me.c.Namespaces().List(s, nil) |
| 139 | + if err != nil { |
| 140 | + return nil, "default", err |
| 141 | + } |
| 142 | + for _, n := range ns.Items { |
| 143 | + p, err := me.c.Pods(n.Name).Get(me.Name) |
| 144 | + |
| 145 | + // If there is no error, we got a matching pod. |
| 146 | + if err == nil { |
| 147 | + return p, n.Name, nil |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + // If we get here, it's really the last ditch. |
| 153 | + p, err := me.c.Pods("default").Get(me.Name) |
| 154 | + return p, "default", err |
| 155 | +} |
| 156 | + |
| 157 | +// MyIP examines the local interfaces and guesses which is its IP. |
| 158 | +// |
| 159 | +// Containers tend to put the IP address in eth0, so this attempts to look up |
| 160 | +// that interface and retrieve its IP. It is fairly naive. To get more |
| 161 | +// thorough IP information, you may prefer to use the `net` package and |
| 162 | +// look up the desired information. |
| 163 | +// |
| 164 | +// Because this queries the interfaces, not the Kube API server, this could, |
| 165 | +// in theory, return an IP address different from Me.IP. |
| 166 | +func MyIP() (string, error) { |
| 167 | + iface, err := net.InterfaceByName("eth0") |
| 168 | + if err != nil { |
| 169 | + return "", err |
| 170 | + } |
| 171 | + addrs, err := iface.Addrs() |
| 172 | + if err != nil { |
| 173 | + return "", err |
| 174 | + } |
| 175 | + var ip string |
| 176 | + for _, a := range addrs { |
| 177 | + if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { |
| 178 | + if ipnet.IP.To4() != nil { |
| 179 | + ip = ipnet.IP.String() |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + if len(ip) == 0 { |
| 184 | + return ip, errors.New("Found no IPv4 addresses.") |
| 185 | + } |
| 186 | + return ip, nil |
| 187 | +} |
0 commit comments