Skip to content

Commit

Permalink
Refactor deleting service from api
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkisaolamb committed Dec 8, 2023
1 parent 074fc0b commit a68cc8c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 48 deletions.
30 changes: 30 additions & 0 deletions controllers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand Down
25 changes: 2 additions & 23 deletions controllers/novaconductor_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
25 changes: 2 additions & 23 deletions controllers/novascheduler_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
5 changes: 3 additions & 2 deletions test/functional/novaconductor_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand All @@ -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())
})
})
Expand Down

0 comments on commit a68cc8c

Please sign in to comment.