forked from lclarkmichalek/etcdhcp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
kubernetes_internal.go
56 lines (48 loc) · 1.27 KB
/
kubernetes_internal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"fmt"
"io/ioutil"
"strings"
"sync"
cni "github.com/K8sNetworkPlumbingWG/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
"github.com/ericchiang/k8s"
metav1 "github.com/ericchiang/k8s/apis/meta/v1"
)
type netAttachDef cni.NetworkAttachmentDefinition
func (net *netAttachDef) GetMetadata() *metav1.ObjectMeta {
return &metav1.ObjectMeta{}
}
func init() {
k8s.Register("k8s.cni.cncf.io", "v1", "network-attachment-definitions", true, &netAttachDef{})
}
func splitNamespaceNetwork(fullName string) (namespace, name string, err error) {
s := strings.SplitN(fullName, "/", 2)
switch len(s) {
case 0:
err = fmt.Errorf("no network specified")
return
case 1:
namespace, err = getCurrentNamespace()
name = fullName
return
case 2:
namespace, name = s[0], s[1]
return
default:
panic(fmt.Sprintf("impossible return value from strings.SplitN: %v", s))
}
}
var (
currentNamespaceOnce sync.Once
currentNamespace string
currentNamespaceErr error
)
func getCurrentNamespace() (string, error) {
currentNamespaceOnce.Do(func() {
var ns []byte
ns, currentNamespaceErr = ioutil.ReadFile(
"/var/run/secrets/kubernetes.io/serviceaccount/namespace")
currentNamespace = string(ns)
})
return currentNamespace, currentNamespaceErr
}