Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Upgrade platformadmin's yurtappset dependencies to v1beta1 #2103

Merged
merged 1 commit into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -535,48 +555,45 @@ 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!
if component.Deployment == nil {
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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()),
Expand Down
Loading