forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
42 lines (38 loc) · 1.17 KB
/
util.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
package user
import (
"fmt"
"strconv"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kubernetes/pkg/api"
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
)
func AnnotationToIntPtr(sUID string) (*int64, error) {
uid, err := strconv.ParseInt(sUID, 10, 64)
if err != nil {
return nil, err
}
return &uid, nil
}
func GetAllocatedID(kClient clientset.Interface, pod *api.Pod, annotation string) (*int64, error) {
if len(pod.Spec.ServiceAccountName) > 0 {
sa, err := kClient.Core().ServiceAccounts(pod.Namespace).Get(pod.Spec.ServiceAccountName, metav1.GetOptions{})
if err != nil {
return nil, err
}
sUID, ok := sa.Annotations[annotation]
if !ok {
return nil, fmt.Errorf("Unable to find annotation %s on service account %s", annotation, pod.Spec.ServiceAccountName)
}
return AnnotationToIntPtr(sUID)
} else {
ns, err := kClient.Core().Namespaces().Get(pod.Namespace, metav1.GetOptions{})
if err != nil {
return nil, err
}
sUID, ok := ns.Annotations[annotation]
if !ok {
return nil, fmt.Errorf("Unable to find annotation %s on namespace %s", annotation, pod.Namespace)
}
return AnnotationToIntPtr(sUID)
}
}