From 542eb180c575bb4ad6da093642e3aba9ad14219b Mon Sep 17 00:00:00 2001 From: LiHui Date: Wed, 24 Mar 2021 18:19:12 +0800 Subject: [PATCH] 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 } -- GitLab