@@ -31,16 +31,20 @@ import (
3131 "k8s.io/client-go/pkg/api/v1"
3232 "k8s.io/client-go/pkg/apis/rbac/v1alpha1"
3333 "k8s.io/client-go/rest"
34- "k8s.io/client-go/tools/clientcmd" )
34+ "k8s.io/client-go/tools/clientcmd"
35+ )
3536
3637var yamlDocumentDelimiter = regexp .MustCompile ("(?m)^---" )
38+
3739const rbacServiceAccountAdmin = "system:serviceaccount-admin"
3840
41+ // TContext is a structure for CLI flags
3942type TContext struct {
4043 Examples string
4144 Version string
4245}
4346
47+ // SkipIf14 makes test to be skipped when running on k8s 1.4
4448func SkipIf14 () {
4549 if strings .Contains (TestContext .Version , "1.4" ) {
4650 Skip ("This test is disabled on kubernetes of versions 1.4" )
@@ -71,6 +75,7 @@ func AddServiceAccountToAdmins(c kubernetes.Interface) {
7175 Expect (err ).NotTo (HaveOccurred (), "Failed to create role binding for serviceaccounts" )
7276}
7377
78+ // RemoveServiceAccountFromAdmins removes system:serviceaccounts from cluster-admin ClusterRole
7479func RemoveServiceAccountFromAdmins (c kubernetes.Interface ) {
7580 if IsVersionOlderThan16 () {
7681 return
@@ -80,6 +85,7 @@ func RemoveServiceAccountFromAdmins(c kubernetes.Interface) {
8085 Expect (err ).NotTo (HaveOccurred (), "Failed to remove serviceaccount from cluster-admin role" )
8186}
8287
88+ // TestContext holds e2e CLI flags
8389var TestContext = TContext {}
8490
8591var url string
@@ -90,6 +96,7 @@ func init() {
9096 flag .StringVar (& url , "cluster-url" , "http://127.0.0.1:8080" , "apiserver address to use with restclient" )
9197}
9298
99+ // SanitizedVersion parses K8s 2-component version string into float64 representation
93100func SanitizedVersion () float64 {
94101 var dirtyVersion string
95102 if strings .HasPrefix (TestContext .Version , "v" ) {
@@ -102,20 +109,24 @@ func SanitizedVersion() float64 {
102109 return version
103110}
104111
112+ // IsVersionOlderThan16 returns true, if k8s version is less than 1.6
105113func IsVersionOlderThan16 () bool {
106114 return SanitizedVersion () < 1.6
107115}
108116
117+ // Logf prints formatted message to the tests log
109118func Logf (format string , a ... interface {}) {
110119 fmt .Fprintf (GinkgoWriter , format , a ... )
111120}
112121
122+ // LoadConfig loads k8s client config
113123func LoadConfig () * rest.Config {
114124 config , err := clientcmd .BuildConfigFromFlags (url , "" )
115125 Expect (err ).NotTo (HaveOccurred ())
116126 return config
117127}
118128
129+ // KubeClient returns client to standard k8s entities
119130func KubeClient () (* kubernetes.Clientset , error ) {
120131 Logf ("Using master %v\n " , url )
121132 config := LoadConfig ()
@@ -130,11 +141,13 @@ func GetAcClient(namespace string) (client.Interface, error) {
130141 return client , err
131142}
132143
144+ // DeleteNS deletes k8s namespace
133145func DeleteNS (clientset * kubernetes.Clientset , namespace * v1.Namespace ) {
134146 defer GinkgoRecover ()
135147 clientset .Namespaces ().Delete (namespace .Name , nil )
136148}
137149
150+ // WaitForPod waits for k8s pod to get to specified running phase
138151func WaitForPod (clientset * kubernetes.Clientset , namespace string , name string , phase v1.PodPhase ) * v1.Pod {
139152 defer GinkgoRecover ()
140153 var podUpdated * v1.Pod
@@ -151,6 +164,7 @@ func WaitForPod(clientset *kubernetes.Clientset, namespace string, name string,
151164 return podUpdated
152165}
153166
167+ // WaitForPodNotToBeCreated waits for pod to be created
154168func WaitForPodNotToBeCreated (clientset * kubernetes.Clientset , namespace string , name string ) {
155169 defer GinkgoRecover ()
156170 Consistently (func () bool {
@@ -160,6 +174,7 @@ func WaitForPodNotToBeCreated(clientset *kubernetes.Clientset, namespace string,
160174 }).Should (BeTrue ())
161175}
162176
177+ // DumpLogs dumps pod logs
163178func DumpLogs (clientset * kubernetes.Clientset , pods ... * v1.Pod ) {
164179 for _ , pod := range pods {
165180 dumpLogs (clientset , pod )
@@ -176,11 +191,12 @@ func dumpLogs(clientset *kubernetes.Clientset, pod *v1.Pod) {
176191 Expect (err ).NotTo (HaveOccurred ())
177192}
178193
194+ // ForEachYamlDocument executes action for each YAML document (parts of YAML file separated by "---")
179195func ForEachYamlDocument (data []byte , action func ([]byte )) {
180196 matches := yamlDocumentDelimiter .FindAllIndex (data , - 1 )
181197 lastStart := 0
182198 for _ , doc := range matches {
183- action (data [lastStart : doc [0 ]])
199+ action (data [lastStart :doc [0 ]])
184200 lastStart = doc [1 ]
185201 }
186202 action (data [lastStart :])
0 commit comments