Skip to content

Commit

Permalink
feat: Use fn patch version for cli autocomplete (#2629)
Browse files Browse the repository at this point in the history
This brings the cli function autocompletion in line with other
updates to kpt function catalog/documentation, in which we display
the fully qualified (patch) version for functions.

This is accomplished using the new /catalog-v2.json endpoint
which serves an updated schema that includes the latest patch
version of each function release.
  • Loading branch information
sdowell committed Dec 21, 2021
1 parent e3057af commit ebfc90c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca
golang.org/x/mod v0.5.1
gotest.tools v2.2.0+incompatible
k8s.io/api v0.22.3
k8s.io/apiextensions-apiserver v0.22.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,8 @@ golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38=
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
16 changes: 12 additions & 4 deletions internal/util/cmdutil/cmdutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/GoogleContainerTools/kpt/internal/fnruntime"
"github.com/GoogleContainerTools/kpt/internal/util/httputil"
"github.com/spf13/cobra"
"golang.org/x/mod/semver"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
)
Expand All @@ -39,7 +40,7 @@ const (
Stdout = "stdout"
Unwrap = "unwrap"
dockerVersionTimeout time.Duration = 5 * time.Second
FunctionsCatalogURL = "https://catalog.kpt.dev/catalog.json"
FunctionsCatalogURL = "https://catalog.kpt.dev/catalog-v2.json"
)

// FixDocs replaces instances of old with new in the docs for c
Expand Down Expand Up @@ -196,18 +197,25 @@ func FetchFunctionImages() []string {
return listImages(content)
}

// fnName -> v<major>.<minor> -> catalogEntry
type catalogV2 map[string]map[string]struct{
LatestPatchVersion string
Examples interface{}
}

// listImages returns the list of latest images from the input catalog content
func listImages(content string) []string {
var result []string
jsonData := map[string]map[string]interface{}{}
var jsonData catalogV2
err := json.Unmarshal([]byte(content), &jsonData)
if err != nil {
return result
}
for fnName, fnInfo := range jsonData {
var latestVersion string
for version := range fnInfo {
if latestVersion < version {
for _, catalogEntry := range fnInfo {
version := catalogEntry.LatestPatchVersion
if semver.Compare(version, latestVersion) == 1 {
latestVersion = version
}
}
Expand Down
35 changes: 22 additions & 13 deletions internal/util/cmdutil/cmdutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,30 +316,39 @@ func TestListImages(t *testing.T) {
result := listImages(`{
"apply-setters": {
"v0.1": {
"apply-setters-simple": {
"LocalExamplePath": "/apply-setters/v0.1/apply-setters-simple",
"RemoteExamplePath": "https://github.com/GoogleContainerTools/kpt-functions-catalog/tree/apply-setters/v0.1/examples/apply-setters-simple",
"RemoteSourcePath": "https://github.com/GoogleContainerTools/kpt-functions-catalog/tree/apply-setters/v0.1/functions/go/apply-setters"
"LatestPatchVersion": "v0.1.1",
"Examples": {
"apply-setters-simple": {
"LocalExamplePath": "/apply-setters/v0.1/apply-setters-simple",
"RemoteExamplePath": "https://github.com/GoogleContainerTools/kpt-functions-catalog/tree/apply-setters/v0.1/examples/apply-setters-simple",
"RemoteSourcePath": "https://github.com/GoogleContainerTools/kpt-functions-catalog/tree/apply-setters/v0.1/functions/go/apply-setters"
}
}
}
},
"gatekeeper": {
"v0.1": {
"gatekeeper-warning-only": {
"LocalExamplePath": "/gatekeeper/v0.1/gatekeeper-warning-only",
"RemoteExamplePath": "https://github.com/GoogleContainerTools/kpt-functions-catalog/tree/gatekeeper/v0.1/examples/gatekeeper-warning-only",
"RemoteSourcePath": "https://github.com/GoogleContainerTools/kpt-functions-catalog/tree/gatekeeper/v0.1/functions/go/gatekeeper"
"LatestPatchVersion": "v0.1.3",
"Examples": {
"gatekeeper-warning-only": {
"LocalExamplePath": "/gatekeeper/v0.1/gatekeeper-warning-only",
"RemoteExamplePath": "https://github.com/GoogleContainerTools/kpt-functions-catalog/tree/gatekeeper/v0.1/examples/gatekeeper-warning-only",
"RemoteSourcePath": "https://github.com/GoogleContainerTools/kpt-functions-catalog/tree/gatekeeper/v0.1/functions/go/gatekeeper"
}
}
},
"v0.2": {
"gatekeeper-warning-only": {
"LocalExamplePath": "/gatekeeper/v0.2/gatekeeper-warning-only",
"RemoteExamplePath": "https://github.com/GoogleContainerTools/kpt-functions-catalog/tree/gatekeeper/v0.2/examples/gatekeeper-warning-only",
"RemoteSourcePath": "https://github.com/GoogleContainerTools/kpt-functions-catalog/tree/gatekeeper/v0.2/functions/go/gatekeeper"
"LatestPatchVersion": "v0.2.1",
"Examples": {
"gatekeeper-warning-only": {
"LocalExamplePath": "/gatekeeper/v0.2/gatekeeper-warning-only",
"RemoteExamplePath": "https://github.com/GoogleContainerTools/kpt-functions-catalog/tree/gatekeeper/v0.2/examples/gatekeeper-warning-only",
"RemoteSourcePath": "https://github.com/GoogleContainerTools/kpt-functions-catalog/tree/gatekeeper/v0.2/functions/go/gatekeeper"
}
}
}
}
}`)
sort.Strings(result)
assert.Equal(t, []string{"apply-setters:v0.1", "gatekeeper:v0.2"}, result)
assert.Equal(t, []string{"apply-setters:v0.1.1", "gatekeeper:v0.2.1"}, result)
}

0 comments on commit ebfc90c

Please sign in to comment.