Skip to content

Commit

Permalink
👷 add oci headers on head req (#697)
Browse files Browse the repository at this point in the history
* add oci headers on head req

* rm

* oci chart version paging

* update method to get
  • Loading branch information
petar-cvit authored Nov 29, 2024
1 parent 4b425a5 commit 416a6b9
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 27 deletions.
118 changes: 91 additions & 27 deletions cyclops-ctrl/internal/template/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ func fetchContentDigest(repo, chart, digest, token string) (string, error) {

var ct struct {
Layers []struct {
Digest string `json:"digest"`
Digest string `json:"digest"`
MediaTyle string `json:"mediaType"`
} `json:"layers"`
}

Expand All @@ -188,6 +189,12 @@ func fetchContentDigest(repo, chart, digest, token string) (string, error) {
return "", nil
}

for _, layer := range ct.Layers {
if layer.MediaTyle == "application/vnd.cncf.helm.chart.content.v1.tar+gzip" {
return layer.Digest, nil
}
}

return ct.Layers[0].Digest, nil
}

Expand Down Expand Up @@ -229,37 +236,62 @@ func getOCIStrictVersion(repo, chart, version string) (string, error) {
return "", err
}

req, err := http.NewRequest(http.MethodGet, tURL.String(), nil)
if err != nil {
return "", err
}
client := &http.Client{}
var allTags []string
for {
req, err := http.NewRequest(http.MethodGet, tURL.String(), nil)
if err != nil {
return "", err
}

req.Header.Set("User-Agent", "Helm/3.13.3")
if len(token) != 0 {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %v", token))
}
req.Header.Set("User-Agent", "Helm/3.13.3")
if len(token) != 0 {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %v", token))
}

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()

responseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
responseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}

var tagsResp struct {
Tags []string `json:"tags"`
}
var tagsResp struct {
Tags []string `json:"tags"`
}

if err := json.Unmarshal(responseBody, &tagsResp); err != nil {
return "", err
if err := json.Unmarshal(responseBody, &tagsResp); err != nil {
return "", err
}

allTags = append(allTags, tagsResp.Tags...)

// continue if response is chunked
linkHeader := resp.Header.Get("Link")
if linkHeader == "" {
break
}

nextURL, err := parseNextLink(linkHeader)
if err != nil {
return "", err
}

if nextURL == "" {
break
}

tURL, err = resolveRelativeURL(tURL, nextURL)
if err != nil {
return "", err
}
}

return resolveSemver(version, tagsResp.Tags)
return resolveSemver(version, allTags)
}

func authorizeOCI(repo, chart, version string) (string, error) {
Expand All @@ -276,6 +308,9 @@ func authorizeOCI(repo, chart, version string) (string, error) {
return "", err
}

req.Header.Set("User-Agent", "Helm/3.13.3")
req.Header.Set("Accept", "application/vnd.docker.distribution.manifest.v2+json, application/vnd.docker.distribution.manifest.list.v2+json, application/vnd.oci.image.manifest.v1+json, application/vnd.oci.image.index.v1+json, */*")

resp, err := client.Do(req)
if err != nil {
return "", err
Expand Down Expand Up @@ -345,7 +380,7 @@ func authorizeOCITags(repo, chart string) (string, error) {

client := &http.Client{}

req, err := http.NewRequest(http.MethodHead, tURL.String(), nil)
req, err := http.NewRequest(http.MethodGet, tURL.String(), nil)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -397,7 +432,7 @@ func authorizeOCITags(repo, chart string) (string, error) {
}

var ar struct {
Token string `json:"access_token"`
Token string `json:"token"`
}

if err := json.Unmarshal(responseBody, &ar); err != nil {
Expand Down Expand Up @@ -485,3 +520,32 @@ func tagsURL(repo, chart string) (*url.URL, error) {
Path: fmt.Sprintf("v2/%v/%v/tags/list", repoURL.Path, chart),
}, nil
}

func parseNextLink(linkHeader string) (string, error) {
links := strings.Split(linkHeader, ",")
for _, link := range links {
parts := strings.SplitN(link, ";", 2)
if len(parts) < 2 {
continue
}

urlPart := strings.Trim(parts[0], " <>")
relPart := strings.Trim(parts[1], " ")
if relPart == `rel="next"` {
return urlPart, nil
}
}
return "", nil
}

func resolveRelativeURL(baseURL *url.URL, nextURL string) (*url.URL, error) {
parsedURL, err := url.Parse(nextURL)
if err != nil {
return nil, err
}

if !parsedURL.IsAbs() {
return baseURL.ResolveReference(parsedURL), nil
}
return parsedURL, nil
}
23 changes: 23 additions & 0 deletions cyclops-ctrl/internal/template/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,15 @@ func (r *Renderer) HelmTemplate(module cyclopsv1alpha1.Module, moduleTemplate *m
return "", err
}

defaultCapabilites := chartutil.DefaultCapabilities
top["Capabilities"] = Capabilities{
KubeVersion: CapabilitiesKubeVersion{
Version: versionInfo.String(),
Minor: versionInfo.Minor,
Major: versionInfo.Major,
GitVersion: versionInfo.GitVersion,
},
HelmVersion: mapCapabilitiesHelmVersion(defaultCapabilites),
}

// TODO fix dependency validation
Expand Down Expand Up @@ -197,14 +199,35 @@ func mapTargetNamespace(namespace string) string {
return namespace
}

func mapCapabilitiesHelmVersion(defaultCapabilities *chartutil.Capabilities) CapabilitiesHelmVersion {
if defaultCapabilities == nil {
return CapabilitiesHelmVersion{}
}

return CapabilitiesHelmVersion{
Version: defaultCapabilities.HelmVersion.Version,
GitCommit: defaultCapabilities.HelmVersion.GitCommit,
GitTreeState: defaultCapabilities.HelmVersion.GitTreeState,
GoVersion: defaultCapabilities.HelmVersion.GoVersion,
}
}

type CapabilitiesKubeVersion struct {
Version string
Minor string
Major string
GitVersion string
}

type CapabilitiesHelmVersion struct {
Version string `json:"version,omitempty"`
GitCommit string `json:"git_commit,omitempty"`
GitTreeState string `json:"git_tree_state,omitempty"`
GoVersion string `json:"go_version,omitempty"`
}

type Capabilities struct {
APIVersions chartutil.VersionSet
KubeVersion CapabilitiesKubeVersion
HelmVersion CapabilitiesHelmVersion
}

0 comments on commit 416a6b9

Please sign in to comment.