Skip to content

Commit

Permalink
feat(setting): add support bundle timeout
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Yang <poan.yang@suse.com>
  • Loading branch information
FrankYang0529 authored and guangbochen committed Nov 30, 2021
1 parent 92a80f9 commit f6bc010
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pkg/controller/master/supportbundle/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package supportbundle

import (
"fmt"
"strconv"
"time"

ctlappsv1 "github.com/rancher/wrangler/pkg/generated/controllers/apps/v1"
Expand Down Expand Up @@ -51,7 +52,15 @@ func (h *Handler) OnSupportBundleChanged(key string, sb *harvesterv1.SupportBund
}

func (h *Handler) checkManagerStatus(sb *harvesterv1.SupportBundle) (*harvesterv1.SupportBundle, error) {
if time.Now().After(sb.CreationTimestamp.Add(types.BundleCreationTimeout)) {
var timeout int
var err error
if timeoutStr := settings.SupportBundleTimeout.Get(); timeoutStr != "" {
if timeout, err = strconv.Atoi(timeoutStr); err != nil {
return nil, err
}
}

if timeout != 0 && time.Now().After(sb.CreationTimestamp.Add(time.Duration(timeout)*time.Minute)) {
return h.setError(sb, "fail to generate supportbundle: timeout")
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
LogLevel = NewSetting("log-level", "info") // options are info, debug and trace
SupportBundleImage = NewSetting("support-bundle-image", "rancher/support-bundle-kit:v0.0.4")
SupportBundleImagePullPolicy = NewSetting("support-bundle-image-pull-policy", "IfNotPresent")
SupportBundleTimeout = NewSetting(SupportBundleTimeoutSettingName, "10") // Unit is minute. 0 means disable timeout.
DefaultStorageClass = NewSetting("default-storage-class", "longhorn")
HTTPProxy = NewSetting("http-proxy", "{}")
VMForceDeletionPolicySet = NewSetting(VMForceDeletionPolicySettingName, InitVMForceDeletionPolicy())
Expand All @@ -42,6 +43,7 @@ var (
const (
BackupTargetSettingName = "backup-target"
VMForceDeletionPolicySettingName = "vm-force-deletion-policy"
SupportBundleTimeoutSettingName = "support-bundle-timeout"
DefaultDashboardUIURL = "https://releases.rancher.com/harvester-ui/dashboard/latest/index.html"
)

Expand Down
17 changes: 17 additions & 0 deletions pkg/webhook/resources/setting/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"strconv"

"github.com/longhorn/backupstore"
_ "github.com/longhorn/backupstore/nfs"
Expand Down Expand Up @@ -34,6 +35,7 @@ var validateSettingFuncs = map[string]validateSettingFunc{
settings.VMForceDeletionPolicySettingName: validateVMForceDeletionPolicy,
overcommitConfigSettingName: validateOvercommitConfig,
vipPoolsConfigSettingName: validateVipPoolsConfig,
settings.SupportBundleTimeoutSettingName: validateSupportBundleTimeout,
}

func NewValidator(settingCache ctlv1beta1.SettingCache) types.Validator {
Expand Down Expand Up @@ -199,3 +201,18 @@ func validateVipPoolsConfig(setting *v1beta1.Setting) error {

return nil
}

func validateSupportBundleTimeout(setting *v1beta1.Setting) error {
if setting.Value == "" {
return nil
}

i, err := strconv.Atoi(setting.Value)
if err != nil {
return werror.NewInvalidError(err.Error(), "value")
}
if i < 0 {
return werror.NewInvalidError("timeout can't be negative", "value")
}
return nil
}
61 changes: 61 additions & 0 deletions pkg/webhook/resources/setting/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/harvester/harvester/pkg/apis/harvesterhci.io/v1beta1"
"github.com/harvester/harvester/pkg/settings"
)

func Test_validateOvercommitConfig(t *testing.T) {
Expand Down Expand Up @@ -59,3 +60,63 @@ func Test_validateOvercommitConfig(t *testing.T) {

}
}

func Test_validateSupportBundleTimeout(t *testing.T) {
tests := []struct {
name string
args *v1beta1.Setting
expectedErr bool
}{
{
name: "invalid int",
args: &v1beta1.Setting{
ObjectMeta: v1.ObjectMeta{Name: settings.SupportBundleTimeoutSettingName},
Value: "not int",
},
expectedErr: true,
},
{
name: "negative int",
args: &v1beta1.Setting{
ObjectMeta: v1.ObjectMeta{Name: settings.SupportBundleTimeoutSettingName},
Value: "-1",
},
expectedErr: true,
},
{
name: "input 0",
args: &v1beta1.Setting{
ObjectMeta: v1.ObjectMeta{Name: settings.SupportBundleTimeoutSettingName},
Value: "0",
},
expectedErr: false,
},
{
name: "empty input",
args: &v1beta1.Setting{
ObjectMeta: v1.ObjectMeta{Name: settings.SupportBundleTimeoutSettingName},
Value: "",
},
expectedErr: false,
},
{
name: "positive int",
args: &v1beta1.Setting{
ObjectMeta: v1.ObjectMeta{Name: settings.SupportBundleTimeoutSettingName},
Value: "1",
},
expectedErr: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateSupportBundleTimeout(tt.args)
if tt.expectedErr {
assert.Error(t, err)
} else {
assert.Nil(t, err)
}
})
}
}

0 comments on commit f6bc010

Please sign in to comment.