Skip to content

Commit e286d21

Browse files
committed
Provide top-level visibility of kubectl modifications of hypervisor CROs
As we want also human operators to modify the CRO, at the very least to set a hypervisor into maintenance, we need top-level visiblity of such an action. The HypervisorTaintController will check the managedFields for any modification through kubeclt (kubectl-apply, kubectl-edit...) and add a condition, which is prominently displayed in the overview table.
1 parent 8422192 commit e286d21

File tree

6 files changed

+218
-0
lines changed

6 files changed

+218
-0
lines changed

api/v1/hypervisor_types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const (
3030
// ConditionTypeReady is the type of condition for ready status of a hypervisor
3131
ConditionTypeReady = "Ready"
3232
ConditionTypeTerminating = "Terminating"
33+
ConditionTypeTainted = "Tainted"
3334

3435
// Reasons for the various being ready...
3536
ConditionReasonReadyReady = "ready"
@@ -218,6 +219,7 @@ type HypervisorStatus struct {
218219
// +kubebuilder:printcolumn:JSONPath=.metadata.labels.worker\.garden\.sapcloud\.io/group,name="Group",type="string",priority=2
219220
// +kubebuilder:printcolumn:JSONPath=".status.conditions[?(@.type==\"Ready\")].status",name="Ready",type="string"
220221
// +kubebuilder:printcolumn:JSONPath=".status.conditions[?(@.type==\"Ready\")].reason",name="State",type="string"
222+
// +kubebuilder:printcolumn:JSONPath=".status.conditions[?(@.type==\"Tainted\")].message",name="Taint",type="string"
221223
// +kubebuilder:printcolumn:JSONPath=".spec.lifecycleEnabled",name="Lifecycle",type="boolean"
222224
// +kubebuilder:printcolumn:JSONPath=".spec.highAvailability",name="High Availability",type="boolean"
223225
// +kubebuilder:printcolumn:JSONPath=".spec.skipTests",name="Skip Tests",type="boolean"

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ spec:
3434
- jsonPath: .status.conditions[?(@.type=="Ready")].reason
3535
name: State
3636
type: string
37+
- jsonPath: .status.conditions[?(@.type=="Tainted")].message
38+
name: Taint
39+
type: string
3740
- jsonPath: .spec.lifecycleEnabled
3841
name: Lifecycle
3942
type: boolean

cmd/main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,14 @@ func main() {
292292
os.Exit(1)
293293
}
294294

295+
if err = (&controller.HypervisorTaintController{
296+
Client: mgr.GetClient(),
297+
Scheme: mgr.GetScheme(),
298+
}).SetupWithManager(mgr); err != nil {
299+
setupLog.Error(err, "unable to create controller", "controller", controller.HypervisorTaintControllerName)
300+
os.Exit(1)
301+
}
302+
295303
// +kubebuilder:scaffold:builder
296304

297305
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ spec:
3535
- jsonPath: .status.conditions[?(@.type=="Ready")].reason
3636
name: State
3737
type: string
38+
- jsonPath: .status.conditions[?(@.type=="Tainted")].message
39+
name: Taint
40+
type: string
3841
- jsonPath: .spec.lifecycleEnabled
3942
name: Lifecycle
4043
type: boolean
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
SPDX-FileCopyrightText: Copyright 2025 SAP SE or an SAP affiliate company and cobaltcore-dev contributors
3+
SPDX-License-Identifier: Apache-2.0
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
package controller
19+
20+
import (
21+
"context"
22+
"strings"
23+
24+
"k8s.io/apimachinery/pkg/api/equality"
25+
"k8s.io/apimachinery/pkg/api/meta"
26+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
"k8s.io/apimachinery/pkg/runtime"
28+
ctrl "sigs.k8s.io/controller-runtime"
29+
"sigs.k8s.io/controller-runtime/pkg/builder"
30+
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
31+
"sigs.k8s.io/controller-runtime/pkg/predicate"
32+
33+
kvmv1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/api/v1"
34+
)
35+
36+
const (
37+
HypervisorTaintControllerName = "HypervisorTaint"
38+
)
39+
40+
type HypervisorTaintController struct {
41+
k8sclient.Client
42+
Scheme *runtime.Scheme
43+
}
44+
45+
// +kubebuilder:rbac:groups=kvm.cloud.sap,resources=hypervisors,verbs=get;list;watch
46+
// +kubebuilder:rbac:groups=kvm.cloud.sap,resources=hypervisors/status,verbs=get;list;watch;create;update;patch
47+
48+
func (r *HypervisorTaintController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
49+
hypervisor := &kvmv1.Hypervisor{
50+
ObjectMeta: metav1.ObjectMeta{
51+
Name: req.Name,
52+
Labels: map[string]string{},
53+
},
54+
Spec: kvmv1.HypervisorSpec{
55+
HighAvailability: true,
56+
InstallCertificate: true,
57+
},
58+
}
59+
60+
// Check if hypervisor already exists
61+
if err := r.Get(ctx, req.NamespacedName, hypervisor); err != nil {
62+
return ctrl.Result{}, k8sclient.IgnoreNotFound(err)
63+
}
64+
65+
tainted := false
66+
for _, field := range hypervisor.GetManagedFields() {
67+
if strings.HasPrefix(field.Manager, "kubectl") {
68+
tainted = true
69+
break
70+
}
71+
}
72+
73+
before := hypervisor.DeepCopy()
74+
if tainted {
75+
meta.SetStatusCondition(&hypervisor.Status.Conditions, metav1.Condition{
76+
Type: kvmv1.ConditionTypeTainted,
77+
Status: metav1.ConditionTrue,
78+
Reason: "Kubectl",
79+
Message: "⚠️",
80+
ObservedGeneration: hypervisor.Generation,
81+
})
82+
} else {
83+
meta.SetStatusCondition(&hypervisor.Status.Conditions, metav1.Condition{
84+
Type: kvmv1.ConditionTypeTainted,
85+
Status: metav1.ConditionFalse,
86+
Reason: "NoKubectl",
87+
Message: "🟢",
88+
ObservedGeneration: hypervisor.Generation,
89+
})
90+
}
91+
92+
if equality.Semantic.DeepEqual(hypervisor, before) {
93+
return ctrl.Result{}, nil
94+
}
95+
96+
return ctrl.Result{}, r.Status().Patch(ctx, hypervisor, k8sclient.MergeFromWithOptions(before, k8sclient.MergeFromWithOptimisticLock{}))
97+
}
98+
99+
func (r *HypervisorTaintController) SetupWithManager(mgr ctrl.Manager) error {
100+
return ctrl.NewControllerManagedBy(mgr).
101+
Named(HypervisorTaintControllerName).
102+
For(&kvmv1.Hypervisor{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
103+
Complete(r)
104+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
SPDX-FileCopyrightText: Copyright 2024 SAP SE or an SAP affiliate company and cobaltcore-dev contributors
3+
SPDX-License-Identifier: Apache-2.0
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
package controller
19+
20+
import (
21+
. "github.com/onsi/ginkgo/v2"
22+
. "github.com/onsi/gomega"
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
"k8s.io/apimachinery/pkg/types"
25+
ctrl "sigs.k8s.io/controller-runtime"
26+
"sigs.k8s.io/controller-runtime/pkg/client"
27+
28+
kvmv1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/api/v1"
29+
)
30+
31+
var _ = Describe("Hypervisor Taint Controller", func() {
32+
const (
33+
hypervisorName = "test-hv"
34+
)
35+
var (
36+
controller *HypervisorTaintController
37+
resource *kvmv1.Hypervisor
38+
namespacedName = types.NamespacedName{Name: hypervisorName}
39+
reconcileReq = ctrl.Request{NamespacedName: namespacedName}
40+
)
41+
42+
BeforeEach(func(ctx SpecContext) {
43+
controller = &HypervisorTaintController{
44+
Client: k8sClient,
45+
Scheme: k8sClient.Scheme(),
46+
}
47+
48+
// pregenerate the resource
49+
resource = &kvmv1.Hypervisor{
50+
ObjectMeta: metav1.ObjectMeta{
51+
Name: hypervisorName,
52+
},
53+
}
54+
55+
Expect(k8sClient.Create(ctx, resource)).To(Succeed())
56+
57+
DeferCleanup(func(ctx SpecContext) {
58+
Expect(k8sClient.Delete(ctx, resource)).To(Succeed())
59+
})
60+
})
61+
62+
Context("When reconciling a new hypervisor", func() {
63+
It("should successfully reconcile the hypervisor", func(ctx SpecContext) {
64+
_, err := controller.Reconcile(ctx, reconcileReq)
65+
Expect(err).NotTo(HaveOccurred())
66+
67+
Expect(k8sClient.Get(ctx, namespacedName, resource)).To(Succeed())
68+
Expect(resource.Status.Conditions).To(ContainElement(
69+
SatisfyAll(
70+
HaveField("Type", kvmv1.ConditionTypeTainted),
71+
HaveField("Status", metav1.ConditionFalse),
72+
HaveField("Message", "🟢"),
73+
HaveField("ObservedGeneration", resource.Generation),
74+
)))
75+
})
76+
})
77+
78+
Context("When reconciling an edited hypervisor ", func() {
79+
BeforeEach(func(ctx SpecContext) {
80+
resource.Spec.SkipTests = true
81+
Expect(k8sClient.Update(ctx, resource, client.FieldOwner("kubectl-edit"))).To(Succeed())
82+
})
83+
84+
It("should successfully reconcile the hypervisor", func(ctx SpecContext) {
85+
_, err := controller.Reconcile(ctx, reconcileReq)
86+
Expect(err).NotTo(HaveOccurred())
87+
88+
Expect(k8sClient.Get(ctx, namespacedName, resource)).To(Succeed())
89+
Expect(resource.Status.Conditions).To(ContainElement(
90+
SatisfyAll(
91+
HaveField("Type", kvmv1.ConditionTypeTainted),
92+
HaveField("Status", metav1.ConditionTrue),
93+
HaveField("Message", "⚠️"),
94+
HaveField("ObservedGeneration", resource.Generation),
95+
)))
96+
})
97+
})
98+
})

0 commit comments

Comments
 (0)