Skip to content

Commit

Permalink
fixing the replication and deletion process
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehul-Kumar-27 committed Oct 12, 2024
1 parent 74fc4b9 commit 3d0e209
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 116 deletions.
5 changes: 3 additions & 2 deletions internal/state/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ type ArtifactReader interface {

// Artifact represents an artifact object in the registry
type Artifact struct {
Deleted bool `json:"deleted,omitempty"`
Repository string `json:"repository,omitempty"`
Tags []string `json:"tag,omitempty"`
Digest string `json:"digest,omitempty"`
Labels []string `json:"labels"`
Type string `json:"type,omitempty"`
Digest string `json:"digest,omitempty"`
Deleted bool `json:"deleted"`
Name string `json:"name,omitempty"`
}

Expand Down
49 changes: 15 additions & 34 deletions internal/state/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

type StateFetcher interface {
FetchStateArtifact() (StateReader, error)
FetchStateArtifact(state interface{}) error
}

type baseStateFetcher struct {
Expand Down Expand Up @@ -58,19 +58,19 @@ func NewFileStateFetcher() StateFetcher {
}
}

func (f *FileStateArtifactFetcher) FetchStateArtifact() (StateReader, error) {
func (f *FileStateArtifactFetcher) FetchStateArtifact(state interface{}) error {
content, err := os.ReadFile(f.filePath)
if err != nil {
return nil, fmt.Errorf("failed to read the state artifact file: %v", err)
return fmt.Errorf("failed to read the state artifact file: %v", err)
}
err = json.Unmarshal(content, &f.state_artifact_reader)
err = json.Unmarshal(content, state)
if err != nil {
return nil, fmt.Errorf("failed to parse the state artifact file: %v", err)
return fmt.Errorf("failed to parse the state artifact file: %v", err)
}
return f.state_artifact_reader, nil
return nil
}

func (f *URLStateFetcher) FetchStateArtifact() (StateReader, error) {
func (f *URLStateFetcher) FetchStateArtifact(state interface{}) error {
auth := authn.FromConfig(authn.AuthConfig{
Username: config.GetHarborUsername(),
Password: config.GetHarborPassword(),
Expand All @@ -86,12 +86,12 @@ func (f *URLStateFetcher) FetchStateArtifact() (StateReader, error) {

img, err := crane.Pull(fmt.Sprintf("%s/%s/%s:%s", sourceRegistry, f.group_name, f.state_artifact_name, tag), options...)
if err != nil {
return nil, fmt.Errorf("failed to pull the state artifact: %v", err)
return fmt.Errorf("failed to pull the state artifact: %v", err)
}

tarContent := new(bytes.Buffer)
if err := crane.Export(img, tarContent); err != nil {
return nil, fmt.Errorf("failed to export the state artifact: %v", err)
return fmt.Errorf("failed to export the state artifact: %v", err)
}

tr := tar.NewReader(tarContent)
Expand All @@ -103,44 +103,25 @@ func (f *URLStateFetcher) FetchStateArtifact() (StateReader, error) {
break
}
if err != nil {
return nil, fmt.Errorf("failed to read the tar archive: %v", err)
return fmt.Errorf("failed to read the tar archive: %v", err)
}

if hdr.Name == "artifacts.json" {
artifactsJSON, err = io.ReadAll(tr)
if err != nil {
return nil, fmt.Errorf("failed to read the artifacts.json file: %v", err)
return fmt.Errorf("failed to read the artifacts.json file: %v", err)
}
break
}
}

if artifactsJSON == nil {
return nil, fmt.Errorf("artifacts.json not found in the state artifact")
}
err = json.Unmarshal(artifactsJSON, &f.state_artifact_reader)
if err != nil {
return nil, fmt.Errorf("failed to parse the artifacts.json file: %v", err)
return fmt.Errorf("artifacts.json not found in the state artifact")
}

state, err := ProcessState(&f.state_artifact_reader)
err = json.Unmarshal(artifactsJSON, &state)
if err != nil {
return nil, fmt.Errorf("failed to process the state: %v", err)
}
return *state, nil
}

func ProcessState(state *StateReader) (*StateReader, error) {
for _, artifact := range (*state).GetArtifacts() {
repo, image, err := utils.GetRepositoryAndImageNameFromArtifact(artifact.GetRepository())
if err != nil {
fmt.Printf("Error in getting repository and image name: %v", err)
return nil, err
}
artifact.SetRepository(repo)
artifact.SetName(image)
return fmt.Errorf("failed to parse the artifacts.json file: %v", err)
}
return state, nil
return nil
}

func FromJSON(data []byte, reg StateReader) (StateReader, error) {
Expand Down
4 changes: 2 additions & 2 deletions internal/state/replicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (r *BasicReplicator) Replicate(ctx context.Context, replicationEntities []A
ociImage := mutate.MediaType(srcImage, types.OCIManifestSchema1)

// Push the converted OCI image to the Zot registry
err = crane.Push(ociImage, fmt.Sprintf("%s/%s:%s", r.remoteRegistryURL, replicationEntity.GetName(), replicationEntity.GetTags()[0]), options...)
err = crane.Push(ociImage, fmt.Sprintf("%s/%s/%s:%s", r.remoteRegistryURL, replicationEntity.GetName(), replicationEntity.GetName(), replicationEntity.GetTags()[0]), options...)
if err != nil {
log.Error().Msgf("Failed to push image: %v", err)
return err
Expand All @@ -90,7 +90,7 @@ func (r *BasicReplicator) DeleteReplicationEntity(ctx context.Context, replicati
for _, entity := range replicationEntity {
log.Info().Msgf("Deleting image %s from repository %s at registry %s with tag %s", entity.GetName(), entity.GetRepository(), r.remoteRegistryURL, entity.GetTags()[0])

err := crane.Delete(fmt.Sprintf("%s/%s/%s:%s", r.remoteRegistryURL, entity.GetName() ,entity.GetName(), entity.GetTags()[0]), options...)
err := crane.Delete(fmt.Sprintf("%s/%s/%s:%s", r.remoteRegistryURL, entity.GetName(), entity.GetName(), entity.GetTags()[0]), options...)
if err != nil {
log.Error().Msgf("Failed to delete image: %v", err)
return err
Expand Down
26 changes: 13 additions & 13 deletions internal/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ type StateReader interface {
GetArtifactByRepository(repo string) (ArtifactReader, error)
// Compare the state artifact with the new state artifact
HasStateChanged(newState StateReader) bool
// RemoveAllArtifacts remove all the artifacts from the state which contains null tags and return the new state reader
RemoveArtifactsWithNullTags(stateWithNullTagsArtifacts StateReader) StateReader
// GetArtifactByName takes in the name of the artifact and returns the artifact associated with it
GetArtifactByNameAndTag(name, tag string) ArtifactReader
// SetArtifacts sets the artifacts in the state
SetArtifacts(artifacts []ArtifactReader)
}

type State struct {
Expand Down Expand Up @@ -73,17 +73,6 @@ func (a *State) HasStateChanged(newState StateReader) bool {
return false
}

func (a *State) RemoveArtifactsWithNullTags(stateWithNullTagsArtifacts StateReader) StateReader {
var newArtifactsWithoutNullTags []Artifact
for _, artifact := range a.Artifacts {
if artifact.Tags != nil || len(artifact.Tags) != 0 {
newArtifactsWithoutNullTags = append(newArtifactsWithoutNullTags, artifact)
}
}
stateWithNullTagsArtifacts.(*State).Artifacts = newArtifactsWithoutNullTags
return stateWithNullTagsArtifacts
}

func (a *State) GetArtifactByNameAndTag(name, tag string) ArtifactReader {
for i := range a.Artifacts {
if a.Artifacts[i].GetName() == name {
Expand All @@ -96,3 +85,14 @@ func (a *State) GetArtifactByNameAndTag(name, tag string) ArtifactReader {
}
return nil
}

func (a *State) SetArtifacts(artifacts []ArtifactReader) {
// Clear existing artifacts
a.Artifacts = []Artifact{}

// Set new artifacts
a.Artifacts = make([]Artifact, len(artifacts))
for i, artifact := range artifacts {
a.Artifacts[i] = *artifact.(*Artifact)
}
}
Loading

0 comments on commit 3d0e209

Please sign in to comment.