Skip to content

Commit 1fb995e

Browse files
Consolidate Status and Condition reporting (#265)
* refactor event/status reporting * only perform update call if the condition changed * don't use private fields of EventStatusRecorder * set ready false at beginning of reconcile loop * add information about kind image loading with containerd * fix match string for chainsaw tests * change format string to wrap error
1 parent d15dc18 commit 1fb995e

File tree

28 files changed

+338
-289
lines changed

28 files changed

+338
-289
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ There are several ways to install NetBox Operator on your Cluster:
3030

3131
## Running both NetBox Operator and NetBox on a local kind cluster
3232

33-
Note: This requires Docker BuildKit.
33+
> **Note:** This requires Docker BuildKit.
34+
35+
> **Note:** There is currently a bug where if Docker uses containerd for pulling and storing images, kind fails to load the image into the cluster ([GitHub Issue](https://github.com/kubernetes-sigs/kind/issues/3795)).
36+
To resolve this, temporarily disable this option in the Docker settings: "General > Use containerd for pulling and storing images"
3437
3538
- Create kind cluster with a NetBox deployment: `make create-kind`
3639
- Deploy the NetBox Operator on the local kind cluster: `make deploy-kind` (In case you're using podman use `CONTAINER_TOOL=podman make deploy-kind`)

api/v1/ipaddress_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ type IpAddress struct {
9999
Status IpAddressStatus `json:"status,omitempty"`
100100
}
101101

102+
func (i *IpAddress) Conditions() *[]metav1.Condition {
103+
return &i.Status.Conditions
104+
}
105+
102106
//+kubebuilder:object:root=true
103107

104108
// IpAddressList contains a list of IpAddress

api/v1/ipaddressclaim_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ type IpAddressClaim struct {
104104
Status IpAddressClaimStatus `json:"status,omitempty"`
105105
}
106106

107+
func (i *IpAddressClaim) Conditions() *[]metav1.Condition {
108+
return &i.Status.Conditions
109+
}
110+
107111
//+kubebuilder:object:root=true
108112

109113
// IpAddressClaimList contains a list of IpAddressClaim

api/v1/iprange_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ type IpRange struct {
108108
Status IpRangeStatus `json:"status,omitempty"`
109109
}
110110

111+
func (i *IpRange) Conditions() *[]metav1.Condition {
112+
return &i.Status.Conditions
113+
}
114+
111115
//+kubebuilder:object:root=true
112116

113117
// IpRangeList contains a list of IpRange

api/v1/iprangeclaim_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ type IpRangeClaim struct {
133133
Status IpRangeClaimStatus `json:"status,omitempty"`
134134
}
135135

136+
func (i *IpRangeClaim) Conditions() *[]metav1.Condition {
137+
return &i.Status.Conditions
138+
}
139+
136140
//+kubebuilder:object:root=true
137141

138142
// IpRangeClaimList contains a list of IpRangeClaim

api/v1/prefix_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ type Prefix struct {
104104
Status PrefixStatus `json:"status,omitempty"`
105105
}
106106

107+
func (p *Prefix) Conditions() *[]metav1.Condition {
108+
return &p.Status.Conditions
109+
}
110+
107111
// +kubebuilder:object:root=true
108112

109113
// PrefixList contains a list of Prefix

api/v1/prefixclaim_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ type PrefixClaim struct {
132132
Status PrefixClaimStatus `json:"status,omitempty"`
133133
}
134134

135+
func (p *PrefixClaim) Conditions() *[]metav1.Condition {
136+
return &p.Status.Conditions
137+
}
138+
135139
// +kubebuilder:object:root=true
136140

137141
// PrefixClaimList contains a list of PrefixClaim

api/v1/shared_conditions.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
Copyright 2024 Swisscom (Schweiz) AG.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1
18+
19+
import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
20+
21+
var ConditionReadyFalseNewResource = metav1.Condition{
22+
Type: "Ready",
23+
Status: "False",
24+
Reason: "NewResource",
25+
Message: "Pending Reconciliation",
26+
}

cmd/main.go

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -168,67 +168,67 @@ func main() {
168168
}
169169

170170
if err = (&controller.IpAddressReconciler{
171-
Client: mgr.GetClient(),
172-
Scheme: mgr.GetScheme(),
173-
Recorder: mgr.GetEventRecorderFor("ip-address-controller"),
174-
NetboxClient: netboxClient,
175-
OperatorNamespace: operatorNamespace,
176-
RestConfig: mgr.GetConfig(),
171+
Client: mgr.GetClient(),
172+
Scheme: mgr.GetScheme(),
173+
EventStatusRecorder: controller.NewEventStatusRecorder(mgr.GetClient(), mgr.GetEventRecorderFor("ip-address-controller")),
174+
NetboxClient: netboxClient,
175+
OperatorNamespace: operatorNamespace,
176+
RestConfig: mgr.GetConfig(),
177177
}).SetupWithManager(mgr); err != nil {
178178
setupLog.Error(err, "unable to create controller", "controller", "IpAddress")
179179
os.Exit(1)
180180
}
181181
if err = (&controller.IpAddressClaimReconciler{
182-
Client: mgr.GetClient(),
183-
Scheme: mgr.GetScheme(),
184-
Recorder: mgr.GetEventRecorderFor("ip-address-claim-controller"),
185-
NetboxClient: netboxClient,
186-
OperatorNamespace: operatorNamespace,
187-
RestConfig: mgr.GetConfig(),
182+
Client: mgr.GetClient(),
183+
Scheme: mgr.GetScheme(),
184+
EventStatusRecorder: controller.NewEventStatusRecorder(mgr.GetClient(), mgr.GetEventRecorderFor("ip-address-claim-controller")),
185+
NetboxClient: netboxClient,
186+
OperatorNamespace: operatorNamespace,
187+
RestConfig: mgr.GetConfig(),
188188
}).SetupWithManager(mgr); err != nil {
189189
setupLog.Error(err, "unable to create controller", "controller", "IpAddressClaim")
190190
os.Exit(1)
191191
}
192192
if err = (&controller.PrefixReconciler{
193-
Client: mgr.GetClient(),
194-
Scheme: mgr.GetScheme(),
195-
Recorder: mgr.GetEventRecorderFor("prefix-controller"),
196-
NetboxClient: netboxClient,
197-
OperatorNamespace: operatorNamespace,
198-
RestConfig: mgr.GetConfig(),
193+
Client: mgr.GetClient(),
194+
Scheme: mgr.GetScheme(),
195+
EventStatusRecorder: controller.NewEventStatusRecorder(mgr.GetClient(), mgr.GetEventRecorderFor("prefix-controller")),
196+
NetboxClient: netboxClient,
197+
OperatorNamespace: operatorNamespace,
198+
RestConfig: mgr.GetConfig(),
199199
}).SetupWithManager(mgr); err != nil {
200200
setupLog.Error(err, "unable to create controller", "controller", "Prefix")
201201
os.Exit(1)
202202
}
203203
if err = (&controller.PrefixClaimReconciler{
204-
Client: mgr.GetClient(),
205-
Scheme: mgr.GetScheme(),
206-
Recorder: mgr.GetEventRecorderFor("prefix-claim-controller"),
207-
NetboxClient: netboxClient,
208-
OperatorNamespace: operatorNamespace,
209-
RestConfig: mgr.GetConfig(),
204+
Client: mgr.GetClient(),
205+
Scheme: mgr.GetScheme(),
206+
EventStatusRecorder: controller.NewEventStatusRecorder(mgr.GetClient(), mgr.GetEventRecorderFor("prefix-claim-controller")),
207+
NetboxClient: netboxClient,
208+
OperatorNamespace: operatorNamespace,
209+
RestConfig: mgr.GetConfig(),
210210
}).SetupWithManager(mgr); err != nil {
211211
setupLog.Error(err, "unable to create controller", "controller", "PrefixClaim")
212212
os.Exit(1)
213213
}
214214
if err = (&controller.IpRangeClaimReconciler{
215-
Client: mgr.GetClient(),
216-
Scheme: mgr.GetScheme(),
217-
Recorder: mgr.GetEventRecorderFor("ip-range-claim-controller"),
218-
NetboxClient: netboxClient,
219-
OperatorNamespace: operatorNamespace,
220-
RestConfig: mgr.GetConfig(),
215+
Client: mgr.GetClient(),
216+
Scheme: mgr.GetScheme(),
217+
EventStatusRecorder: controller.NewEventStatusRecorder(mgr.GetClient(), mgr.GetEventRecorderFor("ip-range-claim-controller")),
218+
NetboxClient: netboxClient,
219+
OperatorNamespace: operatorNamespace,
220+
RestConfig: mgr.GetConfig(),
221221
}).SetupWithManager(mgr); err != nil {
222222
setupLog.Error(err, "unable to create controller", "controller", "IpRangeClaim")
223223
os.Exit(1)
224224
}
225225
if err = (&controller.IpRangeReconciler{
226-
Client: mgr.GetClient(),
227-
Scheme: mgr.GetScheme(),
228-
Recorder: mgr.GetEventRecorderFor("ip-range-controller"),
229-
NetboxClient: netboxClient,
230-
OperatorNamespace: operatorNamespace,
231-
RestConfig: mgr.GetConfig(),
226+
Client: mgr.GetClient(),
227+
Scheme: mgr.GetScheme(),
228+
EventStatusRecorder: controller.NewEventStatusRecorder(mgr.GetClient(), mgr.GetEventRecorderFor("ip-range-controller")),
229+
NetboxClient: netboxClient,
230+
OperatorNamespace: operatorNamespace,
231+
RestConfig: mgr.GetConfig(),
232232
}).SetupWithManager(mgr); err != nil {
233233
setupLog.Error(err, "unable to create controller", "controller", "IpRange")
234234
os.Exit(1)

internal/controller/interfaces.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Copyright 2024 Swisscom (Schweiz) AG.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package controller
18+
19+
import (
20+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
"sigs.k8s.io/controller-runtime/pkg/client"
22+
)
23+
24+
type ObjectWithConditions interface {
25+
client.Object
26+
Conditions() *[]metav1.Condition
27+
}

0 commit comments

Comments
 (0)