@@ -3,11 +3,9 @@ package controllers
33import (
44 "context"
55 "fmt"
6- "os"
7- "strings"
8-
96 cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
107 arbv1 "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/apis/controller/v1beta1"
8+ "os"
119
1210 "k8s.io/klog"
1311 ctrl "sigs.k8s.io/controller-runtime"
@@ -44,13 +42,13 @@ func (r *AppWrapperReconciler) scaleMachinePool(ctx context.Context, aw *arbv1.A
4442 m [aw .Name ] = aw .Name
4543 klog .Infof ("The instanceRequired array: %v" , userRequestedInstanceType )
4644
47- machinePoolID := strings . ReplaceAll (aw .Name + "-" + userRequestedInstanceType , "." , "-" )
48- createMachinePool , err := cmv1 .NewMachinePool ().ID (machinePoolID ).InstanceType (userRequestedInstanceType ).Replicas (replicas ).Labels (m ).Build ()
45+ machinePoolID := r . generateMachineName (aw .Name )
46+ machinePool , err := cmv1 .NewMachinePool ().ID (machinePoolID ).InstanceType (userRequestedInstanceType ).Replicas (replicas ).Labels (m ).Build ()
4947 if err != nil {
5048 klog .Errorf (`Error building MachinePool: %v` , err )
5149 }
52- klog .Infof ("Built MachinePool with instance type %v and name %v" , userRequestedInstanceType , createMachinePool .ID ())
53- response , err := clusterMachinePools .Add ().Body (createMachinePool ).SendContext (ctx )
50+ klog .Infof ("Built MachinePool with instance type %v and name %v" , userRequestedInstanceType , machinePool .ID ())
51+ response , err := clusterMachinePools .Add ().Body (machinePool ).SendContext (ctx )
5452 if err != nil {
5553 klog .Errorf (`Error creating MachinePool: %v` , err )
5654 }
@@ -73,15 +71,17 @@ func (r *AppWrapperReconciler) deleteMachinePool(ctx context.Context, aw *arbv1.
7371 machinePoolsListResponse , _ := machinePoolsConnection .Send ()
7472 machinePoolsList := machinePoolsListResponse .Items ()
7573 machinePoolsList .Range (func (index int , item * cmv1.MachinePool ) bool {
76- id , _ := item .GetID ()
77- if strings . Contains ( id , aw . Name ) {
74+ if hasAwLabel ( item .Labels (), aw ) {
75+ id , _ := item . GetID ()
7876 targetMachinePool , err := connection .ClustersMgmt ().V1 ().Clusters ().Cluster (r .ocmClusterID ).MachinePools ().MachinePool (id ).Delete ().SendContext (ctx )
7977 if err != nil {
8078 klog .Infof ("Error deleting target machinepool %v" , targetMachinePool )
79+ } else {
80+ klog .Infof ("Successfully scaled down target machinepool %v" , id )
8181 }
82- klog .Infof ("Successfully Scaled down target machinepool %v" , id )
8382 }
8483 return true
8584 })
85+
8686 return ctrl.Result {Requeue : false }, nil
8787}
0 commit comments