Skip to content

Commit

Permalink
Set public Tekton Hub API as default catalog
Browse files Browse the repository at this point in the history
Public tekton hub API was not set by default in the hub resolver, i
understand that ArtifactHUB is the future but until then
makes it more easier to use the tekton hub

Signed-off-by: Chmouel Boudjnah <chmouel@redhat.com>
  • Loading branch information
chmouel committed Oct 23, 2023
1 parent 0111021 commit f71aa8b
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 3 deletions.
2 changes: 2 additions & 0 deletions config/resolvers/resolvers-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ spec:
# Override this env var to set a private hub api endpoint
- name: ARTIFACT_HUB_API
value: "https://artifacthub.io/"
- name: TEKTON_HUB_API
value: "https://api.hub.tekton.dev/"
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
Expand Down
5 changes: 4 additions & 1 deletion docs/hub-resolver.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ env
value: "https://artifacthub.io/"
```
When setting the `type` field to `tekton`, you **must** configure your own instance of the Tekton Hub by setting the `TEKTON_HUB_API` environment variable in
When setting the `type` field to `tekton`, the resolver will hit the public
tekton catalog api at https://api.hub.tekton.dev by default but you can configure
your own instance of the Tekton Hub by setting the `TEKTON_HUB_API` environment
variable in
[`../config/resolvers/resolvers-deployment.yaml`](../config/resolvers/resolvers-deployment.yaml). Example:

```yaml
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ require (
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/go-version v1.6.0
github.com/imdario/mergo v0.3.13 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions pkg/resolution/resolver/hub/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ package hub
// DefaultArtifactHubURL is the default url for the Artifact hub api
const DefaultArtifactHubURL = "https://artifacthub.io/api/v1/packages/tekton-%s/%s/%s/%s"

// DefaultArtifactHubListVersionHub is the default url for listing tasks version with Artifact hub api
const DefaultArtifactHubListVersionHub = "https://artifacthub.io/api/v1/packages/tekton-%s/%s/%s"

// TektonHubYamlEndpoint is the suffix for a private custom Tekton hub instance
const TektonHubYamlEndpoint = "v1/resource/%s/%s/%s/%s/yaml"

// TektonHubListVersionEndpoint is the endpoint url for listing tasks version with Tekton hub api
const TektonHubListVersionEndpoint = "v1/resource/%s/%s/%s"

// ArtifactHubYamlEndpoint is the suffix for a private custom Artifact hub instance
const ArtifactHubYamlEndpoint = "api/v1/packages/tekton-%s/%s/%s/%s"

Expand Down
80 changes: 79 additions & 1 deletion pkg/resolution/resolver/hub/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ import (
"fmt"
"io"
"net/http"
"os"
"strings"

goversion "github.com/hashicorp/go-version"
resolverconfig "github.com/tektoncd/pipeline/pkg/apis/config/resolver"
pipelinev1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"github.com/tektoncd/pipeline/pkg/resolution/common"
Expand Down Expand Up @@ -121,6 +123,14 @@ func (r *Resolver) Resolve(ctx context.Context, params []pipelinev1.Param) (fram
return nil, fmt.Errorf("failed to validate params: %w", err)
}

if constraint, err := goversion.NewConstraint(paramsMap[ParamVersion]); err == nil {
chosen, err := resolveVersionConstraint(ctx, paramsMap, constraint)
if err != nil {
return nil, err
}
paramsMap[ParamVersion] = chosen.String()
}

resVer, err := resolveVersion(paramsMap[ParamVersion], paramsMap[ParamType])
if err != nil {
return nil, err
Expand Down Expand Up @@ -218,7 +228,6 @@ func fetchHubResource(ctx context.Context, apiEndpoint string, v interface{}) er
if err != nil {
return fmt.Errorf("error unmarshalling json response: %w", err)
}

return nil
}

Expand Down Expand Up @@ -256,6 +265,75 @@ func resolveCatalogName(paramsMap, conf map[string]string) (string, error) {
return paramsMap[ParamCatalog], nil
}

type artifactHubListResult struct {
AvailableVersions []struct {
Version string `json:"version"`
Prerelease bool `json:"prerelease"`
} `json:"available_versions"`
Version string `json:"version"`
}

type tektonHubListResult struct {
Data struct {
Versions []struct {
Version string `json:"version"`
} `json:"versions"`
} `json:"data"`
}

func resolveVersionConstraint(ctx context.Context, paramsMap map[string]string, constraint goversion.Constraints) (*goversion.Version, error) {
var ret *goversion.Version
if paramsMap[ParamType] == ArtifactHubType {
allVersionsURL := fmt.Sprintf(DefaultArtifactHubListVersionHub,
paramsMap[ParamKind], paramsMap[ParamCatalog], paramsMap[ParamName])
resp := artifactHubListResult{}
if err := fetchHubResource(ctx, allVersionsURL, &resp); err != nil {
return nil, fmt.Errorf("fail to fetch Artifact Hub resource: %w", err)
}
for _, vers := range resp.AvailableVersions {
if vers.Prerelease {
continue
}
checkV, err := goversion.NewVersion(vers.Version)
if err != nil {
return nil, fmt.Errorf("fail to parse version %s from %s: %w", ArtifactHubType, vers.Version, err)
}
if constraint.Check(checkV) {
if ret != nil && ret.GreaterThan(checkV) {
continue
}
ret = checkV
break
}
}
} else if paramsMap[ParamType] == TektonHubType {
//TODO: refactor url building
allVersionsURL := fmt.Sprintf("%s/%s", os.Getenv("TEKTON_HUB_API"),
fmt.Sprintf(TektonHubListVersionEndpoint, paramsMap[ParamCatalog], paramsMap[ParamKind], paramsMap[ParamName]))
resp := tektonHubListResult{}
if err := fetchHubResource(ctx, allVersionsURL, &resp); err != nil {
return nil, fmt.Errorf("fail to fetch Tekton Hub resource: %w", err)
}
for _, vers := range resp.Data.Versions {
checkV, err := goversion.NewVersion(vers.Version)
if err != nil {
return nil, fmt.Errorf("fail to parse version %s from %s: %w", TektonHubType, vers, err)
}
if constraint.Check(checkV) {
if ret != nil && ret.GreaterThan(checkV) {
continue
}
ret = checkV
break
}
}
}
if ret == nil {
return nil, fmt.Errorf("no version found for constraint %s", paramsMap[ParamVersion])
}
return ret, nil
}

// the Artifact Hub follows the semVer (i.e. <major-version>.<minor-version>.0)
// the Tekton Hub follows the simplified semVer (i.e. <major-version>.<minor-version>)
// for resolution request with "artifact" type, we append ".0" suffix if the input version is simplified semVer
Expand Down

0 comments on commit f71aa8b

Please sign in to comment.