github.com/oam-dev/kubevela@v1.9.11/pkg/utils/app/reschedule.go (about) 1 /* 2 Copyright 2023 The KubeVela Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package app 18 19 import ( 20 "context" 21 "reflect" 22 "strings" 23 24 "github.com/kubevela/pkg/controller/sharding" 25 "github.com/kubevela/pkg/util/slices" 26 "k8s.io/apimachinery/pkg/util/errors" 27 "k8s.io/klog/v2" 28 "sigs.k8s.io/controller-runtime/pkg/client" 29 30 "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" 31 "github.com/oam-dev/kubevela/pkg/controller/core.oam.dev/v1beta1/application" 32 "github.com/oam-dev/kubevela/pkg/resourcetracker" 33 ) 34 35 func reschedule(ctx context.Context, cli client.Client, o client.Object, shardID string) error { 36 oldID, scheduled := sharding.GetScheduledShardID(o) 37 if !scheduled || oldID != shardID { 38 sharding.SetScheduledShardID(o, shardID) 39 if err := cli.Update(ctx, o); err != nil { 40 return err 41 } 42 klog.Infof("schedule %s/%s to %s", strings.ToLower(o.GetObjectKind().GroupVersionKind().Kind), o.GetName(), shardID) 43 } 44 return nil 45 } 46 47 // RescheduleAppRevAndRT reschedule ApplicationRevision and ResourceTracker of app to given shard 48 func RescheduleAppRevAndRT(ctx context.Context, cli client.Client, app *v1beta1.Application, shardID string) error { 49 rt, currentRT, ts, crRT, err := resourcetracker.ListApplicationResourceTrackers(ctx, cli, app) 50 if err != nil { 51 return err 52 } 53 appRevs, err := application.GetAppRevisions(ctx, cli, app.Name, app.Namespace) 54 if err != nil { 55 return err 56 } 57 var objs []client.Object 58 objs = append(objs, slices.Map(ts, func(r *v1beta1.ResourceTracker) client.Object { return r })...) 59 objs = append(objs, []client.Object{crRT, currentRT, rt}...) 60 objs = append(objs, slices.Map(appRevs, func(r v1beta1.ApplicationRevision) client.Object { return r.DeepCopy() })...) 61 objs = append(objs, app) 62 objs = slices.Filter(objs, func(o client.Object) bool { 63 return o != nil && !reflect.ValueOf(o).IsNil() 64 }) 65 errs := slices.ParMap(objs, func(o client.Object) error { return reschedule(ctx, cli, o, shardID) }) 66 return errors.NewAggregate(errs) 67 }