diff --git a/pkg/apis/application/v1alpha1/helmapplication_types.go b/pkg/apis/application/v1alpha1/helmapplication_types.go index 93fea801c7fb85960f371e4943382bf90f532d76..044f83930998b41b782be711dfbeee9d5d9ea21d 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 a33b4a040318c411f5e34f04451a45f5a72c53f4..5e2ff7da8c2ee3d055404435ea822072fb8977d4 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/kapis/openpitrix/v1/handler.go b/pkg/kapis/openpitrix/v1/handler.go index a3e2c1dddbda8d724916ff030dc1b5399f8627a5..54be36327681e8bc054cb982f4d48792656ce06e 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 316093a29f76782d01956a0d185f5c1a4a522dc3..fa08e010b047413aa79982c3d32b87f9ec7a96c4 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 f7d2f8929a54f38c41537e849141dc5d89abef42..c48a9d480521552835b0a15a644e4f230b9048e9 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 a619c30140d00941fc9e175744a119a92511e563..022965fd3680520924a3512bad17328f5ad6b141 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), @@ -267,6 +268,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 436b844ee4b958190843abee0956466b8dcd6750..aad2bb0c5b772d3fb08b1ae4e261ee3fbe397720 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) diff --git a/pkg/models/openpitrix/types.go b/pkg/models/openpitrix/types.go index a3bf6a8ded846b754c976ed98cb5254adf7cc334..3e4c7cd4a45d3b425cf21c32e878a55182ff74a6 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"` diff --git a/pkg/models/openpitrix/utils.go b/pkg/models/openpitrix/utils.go index 6dc1341910c0995b530fccd1b1fc42f8e37d0a22..f594d45d93e90609b02856de2a337794cd05f582 100644 --- a/pkg/models/openpitrix/utils.go +++ b/pkg/models/openpitrix/utils.go @@ -336,6 +336,7 @@ func convertApp(app *v1alpha1.HelmApplication, versions []*v1alpha1.HelmApplicat out.Isv = app.GetWorkspace() out.ClusterTotal = &rlsCount + out.Owner = app.GetCreator() return out }