add sync period to helm repo

Signed-off-by: LiHui <andrewli@kubesphere.io>
This commit is contained in:
LiHui
2021-09-14 10:39:01 +08:00
parent bab5cf27e3
commit b8d85fb75c
8 changed files with 77 additions and 10 deletions

View File

@@ -71,6 +71,8 @@ const (
OpenpitrixAttachmentTag = "Attachment"
OpenpitrixRepositoryTag = "Repository"
OpenpitrixManagementTag = "App Management"
// HelmRepoMinSyncPeriod min sync period in seconds
HelmRepoMinSyncPeriod = 180
CleanupDanglingAppOngoing = "ongoing"
CleanupDanglingAppDone = "done"

View File

@@ -21,6 +21,9 @@ import (
"math"
"time"
"kubesphere.io/kubesphere/pkg/constants"
"kubesphere.io/kubesphere/pkg/utils/mathutil"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
@@ -42,9 +45,6 @@ import (
)
const (
// min sync period in seconds
MinSyncPeriod = 180
MinRetryDuration = 60
MaxRetryDuration = 600
HelmRepoSyncStateLen = 10
@@ -156,8 +156,8 @@ func (r *ReconcileHelmRepo) Reconcile(ctx context.Context, request reconcile.Req
copyInstance := instance.DeepCopy()
if copyInstance.Spec.SyncPeriod != 0 && copyInstance.Spec.SyncPeriod < MinSyncPeriod {
copyInstance.Spec.SyncPeriod = MinSyncPeriod
if copyInstance.Spec.SyncPeriod != 0 {
copyInstance.Spec.SyncPeriod = mathutil.Max(copyInstance.Spec.SyncPeriod, constants.HelmRepoMinSyncPeriod)
}
retryAfter := 0
@@ -197,7 +197,7 @@ func (r *ReconcileHelmRepo) Reconcile(ctx context.Context, request reconcile.Req
RequeueAfter: MinRetryDuration * time.Second,
}, err
} else {
retryAfter = MinSyncPeriod
retryAfter = constants.HelmRepoMinSyncPeriod
if syncErr == nil {
retryAfter = copyInstance.Spec.SyncPeriod
}
@@ -256,9 +256,7 @@ func needReSyncNow(instance *v1alpha1.HelmRepo) (syncNow bool, after int) {
} else {
period = instance.Spec.SyncPeriod
if period != 0 {
if period < MinSyncPeriod {
period = MinSyncPeriod
}
period = mathutil.Max(instance.Spec.SyncPeriod, constants.HelmRepoMinSyncPeriod)
if now.After(state.SyncTime.Add(time.Duration(period) * time.Second)) {
return true, 0
}

View File

@@ -20,6 +20,9 @@ import (
"net/url"
"strconv"
"strings"
"time"
"kubesphere.io/kubesphere/pkg/utils/mathutil"
restful "github.com/emicklei/go-restful"
"google.golang.org/grpc/codes"
@@ -90,6 +93,18 @@ func (h *openpitrixHandler) CreateRepo(req *restful.Request, resp *restful.Respo
// trim credential from url
parsedUrl.User = nil
syncPeriod := 0
// If SyncPeriod is empty, ignore it.
if createRepoRequest.SyncPeriod != "" {
duration, err := time.ParseDuration(createRepoRequest.SyncPeriod)
if err != nil {
api.HandleBadRequest(resp, nil, err)
return
} else if duration > 0 {
syncPeriod = mathutil.Max(int(duration/time.Second), constants.HelmRepoMinSyncPeriod)
}
}
repo := v1alpha1.HelmRepo{
ObjectMeta: metav1.ObjectMeta{
Name: idutils.GetUuid36(v1alpha1.HelmRepoIdPrefix),
@@ -103,11 +118,15 @@ func (h *openpitrixHandler) CreateRepo(req *restful.Request, resp *restful.Respo
Spec: v1alpha1.HelmRepoSpec{
Name: createRepoRequest.Name,
Url: parsedUrl.String(),
SyncPeriod: 0,
SyncPeriod: syncPeriod,
Description: stringutils.ShortenString(createRepoRequest.Description, 512),
},
}
if syncPeriod > 0 {
repo.Annotations[v1alpha1.RepoSyncPeriod] = createRepoRequest.SyncPeriod
}
if strings.HasPrefix(createRepoRequest.URL, "https://") || strings.HasPrefix(createRepoRequest.URL, "http://") {
if userInfo != nil {
repo.Spec.Credential.Username = userInfo.Username()

View File

@@ -19,6 +19,7 @@ import (
"net/url"
"sort"
"strings"
"time"
"kubesphere.io/kubesphere/pkg/apiserver/query"
@@ -162,6 +163,32 @@ func (c *repoOperator) ModifyRepo(id string, request *ModifyRepoRequest) error {
repoCopy.Spec.Description = stringutils.ShortenString(*request.Description, DescriptionLen)
}
if repoCopy.Annotations == nil {
repoCopy.Annotations = map[string]string{}
}
if request.SyncPeriod != nil {
syncPeriod := 0
if *request.SyncPeriod == "" {
// disable auto sync
syncPeriod = 0
} else {
if duration, err := time.ParseDuration(*request.SyncPeriod); err != nil {
return err
} else {
syncPeriod = int(duration / time.Second)
}
}
if syncPeriod == 0 {
// disable auto sync
repoCopy.Spec.SyncPeriod = 0
delete(repoCopy.Annotations, v1alpha1.RepoSyncPeriod)
} else {
repoCopy.Spec.SyncPeriod = syncPeriod
repoCopy.Annotations[v1alpha1.RepoSyncPeriod] = *request.SyncPeriod
}
}
// modify name of the repo
if request.Name != nil && len(*request.Name) > 0 && *request.Name != repoCopy.Spec.Name {
items, err := c.repoLister.List(labels.SelectorFromSet(map[string]string{constants.WorkspaceLabelKey: repo.GetWorkspace()}))

View File

@@ -585,6 +585,11 @@ type CreateRepoRequest struct {
// required, runtime provider eg.[qingcloud|aliyun|aws|kubernetes]
Providers []string `json:"providers"`
// min sync period to sync helm repo, a duration string is a sequence of
// decimal numbers, each with optional fraction and a unit suffix,
// such as "180s", "2h" or "45m".
SyncPeriod string `json:"sync_period"`
// repository type
Type string `json:"type,omitempty"`
@@ -612,6 +617,9 @@ type ModifyRepoRequest struct {
Workspace *string `json:"workspace,omitempty"`
// min sync period to sync helm repo
SyncPeriod *string `json:"sync_period"`
// repository name
Name *string `json:"name,omitempty"`
@@ -716,6 +724,8 @@ type Repo struct {
// visibility.eg:[public|private]
Visibility string `json:"visibility,omitempty"`
SyncPeriod string `json:"sync_period,omitempty"`
}
type CreateRepoResponse struct {

View File

@@ -427,6 +427,7 @@ func convertRepo(in *v1alpha1.HelmRepo) *Repo {
cred, _ := json.Marshal(in.Spec.Credential)
out.Credential = string(cred)
out.SyncPeriod = in.Annotations[v1alpha1.RepoSyncPeriod]
out.URL = in.Spec.Url
return &out

View File

@@ -0,0 +1,9 @@
package mathutil
// Max returns the larger of a and b.
func Max(a, b int) int {
if a >= b {
return a
}
return b
}

View File

@@ -59,5 +59,6 @@ const (
ApplicationInstance = "app.kubesphere.io/instance"
RepoSyncPeriod = "app.kubesphere.io/sync-period"
OriginWorkspaceLabelKey = "kubesphere.io/workspace-origin"
)