Skip to content

Commit

Permalink
Removed stdout logger + refactoring
Browse files Browse the repository at this point in the history
Removed stdout logging
Satellite now exclusively logs towards stderr with a single logger that is passed and retrieved via context
Fixed some environment variables names not being set properly and restored configurations to default states
  • Loading branch information
OneFlyingBanana committed Jul 9, 2024
1 parent c93f12c commit a68021a
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 124 deletions.
8 changes: 2 additions & 6 deletions config.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# Wether to us the built-in Zot registry or not
bring_own_registry = true
bring_own_registry = false

# IP address and port of own registry
own_registry_adr = "127.0.0.1"
own_registry_port = "8585"
# URL of own registry
own_registry_adr = "127.0.0.1:5000"

# URL of remote registry OR local file path
url_or_file = "https://demo.goharbor.io/v2/myproject/album-server"
Expand All @@ -14,9 +12,7 @@ url_or_file = "https://demo.goharbor.io/v2/myproject/album-server"
zotConfigPath = "./registry/config.json"

# Set logging level
log_level = "warn"
# url_or_file = "https://demo.goharbor.io/v2/myproject/album-server"
url_or_file = "http://localhost:5001/v2/library/busybox"
log_level = "info"

# For testing purposes :
# https://demo.goharbor.io/v2/myproject/album-server
Expand Down
7 changes: 2 additions & 5 deletions image-list/images.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@
"repository": "myproject",
"images": [
{
"name": "album-server@sha256:71df27326a806ef2946ce502d26212efa11d70e4dcea06ceae612eb29cba398b"
"name": "album-server@sha256:39879890008f12c25ea14125aa8e9ec8ef3e167f0b0ed88057e955a8fa32c430"
},
{
"name": "album-server"
},
{
"name": "album-server:v1-manifest-app"
"name": "album-server:busybox"
}
]
}
Expand Down
55 changes: 21 additions & 34 deletions internal/replicate/replicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,18 @@ func stripPrefix(imageName string) string {

func (r *BasicReplicator) DeleteExtraImages(ctx context.Context, imgs []store.Image) error {
log := logger.FromContext(ctx)
errLog := logger.ErrorLoggerFromContext(ctx)
zotUrl := os.Getenv("ZOT_URL")
host := os.Getenv("HOST")
registry := os.Getenv("REGISTRY")
repository := os.Getenv("REPOSITORY")
image := os.Getenv("IMAGE")

localRegistry := fmt.Sprintf("%s/%s/%s/%s", zotUrl, host, registry, repository)
localRegistry := fmt.Sprintf("%s/%s/%s/%s", zotUrl, registry, repository, image)
log.Info().Msgf("Local registry: %s", localRegistry)

// Get the list of images from the local registry
localImages, err := crane.ListTags(localRegistry)
if err != nil {
errLog.Error().Msgf("failed to list tags: %v", err)
log.Error().Msgf("failed to list tags: %v", err)
return err
}

Expand All @@ -90,7 +89,7 @@ func (r *BasicReplicator) DeleteExtraImages(ctx context.Context, imgs []store.Im
log.Info().Msgf("Deleting image: %s", localImage)
err := crane.Delete(fmt.Sprintf("%s:%s", localRegistry, localImage))
if err != nil {
errLog.Error().Msgf("failed to delete image: %v", err)
log.Error().Msgf("failed to delete image: %v", err)
return err
}
log.Info().Msgf("Image deleted: %s", localImage)
Expand All @@ -101,35 +100,35 @@ func (r *BasicReplicator) DeleteExtraImages(ctx context.Context, imgs []store.Im
}

func getPullSource(ctx context.Context, image string) string {
errLog := logger.ErrorLoggerFromContext(ctx)
log := logger.FromContext(ctx)
input := os.Getenv("USER_INPUT")
scheme := os.Getenv("SCHEME")
if strings.HasPrefix(scheme, "http://") || strings.HasPrefix(scheme, "https://") {
url := os.Getenv("HOST") + "/" + os.Getenv("REGISTRY") + "/" + image
url := os.Getenv("REGISTRY") + "/" + os.Getenv("REPOSITORY") + "/" + image
return url
} else {
registryInfo, err := getFileInfo(ctx, input)
if err != nil {
errLog.Error().Msgf("Error getting file info: %v", err)
log.Error().Msgf("Error getting file info: %v", err)
return ""
}
registryURL := registryInfo.RegistryUrl
registryURL = strings.TrimPrefix(registryURL, "https://")
registryURL = strings.TrimSuffix(registryURL, "v2/")
registryURL = strings.TrimSuffix(registryURL, "/v2/")

// TODO: Handle multiple repositories
repositoryName := registryInfo.Repositories[0].Repository

return registryURL + repositoryName + "/" + image
return registryURL + "/" + repositoryName + "/" + image
}
}

func getFileInfo(ctx context.Context, input string) (*RegistryInfo, error) {
errLog := logger.ErrorLoggerFromContext(ctx)
log := logger.FromContext(ctx)
// Get the current working directory
workingDir, err := os.Getwd()
if err != nil {
errLog.Error().Msgf("Error getting current directory: %v", err)
log.Error().Msgf("Error getting current directory: %v", err)
return nil, err
}

Expand All @@ -139,14 +138,14 @@ func getFileInfo(ctx context.Context, input string) (*RegistryInfo, error) {
// Read the file
jsonData, err := os.ReadFile(fullPath)
if err != nil {
errLog.Error().Msgf("Error reading file: %v", err)
log.Error().Msgf("Error reading file: %v", err)
return nil, err
}

var registryInfo RegistryInfo
err = json.Unmarshal(jsonData, &registryInfo)
if err != nil {
errLog.Error().Msgf("Error unmarshalling JSON data: %v", err)
log.Error().Msgf("Error unmarshalling JSON data: %v", err)
return nil, err
}

Expand All @@ -155,24 +154,22 @@ func getFileInfo(ctx context.Context, input string) (*RegistryInfo, error) {

func CopyImage(ctx context.Context, imageName string) error {
log := logger.FromContext(ctx)
errLog := logger.ErrorLoggerFromContext(ctx)
log.Info().Msgf("Copying image: %s", imageName)
zotUrl := os.Getenv("ZOT_URL")
if zotUrl == "" {
errLog.Error().Msg("ZOT_URL environment variable is not set")
log.Error().Msg("ZOT_URL environment variable is not set")
return fmt.Errorf("ZOT_URL environment variable is not set")
}

// Clean up the image name by removing any host part
cleanedImageName := removeHostName(imageName)
destRef := fmt.Sprintf("%s/%s", zotUrl, cleanedImageName)
fmt.Println("Destination reference:", destRef)
// Build the destination reference
destRef := fmt.Sprintf("%s/%s", zotUrl, imageName)
log.Info().Msgf("Destination reference: %s", destRef)

// Get credentials from environment variables
username := os.Getenv("HARBOR_USERNAME")
password := os.Getenv("HARBOR_PASSWORD")
if username == "" || password == "" {
errLog.Error().Msg("HARBOR_USERNAME or HARBOR_PASSWORD environment variable is not set")
log.Error().Msg("HARBOR_USERNAME or HARBOR_PASSWORD environment variable is not set")
return fmt.Errorf("HARBOR_USERNAME or HARBOR_PASSWORD environment variable is not set")
}

Expand All @@ -184,7 +181,7 @@ func CopyImage(ctx context.Context, imageName string) error {
// Pull the image with authentication
srcImage, err := crane.Pull(imageName, crane.WithAuth(auth), crane.Insecure)
if err != nil {
errLog.Error().Msgf("Failed to pull image: %v", err)
log.Error().Msgf("Failed to pull image: %v", err)
return fmt.Errorf("failed to pull image: %w", err)
} else {
log.Info().Msg("Image pulled successfully")
Expand All @@ -193,7 +190,7 @@ func CopyImage(ctx context.Context, imageName string) error {
// Push the image to the destination registry
err = crane.Push(srcImage, destRef, crane.Insecure)
if err != nil {
errLog.Error().Msgf("Failed to push image: %v", err)
log.Error().Msgf("Failed to push image: %v", err)
return fmt.Errorf("failed to push image: %w", err)
} else {
log.Info().Msg("Image pushed successfully")
Expand All @@ -203,19 +200,9 @@ func CopyImage(ctx context.Context, imageName string) error {
// This is required because it is a temporary directory used by crane to pull and push images to and from
// And crane does not automatically clean it
if err := os.RemoveAll("./local-oci-layout"); err != nil {
errLog.Error().Msgf("Failed to remove directory: %v", err)
log.Error().Msgf("Failed to remove directory: %v", err)
return fmt.Errorf("failed to remove directory: %w", err)
}

return nil
}

// take only the parts after the hostname
func removeHostName(imageName string) string {
parts := strings.Split(imageName, "/")
if len(parts) > 1 {
return strings.Join(parts[1:], "/")
}

return imageName
}
9 changes: 4 additions & 5 deletions internal/satellite/satellite.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ func NewSatellite(ctx context.Context, storer store.Storer, replicator replicate

func (s *Satellite) Run(ctx context.Context) error {
log := logger.FromContext(ctx)
errLog := logger.ErrorLoggerFromContext(ctx)

// Execute the initial operation immediately without waiting for the ticker
imgs, err := s.storer.List(ctx)
if err != nil {
errLog.Error().Err(err).Msg("Error listing images")
log.Error().Err(err).Msg("Error listing images")
return err
}
if len(imgs) == 0 {
Expand All @@ -37,7 +36,7 @@ func (s *Satellite) Run(ctx context.Context) error {
for _, img := range imgs {
err = s.replicator.Replicate(ctx, img.Name)
if err != nil {
errLog.Error().Err(err).Msg("Error replicating image")
log.Error().Err(err).Msg("Error replicating image")
return err
}
}
Expand All @@ -56,7 +55,7 @@ func (s *Satellite) Run(ctx context.Context) error {
case <-ticker.C:
imgs, err := s.storer.List(ctx)
if err != nil {
errLog.Error().Err(err).Msg("Error listing images")
log.Error().Err(err).Msg("Error listing images")
return err
}
if len(imgs) == 0 {
Expand All @@ -65,7 +64,7 @@ func (s *Satellite) Run(ctx context.Context) error {
for _, img := range imgs {
err = s.replicator.Replicate(ctx, img.Name)
if err != nil {
errLog.Error().Err(err).Msg("Error replicating image")
log.Error().Err(err).Msg("Error replicating image")
return err
}
}
Expand Down
10 changes: 5 additions & 5 deletions internal/store/file-fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ func (f *FileImageList) Type(ctx context.Context) string {
}

func FileImageListFetcher(ctx context.Context, relativePath string) *FileImageList {
errLog := logger.ErrorLoggerFromContext(ctx)
log := logger.FromContext(ctx)
// Get the current working directory
dir, err := os.Getwd()
if err != nil {
errLog.Error().Err(err).Msg("Error getting current directory")
log.Error().Err(err).Msg("Error getting current directory")
return nil
}

Expand All @@ -47,21 +47,21 @@ func FileImageListFetcher(ctx context.Context, relativePath string) *FileImageLi
}

func (client *FileImageList) List(ctx context.Context) ([]Image, error) {
errLog := logger.ErrorLoggerFromContext(ctx)
log := logger.FromContext(ctx)
var images []Image

// Read the file
data, err := os.ReadFile(client.Path)
if err != nil {
errLog.Error().Err(err).Msg("Error reading file")
log.Error().Err(err).Msg("Error reading file")
return nil, err
}

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

Expand Down
13 changes: 6 additions & 7 deletions internal/store/http-fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func (r *RemoteImageList) Type(ctx context.Context) string {

func (client *RemoteImageList) List(ctx context.Context) ([]Image, error) {
log := logger.FromContext(ctx)
errLog := logger.ErrorLoggerFromContext(ctx)
// Construct the URL for fetching tags
url := client.BaseURL + "/tags/list"

Expand All @@ -49,7 +48,7 @@ func (client *RemoteImageList) List(ctx context.Context) ([]Image, error) {
// Create a new HTTP request
req, err := http.NewRequest("GET", url, nil)
if err != nil {
errLog.Error().Msgf("failed to create request: %v", err)
log.Error().Msgf("failed to create request: %v", err)
return nil, err
}

Expand All @@ -65,22 +64,22 @@ func (client *RemoteImageList) List(ctx context.Context) ([]Image, error) {
log.Info().Msgf("Sending request to %s", url)
resp, err := httpClient.Do(req)
if err != nil {
errLog.Error().Msgf("failed to send request: %v", err)
log.Error().Msgf("failed to send request: %v", err)
return nil, err
}
defer resp.Body.Close()

// Read the response body
body, err := io.ReadAll(resp.Body)
if err != nil {
errLog.Error().Msgf("failed to read response body: %v", err)
log.Error().Msgf("failed to read response body: %v", err)
return nil, err
}

// Unmarshal the JSON response
var tagListResponse TagListResponse
if err := json.Unmarshal(body, &tagListResponse); err != nil {
errLog.Error().Msgf("failed to unmarshal response: %v", err)
log.Error().Msgf("failed to unmarshal response: %v", err)
return nil, err
}

Expand All @@ -97,7 +96,7 @@ func (client *RemoteImageList) List(ctx context.Context) ([]Image, error) {
}

func (client *RemoteImageList) GetDigest(ctx context.Context, tag string) (string, error) {
errLog := logger.ErrorLoggerFromContext(ctx)
log := logger.FromContext(ctx)
// Construct the image reference
imageRef := fmt.Sprintf("%s:%s", client.BaseURL, tag)
// Remove extra characters from the URL
Expand All @@ -114,7 +113,7 @@ func (client *RemoteImageList) GetDigest(ctx context.Context, tag string) (strin
Password: password,
}), crane.Insecure)
if err != nil {
errLog.Error().Msgf("failed to get digest using crane: %v", err)
log.Error().Msgf("failed to get digest using crane: %v", err)
return "", nil
}

Expand Down
Loading

0 comments on commit a68021a

Please sign in to comment.