diff --git a/config/yurt-app-manager/crd/bases/apps.openyurt.io_uniteddeployments.yaml b/config/yurt-app-manager/crd/bases/apps.openyurt.io_uniteddeployments.yaml index 15ff2e3..f030a76 100644 --- a/config/yurt-app-manager/crd/bases/apps.openyurt.io_uniteddeployments.yaml +++ b/config/yurt-app-manager/crd/bases/apps.openyurt.io_uniteddeployments.yaml @@ -127,6 +127,13 @@ spec: distributed across multiple groups of nodes. A pool's nodeSelectorTerm is not allowed to be updated. type: object + patch: + description: Indicates the patch for the templateSpec Now + support strategic merge path :https://kubernetes.io/docs/tasks/manage-kubernetes-objects/update-api-object-kubectl-patch/#notes-on-the-strategic-merge-patch + Patch takes precedence over Replicas fields If the Patch + also modifies the Replicas, use the Replicas value in the + Patch + type: object replicas: description: Indicates the number of the pod to be created under this pool. diff --git a/pkg/yurtappmanager/apis/apps/v1alpha1/uniteddeployment_types.go b/pkg/yurtappmanager/apis/apps/v1alpha1/uniteddeployment_types.go index cc9aff5..c9d7473 100644 --- a/pkg/yurtappmanager/apis/apps/v1alpha1/uniteddeployment_types.go +++ b/pkg/yurtappmanager/apis/apps/v1alpha1/uniteddeployment_types.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The OpenYurt Authors. +Copyright 2021 The OpenYurt Authors. Copyright 2020 The Kruise Authors. Licensed under the Apache License, Version 2.0 (the "License"); @@ -25,6 +25,7 @@ import ( appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" ) type TemplateType string @@ -124,6 +125,13 @@ type Pool struct { // Indicates the number of the pod to be created under this pool. // +required Replicas *int32 `json:"replicas,omitempty"` + + // Indicates the patch for the templateSpec + // Now support strategic merge path :https://kubernetes.io/docs/tasks/manage-kubernetes-objects/update-api-object-kubectl-patch/#notes-on-the-strategic-merge-patch + // Patch takes precedence over Replicas fields + // If the Patch also modifies the Replicas, use the Replicas value in the Patch + // +optional + Patch *runtime.RawExtension `json:"patch,omitempty"` } // UnitedDeploymentStatus defines the observed state of UnitedDeployment. diff --git a/pkg/yurtappmanager/apis/apps/v1alpha1/well_known_labels_annotations.go b/pkg/yurtappmanager/apis/apps/v1alpha1/well_known_labels_annotations.go index b247ebb..950e702 100644 --- a/pkg/yurtappmanager/apis/apps/v1alpha1/well_known_labels_annotations.go +++ b/pkg/yurtappmanager/apis/apps/v1alpha1/well_known_labels_annotations.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The OpenYurt Authors. +Copyright 2021 The OpenYurt Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -20,7 +20,7 @@ change some const value package v1alpha1 -// UnitedDeployment related labels +// UnitedDeployment related labels and annotations const ( // ControllerRevisionHashLabelKey is used to record the controller revision of current resource. ControllerRevisionHashLabelKey = "apps.openyurt.io/controller-revision-hash" @@ -30,6 +30,9 @@ const ( // SpecifiedDeleteKey indicates this object should be deleted, and the value could be the deletion option. SpecifiedDeleteKey = "apps.openyurt.io/specified-delete" + + // AnnotationPatchKey indicates the patch for every sub pool + AnnotationPatchKey = "apps.openyurt.io/patch" ) // NodePool related labels and annotations @@ -43,12 +46,6 @@ const ( AnnotationPrevAttrs = "nodepool.openyurt.io/previous-attributes" - // Note !!!! - // Can not change this const name , because go build -ldflags will change this values - // @kadisi - // LabelEdgeWorker indicates if the node is an edge node - LabelEdgeWorker = "alibabacloud.com/is-edge-worker" - // DefaultCloudNodePoolName defines the name of the default cloud nodepool DefaultCloudNodePoolName = "default-nodepool" diff --git a/pkg/yurtappmanager/controller/uniteddeployment/adapter/adapter_util.go b/pkg/yurtappmanager/controller/uniteddeployment/adapter/adapter_util.go index 7bbceff..14cc0fb 100644 --- a/pkg/yurtappmanager/controller/uniteddeployment/adapter/adapter_util.go +++ b/pkg/yurtappmanager/controller/uniteddeployment/adapter/adapter_util.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The OpenYurt Authors. +Copyright 2021 The OpenYurt Authors. Copyright 2019 The Kruise Authors. Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,12 +18,16 @@ limitations under the License. package adapter import ( + "encoding/json" "fmt" - unitv1alpha1 "github.com/openyurtio/yurt-app-manager/pkg/yurtappmanager/apis/apps/v1alpha1" + appsv1alpha1 "github.com/openyurtio/yurt-app-manager/pkg/yurtappmanager/apis/apps/v1alpha1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/validation" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/util/strategicpatch" + "k8s.io/klog" ) func getPoolPrefix(controllerName, poolName string) string { @@ -34,12 +38,12 @@ func getPoolPrefix(controllerName, poolName string) string { return prefix } -func attachNodeAffinityAndTolerations(podSpec *corev1.PodSpec, pool *unitv1alpha1.Pool) { +func attachNodeAffinityAndTolerations(podSpec *corev1.PodSpec, pool *appsv1alpha1.Pool) { attachNodeAffinity(podSpec, pool) attachTolerations(podSpec, pool) } -func attachNodeAffinity(podSpec *corev1.PodSpec, pool *unitv1alpha1.Pool) { +func attachNodeAffinity(podSpec *corev1.PodSpec, pool *appsv1alpha1.Pool) { if podSpec.Affinity == nil { podSpec.Affinity = &corev1.Affinity{} } @@ -68,7 +72,7 @@ func attachNodeAffinity(podSpec *corev1.PodSpec, pool *unitv1alpha1.Pool) { } } -func attachTolerations(podSpec *corev1.PodSpec, poolConfig *unitv1alpha1.Pool) { +func attachTolerations(podSpec *corev1.PodSpec, poolConfig *appsv1alpha1.Pool) { if poolConfig.Tolerations == nil { return @@ -89,7 +93,7 @@ func getRevision(objMeta metav1.Object) string { if objMeta.GetLabels() == nil { return "" } - return objMeta.GetLabels()[unitv1alpha1.ControllerRevisionHashLabelKey] + return objMeta.GetLabels()[appsv1alpha1.ControllerRevisionHashLabelKey] } // getCurrentPartition calculates current partition by counting the pods not having the updated revision @@ -103,3 +107,56 @@ func getCurrentPartition(pods []*corev1.Pod, revision string) *int32 { return &partition } + +func StrategicMergeByPatches(oldobj interface{}, patch *runtime.RawExtension, newPatched interface{}) error { + patchMap := make(map[string]interface{}) + if err := json.Unmarshal(patch.Raw, &patchMap); err != nil { + klog.Errorf("Unmarshal pool patch error %v, patch Raw %v", err, string(patch.Raw)) + return err + } + + originalObjMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(oldobj) + if err != nil { + klog.Errorf("ToUnstructured error %v", err) + return err + } + + patchedObjMap, err := strategicpatch.StrategicMergeMapPatch(originalObjMap, patchMap, newPatched) + if err != nil { + klog.Errorf("StartegicMergeMapPatch error %v", err) + return err + } + + if err := runtime.DefaultUnstructuredConverter.FromUnstructured(patchedObjMap, newPatched); err != nil { + klog.Errorf("FromUnstructured error %v", err) + return err + } + return nil +} + +func PoolHasPatch(poolConfig *appsv1alpha1.Pool, set metav1.Object) bool { + if poolConfig.Patch == nil { + // If No Patches, Must Set patches annotation to "" + if anno := set.GetAnnotations(); anno != nil { + anno[appsv1alpha1.AnnotationPatchKey] = "" + } + return false + } + return true +} + +func CreateNewPatchedObject(patchInfo *runtime.RawExtension, set metav1.Object, newPatched metav1.Object) error { + + if err := StrategicMergeByPatches(set, patchInfo, newPatched); err != nil { + return err + } + + if anno := newPatched.GetAnnotations(); anno == nil { + newPatched.SetAnnotations(map[string]string{ + appsv1alpha1.AnnotationPatchKey: string(patchInfo.Raw), + }) + } else { + anno[appsv1alpha1.AnnotationPatchKey] = string(patchInfo.Raw) + } + return nil +} diff --git a/pkg/yurtappmanager/controller/uniteddeployment/adapter/adapter_util_test.go b/pkg/yurtappmanager/controller/uniteddeployment/adapter/adapter_util_test.go index 1967d83..075751f 100644 --- a/pkg/yurtappmanager/controller/uniteddeployment/adapter/adapter_util_test.go +++ b/pkg/yurtappmanager/controller/uniteddeployment/adapter/adapter_util_test.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The OpenYurt Authors. +Copyright 2021 The OpenYurt Authors. Copyright 2019 The Kruise Authors. Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,6 +21,10 @@ import ( "fmt" "testing" + appsv1 "k8s.io/api/apps/v1" + + "k8s.io/apimachinery/pkg/runtime" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -86,3 +90,84 @@ func buildPodList(ordinals []int, revisions []string, t *testing.T) []*corev1.Po return pods } + +func TestCreateNewPatchedObject(t *testing.T) { + cases := []struct { + Name string + PatchInfo *runtime.RawExtension + OldObj *appsv1.Deployment + EqualFuntion func(new *appsv1.Deployment) bool + }{ + { + Name: "replace image", + PatchInfo: &runtime.RawExtension{Raw: []byte(`{"spec":{"template":{"spec":{"containers":[{"image":"nginx:1.18.0","name":"nginx"}]}}}}`)}, + OldObj: &appsv1.Deployment{ + Spec: appsv1.DeploymentSpec{ + Template: corev1.PodTemplateSpec{ + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: "nginx", + Image: "nginx:1.19.0", + }, + }, + }, + }, + }, + }, + EqualFuntion: func(new *appsv1.Deployment) bool { + return new.Spec.Template.Spec.Containers[0].Image == "nginx:1.18.0" + }, + }, + { + Name: "add other image", + PatchInfo: &runtime.RawExtension{Raw: []byte(`{"spec":{"template":{"spec":{"containers":[{"image":"nginx:1.18.0","name":"nginx111"}]}}}}`)}, + OldObj: &appsv1.Deployment{ + Spec: appsv1.DeploymentSpec{ + Template: corev1.PodTemplateSpec{ + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: "nginx", + Image: "nginx:1.19.0", + }, + }, + }, + }, + }, + }, + EqualFuntion: func(new *appsv1.Deployment) bool { + if len(new.Spec.Template.Spec.Containers) != 2 { + return false + } + containerMap := make(map[string]string) + for _, container := range new.Spec.Template.Spec.Containers { + containerMap[container.Name] = container.Image + } + image, ok := containerMap["nginx"] + if !ok { + return false + } + + image1, ok := containerMap["nginx111"] + if !ok { + return false + } + return image == "nginx:1.19.0" && image1 == "nginx:1.18.0" + }, + }, + } + + for _, c := range cases { + t.Run(c.Name, func(t *testing.T) { + newObj := &appsv1.Deployment{} + if err := CreateNewPatchedObject(c.PatchInfo, c.OldObj, newObj); err != nil { + t.Fatalf("%s CreateNewPatchedObject error %v", c.Name, err) + } + if !c.EqualFuntion(newObj) { + t.Fatalf("%s Not Expect equal funtion", c.Name) + } + }) + } + +} diff --git a/pkg/yurtappmanager/controller/uniteddeployment/adapter/deployment_adapter.go b/pkg/yurtappmanager/controller/uniteddeployment/adapter/deployment_adapter.go index 53b2fe4..b60b623 100644 --- a/pkg/yurtappmanager/controller/uniteddeployment/adapter/deployment_adapter.go +++ b/pkg/yurtappmanager/controller/uniteddeployment/adapter/deployment_adapter.go @@ -1,5 +1,5 @@ /* -Copyright 2019 The OpenYurt Authors. +Copyright 2021 The OpenYurt Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -19,13 +19,14 @@ package adapter import ( "fmt" + "k8s.io/klog" + + alpha1 "github.com/openyurtio/yurt-app-manager/pkg/yurtappmanager/apis/apps/v1alpha1" appsv1 "k8s.io/api/apps/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" - - alpha1 "github.com/openyurtio/yurt-app-manager/pkg/yurtappmanager/apis/apps/v1alpha1" ) type DeploymentAdapter struct { @@ -78,9 +79,9 @@ func (a *DeploymentAdapter) ApplyPoolTemplate(ud *alpha1.UnitedDeployment, poolN set := obj.(*appsv1.Deployment) var poolConfig *alpha1.Pool - for _, pool := range ud.Spec.Topology.Pools { + for i, pool := range ud.Spec.Topology.Pools { if pool.Name == poolName { - poolConfig = &pool + poolConfig = &(ud.Spec.Topology.Pools[i]) break } } @@ -136,6 +137,23 @@ func (a *DeploymentAdapter) ApplyPoolTemplate(ud *alpha1.UnitedDeployment, poolN set.Spec.ProgressDeadlineSeconds = ud.Spec.WorkloadTemplate.DeploymentTemplate.Spec.ProgressDeadlineSeconds attachNodeAffinityAndTolerations(&set.Spec.Template.Spec, poolConfig) + + if !PoolHasPatch(poolConfig, set) { + klog.Infof("Deployment[%s/%s-] has no patches, do not need strategicmerge", set.Namespace, + set.GenerateName) + return nil + } + + patched := &appsv1.Deployment{} + if err := CreateNewPatchedObject(poolConfig.Patch, set, patched); err != nil { + klog.Errorf("Deployment[%s/%s-] strategic merge by patch %s error %v", set.Namespace, + set.GenerateName, string(poolConfig.Patch.Raw), err) + return err + } + patched.DeepCopyInto(set) + + klog.Infof("Deployment [%s/%s-] has patches configure successfully:%v", set.Namespace, + set.GenerateName, string(poolConfig.Patch.Raw)) return nil } diff --git a/pkg/yurtappmanager/controller/uniteddeployment/adapter/statefulset_adapter.go b/pkg/yurtappmanager/controller/uniteddeployment/adapter/statefulset_adapter.go index 7d7d023..70c0c1c 100644 --- a/pkg/yurtappmanager/controller/uniteddeployment/adapter/statefulset_adapter.go +++ b/pkg/yurtappmanager/controller/uniteddeployment/adapter/statefulset_adapter.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The OpenYurt Authors. +Copyright 2021 The OpenYurt Authors. Copyright 2019 The Kruise Authors. Licensed under the Apache License, Version 2.0 (the "License"); @@ -90,9 +90,9 @@ func (a *StatefulSetAdapter) ApplyPoolTemplate(ud *alpha1.UnitedDeployment, pool set := obj.(*appsv1.StatefulSet) var poolConfig *alpha1.Pool - for _, pool := range ud.Spec.Topology.Pools { + for i, pool := range ud.Spec.Topology.Pools { if pool.Name == poolName { - poolConfig = &pool + poolConfig = &(ud.Spec.Topology.Pools[i]) break } } @@ -148,6 +148,22 @@ func (a *StatefulSetAdapter) ApplyPoolTemplate(ud *alpha1.UnitedDeployment, pool set.Spec.VolumeClaimTemplates = ud.Spec.WorkloadTemplate.StatefulSetTemplate.Spec.VolumeClaimTemplates attachNodeAffinityAndTolerations(&set.Spec.Template.Spec, poolConfig) + + if !PoolHasPatch(poolConfig, set) { + klog.Infof("StatefulSet[%s/%s-] has no patches, do not need strategicmerge", set.Namespace, + set.GenerateName) + return nil + } + + patched := &appsv1.StatefulSet{} + if err := CreateNewPatchedObject(poolConfig.Patch, set, patched); err != nil { + klog.Errorf("StatefulSet[%s/%s-] strategic merge by patch %s error %v", set.Namespace, + set.GenerateName, string(poolConfig.Patch.Raw), err) + return err + } + patched.DeepCopyInto(set) + klog.Infof("Statefulset [%s/%s-] has patches configure successfully:%v", set.Namespace, + set.GenerateName, string(poolConfig.Patch.Raw)) return nil } diff --git a/pkg/yurtappmanager/controller/uniteddeployment/allocator.go b/pkg/yurtappmanager/controller/uniteddeployment/allocator.go deleted file mode 100644 index 74b5aee..0000000 --- a/pkg/yurtappmanager/controller/uniteddeployment/allocator.go +++ /dev/null @@ -1,33 +0,0 @@ -/* -Copyright 2020 The OpenYurt Authors. -Copyright 2019 The Kruise Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package uniteddeployment - -import ( - unitv1alpha1 "github.com/openyurtio/yurt-app-manager/pkg/yurtappmanager/apis/apps/v1alpha1" -) - -func GetNextReplicas(ud *unitv1alpha1.UnitedDeployment) map[string]int32 { - next := make(map[string]int32) - for _, pool := range ud.Spec.Topology.Pools { - next[pool.Name] = 0 - if pool.Replicas != nil { - next[pool.Name] = *pool.Replicas - } - } - return next -} diff --git a/pkg/yurtappmanager/controller/uniteddeployment/pool.go b/pkg/yurtappmanager/controller/uniteddeployment/pool.go index b045cda..68603d9 100644 --- a/pkg/yurtappmanager/controller/uniteddeployment/pool.go +++ b/pkg/yurtappmanager/controller/uniteddeployment/pool.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The OpenYurt Authors. +Copyright 2021 The OpenYurt Authors. Copyright 2019 The Kruise Authors. Licensed under the Apache License, Version 2.0 (the "License"); @@ -41,6 +41,7 @@ type PoolSpec struct { type PoolStatus struct { ObservedGeneration int64 adapter.ReplicasInfo + PatchInfo string } // ResourceRef stores the Pool resource it represents. diff --git a/pkg/yurtappmanager/controller/uniteddeployment/pool_control.go b/pkg/yurtappmanager/controller/uniteddeployment/pool_control.go index a25c864..8fd2ef9 100644 --- a/pkg/yurtappmanager/controller/uniteddeployment/pool_control.go +++ b/pkg/yurtappmanager/controller/uniteddeployment/pool_control.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The OpenYurt Authors. +Copyright 2021 The OpenYurt Authors. Copyright 2019 The Kruise Authors. Licensed under the Apache License, Version 2.0 (the "License"); @@ -157,7 +157,6 @@ func (m *PoolControl) convertToPool(set metav1.Object) (*Pool, error) { if err != nil { return nil, err } - pool := &Pool{ Name: poolName, Namespace: set.GetNamespace(), @@ -169,6 +168,9 @@ func (m *PoolControl) convertToPool(set metav1.Object) (*Pool, error) { ReplicasInfo: specReplicas, }, } + if data, ok := set.GetAnnotations()[alpha1.AnnotationPatchKey]; ok { + pool.Status.PatchInfo = data + } return pool, nil } diff --git a/pkg/yurtappmanager/controller/uniteddeployment/uniteddeployment_controller.go b/pkg/yurtappmanager/controller/uniteddeployment/uniteddeployment_controller.go index 62b2470..c08eccf 100644 --- a/pkg/yurtappmanager/controller/uniteddeployment/uniteddeployment_controller.go +++ b/pkg/yurtappmanager/controller/uniteddeployment/uniteddeployment_controller.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The OpenYurt Authors. +Copyright 2021 The OpenYurt Authors. Copyright 2019 The Kruise Authors. Licensed under the Apache License, Version 2.0 (the "License"); @@ -188,14 +188,14 @@ func (r *ReconcileUnitedDeployment) Reconcile(_ context.Context, request reconci return reconcile.Result{}, nil } - nextReplicas := GetNextReplicas(instance) - klog.V(4).Infof("Get UnitedDeployment %s/%s next Replicas %v", instance.Namespace, instance.Name, nextReplicas) + nextPatches := GetNextPatches(instance) + klog.V(4).Infof("Get UnitedDeployment %s/%s next Patches %v", instance.Namespace, instance.Name, nextPatches) expectedRevision := currentRevision if updatedRevision != nil { expectedRevision = updatedRevision } - newStatus, err := r.managePools(instance, nameToPool, nextReplicas, expectedRevision, poolType) + newStatus, err := r.managePools(instance, nameToPool, nextPatches, expectedRevision, poolType) if err != nil { klog.Errorf("Fail to update UnitedDeployment %s/%s: %s", instance.Namespace, instance.Name, err) r.recorder.Event(instance.DeepCopy(), corev1.EventTypeWarning, fmt.Sprintf("Failed%s", eventTypePoolsUpdate), err.Error()) diff --git a/pkg/yurtappmanager/controller/uniteddeployment/uniteddeployment_controller_utils.go b/pkg/yurtappmanager/controller/uniteddeployment/uniteddeployment_controller_utils.go index 1d43612..971bbb0 100644 --- a/pkg/yurtappmanager/controller/uniteddeployment/uniteddeployment_controller_utils.go +++ b/pkg/yurtappmanager/controller/uniteddeployment/uniteddeployment_controller_utils.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The OpenYurt Authors. +Copyright 2021 The OpenYurt Authors. Copyright 2019 The Kruise Authors. Copyright 2016 The Kubernetes Authors. @@ -32,6 +32,11 @@ import ( const updateRetries = 5 +type UnitedDeploymentPatches struct { + Replicas int32 + Patch string +} + func getPoolNameFrom(metaObj metav1.Object) (string, error) { name, exist := metaObj.GetLabels()[unitv1alpha1.PoolNameLabelKey] if !exist { @@ -97,3 +102,18 @@ func filterOutCondition(conditions []unitv1alpha1.UnitedDeploymentCondition, con } return newConditions } + +func GetNextPatches(ud *unitv1alpha1.UnitedDeployment) map[string]UnitedDeploymentPatches { + next := make(map[string]UnitedDeploymentPatches) + for _, pool := range ud.Spec.Topology.Pools { + t := UnitedDeploymentPatches{} + if pool.Replicas != nil { + t.Replicas = *pool.Replicas + } + if pool.Patch != nil { + t.Patch = string(pool.Patch.Raw) + } + next[pool.Name] = t + } + return next +} diff --git a/pkg/yurtappmanager/controller/uniteddeployment/uniteddeployment_update.go b/pkg/yurtappmanager/controller/uniteddeployment/uniteddeployment_update.go index 3c2db69..3ab6eb3 100644 --- a/pkg/yurtappmanager/controller/uniteddeployment/uniteddeployment_update.go +++ b/pkg/yurtappmanager/controller/uniteddeployment/uniteddeployment_update.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The OpenYurt Authors. +Copyright 2021 The OpenYurt Authors. Copyright 2019 The Kruise Authors. Licensed under the Apache License, Version 2.0 (the "License"); @@ -36,12 +36,12 @@ import ( ) func (r *ReconcileUnitedDeployment) managePools(ud *unitv1alpha1.UnitedDeployment, - nameToPool map[string]*Pool, nextReplicas map[string]int32, + nameToPool map[string]*Pool, nextPatches map[string]UnitedDeploymentPatches, expectedRevision *appsv1.ControllerRevision, poolType unitv1alpha1.TemplateType) (newStatus *unitv1alpha1.UnitedDeploymentStatus, updateErr error) { newStatus = ud.Status.DeepCopy() - exists, provisioned, err := r.managePoolProvision(ud, nameToPool, nextReplicas, expectedRevision, poolType) + exists, provisioned, err := r.managePoolProvision(ud, nameToPool, nextPatches, expectedRevision, poolType) if err != nil { SetUnitedDeploymentCondition(newStatus, NewUnitedDeploymentCondition(unitv1alpha1.PoolProvisioned, corev1.ConditionFalse, "Error", err.Error())) return newStatus, fmt.Errorf("fail to manage Pool provision: %s", err) @@ -55,7 +55,8 @@ func (r *ReconcileUnitedDeployment) managePools(ud *unitv1alpha1.UnitedDeploymen for _, name := range exists.List() { pool := nameToPool[name] if r.poolControls[poolType].IsExpected(pool, expectedRevision.Name) || - pool.Status.ReplicasInfo.Replicas != nextReplicas[name] { + pool.Status.ReplicasInfo.Replicas != nextPatches[name].Replicas || + pool.Status.PatchInfo != nextPatches[name].Patch { needUpdate = append(needUpdate, name) } } @@ -64,9 +65,9 @@ func (r *ReconcileUnitedDeployment) managePools(ud *unitv1alpha1.UnitedDeploymen _, updateErr = util.SlowStartBatch(len(needUpdate), slowStartInitialBatchSize, func(index int) error { cell := needUpdate[index] pool := nameToPool[cell] - replicas := nextReplicas[cell] + replicas := nextPatches[cell].Replicas - klog.V(0).Infof("UnitedDeployment %s/%s needs to update Pool (%s) %s/%s with revision %s, replicas %d ", + klog.Infof("UnitedDeployment %s/%s needs to update Pool (%s) %s/%s with revision %s, replicas %d ", ud.Namespace, ud.Name, poolType, pool.Namespace, pool.Name, expectedRevision.Name, replicas) updatePoolErr := r.poolControls[poolType].UpdatePool(pool, ud, expectedRevision.Name, replicas) @@ -86,7 +87,7 @@ func (r *ReconcileUnitedDeployment) managePools(ud *unitv1alpha1.UnitedDeploymen } func (r *ReconcileUnitedDeployment) managePoolProvision(ud *unitv1alpha1.UnitedDeployment, - nameToPool map[string]*Pool, nextReplicas map[string]int32, + nameToPool map[string]*Pool, nextPatches map[string]UnitedDeploymentPatches, expectedRevision *appsv1.ControllerRevision, workloadType unitv1alpha1.TemplateType) (sets.String, bool, error) { expectedPools := sets.String{} gotPools := sets.String{} @@ -124,7 +125,7 @@ func (r *ReconcileUnitedDeployment) managePoolProvision(ud *unitv1alpha1.UnitedD // manage creating if len(creates) > 0 { // do not consider deletion - klog.V(0).Infof("UnitedDeployment %s/%s needs creating pool (%s) with name: %v", ud.Namespace, ud.Name, workloadType, creates) + klog.Infof("UnitedDeployment %s/%s needs creating pool (%s) with name: %v", ud.Namespace, ud.Name, workloadType, creates) createdPools := make([]string, len(creates)) for i, pool := range creates { createdPools[i] = pool @@ -135,7 +136,7 @@ func (r *ReconcileUnitedDeployment) managePoolProvision(ud *unitv1alpha1.UnitedD createdNum, createdErr = util.SlowStartBatch(len(creates), slowStartInitialBatchSize, func(idx int) error { poolName := createdPools[idx] - replicas := nextReplicas[poolName] + replicas := nextPatches[poolName].Replicas err := r.poolControls[workloadType].CreatePool(ud, poolName, revision, replicas) if err != nil { if !errors.IsTimeout(err) { @@ -154,7 +155,7 @@ func (r *ReconcileUnitedDeployment) managePoolProvision(ud *unitv1alpha1.UnitedD // manage deleting if len(deletes) > 0 { - klog.V(0).Infof("UnitedDeployment %s/%s needs deleting pool (%s) with name: [%v]", ud.Namespace, ud.Name, workloadType, deletes) + klog.Infof("UnitedDeployment %s/%s needs deleting pool (%s) with name: [%v]", ud.Namespace, ud.Name, workloadType, deletes) var deleteErrs []error for _, poolName := range deletes { pool := nameToPool[poolName]