Skip to content

Commit

Permalink
chore: do not clear the original label
Browse files Browse the repository at this point in the history
  • Loading branch information
0xff-dev committed Apr 3, 2024
1 parent 6b7d139 commit 1253a73
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 14 deletions.
4 changes: 4 additions & 0 deletions api/base/v1alpha1/application_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ type ApplicationSpec struct {
IsPublic bool `json:"isPublic,omitempty"`
// IsRecommended Set whether the current application is recognized as recommended to users
IsRecommended bool `json:"isRecommended,omitempty"`

// Category Application category
Category string `json:"category,omitempty"`

// WebConfig is the configuration for web interface
WebConfig `json:",inline"`
// prologue, show in the chat top
Expand Down
23 changes: 14 additions & 9 deletions apiserver/pkg/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,14 @@ import (

func addCategory(app *v1alpha1.Application, category []*string) *v1alpha1.Application {
if len(category) == 0 {
delete(app.Labels, v1alpha1.AppCategoryLabelKey)
app.Spec.Category = ""
return app
}
if app.Annotations == nil {
app.Annotations = make(map[string]string, 1)
}
c := make([]string, len(category))
for i := range category {
c[i] = *category[i]
}
app.Labels[v1alpha1.AppCategoryLabelKey] = strings.Join(c, ",")
app.Spec.Category = strings.Join(c, ",")
return app
}

Expand Down Expand Up @@ -202,7 +199,9 @@ func CreateApplication(ctx context.Context, c client.Client, input generated.Cre
Nodes: []v1alpha1.Node{},
},
}
app = addCategory(app, input.Category)
if len(input.Category) > 0 {
app = addCategory(app, input.Category)
}
common.SetCreator(ctx, &app.Spec.CommonSpec)
if err := c.Create(ctx, app); err != nil {
return nil, err
Expand All @@ -216,9 +215,15 @@ func UpdateApplication(ctx context.Context, c client.Client, input generated.Upd
return nil, err
}
oldApp := app.DeepCopy()
app.Labels = utils.MapAny2Str(input.Labels)
app.Annotations = utils.MapAny2Str(input.Annotations)
app = addCategory(app, input.Category)
if len(input.Labels) > 0 {
app.Labels = utils.MapAny2Str(input.Labels)
}
if len(input.Annotations) > 0 {
app.Annotations = utils.MapAny2Str(input.Annotations)
}
if len(input.Category) > 0 {
app = addCategory(app, input.Category)
}
app.Spec.DisplayName = input.DisplayName
app.Spec.Description = pointer.StringDeref(input.Description, app.Spec.Description)
app.Spec.Icon = input.Icon
Expand Down
3 changes: 3 additions & 0 deletions config/crd/bases/arcadia.kubeagi.k8s.com.cn_applications.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ spec:
spec:
description: ApplicationSpec defines the desired state of Application
properties:
category:
description: Category Application category
type: string
chatTimeoutSecond:
default: 60
description: ChatTimeoutSecond is the timeout of chat
Expand Down
17 changes: 12 additions & 5 deletions controllers/base/application_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,8 @@ func (r *ApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Request)
func MigrateAppCategory(app *arcadiav1alpha1.Application) bool {
if v, ok := app.Annotations[arcadiav1alpha1.AppCategoryLabelKey]; ok && len(validation.IsValidLabelValue(v)) == 0 {
// TODO: In version 0.3, the categories in annotations will be removed.
if app.Labels == nil {
app.Labels = make(map[string]string)
}
if _, ok := app.Labels[arcadiav1alpha1.AppCategoryLabelKey]; !ok {
app.Labels[arcadiav1alpha1.AppCategoryLabelKey] = v
if app.Spec.Category == "" {
app.Spec.Category = v
return true
}
}
Expand Down Expand Up @@ -323,6 +320,16 @@ func (r *ApplicationReconciler) reconcile(ctx context.Context, log logr.Logger,
} else {
delete(app.Labels, arcadiav1alpha1.AppRecommendedLabelKey)
}
if app.Spec.Category != "" {
if app.Labels == nil {
app.Labels = make(map[string]string)
}
if v, ok := app.Labels[arcadiav1alpha1.AppCategoryLabelKey]; !ok || v != app.Spec.Category {
app.Labels[arcadiav1alpha1.AppCategoryLabelKey] = app.Spec.Category
}
} else {
delete(app.Labels, arcadiav1alpha1.AppCategoryLabelKey)
}

if !reflect.DeepEqual(app, appRaw) {
return app, ctrl.Result{Requeue: true}, r.Patch(ctx, app, client.MergeFrom(appRaw))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ spec:
spec:
description: ApplicationSpec defines the desired state of Application
properties:
category:
description: Category Application category
type: string
chatTimeoutSecond:
default: 60
description: ChatTimeoutSecond is the timeout of chat
Expand Down

0 comments on commit 1253a73

Please sign in to comment.