From 4c107d77ecaba9e89ed8f387c45a3c4a37108d36 Mon Sep 17 00:00:00 2001 From: Liang Deng Date: Wed, 24 Jul 2024 10:52:15 +0800 Subject: [PATCH] feat: Upgrade platformadmin's yurtappset dependencies to v1beta1 Signed-off-by: Liang Deng --- .../platform_admin_controller.go | 109 ++++++++++-------- .../v1alpha1/platformadmin_validation.go | 4 +- .../v1alpha2/platformadmin_validation.go | 4 +- 3 files changed, 67 insertions(+), 50 deletions(-) diff --git a/pkg/yurtmanager/controller/platformadmin/platform_admin_controller.go b/pkg/yurtmanager/controller/platformadmin/platform_admin_controller.go index a044c2b63ec..8fefe2d97fe 100644 --- a/pkg/yurtmanager/controller/platformadmin/platform_admin_controller.go +++ b/pkg/yurtmanager/controller/platformadmin/platform_admin_controller.go @@ -35,6 +35,7 @@ import ( "k8s.io/klog/v2" "k8s.io/kubectl/pkg/scheme" "k8s.io/utils/pointer" + "k8s.io/utils/strings/slices" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" @@ -46,10 +47,9 @@ import ( yurtClient "github.com/openyurtio/openyurt/cmd/yurt-manager/app/client" appconfig "github.com/openyurtio/openyurt/cmd/yurt-manager/app/config" "github.com/openyurtio/openyurt/cmd/yurt-manager/names" - appsv1alpha1 "github.com/openyurtio/openyurt/pkg/apis/apps/v1alpha1" + appsv1beta1 "github.com/openyurtio/openyurt/pkg/apis/apps/v1beta1" iotv1alpha1 "github.com/openyurtio/openyurt/pkg/apis/iot/v1alpha1" iotv1alpha2 "github.com/openyurtio/openyurt/pkg/apis/iot/v1alpha2" - "github.com/openyurtio/openyurt/pkg/projectinfo" "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/platformadmin/config" util "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/platformadmin/utils" ) @@ -163,7 +163,7 @@ func add(mgr manager.Manager, cfg *appconfig.CompletedConfig, r reconcile.Reconc return err } - err = c.Watch(source.Kind(mgr.GetCache(), &appsv1alpha1.YurtAppSet{}), + err = c.Watch(source.Kind(mgr.GetCache(), &appsv1beta1.YurtAppSet{}), handler.EnqueueRequestForOwner(mgr.GetScheme(), mgr.GetRESTMapper(), &iotv1alpha2.PlatformAdmin{}, handler.OnlyControllerOwner())) if err != nil { return err @@ -245,7 +245,7 @@ func (r *ReconcilePlatformAdmin) Reconcile(ctx context.Context, request reconcil func (r *ReconcilePlatformAdmin) reconcileDelete(ctx context.Context, platformAdmin *iotv1alpha2.PlatformAdmin) (reconcile.Result, error) { klog.V(4).Infof(Format("ReconcileDelete PlatformAdmin %s/%s", platformAdmin.Namespace, platformAdmin.Name)) - yas := &appsv1alpha1.YurtAppSet{} + yas := &appsv1beta1.YurtAppSet{} platformAdminFramework, err := r.readFramework(ctx, platformAdmin) if err != nil { @@ -271,12 +271,31 @@ func (r *ReconcilePlatformAdmin) reconcileDelete(ctx context.Context, platformAd oldYas := yas.DeepCopy() - for i, pool := range yas.Spec.Topology.Pools { - if pool.Name == platformAdmin.Spec.PoolName { - yas.Spec.Topology.Pools[i] = yas.Spec.Topology.Pools[len(yas.Spec.Topology.Pools)-1] - yas.Spec.Topology.Pools = yas.Spec.Topology.Pools[:len(yas.Spec.Topology.Pools)-1] + newPools := make([]string, 0) + for _, poolName := range yas.Spec.Pools { + if poolName != platformAdmin.Spec.PoolName { + newPools = append(newPools, poolName) } } + yas.Spec.Pools = newPools + + newTweaks := make([]appsv1beta1.WorkloadTweak, 0) + for _, tweak := range yas.Spec.Workload.WorkloadTweaks { + newTweakPools := make([]string, 0) + for _, poolName := range tweak.Pools { + if poolName != platformAdmin.Spec.PoolName { + newTweakPools = append(newTweakPools, poolName) + } + } + if len(newTweakPools) > 0 { + newTweaks = append(newTweaks, appsv1beta1.WorkloadTweak{ + Pools: newTweakPools, + Tweaks: tweak.Tweaks, + }) + } + } + yas.Spec.Workload.WorkloadTweaks = newTweaks + if err := r.Client.Patch(ctx, yas, client.MergeFrom(oldYas)); err != nil { klog.V(4).ErrorS(err, Format("Patch YurtAppSet %s/%s error", platformAdmin.Namespace, dc.Name)) return reconcile.Result{}, err @@ -413,7 +432,7 @@ func (r *ReconcilePlatformAdmin) reconcileComponent(ctx context.Context, platfor } readyService = true - yas := &appsv1alpha1.YurtAppSet{ + yas := &appsv1beta1.YurtAppSet{ ObjectMeta: metav1.ObjectMeta{ Name: desiredComponent.Name, Namespace: platformAdmin.Namespace, @@ -440,33 +459,34 @@ func (r *ReconcilePlatformAdmin) reconcileComponent(ctx context.Context, platfor // Refresh the YurtAppSet according to the user-defined configuration yas.Spec.WorkloadTemplate.DeploymentTemplate.Spec = *desiredComponent.Deployment - if _, ok := yas.Status.PoolReplicas[platformAdmin.Spec.PoolName]; ok { - if yas.Status.ReadyReplicas == yas.Status.Replicas { + if slices.Contains(yas.Spec.Pools, platformAdmin.Spec.PoolName) { + if yas.Status.ReadyWorkloads == yas.Status.TotalWorkloads { readyDeployment = true if readyDeployment && readyService { readyComponent++ } } } - pool := appsv1alpha1.Pool{ - Name: platformAdmin.Spec.PoolName, - Replicas: pointer.Int32(1), + + pools := []string{platformAdmin.Spec.PoolName} + tweaks := []appsv1beta1.WorkloadTweak{ + { + Pools: []string{platformAdmin.Spec.PoolName}, + Tweaks: appsv1beta1.Tweaks{ + Replicas: pointer.Int32(1), + }, + }, } - pool.NodeSelectorTerm.MatchExpressions = append(pool.NodeSelectorTerm.MatchExpressions, - corev1.NodeSelectorRequirement{ - Key: projectinfo.GetNodePoolLabel(), - Operator: corev1.NodeSelectorOpIn, - Values: []string{platformAdmin.Spec.PoolName}, - }) flag := false - for _, up := range yas.Spec.Topology.Pools { - if up.Name == pool.Name { + for _, name := range yas.Spec.Pools { + if name == platformAdmin.Spec.PoolName { flag = true break } } if !flag { - yas.Spec.Topology.Pools = append(yas.Spec.Topology.Pools, pool) + yas.Spec.Pools = append(yas.Spec.Pools, pools...) + yas.Spec.Workload.WorkloadTweaks = append(yas.Spec.Workload.WorkloadTweaks, tweaks...) } if err := controllerutil.SetOwnerReference(platformAdmin, yas, r.Scheme()); err != nil { return false, err @@ -489,7 +509,7 @@ func (r *ReconcilePlatformAdmin) reconcileComponent(ctx context.Context, platfor } // Remove the yurtappset owner that we do not need - yurtappsetlist := &appsv1alpha1.YurtAppSetList{} + yurtappsetlist := &appsv1beta1.YurtAppSetList{} if err := r.List(ctx, yurtappsetlist, client.InNamespace(platformAdmin.Namespace), client.MatchingLabels{iotv1alpha2.LabelPlatformAdminGenerate: LabelDeployment}); err == nil { for _, s := range yurtappsetlist.Items { if _, ok := needComponents[s.Name]; !ok { @@ -535,7 +555,7 @@ func (r *ReconcilePlatformAdmin) handleService(ctx context.Context, platformAdmi return service, nil } -func (r *ReconcilePlatformAdmin) handleYurtAppSet(ctx context.Context, platformAdmin *iotv1alpha2.PlatformAdmin, component *config.Component) (*appsv1alpha1.YurtAppSet, error) { +func (r *ReconcilePlatformAdmin) handleYurtAppSet(ctx context.Context, platformAdmin *iotv1alpha2.PlatformAdmin, component *config.Component) (*appsv1beta1.YurtAppSet, error) { // It is possible that the component does not need deployment. // Therefore, you need to be careful when calling this function. // It is still possible for deployment to be nil when there is no error! @@ -543,40 +563,37 @@ func (r *ReconcilePlatformAdmin) handleYurtAppSet(ctx context.Context, platformA return nil, nil } - yas := &appsv1alpha1.YurtAppSet{ + yas := &appsv1beta1.YurtAppSet{ ObjectMeta: metav1.ObjectMeta{ Labels: make(map[string]string), Annotations: make(map[string]string), Name: component.Name, Namespace: platformAdmin.Namespace, }, - Spec: appsv1alpha1.YurtAppSetSpec{ - Selector: &metav1.LabelSelector{ - MatchLabels: map[string]string{"app": component.Name}, - }, - WorkloadTemplate: appsv1alpha1.WorkloadTemplate{ - DeploymentTemplate: &appsv1alpha1.DeploymentTemplateSpec{ - ObjectMeta: metav1.ObjectMeta{ - Labels: map[string]string{"app": component.Name}, + Spec: appsv1beta1.YurtAppSetSpec{ + Workload: appsv1beta1.Workload{ + WorkloadTemplate: appsv1beta1.WorkloadTemplate{ + DeploymentTemplate: &appsv1beta1.DeploymentTemplateSpec{ + ObjectMeta: metav1.ObjectMeta{ + Labels: map[string]string{"app": component.Name}, + }, + Spec: *component.Deployment, }, - Spec: *component.Deployment, }, }, }, } yas.Labels[iotv1alpha2.LabelPlatformAdminGenerate] = LabelDeployment - pool := appsv1alpha1.Pool{ - Name: platformAdmin.Spec.PoolName, - Replicas: pointer.Int32(1), - } - pool.NodeSelectorTerm.MatchExpressions = append(pool.NodeSelectorTerm.MatchExpressions, - corev1.NodeSelectorRequirement{ - Key: projectinfo.GetNodePoolLabel(), - Operator: corev1.NodeSelectorOpIn, - Values: []string{platformAdmin.Spec.PoolName}, - }) - yas.Spec.Topology.Pools = append(yas.Spec.Topology.Pools, pool) + yas.Spec.Pools = []string{platformAdmin.Spec.PoolName} + yas.Spec.Workload.WorkloadTweaks = []appsv1beta1.WorkloadTweak{ + { + Pools: []string{platformAdmin.Spec.PoolName}, + Tweaks: appsv1beta1.Tweaks{ + Replicas: pointer.Int32(1), + }, + }, + } if err := controllerutil.SetControllerReference(platformAdmin, yas, r.Scheme()); err != nil { return nil, err } diff --git a/pkg/yurtmanager/webhook/platformadmin/v1alpha1/platformadmin_validation.go b/pkg/yurtmanager/webhook/platformadmin/v1alpha1/platformadmin_validation.go index 7bc0422978b..8e1b28e5c13 100644 --- a/pkg/yurtmanager/webhook/platformadmin/v1alpha1/platformadmin_validation.go +++ b/pkg/yurtmanager/webhook/platformadmin/v1alpha1/platformadmin_validation.go @@ -26,7 +26,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/webhook/admission" - unitv1alpha1 "github.com/openyurtio/openyurt/pkg/apis/apps/v1alpha1" + unitv1beta1 "github.com/openyurtio/openyurt/pkg/apis/apps/v1beta1" "github.com/openyurtio/openyurt/pkg/apis/iot/v1alpha1" util "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/platformadmin/utils" ) @@ -81,7 +81,7 @@ func (webhook *PlatformAdminHandler) validate(ctx context.Context, platformAdmin func (webhook *PlatformAdminHandler) validatePlatformAdminWithNodePools(ctx context.Context, platformAdmin *v1alpha1.PlatformAdmin) field.ErrorList { // verify that the poolname is a right nodepool name - nodePools := &unitv1alpha1.NodePoolList{} + nodePools := &unitv1beta1.NodePoolList{} if err := webhook.Client.List(ctx, nodePools); err != nil { return field.ErrorList{ field.Invalid(field.NewPath("spec", "poolName"), platformAdmin.Spec.PoolName, "can not list nodepools, cause"+err.Error()), diff --git a/pkg/yurtmanager/webhook/platformadmin/v1alpha2/platformadmin_validation.go b/pkg/yurtmanager/webhook/platformadmin/v1alpha2/platformadmin_validation.go index e5ff29546c2..55bbd424e6d 100644 --- a/pkg/yurtmanager/webhook/platformadmin/v1alpha2/platformadmin_validation.go +++ b/pkg/yurtmanager/webhook/platformadmin/v1alpha2/platformadmin_validation.go @@ -27,7 +27,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/webhook/admission" - unitv1alpha1 "github.com/openyurtio/openyurt/pkg/apis/apps/v1alpha1" + unitv1beta1 "github.com/openyurtio/openyurt/pkg/apis/apps/v1beta1" "github.com/openyurtio/openyurt/pkg/apis/iot/v1alpha2" "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/platformadmin/config" util "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/platformadmin/utils" @@ -108,7 +108,7 @@ func (webhook *PlatformAdminHandler) validatePlatformAdminSpec(platformAdmin *v1 func (webhook *PlatformAdminHandler) validatePlatformAdminWithNodePools(ctx context.Context, platformAdmin *v1alpha2.PlatformAdmin) field.ErrorList { // verify that the poolname is a right nodepool name - nodePools := &unitv1alpha1.NodePoolList{} + nodePools := &unitv1beta1.NodePoolList{} if err := webhook.Client.List(ctx, nodePools); err != nil { return field.ErrorList{ field.Invalid(field.NewPath("spec", "poolName"), platformAdmin.Spec.PoolName, "can not list nodepools, cause"+err.Error()),