Skip to content

Commit

Permalink
Fixed remote and file fetchers
Browse files Browse the repository at this point in the history
http-fetcher :
Now using correct Harbor v2 API
Local image list is based on tag list retrieved via API
file-fetcher :
Created JSON struct with name, digest and repository URL
This data, with optional tag name, can be used to make docker pull commands using only url + digest
  • Loading branch information
OneFlyingBanana committed May 7, 2024
1 parent ca048a0 commit 64c31e8
Show file tree
Hide file tree
Showing 9 changed files with 381 additions and 136 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
HARBOR_USERNAME=admin
HARBOR_PASSWORD=Harbor12345
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQb2IpWsCzug=
github.com/opencontainers/image-spec v1.1.0/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2szdn85PY75RI83NrTrM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
24 changes: 12 additions & 12 deletions image-list/images.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
"results": [
"images": [
{
"name": "alpine:3.1"
"name": "album-server:album2.1.0",
"digest": "sha256:74b3ab50ecaf1765d8066221fbcbbbb2df5f2fe898f7d25c480db4fb6ac2effd",
"repositoryUrl": "https://demo.goharbor.io/v2/myproject"
},
{
"name": "alpine:3.2"
"name": "album-server:latest",
"digest": "sha256:0d420809e7f6edca49a308d0233534991aebce5f54ebb3f97ee87dff8b62b701",
"repositoryUrl": "https://demo.goharbor.io/v2/myproject"
},
{
"name": "alpine:3.3"
},
{
"name": "ubuntu:latest"
},
{
"name": "nginx:1.19"
"name": "album-server:zot-linux-arm64",
"digest": "sha256:671b2e3cdcf32a36335949845fa22883daaeced02082595e71df71ee64eae30f",
"repositoryUrl": "https://demo.goharbor.io/v2/myproject"
}
]
}
]
}
144 changes: 124 additions & 20 deletions internal/store/file-fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@ package store

import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
)

type FileImageList struct {
Path string
}

type ImageData struct {
Images []struct {
Name string `json:"name"`
Digest string `json:"digest"`
RepositoryUrl string `json:"repositoryUrl"`
} `json:"images"`
}

func (f *FileImageList) Type() string {
return "File"
}

func FileImageListFetcher(relativePath string) *FileImageList {
// Get the current working directory
dir, err := os.Getwd()
Expand All @@ -31,50 +42,143 @@ func FileImageListFetcher(relativePath string) *FileImageList {
}

func (client *FileImageList) List(ctx context.Context) ([]Image, error) {

fmt.Println("Reading from:", client.Path)

// Read the file
data, err := os.ReadFile(client.Path)
if err != nil {
return nil, err
}

// Define a struct to match the JSON structure
var imageData struct {
Results []struct {
Name string `json:"name"`
} `json:"results"`
}

var imageData ImageData
// Parse the JSON data
err = json.Unmarshal(data, &imageData)
if err != nil {
return nil, err
}

// Convert the parsed data into a slice of Image structs
images := make([]Image, len(imageData.Results))
for i, result := range imageData.Results {
images := make([]Image, len(imageData.Images))
for i, image := range imageData.Images {
images[i] = Image{
Reference: result.Name,
Reference: image.Name,
Digest: image.Digest,
}
}
fmt.Println("Fetched", len(images), "images :", images)
// Print the pull commands to test if stored data is correct and sufficient
fmt.Println("Pull commands for tests :")
client.GetPullCommands(ctx)

return images, nil
}

func (client *FileImageList) GetHash(ctx context.Context) (string, error) {
func (client *FileImageList) GetDigest(ctx context.Context, tag string) (string, error) {
// Read the file
data, err := os.ReadFile(client.Path)
if err != nil {
return "", err
}

var imageData ImageData
// Parse the JSON data
err = json.Unmarshal(data, &imageData)
if err != nil {
return "", err
}

if tag == "" {
return "", fmt.Errorf("tag cannot be empty")
}

// Iterate over the images to find the one with the matching tag
for _, image := range imageData.Images {
if strings.Contains(image.Name, tag) {
return image.Digest, nil
}
}
// If no image with the matching tag is found, return an error
return "", fmt.Errorf("image with tag %s not found", tag)
}

func (client *FileImageList) GetTag(ctx context.Context, digest string) (string, error) {
// Read the file
data, err := os.ReadFile(client.Path)
if err != nil {
return "", err
}

// Hash and return the body
hash := sha256.Sum256(data)
hashString := hex.EncodeToString(hash[:])
var imageData ImageData
// Parse the JSON data
err = json.Unmarshal(data, &imageData)
if err != nil {
return "", err
}

if digest == "" {
return "", fmt.Errorf("digest cannot be empty")
}

return hashString, nil
// Iterate over the images to find the one with the matching digest
for _, image := range imageData.Images {
if image.Digest == digest {
return image.Name, nil
}
}
// If no image with the matching digest is found, return an error
return "", fmt.Errorf("image with digest %s not found", digest)

}

func (client *FileImageList) GetPullCommands(ctx context.Context) {
// Read the file
data, err := os.ReadFile(client.Path)
if err != nil {
fmt.Println("Error reading file:", err)
return
}

var imageData ImageData
// Parse the JSON data
err = json.Unmarshal(data, &imageData)
if err != nil {
fmt.Println("Error parsing JSON:", err)
return
}

// Iterate over the images to construct and print the pull command for each
for _, image := range imageData.Images {
harborUrl := strings.TrimPrefix(image.RepositoryUrl, "https://")
harborUrl = strings.Replace(harborUrl, ".io/v2", ".io", -1)

// Extract the first part of the image name
parts := strings.Split(image.Name, ":")
if len(parts) > 0 {
// Append the first part of the split result to harborUrl
harborUrl += "/" + parts[0]
}

pullCommand := fmt.Sprintf("docker pull %s@%s", harborUrl, image.Digest)
fmt.Println(pullCommand)
}
}

func (client *FileImageList) ListDigests(ctx context.Context) ([]string, error) {
// Read the file
data, err := os.ReadFile(client.Path)
if err != nil {
return nil, err
}

var imageData ImageData
// Parse the JSON data
err = json.Unmarshal(data, &imageData)
if err != nil {
return nil, err
}

// Prepare a slice to store the digests
digests := make([]string, len(imageData.Images))
for i, image := range imageData.Images {
digests[i] = image.Digest
}
return digests, nil
}
Loading

0 comments on commit 64c31e8

Please sign in to comment.