Skip to content

Commit e55c90a

Browse files
authored
🌱 Use strict schema validation (#1711)
1 parent 08d3b8b commit e55c90a

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

main.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"time"
2929

3030
"github.com/spf13/pflag"
31+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3132
"k8s.io/apimachinery/pkg/runtime"
3233
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3334
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
@@ -37,6 +38,7 @@ import (
3738
"sigs.k8s.io/cluster-api/util/record"
3839
ctrl "sigs.k8s.io/controller-runtime"
3940
"sigs.k8s.io/controller-runtime/pkg/cache"
41+
"sigs.k8s.io/controller-runtime/pkg/client"
4042
"sigs.k8s.io/controller-runtime/pkg/controller"
4143
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
4244
"sigs.k8s.io/controller-runtime/pkg/webhook"
@@ -91,6 +93,20 @@ var (
9193
sshAfterInstallImage bool
9294
)
9395

96+
// strictManager is a ctrl.Manager that creates controller-runtime clients that enforce strict
97+
// schema validation. If a CRD's schema does not match the controller's schema, unexpected behavior
98+
// can occur. It's better to return an error than to perform a partial update where only some fields
99+
// are applied. Related:
100+
// https://www.reddit.com/r/kubernetes/comments/1oqnn6v/schema_mismatch_between_controller_and_crd/
101+
type strictManager struct {
102+
ctrl.Manager
103+
strictClient client.Client
104+
}
105+
106+
func (m *strictManager) GetClient() client.Client {
107+
return m.strictClient
108+
}
109+
94110
func main() {
95111
fs := pflag.CommandLine
96112
fs.StringVar(&metricsAddr, "metrics-bind-address", "localhost:8080", "The address the metric endpoint binds to.")
@@ -196,12 +212,17 @@ func main() {
196212
})
197213
}
198214

199-
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), options)
215+
origManager, err := ctrl.NewManager(ctrl.GetConfigOrDie(), options)
200216
if err != nil {
201217
setupLog.Error(err, "unable to start manager")
202218
os.Exit(1)
203219
}
204220

221+
mgr := &strictManager{
222+
Manager: origManager,
223+
strictClient: client.WithFieldValidation(origManager.GetClient(), metav1.FieldValidationStrict),
224+
}
225+
205226
// Initialize event recorder.
206227
record.InitFromRecorder(mgr.GetEventRecorderFor("hetzner-controller"))
207228

0 commit comments

Comments
 (0)