diff --git a/controllers/common.go b/controllers/common.go index 9b4bc984e..4e9e7dad2 100644 --- a/controllers/common.go +++ b/controllers/common.go @@ -38,6 +38,7 @@ import ( "github.com/openstack-k8s-operators/nova-operator/pkg/nova" gophercloud "github.com/gophercloud/gophercloud" + "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/services" "github.com/openstack-k8s-operators/lib-common/modules/common/condition" "github.com/openstack-k8s-operators/lib-common/modules/common/env" helper "github.com/openstack-k8s-operators/lib-common/modules/common/helper" @@ -102,6 +103,35 @@ type conditionsGetter interface { GetConditions() condition.Conditions } +func cleanNovaServiceFromNovaDb(ctx context.Context, + h *helper.Helper, authURL string, adminUser string, + authPassword string, timeout time.Duration, l logr.Logger, serviceName string) error { + computeClient, _, err := getNovaClient(ctx, h, authURL, adminUser, authPassword, defaultRequestTimeout, l) + if err != nil { + return err + } + opts := services.ListOpts{ + Binary: serviceName, + } + + allPages, err := services.List(computeClient, opts).AllPages() + if err != nil { + return err + } + + allServices, err := services.ExtractServices(allPages) + if err != nil { + return err + } + for _, service := range allServices { + if service.State == "down" { + services.Delete(computeClient, service.ID) + } + } + + return err +} + func allSubConditionIsTrue(conditionsGetter conditionsGetter) bool { // It assumes that all of our conditions report success via the True status for _, c := range conditionsGetter.GetConditions() { diff --git a/controllers/novaconductor_controller.go b/controllers/novaconductor_controller.go index 6a4fa74bd..0ae2f765d 100644 --- a/controllers/novaconductor_controller.go +++ b/controllers/novaconductor_controller.go @@ -31,7 +31,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/source" "github.com/go-logr/logr" - "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/services" common "github.com/openstack-k8s-operators/lib-common/modules/common" "github.com/openstack-k8s-operators/lib-common/modules/common/condition" "github.com/openstack-k8s-operators/lib-common/modules/common/env" @@ -462,28 +461,8 @@ func (r *NovaConductorReconciler) cleanServiceFromNovaDb( ) error { authPassword := string(secret.Data[ServicePasswordSelector]) - computeClient, _, err := getNovaClient(ctx, h, instance.Spec.KeystoneAuthURL, instance.Spec.ServiceUser, authPassword, defaultRequestTimeout, l) - if err != nil { - return err - } - opts := services.ListOpts{ - Binary: "nova-conductor", - } - - allPages, err := services.List(computeClient, opts).AllPages() - if err != nil { - return err - } - - allServices, err := services.ExtractServices(allPages) - if err != nil { - return err - } - for _, service := range allServices { - if service.State == "down" { - services.Delete(computeClient, service.ID) - } - } + err := cleanNovaServiceFromNovaDb(ctx, h, instance.Spec.KeystoneAuthURL, + instance.Spec.ServiceUser, authPassword, defaultRequestTimeout, l, "nova-conductor") return err } diff --git a/controllers/novascheduler_controller.go b/controllers/novascheduler_controller.go index 195f80ea1..3653fb28a 100644 --- a/controllers/novascheduler_controller.go +++ b/controllers/novascheduler_controller.go @@ -30,7 +30,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/source" "github.com/go-logr/logr" - "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/services" common "github.com/openstack-k8s-operators/lib-common/modules/common" "github.com/openstack-k8s-operators/lib-common/modules/common/condition" "github.com/openstack-k8s-operators/lib-common/modules/common/env" @@ -422,28 +421,8 @@ func (r *NovaSchedulerReconciler) cleanServiceFromNovaDb( l logr.Logger, ) error { authPassword := string(secret.Data[ServicePasswordSelector]) - computeClient, _, err := getNovaClient(ctx, h, instance.Spec.KeystoneAuthURL, instance.Spec.ServiceUser, authPassword, defaultRequestTimeout, l) - if err != nil { - return err - } - opts := services.ListOpts{ - Binary: "nova-scheduler", - } - - allPages, err := services.List(computeClient, opts).AllPages() - if err != nil { - return err - } - - allServices, err := services.ExtractServices(allPages) - if err != nil { - return err - } - for _, service := range allServices { - if service.State == "down" { - services.Delete(computeClient, service.ID) - } - } + err := cleanNovaServiceFromNovaDb(ctx, h, instance.Spec.KeystoneAuthURL, + instance.Spec.ServiceUser, authPassword, defaultRequestTimeout, l, "nova-scheduler") return err } diff --git a/test/functional/novaconductor_controller_test.go b/test/functional/novaconductor_controller_test.go index 1dd18a3aa..aae60525d 100644 --- a/test/functional/novaconductor_controller_test.go +++ b/test/functional/novaconductor_controller_test.go @@ -642,7 +642,7 @@ var _ = Describe("NovaConductor controller", func() { }) }) -var _ = Describe("NovaConductorcontroller cleaning", func() { +var _ = Describe("NovaConductor controller cleaning", func() { var novaAPIServer *NovaAPIFixture BeforeEach(func() { novaAPIServer = NewNovaAPIFixtureWithServer(logger) @@ -671,7 +671,7 @@ var _ = Describe("NovaConductorcontroller cleaning", func() { th.SimulateJobSuccess(cell0.DBSyncJobName) th.SimulateStatefulSetReplicaReady(cell0.ConductorStatefulSetName) }) - When("NovaScheduler down service is removed from api", func() { + When("NovaConductor down service is removed from api", func() { It("during reconciling", func() { th.SimulateStatefulSetReplicaReady(cell0.ConductorStatefulSetName) th.ExpectCondition( @@ -680,6 +680,7 @@ var _ = Describe("NovaConductorcontroller cleaning", func() { condition.DeploymentReadyCondition, corev1.ConditionTrue, ) + Expect(novaAPIServer.FindRequest("GET", "/compute/os-services/", "binary=nova-conductor")).To(BeTrue()) Expect(novaAPIServer.FindRequest("DELETE", "/compute/os-services/3", "")).To(BeTrue()) }) })