Right now the defaulting logic is embedded in pkg/webhook, and the manner in which it is written synthesizes jsonpatch objects directly. Instead, we should have logic that populates defaults as members of types themselves.
So for example, the logic to default a v1alpha1.ConfigurationSpec would become:
func (cs *ConfigurationSpec) SetDefaults() { ... }
I believe this also tees things up for use in the SetDefaults_Foo functions that defaulter-gen expects (context: #1262).
Then to leverage this in pkg/webhook, we'd need to synthesize jsonpatches post-facto, which the library we are using allows, e.g.:
rawInput := ...
if err := json.Unmarshal(rawInput, &input); err != nil {
// handle err
}
// Default a fresh copy, for the diff.
output := input.DeepCopy()
output.SetDefaults()
rawOutput, err := json.Marshal(output)
if err != nil {
// handle err
}
// Synthesize the jsonpatches
patch, err := jsonpatch.CreatePatch(rawInput, rawOutput)
if err != nil {
// handle err
}
Migrating the validation methods is even more straightforward since they just return errors and AFAIK have no meaningful dependencies on other facets of pkg/webhook
Right now the defaulting logic is embedded in
pkg/webhook, and the manner in which it is written synthesizesjsonpatchobjects directly. Instead, we should have logic that populates defaults as members of types themselves.So for example, the logic to default a
v1alpha1.ConfigurationSpecwould become:I believe this also tees things up for use in the
SetDefaults_Foofunctions thatdefaulter-genexpects (context: #1262).Then to leverage this in
pkg/webhook, we'd need to synthesize jsonpatches post-facto, which the library we are using allows, e.g.:Migrating the validation methods is even more straightforward since they just return errors and AFAIK have no meaningful dependencies on other facets of
pkg/webhook