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

Add spec.min and spec.max query parameter in search endpoint #1058

Merged
merged 16 commits into from
Jul 26, 2023

Conversation

mrodm
Copy link
Contributor

@mrodm mrodm commented Jul 20, 2023

Fixes #1050

This PR adds two new filter parameters spec.min and spec.max for the search endpoint. These values will be used to filter packages according to their format_version value.

If no spec.min nor kibana.version are in the query, the default value for spec.min is going to be 3.0.0.

How to test

  1. Update some test packages to build different packages with different version of format_version
    • Remember to update for each built package the version in manifest and changelog (e.g. 0.0.9) too
    • As an example, elastic_package_registry could be modified as:
     $ git diff
    diff --git packages/elastic_package_registry/manifest.yml packages/elastic_package_registry/manifest.yml
    index b8a41f9b5..0d819ff0e 100644
    --- packages/elastic_package_registry/manifest.yml
    +++ packages/elastic_package_registry/manifest.yml
    @@ -1,4 +1,4 @@
    -format_version: 1.0.0
    +format_version: 2.10.0
     name: elastic_package_registry
     title: "Elastic Package Registry"
     version: 0.0.7
  2. Build the packages
    elastic-package build
  3. Build a new local docker image for package-registry
    cd /path/to/package-registry   
    docker build -t docker.elastic.co/package-registry/package-registry:v1.21.0-rc1 .
  4. Run local package-registry using the packages directory with the built packages as a volume mount point:
    docker run --rm -p 8080:8080 -v /home/mariorodriguez/Coding/work/integrations/build/packages/:/packages/package-registry -it docker.elastic.co/package-registry/package-registry:v1.21.0-rc1
  5. Execute some queries against this local package-registry:
    # filter packages with spec.min and spec.max
    curl -s "http://localhost:8080/search?spec.min=2.2.0&spec.max=3.0.0&prerelease=true" | jq -r '.[]|"\(.name) - \(.version)"'
    # filter packages with spec and kibana.version
    curl -s "http://localhost:8080/search?spec.min=2.2.0&spec.max=3.0.0&kibana.version=8.6.0&prerelease=true" | jq -r '.[]|"\(.name) - \(.version)"'

@mrodm mrodm self-assigned this Jul 20, 2023

// Deprecated, release tags to be removed.
Experimental bool
}

// Apply applies the filter to the list of packages, if the filter is nil, no filtering is done.
func (f *Filter) Apply(ctx context.Context, packages Packages) Packages {
func (f *Filter) Apply(ctx context.Context, packages Packages) (Packages, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Update return parameters, since semver.NewConstraint could fail too with an error.

Comment on lines -415 to -420
if f.Capabilities != nil {
if valid := p.WorksWithCapabilities(f.Capabilities); !valid {
continue
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed from legacy. These clients should not send capabilities in their queries.

}
}

func TestPackagesSpecMinMaxFilter(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Create a new test based on TestPackagesFilter in order to be able to set specific packages.

CHANGELOG.md Outdated Show resolved Hide resolved
Comment on lines 408 to 410
if specMin == nil && kibanaVersion == nil {
specMin = semver.MustParse("3.0.0")
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not totally sure about this, this could be changed depending on what it is decided here #1050 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed this default minimum spec.
So in case there is no spec.min in the default query, it will return all the packages filtered just by spec.max

@mrodm mrodm marked this pull request as ready for review July 20, 2023 18:46
@mrodm mrodm requested a review from a team July 20, 2023 18:46
main_test.go Outdated
Comment on lines 93 to 96
{"/search?capabilities=observability,security&prerelease=true", "/search", "search-prerelease-capabilities-observability-security.json", searchHandler(testLogger, indexer, testCacheTime)},
{"/search?capabilities=none&prerelease=true", "/search", "search-prerelease-capabilities-none.json", searchHandler(testLogger, indexer, testCacheTime)},
{"/search?spec.min=1.1.0&spec.max=2.10.0&prerelease=true", "/search", "search-spec-min-1.1.0-max-2.10.0.json", searchHandler(testLogger, indexer, testCacheTime)},
{"/search?spec.max=2.10.0&prerelease=true", "/search", "search-spec-max-2.10.0.json", searchHandler(testLogger, indexer, testCacheTime)},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added some tests for search endpoint using capabilities and spec parameters.

Some versions of the example and foo test packages have been updated:

  • adding capabilities
  • updating format_version number

main_test.go Outdated
@@ -90,6 +90,10 @@ func TestEndpoints(t *testing.T) {
{"/search?type=input&prerelease=true", "/search", "search-input-packages.json", searchHandler(testLogger, indexer, testCacheTime)},
{"/search?type=input&package=integration_input&prerelease=true", "/search", "search-input-integration-package.json", searchHandler(testLogger, indexer, testCacheTime)},
{"/search?type=integration&package=integration_input&prerelease=true", "/search", "search-integration-integration-package.json", searchHandler(testLogger, indexer, testCacheTime)},
{"/search?capabilities=observability,security&prerelease=true", "/search", "search-prerelease-capabilities-observability-security.json", searchHandler(testLogger, indexer, testCacheTime)},
{"/search?capabilities=none&prerelease=true", "/search", "search-prerelease-capabilities-none.json", searchHandler(testLogger, indexer, testCacheTime)},
{"/search?spec.min=1.1.0&spec.max=2.10.0&prerelease=true", "/search", "search-spec-min-1.1.0-max-2.10.0.json", searchHandler(testLogger, indexer, testCacheTime)},
Copy link
Member

@jsoriano jsoriano Jul 24, 2023

Choose a reason for hiding this comment

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

I see I didn't copy this to the description of the issue, sorry. In the original proposal we thought about using only <major>.<minor>, so if a version of Kibana supports 2.0, it supports any 2.0.X version. Patch versions shouldn't include differences that break compatibility, or new features.

So the API would work like this:

Suggested change
{"/search?spec.min=1.1.0&spec.max=2.10.0&prerelease=true", "/search", "search-spec-min-1.1.0-max-2.10.0.json", searchHandler(testLogger, indexer, testCacheTime)},
{"/search?spec.min=1.1&spec.max=2.10&prerelease=true", "/search", "search-spec-min-1.1.0-max-2.10.0.json", searchHandler(testLogger, indexer, testCacheTime)},

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, it makes sense.

Updated the tests accordingly with that.

I also added a validation to check that versions in the query parameter are just major.minor (added also a test for that).

CHANGELOG.md Outdated Show resolved Hide resolved
Comment on lines +149 to +159
func getSpecVersion(version string) (*semver.Version, error) {
// version must cointain just <major.minor>
if len(strings.Split(version, ".")) != 2 {
return nil, fmt.Errorf("invalid version '%s': it should be <major.version>", version)
}
specVersion, err := semver.NewVersion(version)
if err != nil {
return nil, fmt.Errorf("invalid spec version '%s': %w", version, err)
}
return specVersion, nil
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ensure that versions in query parameters are as expected. That means with the pattern major.minor

@mrodm mrodm requested a review from jsoriano July 25, 2023 07:59
packages/package.go Outdated Show resolved Hide resolved
}

versionSplit := strings.Split(p.FormatVersion, ".")
versionSplit[2] = "0"
Copy link
Member

Choose a reason for hiding this comment

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

Nit. Check length to avoid panics?

Copy link
Member

Choose a reason for hiding this comment

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

Or maybe we can just build this version based on the previously parsed one.

specMajorMinorVersion := fmt.Sprintf("%d.%d.0", p.specSemVer.Major(), p.specSemVer.Minor())

@elasticmachine
Copy link

💚 Build Succeeded

History

cc @mrodm

@mrodm mrodm requested a review from jsoriano July 26, 2023 09:54
@mrodm mrodm merged commit d009793 into elastic:main Jul 26, 2023
1 check passed
@mrodm mrodm deleted the add_spec_min_max_search branch July 26, 2023 16:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support search based on spec version
3 participants