Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: GreetingModule sometimes occurs a timeout error #1371

Merged
merged 1 commit into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apis/kubekey/v1alpha2/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const (
DefaultClusterName = "cluster.local"
DefaultDNSDomain = "cluster.local"
DefaultArch = "amd64"
DefaultSSHTimeout = 10
DefaultSSHTimeout = 30
DefaultEtcdVersion = "v3.4.13"
DefaultEtcdPort = "2379"
DefaultDockerVersion = "20.10.8"
Expand Down
10 changes: 8 additions & 2 deletions pkg/bootstrap/precheck/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
package precheck

import (
"time"

"github.com/kubesphere/kubekey/pkg/common"
"github.com/kubesphere/kubekey/pkg/core/module"
"github.com/kubesphere/kubekey/pkg/core/prepare"
"github.com/kubesphere/kubekey/pkg/core/task"
"time"
)

type GreetingsModule struct {
Expand All @@ -32,13 +33,18 @@ func (h *GreetingsModule) Init() {
h.Name = "GreetingsModule"
h.Desc = "Greetings"

var timeout int64
for _, v := range h.Runtime.GetAllHosts() {
timeout += v.GetTimeout()
}

hello := &task.RemoteTask{
Name: "Greetings",
Desc: "Greetings",
Hosts: h.Runtime.GetAllHosts(),
Action: new(GreetingsTask),
Parallel: true,
Timeout: 30 * time.Second,
Timeout: time.Duration(timeout) * time.Second,
}

h.Tasks = []task.Interface{
Expand Down
8 changes: 5 additions & 3 deletions pkg/core/task/local_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@ package task
import (
"context"
"fmt"
"github.com/kubesphere/kubekey/pkg/core/rollback"
"time"

"github.com/pkg/errors"

"github.com/kubesphere/kubekey/pkg/core/action"
"github.com/kubesphere/kubekey/pkg/core/cache"
"github.com/kubesphere/kubekey/pkg/core/common"
"github.com/kubesphere/kubekey/pkg/core/connector"
"github.com/kubesphere/kubekey/pkg/core/ending"
"github.com/kubesphere/kubekey/pkg/core/logger"
"github.com/kubesphere/kubekey/pkg/core/prepare"
"github.com/pkg/errors"
"github.com/kubesphere/kubekey/pkg/core/rollback"
"github.com/kubesphere/kubekey/pkg/core/util"
)

type LocalTask struct {
Expand Down Expand Up @@ -119,7 +121,7 @@ func (l *LocalTask) RunWithTimeout(runtime connector.Runtime, host connector.Hos
go l.Run(runtime, host, resCh)
select {
case <-ctx.Done():
l.TaskResult.AppendErr(host, fmt.Errorf("execute task timeout, Timeout=%d", l.Timeout))
l.TaskResult.AppendErr(host, fmt.Errorf("execute task timeout, Timeout=%s", util.ShortDur(l.Timeout)))
case e := <-resCh:
if e != nil {
l.TaskResult.AppendErr(host, e)
Expand Down
7 changes: 4 additions & 3 deletions pkg/core/task/remote_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ package task
import (
"context"
"fmt"
"github.com/kubesphere/kubekey/pkg/core/rollback"
"sync"
"time"

"github.com/pkg/errors"

"github.com/kubesphere/kubekey/pkg/core/action"
"github.com/kubesphere/kubekey/pkg/core/cache"
"github.com/kubesphere/kubekey/pkg/core/connector"
"github.com/kubesphere/kubekey/pkg/core/ending"
"github.com/kubesphere/kubekey/pkg/core/logger"
"github.com/kubesphere/kubekey/pkg/core/prepare"
"github.com/kubesphere/kubekey/pkg/core/rollback"
"github.com/kubesphere/kubekey/pkg/core/util"
"github.com/pkg/errors"
)

type RemoteTask struct {
Expand Down Expand Up @@ -110,7 +111,7 @@ func (t *RemoteTask) RunWithTimeout(ctx context.Context, runtime connector.Runti

select {
case <-ctx.Done():
t.TaskResult.AppendErr(host, fmt.Errorf("execute task timeout, Timeout=%d", t.Timeout))
t.TaskResult.AppendErr(host, fmt.Errorf("execute task timeout, Timeout=%s", util.ShortDur(t.Timeout)))
case e := <-resCh:
if e != nil {
t.TaskResult.AppendErr(host, e)
Expand Down
33 changes: 33 additions & 0 deletions pkg/core/util/time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright 2022 The KubeSphere Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package util

import (
"strings"
"time"
)

func ShortDur(d time.Duration) string {
s := d.String()
if strings.HasSuffix(s, "m0s") {
s = s[:len(s)-2]
}
if strings.HasSuffix(s, "h0m") {
s = s[:len(s)-2]
}
return s
}
61 changes: 61 additions & 0 deletions pkg/core/util/time_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2022 The KubeSphere Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package util

import (
"testing"
"time"
)

func TestShortDur(t *testing.T) {
tests := []struct {
d time.Duration
want string
}{
{
d: 3 * time.Second,
want: "3s",
},
{
d: 60 * time.Second,
want: "1m",
},
{
d: 4 * time.Minute,
want: "4m",
},
{
d: 1*time.Hour + 3*time.Second,
want: "1h0m3s",
},
{
d: 1*time.Hour + 4*time.Minute,
want: "1h0m",
},
{
d: 1*time.Hour + 4*time.Minute + 3*time.Second,
want: "1h4m3s",
},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
if got := ShortDur(tt.d); got != tt.want {
t.Errorf("ShortDur() = %v, want %v", got, tt.want)
}
})
}
}