@@ -1392,7 +1392,7 @@ func CheckInvariants(events []watch.Event, fns ...InvariantFunc) error {
1392
1392
return nil
1393
1393
}
1394
1394
1395
- // Waits default amount of time (PodStartTimeout) for the specified pod to become running.
1395
+ // WaitForPodRunningInNamespace waits default amount of time (PodStartTimeout) for the specified pod to become running.
1396
1396
// Returns an error if timeout occurs first, or pod goes in to failed state.
1397
1397
func WaitForPodRunningInNamespace (c clientset.Interface , pod * v1.Pod ) error {
1398
1398
if pod .Status .Phase == v1 .PodRunning {
@@ -1401,19 +1401,20 @@ func WaitForPodRunningInNamespace(c clientset.Interface, pod *v1.Pod) error {
1401
1401
return WaitTimeoutForPodRunningInNamespace (c , pod .Name , pod .Namespace , PodStartTimeout )
1402
1402
}
1403
1403
1404
- // Waits default amount of time (PodStartTimeout) for the specified pod to become running.
1404
+ // WaitForPodNameRunningInNamespace waits default amount of time (PodStartTimeout) for the specified pod to become running.
1405
1405
// Returns an error if timeout occurs first, or pod goes in to failed state.
1406
1406
func WaitForPodNameRunningInNamespace (c clientset.Interface , podName , namespace string ) error {
1407
1407
return WaitTimeoutForPodRunningInNamespace (c , podName , namespace , PodStartTimeout )
1408
1408
}
1409
1409
1410
- // Waits an extended amount of time (slowPodStartTimeout) for the specified pod to become running.
1410
+ // waitForPodRunningInNamespaceSlow waits an extended amount of time (slowPodStartTimeout) for the specified pod to become running.
1411
1411
// The resourceVersion is used when Watching object changes, it tells since when we care
1412
1412
// about changes to the pod. Returns an error if timeout occurs first, or pod goes in to failed state.
1413
1413
func waitForPodRunningInNamespaceSlow (c clientset.Interface , podName , namespace string ) error {
1414
1414
return WaitTimeoutForPodRunningInNamespace (c , podName , namespace , slowPodStartTimeout )
1415
1415
}
1416
1416
1417
+ // WaitTimeoutForPodRunningInNamespace waits the given timeout duration for the specified pod to become running.
1417
1418
func WaitTimeoutForPodRunningInNamespace (c clientset.Interface , podName , namespace string , timeout time.Duration ) error {
1418
1419
return wait .PollImmediate (Poll , timeout , podRunning (c , podName , namespace ))
1419
1420
}
@@ -1434,7 +1435,7 @@ func podRunning(c clientset.Interface, podName, namespace string) wait.Condition
1434
1435
}
1435
1436
}
1436
1437
1437
- // WaitTimeoutForPodEvent waits for an event to occur for a pod
1438
+ // WaitTimeoutForPodEvent waits the given timeout duration for a pod event to occur.
1438
1439
func WaitTimeoutForPodEvent (c clientset.Interface , podName , namespace , eventSelector , msg string , timeout time.Duration ) error {
1439
1440
return wait .PollImmediate (Poll , timeout , eventOccurred (c , podName , namespace , eventSelector , msg ))
1440
1441
}
@@ -1455,12 +1456,13 @@ func eventOccurred(c clientset.Interface, podName, namespace, eventSelector, msg
1455
1456
}
1456
1457
}
1457
1458
1458
- // Waits default amount of time (DefaultPodDeletionTimeout) for the specified pod to stop running.
1459
+ // WaitForPodNoLongerRunningInNamespace waits default amount of time (DefaultPodDeletionTimeout) for the specified pod to stop running.
1459
1460
// Returns an error if timeout occurs first.
1460
1461
func WaitForPodNoLongerRunningInNamespace (c clientset.Interface , podName , namespace string ) error {
1461
1462
return WaitTimeoutForPodNoLongerRunningInNamespace (c , podName , namespace , DefaultPodDeletionTimeout )
1462
1463
}
1463
1464
1465
+ // WaitTimeoutForPodNoLongerRunningInNamespace waits the given timeout duration for the specified pod to stop.
1464
1466
func WaitTimeoutForPodNoLongerRunningInNamespace (c clientset.Interface , podName , namespace string , timeout time.Duration ) error {
1465
1467
return wait .PollImmediate (Poll , timeout , podCompleted (c , podName , namespace ))
1466
1468
}
@@ -1533,9 +1535,8 @@ func waitForPodTerminatedInNamespace(c clientset.Interface, podName, reason, nam
1533
1535
if pod .Status .Phase == v1 .PodFailed {
1534
1536
if pod .Status .Reason == reason { // short-circuit waitForPodCondition's loop
1535
1537
return true , nil
1536
- } else {
1537
- 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 )
1538
1538
}
1539
+ 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 )
1539
1540
}
1540
1541
return false , nil
1541
1542
})
@@ -1618,6 +1619,7 @@ func WaitForRCToStabilize(c clientset.Interface, ns, name string, timeout time.D
1618
1619
return err
1619
1620
}
1620
1621
1622
+ // WaitForPodToDisappear waits the given timeout duration for the specified pod to disappear.
1621
1623
func WaitForPodToDisappear (c clientset.Interface , ns , podName string , label labels.Selector , interval , timeout time.Duration ) error {
1622
1624
return wait .PollImmediate (interval , timeout , func () (bool , error ) {
1623
1625
Logf ("Waiting for pod %s to disappear" , podName )
@@ -1746,6 +1748,7 @@ func countEndpointsNum(e *v1.Endpoints) int {
1746
1748
return num
1747
1749
}
1748
1750
1751
+ // WaitForEndpoint waits for the specified endpoint to be ready.
1749
1752
func WaitForEndpoint (c clientset.Interface , ns , name string ) error {
1750
1753
for t := time .Now (); time .Since (t ) < EndpointRegisterTimeout ; time .Sleep (Poll ) {
1751
1754
endpoint , err := c .CoreV1 ().Endpoints (ns ).Get (name , metav1.GetOptions {})
@@ -1764,9 +1767,9 @@ func WaitForEndpoint(c clientset.Interface, ns, name string) error {
1764
1767
return fmt .Errorf ("Failed to get endpoints for %s/%s" , ns , name )
1765
1768
}
1766
1769
1767
- // Context for checking pods responses by issuing GETs to them (via the API
1770
+ // PodProxyResponseChecker is a context for checking pods responses by issuing GETs to them (via the API
1768
1771
// proxy) and verifying that they answer with their own pod name.
1769
- type podProxyResponseChecker struct {
1772
+ type PodProxyResponseChecker struct {
1770
1773
c clientset.Interface
1771
1774
ns string
1772
1775
label labels.Selector
@@ -1775,13 +1778,14 @@ type podProxyResponseChecker struct {
1775
1778
pods * v1.PodList
1776
1779
}
1777
1780
1778
- func PodProxyResponseChecker (c clientset.Interface , ns string , label labels.Selector , controllerName string , respondName bool , pods * v1.PodList ) podProxyResponseChecker {
1779
- return podProxyResponseChecker {c , ns , label , controllerName , respondName , pods }
1781
+ // NewPodProxyResponseChecker returns a context for checking pods responses.
1782
+ func NewPodProxyResponseChecker (c clientset.Interface , ns string , label labels.Selector , controllerName string , respondName bool , pods * v1.PodList ) PodProxyResponseChecker {
1783
+ return PodProxyResponseChecker {c , ns , label , controllerName , respondName , pods }
1780
1784
}
1781
1785
1782
1786
// CheckAllResponses issues GETs to all pods in the context and verify they
1783
1787
// reply with their own pod name.
1784
- func (r podProxyResponseChecker ) CheckAllResponses () (done bool , err error ) {
1788
+ func (r PodProxyResponseChecker ) CheckAllResponses () (done bool , err error ) {
1785
1789
successes := 0
1786
1790
options := metav1.ListOptions {LabelSelector : r .label .String ()}
1787
1791
currentPods , err := r .c .CoreV1 ().Pods (r .ns ).List (options )
@@ -1873,17 +1877,20 @@ func KubectlVersion() (*utilversion.Version, error) {
1873
1877
return utilversion .ParseSemantic (matches [1 ])
1874
1878
}
1875
1879
1880
+ // PodsResponding waits for the pods to response.
1876
1881
func PodsResponding (c clientset.Interface , ns , name string , wantName bool , pods * v1.PodList ) error {
1877
1882
ginkgo .By ("trying to dial each unique pod" )
1878
1883
label := labels .SelectorFromSet (labels .Set (map [string ]string {"name" : name }))
1879
- return wait .PollImmediate (Poll , podRespondingTimeout , PodProxyResponseChecker (c , ns , label , name , wantName , pods ).CheckAllResponses )
1884
+ return wait .PollImmediate (Poll , podRespondingTimeout , NewPodProxyResponseChecker (c , ns , label , name , wantName , pods ).CheckAllResponses )
1880
1885
}
1881
1886
1887
+ // PodsCreated returns a pod list matched by the given name.
1882
1888
func PodsCreated (c clientset.Interface , ns , name string , replicas int32 ) (* v1.PodList , error ) {
1883
1889
label := labels .SelectorFromSet (labels .Set (map [string ]string {"name" : name }))
1884
1890
return PodsCreatedByLabel (c , ns , name , replicas , label )
1885
1891
}
1886
1892
1893
+ // PodsCreatedByLabel returns a created pod list matched by the given label.
1887
1894
func PodsCreatedByLabel (c clientset.Interface , ns , name string , replicas int32 , label labels.Selector ) (* v1.PodList , error ) {
1888
1895
timeout := 2 * time .Minute
1889
1896
for start := time .Now (); time .Since (start ) < timeout ; time .Sleep (5 * time .Second ) {
@@ -1917,16 +1924,16 @@ func podsRunning(c clientset.Interface, pods *v1.PodList) []error {
1917
1924
// are running so non-running pods cause a timeout for this test.
1918
1925
ginkgo .By ("ensuring each pod is running" )
1919
1926
e := []error {}
1920
- error_chan := make (chan error )
1927
+ errorChan := make (chan error )
1921
1928
1922
1929
for _ , pod := range pods .Items {
1923
1930
go func (p v1.Pod ) {
1924
- error_chan <- WaitForPodRunningInNamespace (c , & p )
1931
+ errorChan <- WaitForPodRunningInNamespace (c , & p )
1925
1932
}(pod )
1926
1933
}
1927
1934
1928
1935
for range pods .Items {
1929
- err := <- error_chan
1936
+ err := <- errorChan
1930
1937
if err != nil {
1931
1938
e = append (e , err )
1932
1939
}
@@ -1935,10 +1942,12 @@ func podsRunning(c clientset.Interface, pods *v1.PodList) []error {
1935
1942
return e
1936
1943
}
1937
1944
1945
+ // VerifyPods checks if the specified pod is responding.
1938
1946
func VerifyPods (c clientset.Interface , ns , name string , wantName bool , replicas int32 ) error {
1939
1947
return podRunningMaybeResponding (c , ns , name , wantName , replicas , true )
1940
1948
}
1941
1949
1950
+ // VerifyPodsRunning checks if the specified pod is running.
1942
1951
func VerifyPodsRunning (c clientset.Interface , ns , name string , wantName bool , replicas int32 ) error {
1943
1952
return podRunningMaybeResponding (c , ns , name , wantName , replicas , false )
1944
1953
}
@@ -1961,6 +1970,7 @@ func podRunningMaybeResponding(c clientset.Interface, ns, name string, wantName
1961
1970
return nil
1962
1971
}
1963
1972
1973
+ // ServiceResponding waits for the service to be responding.
1964
1974
func ServiceResponding (c clientset.Interface , ns , name string ) error {
1965
1975
ginkgo .By (fmt .Sprintf ("trying to dial the service %s.%s via the proxy" , ns , name ))
1966
1976
@@ -1997,6 +2007,7 @@ func ServiceResponding(c clientset.Interface, ns, name string) error {
1997
2007
})
1998
2008
}
1999
2009
2010
+ // RestclientConfig returns a config holds the information needed to build connection to kubernetes clusters.
2000
2011
func RestclientConfig (kubeContext string ) (* clientcmdapi.Config , error ) {
2001
2012
Logf (">>> kubeConfig: %s" , TestContext .KubeConfig )
2002
2013
if TestContext .KubeConfig == "" {
@@ -2013,8 +2024,10 @@ func RestclientConfig(kubeContext string) (*clientcmdapi.Config, error) {
2013
2024
return c , nil
2014
2025
}
2015
2026
2027
+ // ClientConfigGetter is a func that returns getter to return a config.
2016
2028
type ClientConfigGetter func () (* restclient.Config , error )
2017
2029
2030
+ // LoadConfig returns a config for a rest client.
2018
2031
func LoadConfig () (* restclient.Config , error ) {
2019
2032
if TestContext .NodeE2E {
2020
2033
// This is a node e2e test, apply the node e2e configuration
@@ -2024,14 +2037,14 @@ func LoadConfig() (*restclient.Config, error) {
2024
2037
if err != nil {
2025
2038
if TestContext .KubeConfig == "" {
2026
2039
return restclient .InClusterConfig ()
2027
- } else {
2028
- return nil , err
2029
2040
}
2041
+ return nil , err
2030
2042
}
2031
2043
2032
2044
return clientcmd .NewDefaultClientConfig (* c , & clientcmd.ConfigOverrides {ClusterInfo : clientcmdapi.Cluster {Server : TestContext .Host }}).ClientConfig ()
2033
2045
}
2034
2046
2047
+ // LoadClientset returns clientset for connecting to kubernetes clusters.
2035
2048
func LoadClientset () (* clientset.Clientset , error ) {
2036
2049
config , err := LoadConfig ()
2037
2050
if err != nil {
@@ -2040,7 +2053,7 @@ func LoadClientset() (*clientset.Clientset, error) {
2040
2053
return clientset .NewForConfig (config )
2041
2054
}
2042
2055
2043
- // randomSuffix provides a random string to append to pods,services,rcs.
2056
+ // RandomSuffix provides a random string to append to pods,services,rcs.
2044
2057
// TODO: Allow service names to have the same form as names
2045
2058
// for pods and replication controllers so we don't
2046
2059
// need to use such a function and can instead
@@ -2050,6 +2063,7 @@ func RandomSuffix() string {
2050
2063
return strconv .Itoa (r .Int () % 10000 )
2051
2064
}
2052
2065
2066
+ // ExpectNoError checks if "err" is set, and if so, fails assertion while logging the error.
2053
2067
func ExpectNoError (err error , explain ... interface {}) {
2054
2068
ExpectNoErrorWithOffset (1 , err , explain ... )
2055
2069
}
@@ -2063,6 +2077,7 @@ func ExpectNoErrorWithOffset(offset int, err error, explain ...interface{}) {
2063
2077
gomega .ExpectWithOffset (1 + offset , err ).NotTo (gomega .HaveOccurred (), explain ... )
2064
2078
}
2065
2079
2080
+ // ExpectNoErrorWithRetries checks if an error occurs with the given retry count.
2066
2081
func ExpectNoErrorWithRetries (fn func () error , maxRetries int , explain ... interface {}) {
2067
2082
var err error
2068
2083
for i := 0 ; i < maxRetries ; i ++ {
@@ -2075,7 +2090,7 @@ func ExpectNoErrorWithRetries(fn func() error, maxRetries int, explain ...interf
2075
2090
gomega .ExpectWithOffset (1 , err ).NotTo (gomega .HaveOccurred (), explain ... )
2076
2091
}
2077
2092
2078
- // Stops everything from filePath from namespace ns and checks if everything matching selectors from the given namespace is correctly stopped.
2093
+ // Cleanup stops everything from filePath from namespace ns and checks if everything matching selectors from the given namespace is correctly stopped.
2079
2094
func Cleanup (filePath , ns string , selectors ... string ) {
2080
2095
ginkgo .By ("using delete to clean up resources" )
2081
2096
var nsArg string
@@ -2086,7 +2101,7 @@ func Cleanup(filePath, ns string, selectors ...string) {
2086
2101
AssertCleanup (ns , selectors ... )
2087
2102
}
2088
2103
2089
- // Asserts that cleanup of a namespace wrt selectors occurred.
2104
+ // AssertCleanup asserts that cleanup of a namespace wrt selectors occurred.
2090
2105
func AssertCleanup (ns string , selectors ... string ) {
2091
2106
var nsArg string
2092
2107
if ns != "" {
@@ -2150,40 +2165,46 @@ func KubectlCmd(args ...string) *exec.Cmd {
2150
2165
return cmd
2151
2166
}
2152
2167
2153
- // kubectlBuilder is used to build, customize and execute a kubectl Command.
2168
+ // KubectlBuilder is used to build, customize and execute a kubectl Command.
2154
2169
// Add more functions to customize the builder as needed.
2155
- type kubectlBuilder struct {
2170
+ type KubectlBuilder struct {
2156
2171
cmd * exec.Cmd
2157
2172
timeout <- chan time.Time
2158
2173
}
2159
2174
2160
- func NewKubectlCommand (args ... string ) * kubectlBuilder {
2161
- b := new (kubectlBuilder )
2175
+ // NewKubectlCommand returns a KubectlBuilder for running kubectl.
2176
+ func NewKubectlCommand (args ... string ) * KubectlBuilder {
2177
+ b := new (KubectlBuilder )
2162
2178
b .cmd = KubectlCmd (args ... )
2163
2179
return b
2164
2180
}
2165
2181
2166
- func (b * kubectlBuilder ) WithEnv (env []string ) * kubectlBuilder {
2182
+ // WithEnv sets the given environment and returns itself.
2183
+ func (b * KubectlBuilder ) WithEnv (env []string ) * KubectlBuilder {
2167
2184
b .cmd .Env = env
2168
2185
return b
2169
2186
}
2170
2187
2171
- func (b * kubectlBuilder ) WithTimeout (t <- chan time.Time ) * kubectlBuilder {
2188
+ // WithTimeout sets the given timeout and returns itself.
2189
+ func (b * KubectlBuilder ) WithTimeout (t <- chan time.Time ) * KubectlBuilder {
2172
2190
b .timeout = t
2173
2191
return b
2174
2192
}
2175
2193
2176
- func (b kubectlBuilder ) WithStdinData (data string ) * kubectlBuilder {
2194
+ // WithStdinData sets the given data to stdin and returns itself.
2195
+ func (b KubectlBuilder ) WithStdinData (data string ) * KubectlBuilder {
2177
2196
b .cmd .Stdin = strings .NewReader (data )
2178
2197
return & b
2179
2198
}
2180
2199
2181
- func (b kubectlBuilder ) WithStdinReader (reader io.Reader ) * kubectlBuilder {
2200
+ // WithStdinReader sets the given reader and returns itself.
2201
+ func (b KubectlBuilder ) WithStdinReader (reader io.Reader ) * KubectlBuilder {
2182
2202
b .cmd .Stdin = reader
2183
2203
return & b
2184
2204
}
2185
2205
2186
- func (b kubectlBuilder ) ExecOrDie () string {
2206
+ // ExecOrDie runs the kubectl executable or dies if error occurs.
2207
+ func (b KubectlBuilder ) ExecOrDie () string {
2187
2208
str , err := b .Exec ()
2188
2209
// In case of i/o timeout error, try talking to the apiserver again after 2s before dying.
2189
2210
// Note that we're still dying after retrying so that we can get visibility to triage it further.
@@ -2212,14 +2233,15 @@ func isTimeout(err error) bool {
2212
2233
return false
2213
2234
}
2214
2235
2215
- func (b kubectlBuilder ) Exec () (string , error ) {
2236
+ // Exec runs the kubectl executable.
2237
+ func (b KubectlBuilder ) Exec () (string , error ) {
2216
2238
var stdout , stderr bytes.Buffer
2217
2239
cmd := b .cmd
2218
2240
cmd .Stdout , cmd .Stderr = & stdout , & stderr
2219
2241
2220
2242
Logf ("Running '%s %s'" , cmd .Path , strings .Join (cmd .Args [1 :], " " )) // skip arg[0] as it is printed separately
2221
2243
if err := cmd .Start (); err != nil {
2222
- 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 )
2244
+ 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 )
2223
2245
}
2224
2246
errCh := make (chan error , 1 )
2225
2247
go func () {
@@ -2228,19 +2250,19 @@ func (b kubectlBuilder) Exec() (string, error) {
2228
2250
select {
2229
2251
case err := <- errCh :
2230
2252
if err != nil {
2231
- var rc int = 127
2253
+ var rc = 127
2232
2254
if ee , ok := err .(* exec.ExitError ); ok {
2233
2255
rc = int (ee .Sys ().(syscall.WaitStatus ).ExitStatus ())
2234
2256
Logf ("rc: %d" , rc )
2235
2257
}
2236
2258
return "" , uexec.CodeExitError {
2237
- 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 ),
2259
+ 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 ),
2238
2260
Code : rc ,
2239
2261
}
2240
2262
}
2241
2263
case <- b .timeout :
2242
2264
b .cmd .Process .Kill ()
2243
- return "" , fmt .Errorf ("timed out waiting for command %v:\n Command stdout:\n %v\n stderr:\n %v\n " , cmd , cmd .Stdout , cmd .Stderr )
2265
+ return "" , fmt .Errorf ("timed out waiting for command %v:\n Command stdout:\n %v\n stderr:\n %v" , cmd , cmd .Stdout , cmd .Stderr )
2244
2266
}
2245
2267
Logf ("stderr: %q" , stderr .String ())
2246
2268
Logf ("stdout: %q" , stdout .String ())
@@ -2280,13 +2302,14 @@ func RunKubemciWithKubeconfig(args ...string) (string, error) {
2280
2302
func RunKubemciCmd (args ... string ) (string , error ) {
2281
2303
// kubemci is assumed to be in PATH.
2282
2304
kubemci := "kubemci"
2283
- b := new (kubectlBuilder )
2305
+ b := new (KubectlBuilder )
2284
2306
args = append (args , "--gcp-project=" + TestContext .CloudConfig .ProjectID )
2285
2307
2286
2308
b .cmd = exec .Command (kubemci , args ... )
2287
2309
return b .Exec ()
2288
2310
}
2289
2311
2312
+ // StartCmdAndStreamOutput returns stdout and stderr after starting the given cmd.
2290
2313
func StartCmdAndStreamOutput (cmd * exec.Cmd ) (stdout , stderr io.ReadCloser , err error ) {
2291
2314
stdout , err = cmd .StdoutPipe ()
2292
2315
if err != nil {
@@ -2301,7 +2324,7 @@ func StartCmdAndStreamOutput(cmd *exec.Cmd) (stdout, stderr io.ReadCloser, err e
2301
2324
return
2302
2325
}
2303
2326
2304
- // Rough equivalent of ctrl+c for cleaning up processes. Intended to be run in defer.
2327
+ // TryKill is rough equivalent of ctrl+c for cleaning up processes. Intended to be run in defer.
2305
2328
func TryKill (cmd * exec.Cmd ) {
2306
2329
if err := cmd .Process .Kill (); err != nil {
2307
2330
Logf ("ERROR failed to kill command %v! The process may leak" , cmd )
0 commit comments