Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

revert leaving optional for required init fields #257

Merged
merged 1 commit into from
Aug 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions pkg/types/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,31 +306,32 @@ func newTopLevelRequiredParam(path string, includeInit bool) *topLevelRequiredPa
}

func (r *resource) addParameterField(f *Field, field *types.Var) {
req := !f.Schema.Optional
requiredBySchema := !f.Schema.Optional
// Note(turkenh): We are collecting the top level required parameters that
// are not identifier fields. This is for generating CEL validation rules for
// those parameters and not to require them if the management policy is set
// Observe Only. In other words, if we are not creating or managing the
// resource, we don't need to provide those parameters which are:
// - req => required
// - requiredBySchema => required
// - !f.Identifier => not identifiers - i.e. region, zone, etc.
// - len(f.CanonicalPaths) == 1 => top level, i.e. not a nested field
// TODO (lsviben): We should add CEL rules for all required fields,
// not just the top level ones, due to having all forProvider
// fields now optional. CEL rules should check if a field is
// present either in forProvider or initProvider.
// https://github.com/upbound/upjet/issues/239
if req && !f.Identifier && len(f.CanonicalPaths) == 1 {
req = false
if requiredBySchema && !f.Identifier && len(f.CanonicalPaths) == 1 {
requiredBySchema = false
// If the field is not a terraform field, we should not require it in init,
// as it is not an initProvider field.
r.topLevelRequiredParams = append(r.topLevelRequiredParams, newTopLevelRequiredParam(f.TransformedName, f.TFTag != "-"))
}

// Note(lsviben): Only fields which are not also initProvider fields should be required.
paramRequired := req && !f.isInit()
f.Comment.Required = pointer.Bool(paramRequired)
if paramRequired {
// Note(lsviben): Only fields which are not also initProvider fields should have a required kubebuilder comment.
f.Comment.Required = pointer.Bool(requiredBySchema && !f.isInit())

// For removing omitempty tag from json tag, we are just checking if the field is required by the schema.
if requiredBySchema {
// Required fields should not have omitempty tag in json tag.
// TODO(muvaf): This overrides user intent if they provided custom
// JSON tag.
Expand Down
Loading