Skip to content

Commit dfa82dd

Browse files
committed
Switch to eviction condition, improve tests, imports, auth
Instead of duplicated and mostly inconsistent eviction status as a status field, use conditions to display and check for the eviction status. More conditions are anticapted to provide insights about current state of eviction. refactored GetServiceClientAuth into GetServiceClient to support not-password-polluted environents. Remove httpmock requirement, httptest should be enough. Improve tests by explicitly checking every iteration of a reconciliation. This can be used as a template for other tests to increase coverage.
1 parent a94684c commit dfa82dd

17 files changed

Lines changed: 207 additions & 182 deletions

api/v1/eviction_types.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ import (
2121
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2222
)
2323

24-
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
25-
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
26-
2724
// EvictionSpec defines the desired state of Eviction
2825
type EvictionSpec struct {
2926
// +kubebuilder:validation:Required
@@ -40,11 +37,22 @@ type EvictionSpec struct {
4037
Reason string `json:"reason"`
4138
}
4239

40+
const (
41+
// ConditionTypeEviction is the type of condition for eviction status
42+
ConditionTypeEviction = "Eviction"
43+
44+
// ConditionReasonRunning means the eviction is currently running
45+
ConditionReasonRunning string = "Running"
46+
47+
// ConditionReasonFailed means the eviction has failed
48+
ConditionReasonFailed string = "Failed"
49+
50+
// ConditionReasonSuceeded means the eviction has succeeded
51+
ConditionReasonSuceeded string = "Succeeded"
52+
)
53+
4354
// EvictionStatus defines the observed state of Eviction
4455
type EvictionStatus struct {
45-
// +kubebuilder:validation:Enum=Pending;Running;Failed;Succeeded
46-
// +kubebuilder:default=Pending
47-
EvictionState string `json:"evictionState"`
4856
// +kubebuilder:validation:Optional
4957
HypervisorServiceId string `json:"hypervisorServiceId"`
5058
// +kubebuilder:validation:Optional
@@ -62,7 +70,7 @@ type EvictionStatus struct {
6270
// +kubebuilder:resource:scope=Cluster,shortName=evi
6371
// +kubebuilder:printcolumn:JSONPath=".spec.hypervisor",name="Hypervisor",type="string"
6472
// +kubebuilder:printcolumn:JSONPath=".spec.reason",name="Reason",type="string"
65-
// +kubebuilder:printcolumn:JSONPath=".status.evictionState",name="State",type="string"
73+
// +kubebuilder:printcolumn:JSONPath=".status.conditions[?(@.type==\"Eviction\")].reason",name="State",type="string"
6674
// +kubebuilder:printcolumn:JSONPath=".metadata.creationTimestamp",name="Created",type="date"
6775

6876
// Eviction is the Schema for the evictions API

charts/openstack-hypervisor-operator/crds/eviction-crd.yaml

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ spec:
2222
- jsonPath: .spec.reason
2323
name: Reason
2424
type: string
25-
- jsonPath: .status.evictionState
25+
- jsonPath: .status.conditions[?(@.type=="Eviction")].reason
2626
name: State
2727
type: string
2828
- jsonPath: .metadata.creationTimestamp
@@ -128,14 +128,6 @@ spec:
128128
- type
129129
type: object
130130
type: array
131-
evictionState:
132-
default: Pending
133-
enum:
134-
- Pending
135-
- Running
136-
- Failed
137-
- Succeeded
138-
type: string
139131
hypervisorServiceId:
140132
type: string
141133
outstandingInstances:
@@ -146,8 +138,6 @@ spec:
146138
default: 0
147139
format: int64
148140
type: integer
149-
required:
150-
- evictionState
151141
type: object
152142
type: object
153143
served: true

config/crd/bases/kvm.cloud.sap_evictions.yaml

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ spec:
2323
- jsonPath: .spec.reason
2424
name: Reason
2525
type: string
26-
- jsonPath: .status.evictionState
26+
- jsonPath: .status.conditions[?(@.type=="Eviction")].reason
2727
name: State
2828
type: string
2929
- jsonPath: .metadata.creationTimestamp
@@ -129,14 +129,6 @@ spec:
129129
- type
130130
type: object
131131
type: array
132-
evictionState:
133-
default: Pending
134-
enum:
135-
- Pending
136-
- Running
137-
- Failed
138-
- Succeeded
139-
type: string
140132
hypervisorServiceId:
141133
type: string
142134
outstandingInstances:
@@ -147,8 +139,6 @@ spec:
147139
default: 0
148140
format: int64
149141
type: integer
150-
required:
151-
- evictionState
152142
type: object
153143
type: object
154144
served: true

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ require (
99
github.com/go-openapi/swag v0.23.1
1010
github.com/gophercloud/gophercloud/v2 v2.7.0
1111
github.com/gophercloud/utils/v2 v2.0.0-20250711132455-9770683b100a
12-
github.com/jarcoal/httpmock v1.4.0
1312
github.com/onsi/ginkgo/v2 v2.23.4
1413
github.com/onsi/gomega v1.37.0
1514
k8s.io/api v0.33.3

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ github.com/grpc-ecosystem/grpc-gateway/v2 v2.25.1 h1:VNqngBF40hVlDloBruUehVYC3Ar
7474
github.com/grpc-ecosystem/grpc-gateway/v2 v2.25.1/go.mod h1:RBRO7fro65R6tjKzYgLAFo0t1QEXY1Dp+i/bvpRiqiQ=
7575
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
7676
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
77-
github.com/jarcoal/httpmock v1.4.0 h1:BvhqnH0JAYbNudL2GMJKgOHe2CtKlzJ/5rWKyp+hc2k=
78-
github.com/jarcoal/httpmock v1.4.0/go.mod h1:ftW1xULwo+j0R0JJkJIIi7UKigZUXCLLanykgjwBXL0=
7977
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
8078
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
8179
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
@@ -92,8 +90,6 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0
9290
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
9391
github.com/mailru/easyjson v0.9.0 h1:PrnmzHw7262yW8sTBwxi1PdJA3Iw/EKBa8psRf7d9a4=
9492
github.com/mailru/easyjson v0.9.0/go.mod h1:1+xMtQp2MRNVL/V1bOzuP3aP8VNwRW55fQUto+XFtTU=
95-
github.com/maxatome/go-testdeep v1.14.0 h1:rRlLv1+kI8eOI3OaBXZwb3O7xY3exRzdW5QyX48g9wI=
96-
github.com/maxatome/go-testdeep v1.14.0/go.mod h1:lPZc/HAcJMP92l7yI6TRz1aZN5URwUBUAfUNvrclaNM=
9793
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
9894
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
9995
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=

internal/controller/aggregates_controller.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ import (
3030
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
3131
logger "sigs.k8s.io/controller-runtime/pkg/log"
3232

33-
"github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/openstack"
3433
"github.com/gophercloud/gophercloud/v2"
3534
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/aggregates"
35+
36+
"github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/openstack"
3637
)
3738

3839
const (
@@ -109,7 +110,7 @@ func (r *AggregatesController) SetupWithManager(mgr ctrl.Manager) error {
109110
_ = logger.FromContext(ctx)
110111

111112
var err error
112-
if r.computeClient, err = openstack.GetServiceClient(ctx, "compute"); err != nil {
113+
if r.computeClient, err = openstack.GetServiceClient(ctx, "compute", nil); err != nil {
113114
return err
114115
}
115116
r.computeClient.Microversion = "2.40" // gophercloud only supports numeric ids

internal/controller/decomission_controller.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@ limitations under the License.
1717

1818
package controller
1919

20+
import "C"
2021
import (
2122
"context"
2223
"fmt"
2324
"net/http"
2425

26+
"github.com/gophercloud/gophercloud/v2"
27+
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/hypervisors"
28+
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/services"
29+
"github.com/gophercloud/gophercloud/v2/openstack/placement/v1/resourceproviders"
2530
corev1 "k8s.io/api/core/v1"
2631
"k8s.io/apimachinery/pkg/labels"
2732
"k8s.io/apimachinery/pkg/runtime"
@@ -32,10 +37,6 @@ import (
3237

3338
kvmv1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/api/v1"
3439
"github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/openstack"
35-
"github.com/gophercloud/gophercloud/v2"
36-
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/hypervisors"
37-
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/services"
38-
"github.com/gophercloud/gophercloud/v2/openstack/placement/v1/resourceproviders"
3940
)
4041

4142
const (
@@ -159,13 +160,13 @@ func (r *NodeDecommissionReconciler) SetupWithManager(mgr ctrl.Manager) error {
159160
ctx := context.Background()
160161

161162
var err error
162-
if r.computeClient, err = openstack.GetServiceClient(ctx, "compute"); err != nil {
163+
if r.computeClient, err = openstack.GetServiceClient(ctx, "compute", nil); err != nil {
163164
return err
164165
}
165166

166167
r.computeClient.Microversion = "2.93"
167168

168-
r.placementClient, err = openstack.GetServiceClient(ctx, "placement")
169+
r.placementClient, err = openstack.GetServiceClient(ctx, "placement", nil)
169170
if err != nil {
170171
return err
171172
}

internal/controller/decomission_controller_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package controller
2020
import (
2121
"context"
2222

23+
"github.com/gophercloud/gophercloud/v2/testhelper"
2324
. "github.com/onsi/ginkgo/v2"
2425
. "github.com/onsi/gomega"
2526
corev1 "k8s.io/api/core/v1"
@@ -77,7 +78,10 @@ var _ = Describe("Decommission Controller", func() {
7778
})
7879

7980
It("should successfully reconcile the resource", func() {
80-
By("Reconciling the created resource")
81+
By("ConditionType the created resource")
82+
testhelper.SetupHTTP()
83+
defer testhelper.TeardownHTTP()
84+
8185
_, err := reconcileNodeLoop(1)
8286
Expect(err).NotTo(HaveOccurred())
8387
})

0 commit comments

Comments
 (0)