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

Private lib schema #319

Merged
merged 18 commits into from
Apr 7, 2021
Merged

Private lib schema #319

merged 18 commits into from
Apr 7, 2021

Conversation

pivotaljohn
Copy link
Contributor

@pivotaljohn pivotaljohn commented Mar 12, 2021

As part of this PR we decided on some UX decisions described below:

  • When --enable-experiement-schema flag is not present, and a schema file is provided, a warning is shown. New in this PR the relative path to the file is also shown. (Consider the case where root library has a schema, and a private library has a schema, there will be two warning messages now).
  • Rules around how the --enable-experiement-schema behaves:
    When the --enable-experiement-schema flag is present, and a schema file is given in the root or in a private library, it is used as the base data values and type checked.
    When --enable-experiement-schema flag is not enabled, then if a schema file appears anywhere, the schema is IGNORED and a warning is given. This is because the schema flag is protecting the user against non-GA code.

⚠️ Update based on feedback ⚠️
We decided to split this original PR into three separate ones.

develop 
|_overlay-position       <- PR to address position changes within overlay module (separate for this PR entirely)
|_ refactor-dvpp         <- PR to address refactoring done in data_values_pre_processing.go (must merge prior to this PR)
    |_ private-lib-schema <- this PR

@pivotaljohn pivotaljohn marked this pull request as draft March 12, 2021 16:34
@pivotaljohn pivotaljohn linked an issue Mar 12, 2021 that may be closed by this pull request
@pivotaljohn pivotaljohn force-pushed the private-lib-schema branch 2 times, most recently from 02d2f76 to e86994c Compare March 13, 2021 06:32
@cari-lynn cari-lynn marked this pull request as ready for review March 22, 2021 23:07
@cari-lynn cari-lynn marked this pull request as draft March 22, 2021 23:08
@pivotaljohn
Copy link
Contributor Author

When --enable-experiement-schema flag is not enabled, then if a schema file appears anywhere, the schema is IGNORED and a warning is given. This is because the schema flag is protecting the user against non-GA code.

I really like this choice. It gives the author the ability to support both modes. Include both a schema.yml and values/default.yml with basically the same content. If I enable schemas, I get the schema checks. If I don't I get the overlay checks.

@cari-lynn cari-lynn marked this pull request as ready for review March 23, 2021 18:01
@cari-lynn cari-lynn marked this pull request as draft March 23, 2021 18:13
@cari-lynn cari-lynn force-pushed the private-lib-schema branch from 5b9eb3a to 2e038a0 Compare March 23, 2021 18:56
@cari-lynn cari-lynn marked this pull request as ready for review March 23, 2021 18:58
@cari-lynn cari-lynn marked this pull request as draft March 23, 2021 22:48
@cari-lynn cari-lynn force-pushed the private-lib-schema branch from 2e038a0 to 3223f2f Compare March 23, 2021 23:11
@cari-lynn cari-lynn marked this pull request as ready for review March 23, 2021 23:25
@@ -43,6 +43,8 @@ func (o Op) mergeArrayItem(
}
if replace {
leftArray.Items[leftIdx].Value = newItem.Value
//todo: should we be setting the position elsewhere?
leftArray.Items[leftIdx].Position = newItem.Position
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we keep position related improvements separate from this PR. it's a bit hard to tell if these are the only places where we need to change this -- would love to see holistic evaluation within overlay module.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... yup and that PR would precede this one — this fix addresses an actual bug: the wrong file is attributed a value.

Copy link
Contributor

@cari-lynn cari-lynn Apr 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These position changes are now necessary to report the position of the overlay in the schema error message, if it's acceptable, we want to keep the change made to the position of arrays and maps here in this PR, and then address this more comprehensively in the Overlay Positions PR.

})

libraryCtx := workspace.LibraryExecutionContext{Current: rootLibrary, Root: rootLibrary}
libraryLoader := libraryExecutionFactory.New(libraryCtx)

schemaDocs, err := libraryLoader.Schemas()
currSchema, err := libraryLoader.Schemas()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it worth renaming libraryLoader -> rootLibraryLoader

@@ -73,10 +73,6 @@ func NewMapType(m *yamlmeta.Map) (*MapType, error) {
}
mapType.Items = append(mapType.Items, mapItemType)
}
annotations := template.NewAnnotations(m)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note for self: removed because it's dead code.

if err != nil {
return nil, nil, fmt.Errorf("Templating file '%s': %s", fileInLib.File.RelativePath(), err)
}
_, checkSchema := o.loader.schema.(*schema.DocumentSchema)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something to think about... why do we need a boolean here? why cant we just tell schema to type check against itself. all schemas should be able to do it succesfully.


for _, valuesDoc := range valuesDocs {
dv, err := NewDataValues(valuesDoc)
allDvs, err := o.collectDataValuesDocs(files)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice little refactor!

return nil, fmt.Errorf("Overlaying additional data values on top of "+
"data values from files (marked as @data/values): %s", err)
}
func (o DataValuesPreProcessing) NewEmptyDataValuesDocument() *yamlmeta.Document {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like this method could be inlined where it's used, or at least made private.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... in part because there's no need for any code outside this module to generate an empty Data Values.

@@ -46,30 +46,36 @@ func NewLibraryLoader(libraryCtx LibraryExecutionContext,
}
}

func (ll *LibraryLoader) Schemas() ([]*yamlmeta.Document, error) {
func (ll *LibraryLoader) Schemas() (Schema, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we are returning a single schema should this method be named Schema() as well?

docs, _, err := DocExtractor{resultDocSet}.Extract(AnnotationSchemaMatch)
if err != nil {
return nil, err
docs, _, err := DocExtractor{resultDocSet}.Extract(AnnotationSchemaMatch)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we get multiple schema docs i would expect that we would blow up for now since we do not allow merging schemas.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, we have a story coming up to allow multiple schema docs #294. Prior to this story we would also blow up if we get multiple docs, so we haven't added guards for that case.

@@ -318,7 +317,8 @@ func (l *libraryValue) libraryValues(ll *LibraryLoader) (*DataValues, []*DataVal
}
}

dvs, foundChildDVss, err := ll.Values(append(dvss, afterLibModDVss...), &schema.AnySchema{})
schema, err := ll.Schemas()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing err check.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😨

@@ -318,7 +317,8 @@ func (l *libraryValue) libraryValues(ll *LibraryLoader) (*DataValues, []*DataVal
}
}

dvs, foundChildDVss, err := ll.Values(append(dvss, afterLibModDVss...), &schema.AnySchema{})
schema, err := ll.Schemas()
dvs, foundChildDVss, err := ll.Values(append(dvss, afterLibModDVss...), schema)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems that we are using ll.Schemas() and then passing it to ll.Values(). i would argue Values() shouldnt require schema as input and should just use ll.Schemas() internally.

@cari-lynn cari-lynn changed the base branch from develop to refactor-dvpp March 30, 2021 18:39
@cari-lynn cari-lynn changed the base branch from refactor-dvpp to develop March 30, 2021 18:40
@cari-lynn cari-lynn force-pushed the private-lib-schema branch from 3223f2f to 58b1ec2 Compare March 30, 2021 18:43
@cari-lynn cari-lynn marked this pull request as draft April 5, 2021 17:40
@cari-lynn cari-lynn force-pushed the private-lib-schema branch 2 times, most recently from 8d599c5 to a548806 Compare April 5, 2021 23:32
@gcheadle-vmware gcheadle-vmware marked this pull request as ready for review April 6, 2021 21:04
cari-lynn and others added 18 commits April 7, 2021 10:32
Signed-off-by: John Ryan <[email protected]># Please enter the commit message for your changes. Lines starting
- ensure that every overlay of data values (regardless of source) is
  checked against schema (if enabled).
- inline ApplyWithDefaults()
- Provide direct error messages by reporting schema errors instead of overlay errors when possible

Co-authored-by: Garrett Cheadle <[email protected]>
- Does not yet type check schema
- Does not respect `--schema-enabled` flag

Co-authored-by: Garrett Cheadle <[email protected]>
* ytt does not place annotations on maps

Signed-off-by: Steven Locke <[email protected]>
- DataValues.HasLib() => DataValues.HasLibRef()
  (without interpretion, conveys that data values has a
   reference to a library)
- Schema.AsDataValues() => Schema.DefaultDataValues()
  (slightly more revealing of intented use)
- in data_values_pre_processing.go, sorted some function
  definitions in order of usage.
  (eases reading when order of usage matches order of
   definition)
Renaming:
- use snake_case for test names to significantly improve readability.
  (such methods are *never* invoked)
- use [slightly] more verbose sentence structure to expose relevant
  details, in a fluid expression, remaining frugal with words.
- reword sub-test names to be phrases extending the containing test
  name as a sentence.

Also:
- delete two (2) redundant test cases
- reorder tests and sub-tests like an outline of a feature spec.
- Test that when schema is disabled, schema is not used

Signed-off-by: John Ryan <[email protected]>
- Rename Schemas() to Schema()
- Rename variables to use qualifier - noun naming scheme
- dvpp has Schema instead of templateLoader
- move logic for schema assinging type and dvs checking themselves into typeAndCheck()
- change swtich statement in dvpp to if statements
- make newEmptyDataValuesDocument() private

Signed-off-by: Cari Dean <[email protected]>
- In favor of a more complete solution, we are removing this typecheck
  to be addressed in a later issue.
- Skipping tests where the overlay fails, but we want a schema error.

Signed-off-by: Garrett Cheadle <[email protected]>
@cppforlife cppforlife merged commit 82645a2 into develop Apr 7, 2021
@gcheadle-vmware gcheadle-vmware mentioned this pull request Apr 21, 2021
20 tasks
@gcheadle-vmware gcheadle-vmware deleted the private-lib-schema branch May 26, 2021 21:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Type check private library data values
7 participants