@@ -1354,7 +1354,7 @@ func CheckInvariants(events []watch.Event, fns ...InvariantFunc) error {
1354
1354
return nil
1355
1355
}
1356
1356
1357
- // Waits default amount of time (PodStartTimeout) for the specified pod to become running.
1357
+ // WaitForPodRunningInNamespace waits default amount of time (PodStartTimeout) for the specified pod to become running.
1358
1358
// Returns an error if timeout occurs first, or pod goes in to failed state.
1359
1359
func WaitForPodRunningInNamespace (c clientset.Interface , pod * v1.Pod ) error {
1360
1360
if pod .Status .Phase == v1 .PodRunning {
@@ -1363,19 +1363,20 @@ func WaitForPodRunningInNamespace(c clientset.Interface, pod *v1.Pod) error {
1363
1363
return WaitTimeoutForPodRunningInNamespace (c , pod .Name , pod .Namespace , PodStartTimeout )
1364
1364
}
1365
1365
1366
- // Waits default amount of time (PodStartTimeout) for the specified pod to become running.
1366
+ // WaitForPodNameRunningInNamespace waits default amount of time (PodStartTimeout) for the specified pod to become running.
1367
1367
// Returns an error if timeout occurs first, or pod goes in to failed state.
1368
1368
func WaitForPodNameRunningInNamespace (c clientset.Interface , podName , namespace string ) error {
1369
1369
return WaitTimeoutForPodRunningInNamespace (c , podName , namespace , PodStartTimeout )
1370
1370
}
1371
1371
1372
- // Waits an extended amount of time (slowPodStartTimeout) for the specified pod to become running.
1372
+ // waitForPodRunningInNamespaceSlow waits an extended amount of time (slowPodStartTimeout) for the specified pod to become running.
1373
1373
// The resourceVersion is used when Watching object changes, it tells since when we care
1374
1374
// about changes to the pod. Returns an error if timeout occurs first, or pod goes in to failed state.
1375
1375
func waitForPodRunningInNamespaceSlow (c clientset.Interface , podName , namespace string ) error {
1376
1376
return WaitTimeoutForPodRunningInNamespace (c , podName , namespace , slowPodStartTimeout )
1377
1377
}
1378
1378
1379
+ // WaitTimeoutForPodRunningInNamespace waits the given timeout duration for the specified pod to become running.
1379
1380
func WaitTimeoutForPodRunningInNamespace (c clientset.Interface , podName , namespace string , timeout time.Duration ) error {
1380
1381
return wait .PollImmediate (Poll , timeout , podRunning (c , podName , namespace ))
1381
1382
}
@@ -1396,7 +1397,7 @@ func podRunning(c clientset.Interface, podName, namespace string) wait.Condition
1396
1397
}
1397
1398
}
1398
1399
1399
- // WaitTimeoutForPodEvent waits for an event to occur for a pod
1400
+ // WaitTimeoutForPodEvent waits the given timeout duration for a pod event to occur.
1400
1401
func WaitTimeoutForPodEvent (c clientset.Interface , podName , namespace , eventSelector , msg string , timeout time.Duration ) error {
1401
1402
return wait .PollImmediate (Poll , timeout , eventOccurred (c , podName , namespace , eventSelector , msg ))
1402
1403
}
@@ -1417,12 +1418,13 @@ func eventOccurred(c clientset.Interface, podName, namespace, eventSelector, msg
1417
1418
}
1418
1419
}
1419
1420
1420
- // Waits default amount of time (DefaultPodDeletionTimeout) for the specified pod to stop running.
1421
+ // WaitForPodNoLongerRunningInNamespace waits default amount of time (DefaultPodDeletionTimeout) for the specified pod to stop running.
1421
1422
// Returns an error if timeout occurs first.
1422
1423
func WaitForPodNoLongerRunningInNamespace (c clientset.Interface , podName , namespace string ) error {
1423
1424
return WaitTimeoutForPodNoLongerRunningInNamespace (c , podName , namespace , DefaultPodDeletionTimeout )
1424
1425
}
1425
1426
1427
+ // WaitTimeoutForPodNoLongerRunningInNamespace waits the given timeout duration for the specified pod to stop.
1426
1428
func WaitTimeoutForPodNoLongerRunningInNamespace (c clientset.Interface , podName , namespace string , timeout time.Duration ) error {
1427
1429
return wait .PollImmediate (Poll , timeout , podCompleted (c , podName , namespace ))
1428
1430
}
@@ -1495,9 +1497,8 @@ func waitForPodTerminatedInNamespace(c clientset.Interface, podName, reason, nam
1495
1497
if pod .Status .Phase == v1 .PodFailed {
1496
1498
if pod .Status .Reason == reason { // short-circuit waitForPodCondition's loop
1497
1499
return true , nil
1498
- } else {
1499
- return true , fmt .Errorf ("Expected pod %q in namespace %q to be terminated with reason %q, got reason: %q" , podName , namespace , reason , pod .Status .Reason )
1500
1500
}
1501
+ return true , fmt .Errorf ("Expected pod %q in namespace %q to be terminated with reason %q, got reason: %q" , podName , namespace , reason , pod .Status .Reason )
1501
1502
}
1502
1503
return false , nil
1503
1504
})
@@ -1580,6 +1581,7 @@ func WaitForRCToStabilize(c clientset.Interface, ns, name string, timeout time.D
1580
1581
return err
1581
1582
}
1582
1583
1584
+ // WaitForPodToDisappear waits the given timeout duration for the specified pod to disappear.
1583
1585
func WaitForPodToDisappear (c clientset.Interface , ns , podName string , label labels.Selector , interval , timeout time.Duration ) error {
1584
1586
return wait .PollImmediate (interval , timeout , func () (bool , error ) {
1585
1587
Logf ("Waiting for pod %s to disappear" , podName )
@@ -1708,6 +1710,7 @@ func countEndpointsNum(e *v1.Endpoints) int {
1708
1710
return num
1709
1711
}
1710
1712
1713
+ // WaitForEndpoint waits for the specified endpoint to be ready.
1711
1714
func WaitForEndpoint (c clientset.Interface , ns , name string ) error {
1712
1715
for t := time .Now (); time .Since (t ) < EndpointRegisterTimeout ; time .Sleep (Poll ) {
1713
1716
endpoint , err := c .CoreV1 ().Endpoints (ns ).Get (name , metav1.GetOptions {})
@@ -1726,9 +1729,9 @@ func WaitForEndpoint(c clientset.Interface, ns, name string) error {
1726
1729
return fmt .Errorf ("Failed to get endpoints for %s/%s" , ns , name )
1727
1730
}
1728
1731
1729
- // Context for checking pods responses by issuing GETs to them (via the API
1732
+ // PodProxyResponseChecker is a context for checking pods responses by issuing GETs to them (via the API
1730
1733
// proxy) and verifying that they answer with their own pod name.
1731
- type podProxyResponseChecker struct {
1734
+ type PodProxyResponseChecker struct {
1732
1735
c clientset.Interface
1733
1736
ns string
1734
1737
label labels.Selector
@@ -1737,13 +1740,14 @@ type podProxyResponseChecker struct {
1737
1740
pods * v1.PodList
1738
1741
}
1739
1742
1740
- func PodProxyResponseChecker (c clientset.Interface , ns string , label labels.Selector , controllerName string , respondName bool , pods * v1.PodList ) podProxyResponseChecker {
1741
- return podProxyResponseChecker {c , ns , label , controllerName , respondName , pods }
1743
+ // NewPodProxyResponseChecker returns a context for checking pods responses.
1744
+ func NewPodProxyResponseChecker (c clientset.Interface , ns string , label labels.Selector , controllerName string , respondName bool , pods * v1.PodList ) PodProxyResponseChecker {
1745
+ return PodProxyResponseChecker {c , ns , label , controllerName , respondName , pods }
1742
1746
}
1743
1747
1744
1748
// CheckAllResponses issues GETs to all pods in the context and verify they
1745
1749
// reply with their own pod name.
1746
- func (r podProxyResponseChecker ) CheckAllResponses () (done bool , err error ) {
1750
+ func (r PodProxyResponseChecker ) CheckAllResponses () (done bool , err error ) {
1747
1751
successes := 0
1748
1752
options := metav1.ListOptions {LabelSelector : r .label .String ()}
1749
1753
currentPods , err := r .c .CoreV1 ().Pods (r .ns ).List (options )
@@ -1835,17 +1839,20 @@ func KubectlVersion() (*utilversion.Version, error) {
1835
1839
return utilversion .ParseSemantic (matches [1 ])
1836
1840
}
1837
1841
1842
+ // PodsResponding waits for the pods to response.
1838
1843
func PodsResponding (c clientset.Interface , ns , name string , wantName bool , pods * v1.PodList ) error {
1839
1844
By ("trying to dial each unique pod" )
1840
1845
label := labels .SelectorFromSet (labels .Set (map [string ]string {"name" : name }))
1841
- return wait .PollImmediate (Poll , podRespondingTimeout , PodProxyResponseChecker (c , ns , label , name , wantName , pods ).CheckAllResponses )
1846
+ return wait .PollImmediate (Poll , podRespondingTimeout , NewPodProxyResponseChecker (c , ns , label , name , wantName , pods ).CheckAllResponses )
1842
1847
}
1843
1848
1849
+ // PodsCreated returns a pod list matched by the given name.
1844
1850
func PodsCreated (c clientset.Interface , ns , name string , replicas int32 ) (* v1.PodList , error ) {
1845
1851
label := labels .SelectorFromSet (labels .Set (map [string ]string {"name" : name }))
1846
1852
return PodsCreatedByLabel (c , ns , name , replicas , label )
1847
1853
}
1848
1854
1855
+ // PodsCreatedByLabel returns a created pod list matched by the given label.
1849
1856
func PodsCreatedByLabel (c clientset.Interface , ns , name string , replicas int32 , label labels.Selector ) (* v1.PodList , error ) {
1850
1857
timeout := 2 * time .Minute
1851
1858
for start := time .Now (); time .Since (start ) < timeout ; time .Sleep (5 * time .Second ) {
@@ -1879,16 +1886,16 @@ func podsRunning(c clientset.Interface, pods *v1.PodList) []error {
1879
1886
// are running so non-running pods cause a timeout for this test.
1880
1887
By ("ensuring each pod is running" )
1881
1888
e := []error {}
1882
- error_chan := make (chan error )
1889
+ errorChan := make (chan error )
1883
1890
1884
1891
for _ , pod := range pods .Items {
1885
1892
go func (p v1.Pod ) {
1886
- error_chan <- WaitForPodRunningInNamespace (c , & p )
1893
+ errorChan <- WaitForPodRunningInNamespace (c , & p )
1887
1894
}(pod )
1888
1895
}
1889
1896
1890
1897
for range pods .Items {
1891
- err := <- error_chan
1898
+ err := <- errorChan
1892
1899
if err != nil {
1893
1900
e = append (e , err )
1894
1901
}
@@ -1897,10 +1904,12 @@ func podsRunning(c clientset.Interface, pods *v1.PodList) []error {
1897
1904
return e
1898
1905
}
1899
1906
1907
+ // VerifyPods checks if the specified pod is responding.
1900
1908
func VerifyPods (c clientset.Interface , ns , name string , wantName bool , replicas int32 ) error {
1901
1909
return podRunningMaybeResponding (c , ns , name , wantName , replicas , true )
1902
1910
}
1903
1911
1912
+ // VerifyPodsRunning checks if the specified pod is running.
1904
1913
func VerifyPodsRunning (c clientset.Interface , ns , name string , wantName bool , replicas int32 ) error {
1905
1914
return podRunningMaybeResponding (c , ns , name , wantName , replicas , false )
1906
1915
}
@@ -1923,6 +1932,7 @@ func podRunningMaybeResponding(c clientset.Interface, ns, name string, wantName
1923
1932
return nil
1924
1933
}
1925
1934
1935
+ // ServiceResponding waits for the service to be responding.
1926
1936
func ServiceResponding (c clientset.Interface , ns , name string ) error {
1927
1937
By (fmt .Sprintf ("trying to dial the service %s.%s via the proxy" , ns , name ))
1928
1938
@@ -1959,6 +1969,7 @@ func ServiceResponding(c clientset.Interface, ns, name string) error {
1959
1969
})
1960
1970
}
1961
1971
1972
+ // RestclientConfig returns a config holds the information needed to build connection to kubernetes clusters.
1962
1973
func RestclientConfig (kubeContext string ) (* clientcmdapi.Config , error ) {
1963
1974
Logf (">>> kubeConfig: %s" , TestContext .KubeConfig )
1964
1975
if TestContext .KubeConfig == "" {
@@ -1975,8 +1986,10 @@ func RestclientConfig(kubeContext string) (*clientcmdapi.Config, error) {
1975
1986
return c , nil
1976
1987
}
1977
1988
1989
+ // ClientConfigGetter is a func that returns getter to return a config.
1978
1990
type ClientConfigGetter func () (* restclient.Config , error )
1979
1991
1992
+ // LoadConfig returns a config for a rest client.
1980
1993
func LoadConfig () (* restclient.Config , error ) {
1981
1994
if TestContext .NodeE2E {
1982
1995
// This is a node e2e test, apply the node e2e configuration
@@ -1986,14 +1999,14 @@ func LoadConfig() (*restclient.Config, error) {
1986
1999
if err != nil {
1987
2000
if TestContext .KubeConfig == "" {
1988
2001
return restclient .InClusterConfig ()
1989
- } else {
1990
- return nil , err
1991
2002
}
2003
+ return nil , err
1992
2004
}
1993
2005
1994
2006
return clientcmd .NewDefaultClientConfig (* c , & clientcmd.ConfigOverrides {ClusterInfo : clientcmdapi.Cluster {Server : TestContext .Host }}).ClientConfig ()
1995
2007
}
1996
2008
2009
+ // LoadClientset returns clientset for connecting to kubernetes clusters.
1997
2010
func LoadClientset () (* clientset.Clientset , error ) {
1998
2011
config , err := LoadConfig ()
1999
2012
if err != nil {
@@ -2002,7 +2015,7 @@ func LoadClientset() (*clientset.Clientset, error) {
2002
2015
return clientset .NewForConfig (config )
2003
2016
}
2004
2017
2005
- // randomSuffix provides a random string to append to pods,services,rcs.
2018
+ // RandomSuffix provides a random string to append to pods,services,rcs.
2006
2019
// TODO: Allow service names to have the same form as names
2007
2020
// for pods and replication controllers so we don't
2008
2021
// need to use such a function and can instead
@@ -2012,6 +2025,7 @@ func RandomSuffix() string {
2012
2025
return strconv .Itoa (r .Int () % 10000 )
2013
2026
}
2014
2027
2028
+ // ExpectNoError checks if "err" is set, and if so, fails assertion while logging the error.
2015
2029
func ExpectNoError (err error , explain ... interface {}) {
2016
2030
ExpectNoErrorWithOffset (1 , err , explain ... )
2017
2031
}
@@ -2025,6 +2039,7 @@ func ExpectNoErrorWithOffset(offset int, err error, explain ...interface{}) {
2025
2039
ExpectWithOffset (1 + offset , err ).NotTo (HaveOccurred (), explain ... )
2026
2040
}
2027
2041
2042
+ // ExpectNoErrorWithRetries checks if an error occurs with the given retry count.
2028
2043
func ExpectNoErrorWithRetries (fn func () error , maxRetries int , explain ... interface {}) {
2029
2044
var err error
2030
2045
for i := 0 ; i < maxRetries ; i ++ {
@@ -2037,7 +2052,7 @@ func ExpectNoErrorWithRetries(fn func() error, maxRetries int, explain ...interf
2037
2052
ExpectWithOffset (1 , err ).NotTo (HaveOccurred (), explain ... )
2038
2053
}
2039
2054
2040
- // Stops everything from filePath from namespace ns and checks if everything matching selectors from the given namespace is correctly stopped.
2055
+ // Cleanup stops everything from filePath from namespace ns and checks if everything matching selectors from the given namespace is correctly stopped.
2041
2056
func Cleanup (filePath , ns string , selectors ... string ) {
2042
2057
By ("using delete to clean up resources" )
2043
2058
var nsArg string
@@ -2048,7 +2063,7 @@ func Cleanup(filePath, ns string, selectors ...string) {
2048
2063
AssertCleanup (ns , selectors ... )
2049
2064
}
2050
2065
2051
- // Asserts that cleanup of a namespace wrt selectors occurred.
2066
+ // AssertCleanup asserts that cleanup of a namespace wrt selectors occurred.
2052
2067
func AssertCleanup (ns string , selectors ... string ) {
2053
2068
var nsArg string
2054
2069
if ns != "" {
@@ -2112,40 +2127,46 @@ func KubectlCmd(args ...string) *exec.Cmd {
2112
2127
return cmd
2113
2128
}
2114
2129
2115
- // kubectlBuilder is used to build, customize and execute a kubectl Command.
2130
+ // KubectlBuilder is used to build, customize and execute a kubectl Command.
2116
2131
// Add more functions to customize the builder as needed.
2117
- type kubectlBuilder struct {
2132
+ type KubectlBuilder struct {
2118
2133
cmd * exec.Cmd
2119
2134
timeout <- chan time.Time
2120
2135
}
2121
2136
2122
- func NewKubectlCommand (args ... string ) * kubectlBuilder {
2123
- b := new (kubectlBuilder )
2137
+ // NewKubectlCommand returns a KubectlBuilder for running kubectl.
2138
+ func NewKubectlCommand (args ... string ) * KubectlBuilder {
2139
+ b := new (KubectlBuilder )
2124
2140
b .cmd = KubectlCmd (args ... )
2125
2141
return b
2126
2142
}
2127
2143
2128
- func (b * kubectlBuilder ) WithEnv (env []string ) * kubectlBuilder {
2144
+ // WithEnv sets the given environment and returns itself.
2145
+ func (b * KubectlBuilder ) WithEnv (env []string ) * KubectlBuilder {
2129
2146
b .cmd .Env = env
2130
2147
return b
2131
2148
}
2132
2149
2133
- func (b * kubectlBuilder ) WithTimeout (t <- chan time.Time ) * kubectlBuilder {
2150
+ // WithTimeout sets the given timeout and returns itself.
2151
+ func (b * KubectlBuilder ) WithTimeout (t <- chan time.Time ) * KubectlBuilder {
2134
2152
b .timeout = t
2135
2153
return b
2136
2154
}
2137
2155
2138
- func (b kubectlBuilder ) WithStdinData (data string ) * kubectlBuilder {
2156
+ // WithStdinData sets the given data to stdin and returns itself.
2157
+ func (b KubectlBuilder ) WithStdinData (data string ) * KubectlBuilder {
2139
2158
b .cmd .Stdin = strings .NewReader (data )
2140
2159
return & b
2141
2160
}
2142
2161
2143
- func (b kubectlBuilder ) WithStdinReader (reader io.Reader ) * kubectlBuilder {
2162
+ // WithStdinReader sets the given reader and returns itself.
2163
+ func (b KubectlBuilder ) WithStdinReader (reader io.Reader ) * KubectlBuilder {
2144
2164
b .cmd .Stdin = reader
2145
2165
return & b
2146
2166
}
2147
2167
2148
- func (b kubectlBuilder ) ExecOrDie () string {
2168
+ // ExecOrDie runs the kubectl executable or dies if error occurs.
2169
+ func (b KubectlBuilder ) ExecOrDie () string {
2149
2170
str , err := b .Exec ()
2150
2171
// In case of i/o timeout error, try talking to the apiserver again after 2s before dying.
2151
2172
// Note that we're still dying after retrying so that we can get visibility to triage it further.
@@ -2174,14 +2195,15 @@ func isTimeout(err error) bool {
2174
2195
return false
2175
2196
}
2176
2197
2177
- func (b kubectlBuilder ) Exec () (string , error ) {
2198
+ // Exec runs the kubectl executable.
2199
+ func (b KubectlBuilder ) Exec () (string , error ) {
2178
2200
var stdout , stderr bytes.Buffer
2179
2201
cmd := b .cmd
2180
2202
cmd .Stdout , cmd .Stderr = & stdout , & stderr
2181
2203
2182
2204
Logf ("Running '%s %s'" , cmd .Path , strings .Join (cmd .Args [1 :], " " )) // skip arg[0] as it is printed separately
2183
2205
if err := cmd .Start (); err != nil {
2184
- return "" , fmt .Errorf ("error starting %v:\n Command stdout:\n %v\n stderr:\n %v\n error:\n %v\n " , cmd , cmd .Stdout , cmd .Stderr , err )
2206
+ return "" , fmt .Errorf ("error starting %v:\n Command stdout:\n %v\n stderr:\n %v\n error:\n %v" , cmd , cmd .Stdout , cmd .Stderr , err )
2185
2207
}
2186
2208
errCh := make (chan error , 1 )
2187
2209
go func () {
@@ -2190,19 +2212,19 @@ func (b kubectlBuilder) Exec() (string, error) {
2190
2212
select {
2191
2213
case err := <- errCh :
2192
2214
if err != nil {
2193
- var rc int = 127
2215
+ var rc = 127
2194
2216
if ee , ok := err .(* exec.ExitError ); ok {
2195
2217
rc = int (ee .Sys ().(syscall.WaitStatus ).ExitStatus ())
2196
2218
Logf ("rc: %d" , rc )
2197
2219
}
2198
2220
return "" , uexec.CodeExitError {
2199
- Err : fmt .Errorf ("error running %v:\n Command stdout:\n %v\n stderr:\n %v\n error:\n %v\n " , cmd , cmd .Stdout , cmd .Stderr , err ),
2221
+ Err : fmt .Errorf ("error running %v:\n Command stdout:\n %v\n stderr:\n %v\n error:\n %v" , cmd , cmd .Stdout , cmd .Stderr , err ),
2200
2222
Code : rc ,
2201
2223
}
2202
2224
}
2203
2225
case <- b .timeout :
2204
2226
b .cmd .Process .Kill ()
2205
- return "" , fmt .Errorf ("timed out waiting for command %v:\n Command stdout:\n %v\n stderr:\n %v\n " , cmd , cmd .Stdout , cmd .Stderr )
2227
+ return "" , fmt .Errorf ("timed out waiting for command %v:\n Command stdout:\n %v\n stderr:\n %v" , cmd , cmd .Stdout , cmd .Stderr )
2206
2228
}
2207
2229
Logf ("stderr: %q" , stderr .String ())
2208
2230
Logf ("stdout: %q" , stdout .String ())
@@ -2242,13 +2264,14 @@ func RunKubemciWithKubeconfig(args ...string) (string, error) {
2242
2264
func RunKubemciCmd (args ... string ) (string , error ) {
2243
2265
// kubemci is assumed to be in PATH.
2244
2266
kubemci := "kubemci"
2245
- b := new (kubectlBuilder )
2267
+ b := new (KubectlBuilder )
2246
2268
args = append (args , "--gcp-project=" + TestContext .CloudConfig .ProjectID )
2247
2269
2248
2270
b .cmd = exec .Command (kubemci , args ... )
2249
2271
return b .Exec ()
2250
2272
}
2251
2273
2274
+ // StartCmdAndStreamOutput returns stdout and stderr after starting the given cmd.
2252
2275
func StartCmdAndStreamOutput (cmd * exec.Cmd ) (stdout , stderr io.ReadCloser , err error ) {
2253
2276
stdout , err = cmd .StdoutPipe ()
2254
2277
if err != nil {
@@ -2263,7 +2286,7 @@ func StartCmdAndStreamOutput(cmd *exec.Cmd) (stdout, stderr io.ReadCloser, err e
2263
2286
return
2264
2287
}
2265
2288
2266
- // Rough equivalent of ctrl+c for cleaning up processes. Intended to be run in defer.
2289
+ // TryKill is rough equivalent of ctrl+c for cleaning up processes. Intended to be run in defer.
2267
2290
func TryKill (cmd * exec.Cmd ) {
2268
2291
if err := cmd .Process .Kill (); err != nil {
2269
2292
Logf ("ERROR failed to kill command %v! The process may leak" , cmd )
0 commit comments