Skip to content

Commit 4098347

Browse files
authored
Merge pull request kubernetes#76488 from atoato88/fix-golint-e2e-framework-util-go-part2
Fix golint failures of e2e/framework/util.go - part2
2 parents 56d7912 + 85f21c1 commit 4098347

File tree

3 files changed

+62
-39
lines changed

3 files changed

+62
-39
lines changed

test/e2e/apps/rc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func TestReplicationControllerServeImageOrFail(f *framework.Framework, test stri
164164
retryTimeout := 2 * time.Minute
165165
retryInterval := 5 * time.Second
166166
label := labels.SelectorFromSet(labels.Set(map[string]string{"name": name}))
167-
err = wait.Poll(retryInterval, retryTimeout, framework.PodProxyResponseChecker(f.ClientSet, f.Namespace.Name, label, name, true, pods).CheckAllResponses)
167+
err = wait.Poll(retryInterval, retryTimeout, framework.NewPodProxyResponseChecker(f.ClientSet, f.Namespace.Name, label, name, true, pods).CheckAllResponses)
168168
if err != nil {
169169
framework.Failf("Did not get expected responses within the timeout period of %.2f seconds.", retryTimeout.Seconds())
170170
}

test/e2e/apps/replica_set.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func testReplicaSetServeImageOrFail(f *framework.Framework, test string, image s
166166
retryTimeout := 2 * time.Minute
167167
retryInterval := 5 * time.Second
168168
label := labels.SelectorFromSet(labels.Set(map[string]string{"name": name}))
169-
err = wait.Poll(retryInterval, retryTimeout, framework.PodProxyResponseChecker(f.ClientSet, f.Namespace.Name, label, name, true, pods).CheckAllResponses)
169+
err = wait.Poll(retryInterval, retryTimeout, framework.NewPodProxyResponseChecker(f.ClientSet, f.Namespace.Name, label, name, true, pods).CheckAllResponses)
170170
if err != nil {
171171
framework.Failf("Did not get expected responses within the timeout period of %.2f seconds.", retryTimeout.Seconds())
172172
}

test/e2e/framework/util.go

Lines changed: 60 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,7 +1392,7 @@ func CheckInvariants(events []watch.Event, fns ...InvariantFunc) error {
13921392
return nil
13931393
}
13941394

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.
13961396
// Returns an error if timeout occurs first, or pod goes in to failed state.
13971397
func WaitForPodRunningInNamespace(c clientset.Interface, pod *v1.Pod) error {
13981398
if pod.Status.Phase == v1.PodRunning {
@@ -1401,19 +1401,20 @@ func WaitForPodRunningInNamespace(c clientset.Interface, pod *v1.Pod) error {
14011401
return WaitTimeoutForPodRunningInNamespace(c, pod.Name, pod.Namespace, PodStartTimeout)
14021402
}
14031403

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.
14051405
// Returns an error if timeout occurs first, or pod goes in to failed state.
14061406
func WaitForPodNameRunningInNamespace(c clientset.Interface, podName, namespace string) error {
14071407
return WaitTimeoutForPodRunningInNamespace(c, podName, namespace, PodStartTimeout)
14081408
}
14091409

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.
14111411
// The resourceVersion is used when Watching object changes, it tells since when we care
14121412
// about changes to the pod. Returns an error if timeout occurs first, or pod goes in to failed state.
14131413
func waitForPodRunningInNamespaceSlow(c clientset.Interface, podName, namespace string) error {
14141414
return WaitTimeoutForPodRunningInNamespace(c, podName, namespace, slowPodStartTimeout)
14151415
}
14161416

1417+
// WaitTimeoutForPodRunningInNamespace waits the given timeout duration for the specified pod to become running.
14171418
func WaitTimeoutForPodRunningInNamespace(c clientset.Interface, podName, namespace string, timeout time.Duration) error {
14181419
return wait.PollImmediate(Poll, timeout, podRunning(c, podName, namespace))
14191420
}
@@ -1434,7 +1435,7 @@ func podRunning(c clientset.Interface, podName, namespace string) wait.Condition
14341435
}
14351436
}
14361437

1437-
// WaitTimeoutForPodEvent waits for an event to occur for a pod
1438+
// WaitTimeoutForPodEvent waits the given timeout duration for a pod event to occur.
14381439
func WaitTimeoutForPodEvent(c clientset.Interface, podName, namespace, eventSelector, msg string, timeout time.Duration) error {
14391440
return wait.PollImmediate(Poll, timeout, eventOccurred(c, podName, namespace, eventSelector, msg))
14401441
}
@@ -1455,12 +1456,13 @@ func eventOccurred(c clientset.Interface, podName, namespace, eventSelector, msg
14551456
}
14561457
}
14571458

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.
14591460
// Returns an error if timeout occurs first.
14601461
func WaitForPodNoLongerRunningInNamespace(c clientset.Interface, podName, namespace string) error {
14611462
return WaitTimeoutForPodNoLongerRunningInNamespace(c, podName, namespace, DefaultPodDeletionTimeout)
14621463
}
14631464

1465+
// WaitTimeoutForPodNoLongerRunningInNamespace waits the given timeout duration for the specified pod to stop.
14641466
func WaitTimeoutForPodNoLongerRunningInNamespace(c clientset.Interface, podName, namespace string, timeout time.Duration) error {
14651467
return wait.PollImmediate(Poll, timeout, podCompleted(c, podName, namespace))
14661468
}
@@ -1533,9 +1535,8 @@ func waitForPodTerminatedInNamespace(c clientset.Interface, podName, reason, nam
15331535
if pod.Status.Phase == v1.PodFailed {
15341536
if pod.Status.Reason == reason { // short-circuit waitForPodCondition's loop
15351537
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)
15381538
}
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)
15391540
}
15401541
return false, nil
15411542
})
@@ -1618,6 +1619,7 @@ func WaitForRCToStabilize(c clientset.Interface, ns, name string, timeout time.D
16181619
return err
16191620
}
16201621

1622+
// WaitForPodToDisappear waits the given timeout duration for the specified pod to disappear.
16211623
func WaitForPodToDisappear(c clientset.Interface, ns, podName string, label labels.Selector, interval, timeout time.Duration) error {
16221624
return wait.PollImmediate(interval, timeout, func() (bool, error) {
16231625
Logf("Waiting for pod %s to disappear", podName)
@@ -1746,6 +1748,7 @@ func countEndpointsNum(e *v1.Endpoints) int {
17461748
return num
17471749
}
17481750

1751+
// WaitForEndpoint waits for the specified endpoint to be ready.
17491752
func WaitForEndpoint(c clientset.Interface, ns, name string) error {
17501753
for t := time.Now(); time.Since(t) < EndpointRegisterTimeout; time.Sleep(Poll) {
17511754
endpoint, err := c.CoreV1().Endpoints(ns).Get(name, metav1.GetOptions{})
@@ -1764,9 +1767,9 @@ func WaitForEndpoint(c clientset.Interface, ns, name string) error {
17641767
return fmt.Errorf("Failed to get endpoints for %s/%s", ns, name)
17651768
}
17661769

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
17681771
// proxy) and verifying that they answer with their own pod name.
1769-
type podProxyResponseChecker struct {
1772+
type PodProxyResponseChecker struct {
17701773
c clientset.Interface
17711774
ns string
17721775
label labels.Selector
@@ -1775,13 +1778,14 @@ type podProxyResponseChecker struct {
17751778
pods *v1.PodList
17761779
}
17771780

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}
17801784
}
17811785

17821786
// CheckAllResponses issues GETs to all pods in the context and verify they
17831787
// reply with their own pod name.
1784-
func (r podProxyResponseChecker) CheckAllResponses() (done bool, err error) {
1788+
func (r PodProxyResponseChecker) CheckAllResponses() (done bool, err error) {
17851789
successes := 0
17861790
options := metav1.ListOptions{LabelSelector: r.label.String()}
17871791
currentPods, err := r.c.CoreV1().Pods(r.ns).List(options)
@@ -1873,17 +1877,20 @@ func KubectlVersion() (*utilversion.Version, error) {
18731877
return utilversion.ParseSemantic(matches[1])
18741878
}
18751879

1880+
// PodsResponding waits for the pods to response.
18761881
func PodsResponding(c clientset.Interface, ns, name string, wantName bool, pods *v1.PodList) error {
18771882
ginkgo.By("trying to dial each unique pod")
18781883
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)
18801885
}
18811886

1887+
// PodsCreated returns a pod list matched by the given name.
18821888
func PodsCreated(c clientset.Interface, ns, name string, replicas int32) (*v1.PodList, error) {
18831889
label := labels.SelectorFromSet(labels.Set(map[string]string{"name": name}))
18841890
return PodsCreatedByLabel(c, ns, name, replicas, label)
18851891
}
18861892

1893+
// PodsCreatedByLabel returns a created pod list matched by the given label.
18871894
func PodsCreatedByLabel(c clientset.Interface, ns, name string, replicas int32, label labels.Selector) (*v1.PodList, error) {
18881895
timeout := 2 * time.Minute
18891896
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 {
19171924
// are running so non-running pods cause a timeout for this test.
19181925
ginkgo.By("ensuring each pod is running")
19191926
e := []error{}
1920-
error_chan := make(chan error)
1927+
errorChan := make(chan error)
19211928

19221929
for _, pod := range pods.Items {
19231930
go func(p v1.Pod) {
1924-
error_chan <- WaitForPodRunningInNamespace(c, &p)
1931+
errorChan <- WaitForPodRunningInNamespace(c, &p)
19251932
}(pod)
19261933
}
19271934

19281935
for range pods.Items {
1929-
err := <-error_chan
1936+
err := <-errorChan
19301937
if err != nil {
19311938
e = append(e, err)
19321939
}
@@ -1935,10 +1942,12 @@ func podsRunning(c clientset.Interface, pods *v1.PodList) []error {
19351942
return e
19361943
}
19371944

1945+
// VerifyPods checks if the specified pod is responding.
19381946
func VerifyPods(c clientset.Interface, ns, name string, wantName bool, replicas int32) error {
19391947
return podRunningMaybeResponding(c, ns, name, wantName, replicas, true)
19401948
}
19411949

1950+
// VerifyPodsRunning checks if the specified pod is running.
19421951
func VerifyPodsRunning(c clientset.Interface, ns, name string, wantName bool, replicas int32) error {
19431952
return podRunningMaybeResponding(c, ns, name, wantName, replicas, false)
19441953
}
@@ -1961,6 +1970,7 @@ func podRunningMaybeResponding(c clientset.Interface, ns, name string, wantName
19611970
return nil
19621971
}
19631972

1973+
// ServiceResponding waits for the service to be responding.
19641974
func ServiceResponding(c clientset.Interface, ns, name string) error {
19651975
ginkgo.By(fmt.Sprintf("trying to dial the service %s.%s via the proxy", ns, name))
19661976

@@ -1997,6 +2007,7 @@ func ServiceResponding(c clientset.Interface, ns, name string) error {
19972007
})
19982008
}
19992009

2010+
// RestclientConfig returns a config holds the information needed to build connection to kubernetes clusters.
20002011
func RestclientConfig(kubeContext string) (*clientcmdapi.Config, error) {
20012012
Logf(">>> kubeConfig: %s", TestContext.KubeConfig)
20022013
if TestContext.KubeConfig == "" {
@@ -2013,8 +2024,10 @@ func RestclientConfig(kubeContext string) (*clientcmdapi.Config, error) {
20132024
return c, nil
20142025
}
20152026

2027+
// ClientConfigGetter is a func that returns getter to return a config.
20162028
type ClientConfigGetter func() (*restclient.Config, error)
20172029

2030+
// LoadConfig returns a config for a rest client.
20182031
func LoadConfig() (*restclient.Config, error) {
20192032
if TestContext.NodeE2E {
20202033
// This is a node e2e test, apply the node e2e configuration
@@ -2024,14 +2037,14 @@ func LoadConfig() (*restclient.Config, error) {
20242037
if err != nil {
20252038
if TestContext.KubeConfig == "" {
20262039
return restclient.InClusterConfig()
2027-
} else {
2028-
return nil, err
20292040
}
2041+
return nil, err
20302042
}
20312043

20322044
return clientcmd.NewDefaultClientConfig(*c, &clientcmd.ConfigOverrides{ClusterInfo: clientcmdapi.Cluster{Server: TestContext.Host}}).ClientConfig()
20332045
}
20342046

2047+
// LoadClientset returns clientset for connecting to kubernetes clusters.
20352048
func LoadClientset() (*clientset.Clientset, error) {
20362049
config, err := LoadConfig()
20372050
if err != nil {
@@ -2040,7 +2053,7 @@ func LoadClientset() (*clientset.Clientset, error) {
20402053
return clientset.NewForConfig(config)
20412054
}
20422055

2043-
// randomSuffix provides a random string to append to pods,services,rcs.
2056+
// RandomSuffix provides a random string to append to pods,services,rcs.
20442057
// TODO: Allow service names to have the same form as names
20452058
// for pods and replication controllers so we don't
20462059
// need to use such a function and can instead
@@ -2050,6 +2063,7 @@ func RandomSuffix() string {
20502063
return strconv.Itoa(r.Int() % 10000)
20512064
}
20522065

2066+
// ExpectNoError checks if "err" is set, and if so, fails assertion while logging the error.
20532067
func ExpectNoError(err error, explain ...interface{}) {
20542068
ExpectNoErrorWithOffset(1, err, explain...)
20552069
}
@@ -2063,6 +2077,7 @@ func ExpectNoErrorWithOffset(offset int, err error, explain ...interface{}) {
20632077
gomega.ExpectWithOffset(1+offset, err).NotTo(gomega.HaveOccurred(), explain...)
20642078
}
20652079

2080+
// ExpectNoErrorWithRetries checks if an error occurs with the given retry count.
20662081
func ExpectNoErrorWithRetries(fn func() error, maxRetries int, explain ...interface{}) {
20672082
var err error
20682083
for i := 0; i < maxRetries; i++ {
@@ -2075,7 +2090,7 @@ func ExpectNoErrorWithRetries(fn func() error, maxRetries int, explain ...interf
20752090
gomega.ExpectWithOffset(1, err).NotTo(gomega.HaveOccurred(), explain...)
20762091
}
20772092

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.
20792094
func Cleanup(filePath, ns string, selectors ...string) {
20802095
ginkgo.By("using delete to clean up resources")
20812096
var nsArg string
@@ -2086,7 +2101,7 @@ func Cleanup(filePath, ns string, selectors ...string) {
20862101
AssertCleanup(ns, selectors...)
20872102
}
20882103

2089-
// Asserts that cleanup of a namespace wrt selectors occurred.
2104+
// AssertCleanup asserts that cleanup of a namespace wrt selectors occurred.
20902105
func AssertCleanup(ns string, selectors ...string) {
20912106
var nsArg string
20922107
if ns != "" {
@@ -2150,40 +2165,46 @@ func KubectlCmd(args ...string) *exec.Cmd {
21502165
return cmd
21512166
}
21522167

2153-
// kubectlBuilder is used to build, customize and execute a kubectl Command.
2168+
// KubectlBuilder is used to build, customize and execute a kubectl Command.
21542169
// Add more functions to customize the builder as needed.
2155-
type kubectlBuilder struct {
2170+
type KubectlBuilder struct {
21562171
cmd *exec.Cmd
21572172
timeout <-chan time.Time
21582173
}
21592174

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)
21622178
b.cmd = KubectlCmd(args...)
21632179
return b
21642180
}
21652181

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 {
21672184
b.cmd.Env = env
21682185
return b
21692186
}
21702187

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 {
21722190
b.timeout = t
21732191
return b
21742192
}
21752193

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 {
21772196
b.cmd.Stdin = strings.NewReader(data)
21782197
return &b
21792198
}
21802199

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 {
21822202
b.cmd.Stdin = reader
21832203
return &b
21842204
}
21852205

2186-
func (b kubectlBuilder) ExecOrDie() string {
2206+
// ExecOrDie runs the kubectl executable or dies if error occurs.
2207+
func (b KubectlBuilder) ExecOrDie() string {
21872208
str, err := b.Exec()
21882209
// In case of i/o timeout error, try talking to the apiserver again after 2s before dying.
21892210
// 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 {
22122233
return false
22132234
}
22142235

2215-
func (b kubectlBuilder) Exec() (string, error) {
2236+
// Exec runs the kubectl executable.
2237+
func (b KubectlBuilder) Exec() (string, error) {
22162238
var stdout, stderr bytes.Buffer
22172239
cmd := b.cmd
22182240
cmd.Stdout, cmd.Stderr = &stdout, &stderr
22192241

22202242
Logf("Running '%s %s'", cmd.Path, strings.Join(cmd.Args[1:], " ")) // skip arg[0] as it is printed separately
22212243
if err := cmd.Start(); err != nil {
2222-
return "", fmt.Errorf("error starting %v:\nCommand stdout:\n%v\nstderr:\n%v\nerror:\n%v\n", cmd, cmd.Stdout, cmd.Stderr, err)
2244+
return "", fmt.Errorf("error starting %v:\nCommand stdout:\n%v\nstderr:\n%v\nerror:\n%v", cmd, cmd.Stdout, cmd.Stderr, err)
22232245
}
22242246
errCh := make(chan error, 1)
22252247
go func() {
@@ -2228,19 +2250,19 @@ func (b kubectlBuilder) Exec() (string, error) {
22282250
select {
22292251
case err := <-errCh:
22302252
if err != nil {
2231-
var rc int = 127
2253+
var rc = 127
22322254
if ee, ok := err.(*exec.ExitError); ok {
22332255
rc = int(ee.Sys().(syscall.WaitStatus).ExitStatus())
22342256
Logf("rc: %d", rc)
22352257
}
22362258
return "", uexec.CodeExitError{
2237-
Err: fmt.Errorf("error running %v:\nCommand stdout:\n%v\nstderr:\n%v\nerror:\n%v\n", cmd, cmd.Stdout, cmd.Stderr, err),
2259+
Err: fmt.Errorf("error running %v:\nCommand stdout:\n%v\nstderr:\n%v\nerror:\n%v", cmd, cmd.Stdout, cmd.Stderr, err),
22382260
Code: rc,
22392261
}
22402262
}
22412263
case <-b.timeout:
22422264
b.cmd.Process.Kill()
2243-
return "", fmt.Errorf("timed out waiting for command %v:\nCommand stdout:\n%v\nstderr:\n%v\n", cmd, cmd.Stdout, cmd.Stderr)
2265+
return "", fmt.Errorf("timed out waiting for command %v:\nCommand stdout:\n%v\nstderr:\n%v", cmd, cmd.Stdout, cmd.Stderr)
22442266
}
22452267
Logf("stderr: %q", stderr.String())
22462268
Logf("stdout: %q", stdout.String())
@@ -2280,13 +2302,14 @@ func RunKubemciWithKubeconfig(args ...string) (string, error) {
22802302
func RunKubemciCmd(args ...string) (string, error) {
22812303
// kubemci is assumed to be in PATH.
22822304
kubemci := "kubemci"
2283-
b := new(kubectlBuilder)
2305+
b := new(KubectlBuilder)
22842306
args = append(args, "--gcp-project="+TestContext.CloudConfig.ProjectID)
22852307

22862308
b.cmd = exec.Command(kubemci, args...)
22872309
return b.Exec()
22882310
}
22892311

2312+
// StartCmdAndStreamOutput returns stdout and stderr after starting the given cmd.
22902313
func StartCmdAndStreamOutput(cmd *exec.Cmd) (stdout, stderr io.ReadCloser, err error) {
22912314
stdout, err = cmd.StdoutPipe()
22922315
if err != nil {
@@ -2301,7 +2324,7 @@ func StartCmdAndStreamOutput(cmd *exec.Cmd) (stdout, stderr io.ReadCloser, err e
23012324
return
23022325
}
23032326

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.
23052328
func TryKill(cmd *exec.Cmd) {
23062329
if err := cmd.Process.Kill(); err != nil {
23072330
Logf("ERROR failed to kill command %v! The process may leak", cmd)

0 commit comments

Comments
 (0)