Skip to content

Move Default and Validate logic into pkg/apis/serving/v1alpha1 #1518

@mattmoor

Description

@mattmoor

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

Metadata

Metadata

Assignees

Labels

area/APIAPI objects and controllerskind/cleanupCategorizes issue or PR as related to cleaning up code, process, or technical debt.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions