-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5872c1a
commit c84fbf4
Showing
5 changed files
with
166 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package helm | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
|
||
"ocm.software/ocm/api/oci" | ||
"ocm.software/ocm/api/oci/extensions/repositories/artifactset" | ||
"ocm.software/ocm/api/utils/accessio" | ||
"ocm.software/ocm/api/utils/accessobj" | ||
) | ||
|
||
func ConvertArtifactSetHelmChartToPlainTGZChart(reader io.Reader) (_ io.ReadCloser, _ string, err error) { | ||
set, err := artifactset.Open(accessobj.ACC_READONLY, "", 0, accessio.Reader(io.NopCloser(reader))) | ||
if err != nil { | ||
return nil, "", fmt.Errorf("failed to open helm OCI artifact as artifact set: %w", err) | ||
} | ||
defer func() { | ||
err = errors.Join(err, set.Close()) | ||
}() | ||
|
||
art, err := set.GetArtifact(set.GetMain().String()) | ||
if err != nil { | ||
return nil, "", fmt.Errorf("failed to get artifact from set: %w", err) | ||
} | ||
defer func() { | ||
err = errors.Join(err, art.Close()) | ||
}() | ||
|
||
chartTgz, provenance, err := accessSingleLayerOCIHelmChart(art) | ||
if err != nil { | ||
return nil, "", fmt.Errorf("failed to access OCI artifact as a single layer helm OCI image: %w", err) | ||
} | ||
defer func() { | ||
err = errors.Join(err, chartTgz.Close()) | ||
if provenance != nil { | ||
err = errors.Join(err, provenance.Close()) | ||
} | ||
}() | ||
|
||
chartReader, err := chartTgz.Reader() | ||
if err != nil { | ||
return nil, "", fmt.Errorf("failed to get reader for chart tgz: %w", err) | ||
} | ||
|
||
digest := chartTgz.Digest().String() | ||
|
||
return chartReader, digest, nil | ||
} | ||
|
||
func accessSingleLayerOCIHelmChart(art oci.ArtifactAccess) (chart oci.BlobAccess, prov oci.BlobAccess, err error) { | ||
m := art.ManifestAccess() | ||
if m == nil { | ||
return nil, nil, errors.New("artifact is no image manifest") | ||
} | ||
if len(m.GetDescriptor().Layers) < 1 { | ||
return nil, nil, errors.New("no layers found") | ||
} | ||
|
||
if chart, err = m.GetBlob(m.GetDescriptor().Layers[0].Digest); err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
if len(m.GetDescriptor().Layers) > 1 { | ||
if prov, err = m.GetBlob(m.GetDescriptor().Layers[1].Digest); err != nil { | ||
return nil, nil, err | ||
} | ||
} | ||
|
||
return chart, prov, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package helm | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"path" | ||
|
||
"golang.org/x/net/context" | ||
|
||
"ocm.software/ocm/api/credentials" | ||
) | ||
|
||
func ReindexChart(ctx context.Context, client *http.Client, artifactoryURL string, | ||
repository string, | ||
creds credentials.Credentials) (err error) { | ||
reindexURL, err := convertToReindexURL(artifactoryURL, repository) | ||
if err != nil { | ||
return fmt.Errorf("failed to convert to reindex URL: %w", err) | ||
} | ||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reindexURL, nil) | ||
if err != nil { | ||
return fmt.Errorf("failed to create reindex request: %w", err) | ||
} | ||
SetHeadersFromCredentials(req, creds) | ||
req.Header = req.Header.Clone() | ||
|
||
res, err := client.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("failed to reindex chart: %w", err) | ||
} | ||
|
||
if res.StatusCode != http.StatusOK { | ||
responseBytes, err := io.ReadAll(res.Body) | ||
if err != nil { | ||
return fmt.Errorf("failed to read response body but server returned %v: %w", res.StatusCode, err) | ||
} | ||
var body string | ||
if len(responseBytes) > 0 { | ||
body = fmt.Sprintf(": %s", string(responseBytes)) | ||
} | ||
return fmt.Errorf("invalid response (status %v) while reindexing at %q: %s", res.StatusCode, reindexURL, body) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// convertToReindexURL converts the base URL and repository to a reindex URL. | ||
// see https://jfrog.com/help/r/jfrog-rest-apis/calculate-helm-chart-index for the reindex API | ||
func convertToReindexURL(baseURL string, repository string) (string, error) { | ||
u, err := parseURLAllowNoScheme(baseURL) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to parse url: %w", err) | ||
} | ||
u.Path = path.Join(u.Path, "api", "helm", repository, "reindex") | ||
return u.String(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters