From 542eb180c575bb4ad6da093642e3aba9ad14219b Mon Sep 17 00:00:00 2001 From: LiHui Date: Wed, 24 Mar 2021 18:19:12 +0800 Subject: [PATCH 1/3] Fix: return app creator Signed-off-by: LiHui --- .../v1alpha1/helmapplication_types.go | 4 +++ .../helm_application_controller.go | 28 +++++++++++-------- pkg/models/openpitrix/utils.go | 1 + 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/pkg/apis/application/v1alpha1/helmapplication_types.go b/pkg/apis/application/v1alpha1/helmapplication_types.go index 93fea801c..044f83930 100644 --- a/pkg/apis/application/v1alpha1/helmapplication_types.go +++ b/pkg/apis/application/v1alpha1/helmapplication_types.go @@ -126,3 +126,7 @@ func (in *HelmApplication) State() string { } return in.Status.State } + +func (in *HelmApplication) GetCreator() string { + return getValue(in.Annotations, constants.CreatorAnnotationKey) +} diff --git a/pkg/controller/openpitrix/helmapplication/helm_application_controller.go b/pkg/controller/openpitrix/helmapplication/helm_application_controller.go index ce2bd8d95..eef07c772 100644 --- a/pkg/controller/openpitrix/helmapplication/helm_application_controller.go +++ b/pkg/controller/openpitrix/helmapplication/helm_application_controller.go @@ -69,7 +69,7 @@ func (r *ReconcileHelmApplication) Reconcile(request reconcile.Request) (reconci if !sliceutil.HasString(app.ObjectMeta.Finalizers, appFinalizer) { app.ObjectMeta.Finalizers = append(app.ObjectMeta.Finalizers, appFinalizer) if err := r.Update(rootCtx, app); err != nil { - return ctrl.Result{}, err + return reconcile.Result{}, err } // create app success appOperationTotal.WithLabelValues("creation", app.GetTrueName(), strconv.FormatBool(inAppStore(app))).Inc() @@ -78,7 +78,11 @@ func (r *ReconcileHelmApplication) Reconcile(request reconcile.Request) (reconci if !inAppStore(app) { if app.Status.State == v1alpha1.StateActive || app.Status.State == v1alpha1.StateSuspended { - return reconcile.Result{}, r.createAppCopyInAppStore(rootCtx, app) + if err := r.createAppCopyInAppStore(rootCtx, app); err != nil { + klog.Errorf("create app copy failed, error: %s", err) + return reconcile.Result{}, err + } + return reconcile.Result{}, nil } } } else { @@ -120,9 +124,9 @@ func (r *ReconcileHelmApplication) deleteAppCopyInAppStore(ctx context.Context, return nil } -// create a application copy in app store -func (r *ReconcileHelmApplication) createAppCopyInAppStore(ctx context.Context, from *v1alpha1.HelmApplication) error { - name := fmt.Sprintf("%s%s", from.Name, v1alpha1.HelmApplicationAppStoreSuffix) +// createAppCopyInAppStore create a application copy in app store +func (r *ReconcileHelmApplication) createAppCopyInAppStore(ctx context.Context, originApp *v1alpha1.HelmApplication) error { + name := fmt.Sprintf("%s%s", originApp.Name, v1alpha1.HelmApplicationAppStoreSuffix) app := &v1alpha1.HelmApplication{} err := r.Get(ctx, types.NamespacedName{Name: name}, app) @@ -132,23 +136,25 @@ func (r *ReconcileHelmApplication) createAppCopyInAppStore(ctx context.Context, if app.Name == "" { app.Name = name - labels := from.Labels + labels := originApp.Labels if len(labels) == 0 { labels = make(map[string]string, 3) } labels[constants.ChartRepoIdLabelKey] = v1alpha1.AppStoreRepoId - // assign a category to app + // assign a default category to app if labels[constants.CategoryIdLabelKey] == "" { labels[constants.CategoryIdLabelKey] = v1alpha1.UncategorizedId } - labels[v1alpha1.OriginWorkspaceLabelKey] = from.GetWorkspace() - + // record the original workspace + labels[v1alpha1.OriginWorkspaceLabelKey] = originApp.GetWorkspace() // apps in store are global resource. delete(labels, constants.WorkspaceLabelKey) + + app.Annotations = originApp.Annotations app.Labels = labels - app.Spec = *from.Spec.DeepCopy() + app.Spec = *originApp.Spec.DeepCopy() err = r.Create(context.TODO(), app) if err != nil { @@ -158,7 +164,7 @@ func (r *ReconcileHelmApplication) createAppCopyInAppStore(ctx context.Context, if app.Status.State == "" { // update status if needed - return updateHelmApplicationStatus(r.Client, from.Name, true) + return updateHelmApplicationStatus(r.Client, originApp.Name, true) } return nil diff --git a/pkg/models/openpitrix/utils.go b/pkg/models/openpitrix/utils.go index 9ca7c4cfe..d28ca70d1 100644 --- a/pkg/models/openpitrix/utils.go +++ b/pkg/models/openpitrix/utils.go @@ -331,6 +331,7 @@ func convertApp(app *v1alpha1.HelmApplication, versions []*v1alpha1.HelmApplicat out.Isv = app.GetWorkspace() out.ClusterTotal = &rlsCount + out.Owner = app.GetCreator() return out } From 9fc0f8d89b43148c8ba4a2007dbfdd40eff87976 Mon Sep 17 00:00:00 2001 From: LiHui Date: Wed, 24 Mar 2021 18:33:05 +0800 Subject: [PATCH 2/3] Fix: remove checks of the exists of release when delete app and repo Signed-off-by: LiHui --- pkg/kapis/openpitrix/v1/handler.go | 7 ++++++- pkg/models/openpitrix/applications.go | 13 ++---------- pkg/models/openpitrix/applicationversions.go | 22 -------------------- pkg/models/openpitrix/release.go | 5 +++++ pkg/models/openpitrix/repos.go | 19 ++++------------- 5 files changed, 17 insertions(+), 49 deletions(-) diff --git a/pkg/kapis/openpitrix/v1/handler.go b/pkg/kapis/openpitrix/v1/handler.go index a3e2c1ddd..54be36327 100644 --- a/pkg/kapis/openpitrix/v1/handler.go +++ b/pkg/kapis/openpitrix/v1/handler.go @@ -20,6 +20,7 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "io/ioutil" + apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/klog" "kubesphere.io/kubesphere/pkg/api" @@ -595,7 +596,11 @@ func (h *openpitrixHandler) GetAppVersionFiles(req *restful.Request, resp *restf if err != nil { klog.Errorln(err) - handleOpenpitrixError(resp, err) + if apierrors.IsNotFound(err) { + api.HandleNotFound(resp, nil, err) + } else { + api.HandleBadRequest(resp, nil, err) + } return } diff --git a/pkg/models/openpitrix/applications.go b/pkg/models/openpitrix/applications.go index 316093a29..fa08e010b 100644 --- a/pkg/models/openpitrix/applications.go +++ b/pkg/models/openpitrix/applications.go @@ -324,25 +324,16 @@ func (c *applicationOperator) DeleteApp(id string) error { app, err := c.appLister.Get(id) if err != nil { if apierrors.IsNotFound(err) { - klog.V(4).Infof("app %s has been deleted", id) return nil } else { - klog.Error(err) - return nil + klog.Errorf("get app %s failed, error: %s", id, err) + return err } } ls := map[string]string{ constants.ChartApplicationIdLabelKey: app.GetHelmApplicationId(), } - releases, err := c.rlsLister.List(labels.SelectorFromSet(ls)) - - if err != nil && !apierrors.IsNotFound(err) { - klog.Error(err) - return err - } else if len(releases) > 0 { - return fmt.Errorf("app %s has releases not deleted", id) - } list, err := c.versionLister.List(labels.SelectorFromSet(ls)) if err != nil { diff --git a/pkg/models/openpitrix/applicationversions.go b/pkg/models/openpitrix/applicationversions.go index f7d2f8929..c48a9d480 100644 --- a/pkg/models/openpitrix/applicationversions.go +++ b/pkg/models/openpitrix/applicationversions.go @@ -114,28 +114,6 @@ func (c *applicationOperator) DeleteAppVersion(id string) error { return actionNotPermitted } - // check release - rls, err := c.rlsLister.List(labels.SelectorFromSet(map[string]string{constants.ChartApplicationVersionIdLabelKey: id})) - if err != nil && !apierrors.IsNotFound(err) { - return err - } - if len(rls) > 0 { - klog.V(4).Infof("There are releases use data from app version %s", id) - infoMap := make(map[string]string) - allString := &bytes.Buffer{} - for _, r := range rls { - info := fmt.Sprintf("%s/%s", r.GetWorkspace(), r.GetRlsNamespace()) - if _, exists := infoMap[info]; !exists { - infoMap[info] = "" - allString.WriteString(info) - if len(infoMap) > 1 { - allString.WriteString(",") - } - } - } - return fmt.Errorf("release exists: %s", allString.String()) - } - // Delete data in storage err = c.backingStoreClient.Delete(dataKeyInStorage(appVersion.GetWorkspace(), id)) if err != nil { diff --git a/pkg/models/openpitrix/release.go b/pkg/models/openpitrix/release.go index a619c3014..03a3b677e 100644 --- a/pkg/models/openpitrix/release.go +++ b/pkg/models/openpitrix/release.go @@ -267,6 +267,11 @@ func (c *releaseOperator) ListApplications(workspace, clusterName, namespace str ls[constants.ChartApplicationVersionIdLabelKey] = versionId } + repoId := conditions.Match[RepoId] + if repoId != "" { + ls[constants.ChartRepoIdLabelKey] = repoId + } + if workspace != "" { ls[constants.WorkspaceLabelKey] = workspace } diff --git a/pkg/models/openpitrix/repos.go b/pkg/models/openpitrix/repos.go index 436b844ee..aad2bb0c5 100644 --- a/pkg/models/openpitrix/repos.go +++ b/pkg/models/openpitrix/repos.go @@ -16,7 +16,6 @@ package openpitrix import ( "context" "encoding/json" - "fmt" "github.com/go-openapi/strfmt" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -126,7 +125,7 @@ func (c *repoOperator) CreateRepo(repo *v1alpha1.HelmRepo) (*CreateRepoResponse, repo.Spec.Description = stringutils.ShortenString(repo.Spec.Description, DescriptionLen) _, err = c.repoClient.HelmRepos().Create(context.TODO(), repo, metav1.CreateOptions{}) if err != nil { - klog.Errorf("create helm repo failed, repod_id: %s, error: %s", repo.GetHelmRepoId(), err) + klog.Errorf("create helm repo failed, repo_id: %s, error: %s", repo.GetHelmRepoId(), err) return nil, err } else { klog.V(4).Infof("create helm repo success, repo_id: %s", repo.GetHelmRepoId()) @@ -136,20 +135,10 @@ func (c *repoOperator) CreateRepo(repo *v1alpha1.HelmRepo) (*CreateRepoResponse, } func (c *repoOperator) DeleteRepo(id string) error { - ls := map[string]string{ - constants.ChartRepoIdLabelKey: id, - } - releases, err := c.rlsLister.List(labels.SelectorFromSet(ls)) - - if err != nil && apierrors.IsNotFound(err) { - return err - } else if len(releases) > 0 { - return fmt.Errorf("repo %s has releases not deleted", id) - } - + var err error err = c.repoClient.HelmRepos().Delete(context.TODO(), id, metav1.DeleteOptions{}) - if err != nil && apierrors.IsNotFound(err) { - klog.Error(err) + if err != nil && !apierrors.IsNotFound(err) { + klog.Errorf("delete repo %s failed, error: %s", id, err) return err } klog.V(4).Infof("repo %s deleted", id) From cac5daa42981ad6dde2644a7eb9f4772b7ae138b Mon Sep 17 00:00:00 2001 From: LiHui Date: Wed, 24 Mar 2021 18:33:43 +0800 Subject: [PATCH 3/3] Fix: save description when install release Signed-off-by: LiHui --- pkg/models/openpitrix/release.go | 1 + pkg/models/openpitrix/types.go | 3 +++ 2 files changed, 4 insertions(+) diff --git a/pkg/models/openpitrix/release.go b/pkg/models/openpitrix/release.go index 03a3b677e..022965fd3 100644 --- a/pkg/models/openpitrix/release.go +++ b/pkg/models/openpitrix/release.go @@ -171,6 +171,7 @@ func (c *releaseOperator) CreateApplication(workspace, clusterName, namespace st }, Spec: v1alpha1.HelmReleaseSpec{ Name: request.Name, + Description: stringutils.ShortenString(request.Description, v1alpha1.MsgLen), Version: 1, Values: strfmt.Base64(request.Conf), ApplicationId: strings.TrimSuffix(request.AppId, v1alpha1.HelmApplicationAppStoreSuffix), diff --git a/pkg/models/openpitrix/types.go b/pkg/models/openpitrix/types.go index a3bf6a8de..3e4c7cd4a 100644 --- a/pkg/models/openpitrix/types.go +++ b/pkg/models/openpitrix/types.go @@ -738,6 +738,9 @@ type CreateClusterRequest struct { // release name Name string `json:"name"` + // release install description + Description string `json:"description"` + // advanced param AdvancedParam []string `json:"advanced_param"`