From b8d85fb75c14e6d5af768b39b37cec73e8b962f3 Mon Sep 17 00:00:00 2001 From: LiHui Date: Tue, 14 Sep 2021 10:39:01 +0800 Subject: [PATCH] add sync period to helm repo Signed-off-by: LiHui --- pkg/constants/constants.go | 2 ++ .../helmrepo/helm_repo_controller.go | 16 +++++------ pkg/kapis/openpitrix/v1/handler.go | 21 ++++++++++++++- pkg/models/openpitrix/repos.go | 27 +++++++++++++++++++ pkg/models/openpitrix/types.go | 10 +++++++ pkg/models/openpitrix/utils.go | 1 + pkg/utils/mathutil/mathutil.go | 9 +++++++ .../api/application/v1alpha1/constants.go | 1 + 8 files changed, 77 insertions(+), 10 deletions(-) create mode 100644 pkg/utils/mathutil/mathutil.go diff --git a/pkg/constants/constants.go b/pkg/constants/constants.go index 5d6169186..f5433ebfd 100644 --- a/pkg/constants/constants.go +++ b/pkg/constants/constants.go @@ -71,6 +71,8 @@ const ( OpenpitrixAttachmentTag = "Attachment" OpenpitrixRepositoryTag = "Repository" OpenpitrixManagementTag = "App Management" + // HelmRepoMinSyncPeriod min sync period in seconds + HelmRepoMinSyncPeriod = 180 CleanupDanglingAppOngoing = "ongoing" CleanupDanglingAppDone = "done" diff --git a/pkg/controller/openpitrix/helmrepo/helm_repo_controller.go b/pkg/controller/openpitrix/helmrepo/helm_repo_controller.go index 2a39c55c7..ddcdfd54a 100644 --- a/pkg/controller/openpitrix/helmrepo/helm_repo_controller.go +++ b/pkg/controller/openpitrix/helmrepo/helm_repo_controller.go @@ -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 } diff --git a/pkg/kapis/openpitrix/v1/handler.go b/pkg/kapis/openpitrix/v1/handler.go index e062fd136..fec511a51 100644 --- a/pkg/kapis/openpitrix/v1/handler.go +++ b/pkg/kapis/openpitrix/v1/handler.go @@ -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() diff --git a/pkg/models/openpitrix/repos.go b/pkg/models/openpitrix/repos.go index d4d587667..a8f696b18 100644 --- a/pkg/models/openpitrix/repos.go +++ b/pkg/models/openpitrix/repos.go @@ -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()})) diff --git a/pkg/models/openpitrix/types.go b/pkg/models/openpitrix/types.go index 3bff4d2ef..405c8ebc2 100644 --- a/pkg/models/openpitrix/types.go +++ b/pkg/models/openpitrix/types.go @@ -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 { diff --git a/pkg/models/openpitrix/utils.go b/pkg/models/openpitrix/utils.go index 0ea7ba7ae..cca74db47 100644 --- a/pkg/models/openpitrix/utils.go +++ b/pkg/models/openpitrix/utils.go @@ -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 diff --git a/pkg/utils/mathutil/mathutil.go b/pkg/utils/mathutil/mathutil.go new file mode 100644 index 000000000..b82829fc2 --- /dev/null +++ b/pkg/utils/mathutil/mathutil.go @@ -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 +} diff --git a/staging/src/kubesphere.io/api/application/v1alpha1/constants.go b/staging/src/kubesphere.io/api/application/v1alpha1/constants.go index 50d4ca7bd..426aa2d50 100644 --- a/staging/src/kubesphere.io/api/application/v1alpha1/constants.go +++ b/staging/src/kubesphere.io/api/application/v1alpha1/constants.go @@ -59,5 +59,6 @@ const ( ApplicationInstance = "app.kubesphere.io/instance" + RepoSyncPeriod = "app.kubesphere.io/sync-period" OriginWorkspaceLabelKey = "kubesphere.io/workspace-origin" )