add sync period to helm repo
Signed-off-by: LiHui <andrewli@kubesphere.io>
This commit is contained in:
@@ -71,6 +71,8 @@ const (
|
|||||||
OpenpitrixAttachmentTag = "Attachment"
|
OpenpitrixAttachmentTag = "Attachment"
|
||||||
OpenpitrixRepositoryTag = "Repository"
|
OpenpitrixRepositoryTag = "Repository"
|
||||||
OpenpitrixManagementTag = "App Management"
|
OpenpitrixManagementTag = "App Management"
|
||||||
|
// HelmRepoMinSyncPeriod min sync period in seconds
|
||||||
|
HelmRepoMinSyncPeriod = 180
|
||||||
|
|
||||||
CleanupDanglingAppOngoing = "ongoing"
|
CleanupDanglingAppOngoing = "ongoing"
|
||||||
CleanupDanglingAppDone = "done"
|
CleanupDanglingAppDone = "done"
|
||||||
|
|||||||
@@ -21,6 +21,9 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"kubesphere.io/kubesphere/pkg/constants"
|
||||||
|
"kubesphere.io/kubesphere/pkg/utils/mathutil"
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/api/errors"
|
"k8s.io/apimachinery/pkg/api/errors"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
@@ -42,9 +45,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// min sync period in seconds
|
|
||||||
MinSyncPeriod = 180
|
|
||||||
|
|
||||||
MinRetryDuration = 60
|
MinRetryDuration = 60
|
||||||
MaxRetryDuration = 600
|
MaxRetryDuration = 600
|
||||||
HelmRepoSyncStateLen = 10
|
HelmRepoSyncStateLen = 10
|
||||||
@@ -156,8 +156,8 @@ func (r *ReconcileHelmRepo) Reconcile(ctx context.Context, request reconcile.Req
|
|||||||
|
|
||||||
copyInstance := instance.DeepCopy()
|
copyInstance := instance.DeepCopy()
|
||||||
|
|
||||||
if copyInstance.Spec.SyncPeriod != 0 && copyInstance.Spec.SyncPeriod < MinSyncPeriod {
|
if copyInstance.Spec.SyncPeriod != 0 {
|
||||||
copyInstance.Spec.SyncPeriod = MinSyncPeriod
|
copyInstance.Spec.SyncPeriod = mathutil.Max(copyInstance.Spec.SyncPeriod, constants.HelmRepoMinSyncPeriod)
|
||||||
}
|
}
|
||||||
|
|
||||||
retryAfter := 0
|
retryAfter := 0
|
||||||
@@ -197,7 +197,7 @@ func (r *ReconcileHelmRepo) Reconcile(ctx context.Context, request reconcile.Req
|
|||||||
RequeueAfter: MinRetryDuration * time.Second,
|
RequeueAfter: MinRetryDuration * time.Second,
|
||||||
}, err
|
}, err
|
||||||
} else {
|
} else {
|
||||||
retryAfter = MinSyncPeriod
|
retryAfter = constants.HelmRepoMinSyncPeriod
|
||||||
if syncErr == nil {
|
if syncErr == nil {
|
||||||
retryAfter = copyInstance.Spec.SyncPeriod
|
retryAfter = copyInstance.Spec.SyncPeriod
|
||||||
}
|
}
|
||||||
@@ -256,9 +256,7 @@ func needReSyncNow(instance *v1alpha1.HelmRepo) (syncNow bool, after int) {
|
|||||||
} else {
|
} else {
|
||||||
period = instance.Spec.SyncPeriod
|
period = instance.Spec.SyncPeriod
|
||||||
if period != 0 {
|
if period != 0 {
|
||||||
if period < MinSyncPeriod {
|
period = mathutil.Max(instance.Spec.SyncPeriod, constants.HelmRepoMinSyncPeriod)
|
||||||
period = MinSyncPeriod
|
|
||||||
}
|
|
||||||
if now.After(state.SyncTime.Add(time.Duration(period) * time.Second)) {
|
if now.After(state.SyncTime.Add(time.Duration(period) * time.Second)) {
|
||||||
return true, 0
|
return true, 0
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,9 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"kubesphere.io/kubesphere/pkg/utils/mathutil"
|
||||||
|
|
||||||
restful "github.com/emicklei/go-restful"
|
restful "github.com/emicklei/go-restful"
|
||||||
"google.golang.org/grpc/codes"
|
"google.golang.org/grpc/codes"
|
||||||
@@ -90,6 +93,18 @@ func (h *openpitrixHandler) CreateRepo(req *restful.Request, resp *restful.Respo
|
|||||||
// trim credential from url
|
// trim credential from url
|
||||||
parsedUrl.User = nil
|
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{
|
repo := v1alpha1.HelmRepo{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: idutils.GetUuid36(v1alpha1.HelmRepoIdPrefix),
|
Name: idutils.GetUuid36(v1alpha1.HelmRepoIdPrefix),
|
||||||
@@ -103,11 +118,15 @@ func (h *openpitrixHandler) CreateRepo(req *restful.Request, resp *restful.Respo
|
|||||||
Spec: v1alpha1.HelmRepoSpec{
|
Spec: v1alpha1.HelmRepoSpec{
|
||||||
Name: createRepoRequest.Name,
|
Name: createRepoRequest.Name,
|
||||||
Url: parsedUrl.String(),
|
Url: parsedUrl.String(),
|
||||||
SyncPeriod: 0,
|
SyncPeriod: syncPeriod,
|
||||||
Description: stringutils.ShortenString(createRepoRequest.Description, 512),
|
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 strings.HasPrefix(createRepoRequest.URL, "https://") || strings.HasPrefix(createRepoRequest.URL, "http://") {
|
||||||
if userInfo != nil {
|
if userInfo != nil {
|
||||||
repo.Spec.Credential.Username = userInfo.Username()
|
repo.Spec.Credential.Username = userInfo.Username()
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"kubesphere.io/kubesphere/pkg/apiserver/query"
|
"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)
|
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
|
// modify name of the repo
|
||||||
if request.Name != nil && len(*request.Name) > 0 && *request.Name != repoCopy.Spec.Name {
|
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()}))
|
items, err := c.repoLister.List(labels.SelectorFromSet(map[string]string{constants.WorkspaceLabelKey: repo.GetWorkspace()}))
|
||||||
|
|||||||
@@ -585,6 +585,11 @@ type CreateRepoRequest struct {
|
|||||||
// required, runtime provider eg.[qingcloud|aliyun|aws|kubernetes]
|
// required, runtime provider eg.[qingcloud|aliyun|aws|kubernetes]
|
||||||
Providers []string `json:"providers"`
|
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
|
// repository type
|
||||||
Type string `json:"type,omitempty"`
|
Type string `json:"type,omitempty"`
|
||||||
|
|
||||||
@@ -612,6 +617,9 @@ type ModifyRepoRequest struct {
|
|||||||
|
|
||||||
Workspace *string `json:"workspace,omitempty"`
|
Workspace *string `json:"workspace,omitempty"`
|
||||||
|
|
||||||
|
// min sync period to sync helm repo
|
||||||
|
SyncPeriod *string `json:"sync_period"`
|
||||||
|
|
||||||
// repository name
|
// repository name
|
||||||
Name *string `json:"name,omitempty"`
|
Name *string `json:"name,omitempty"`
|
||||||
|
|
||||||
@@ -716,6 +724,8 @@ type Repo struct {
|
|||||||
|
|
||||||
// visibility.eg:[public|private]
|
// visibility.eg:[public|private]
|
||||||
Visibility string `json:"visibility,omitempty"`
|
Visibility string `json:"visibility,omitempty"`
|
||||||
|
|
||||||
|
SyncPeriod string `json:"sync_period,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CreateRepoResponse struct {
|
type CreateRepoResponse struct {
|
||||||
|
|||||||
@@ -427,6 +427,7 @@ func convertRepo(in *v1alpha1.HelmRepo) *Repo {
|
|||||||
|
|
||||||
cred, _ := json.Marshal(in.Spec.Credential)
|
cred, _ := json.Marshal(in.Spec.Credential)
|
||||||
out.Credential = string(cred)
|
out.Credential = string(cred)
|
||||||
|
out.SyncPeriod = in.Annotations[v1alpha1.RepoSyncPeriod]
|
||||||
|
|
||||||
out.URL = in.Spec.Url
|
out.URL = in.Spec.Url
|
||||||
return &out
|
return &out
|
||||||
|
|||||||
9
pkg/utils/mathutil/mathutil.go
Normal file
9
pkg/utils/mathutil/mathutil.go
Normal 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
|
||||||
|
}
|
||||||
@@ -59,5 +59,6 @@ const (
|
|||||||
|
|
||||||
ApplicationInstance = "app.kubesphere.io/instance"
|
ApplicationInstance = "app.kubesphere.io/instance"
|
||||||
|
|
||||||
|
RepoSyncPeriod = "app.kubesphere.io/sync-period"
|
||||||
OriginWorkspaceLabelKey = "kubesphere.io/workspace-origin"
|
OriginWorkspaceLabelKey = "kubesphere.io/workspace-origin"
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user