Skip to content

Commit

Permalink
revert adding optional to all required fields
Browse files Browse the repository at this point in the history
  • Loading branch information
lsviben committed Aug 17, 2023
1 parent 8d86393 commit 2f62fe7
Showing 1 changed file with 9 additions and 8 deletions.
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

0 comments on commit 2f62fe7

Please sign in to comment.