@@ -59,19 +59,18 @@ func stripPrefix(imageName string) string {
59
59
60
60
func (r * BasicReplicator ) DeleteExtraImages (ctx context.Context , imgs []store.Image ) error {
61
61
log := logger .FromContext (ctx )
62
- errLog := logger .ErrorLoggerFromContext (ctx )
63
62
zotUrl := os .Getenv ("ZOT_URL" )
64
- host := os .Getenv ("HOST" )
65
63
registry := os .Getenv ("REGISTRY" )
66
64
repository := os .Getenv ("REPOSITORY" )
65
+ image := os .Getenv ("IMAGE" )
67
66
68
- localRegistry := fmt .Sprintf ("%s/%s/%s/%s" , zotUrl , host , registry , repository )
67
+ localRegistry := fmt .Sprintf ("%s/%s/%s/%s" , zotUrl , registry , repository , image )
69
68
log .Info ().Msgf ("Local registry: %s" , localRegistry )
70
69
71
70
// Get the list of images from the local registry
72
71
localImages , err := crane .ListTags (localRegistry )
73
72
if err != nil {
74
- errLog .Error ().Msgf ("failed to list tags: %v" , err )
73
+ log .Error ().Msgf ("failed to list tags: %v" , err )
75
74
return err
76
75
}
77
76
@@ -90,7 +89,7 @@ func (r *BasicReplicator) DeleteExtraImages(ctx context.Context, imgs []store.Im
90
89
log .Info ().Msgf ("Deleting image: %s" , localImage )
91
90
err := crane .Delete (fmt .Sprintf ("%s:%s" , localRegistry , localImage ))
92
91
if err != nil {
93
- errLog .Error ().Msgf ("failed to delete image: %v" , err )
92
+ log .Error ().Msgf ("failed to delete image: %v" , err )
94
93
return err
95
94
}
96
95
log .Info ().Msgf ("Image deleted: %s" , localImage )
@@ -101,35 +100,35 @@ func (r *BasicReplicator) DeleteExtraImages(ctx context.Context, imgs []store.Im
101
100
}
102
101
103
102
func getPullSource (ctx context.Context , image string ) string {
104
- errLog := logger .ErrorLoggerFromContext (ctx )
103
+ log := logger .FromContext (ctx )
105
104
input := os .Getenv ("USER_INPUT" )
106
105
scheme := os .Getenv ("SCHEME" )
107
106
if strings .HasPrefix (scheme , "http://" ) || strings .HasPrefix (scheme , "https://" ) {
108
- url := os .Getenv ("HOST " ) + "/" + os .Getenv ("REGISTRY " ) + "/" + image
107
+ url := os .Getenv ("REGISTRY " ) + "/" + os .Getenv ("REPOSITORY " ) + "/" + image
109
108
return url
110
109
} else {
111
110
registryInfo , err := getFileInfo (ctx , input )
112
111
if err != nil {
113
- errLog .Error ().Msgf ("Error getting file info: %v" , err )
112
+ log .Error ().Msgf ("Error getting file info: %v" , err )
114
113
return ""
115
114
}
116
115
registryURL := registryInfo .RegistryUrl
117
116
registryURL = strings .TrimPrefix (registryURL , "https://" )
118
- registryURL = strings .TrimSuffix (registryURL , "v2/" )
117
+ registryURL = strings .TrimSuffix (registryURL , "/ v2/" )
119
118
120
119
// TODO: Handle multiple repositories
121
120
repositoryName := registryInfo .Repositories [0 ].Repository
122
121
123
- return registryURL + repositoryName + "/" + image
122
+ return registryURL + "/" + repositoryName + "/" + image
124
123
}
125
124
}
126
125
127
126
func getFileInfo (ctx context.Context , input string ) (* RegistryInfo , error ) {
128
- errLog := logger .ErrorLoggerFromContext (ctx )
127
+ log := logger .FromContext (ctx )
129
128
// Get the current working directory
130
129
workingDir , err := os .Getwd ()
131
130
if err != nil {
132
- errLog .Error ().Msgf ("Error getting current directory: %v" , err )
131
+ log .Error ().Msgf ("Error getting current directory: %v" , err )
133
132
return nil , err
134
133
}
135
134
@@ -139,14 +138,14 @@ func getFileInfo(ctx context.Context, input string) (*RegistryInfo, error) {
139
138
// Read the file
140
139
jsonData , err := os .ReadFile (fullPath )
141
140
if err != nil {
142
- errLog .Error ().Msgf ("Error reading file: %v" , err )
141
+ log .Error ().Msgf ("Error reading file: %v" , err )
143
142
return nil , err
144
143
}
145
144
146
145
var registryInfo RegistryInfo
147
146
err = json .Unmarshal (jsonData , & registryInfo )
148
147
if err != nil {
149
- errLog .Error ().Msgf ("Error unmarshalling JSON data: %v" , err )
148
+ log .Error ().Msgf ("Error unmarshalling JSON data: %v" , err )
150
149
return nil , err
151
150
}
152
151
@@ -155,24 +154,22 @@ func getFileInfo(ctx context.Context, input string) (*RegistryInfo, error) {
155
154
156
155
func CopyImage (ctx context.Context , imageName string ) error {
157
156
log := logger .FromContext (ctx )
158
- errLog := logger .ErrorLoggerFromContext (ctx )
159
157
log .Info ().Msgf ("Copying image: %s" , imageName )
160
158
zotUrl := os .Getenv ("ZOT_URL" )
161
159
if zotUrl == "" {
162
- errLog .Error ().Msg ("ZOT_URL environment variable is not set" )
160
+ log .Error ().Msg ("ZOT_URL environment variable is not set" )
163
161
return fmt .Errorf ("ZOT_URL environment variable is not set" )
164
162
}
165
163
166
- // Clean up the image name by removing any host part
167
- cleanedImageName := removeHostName (imageName )
168
- destRef := fmt .Sprintf ("%s/%s" , zotUrl , cleanedImageName )
169
- fmt .Println ("Destination reference:" , destRef )
164
+ // Build the destination reference
165
+ destRef := fmt .Sprintf ("%s/%s" , zotUrl , imageName )
166
+ log .Info ().Msgf ("Destination reference: %s" , destRef )
170
167
171
168
// Get credentials from environment variables
172
169
username := os .Getenv ("HARBOR_USERNAME" )
173
170
password := os .Getenv ("HARBOR_PASSWORD" )
174
171
if username == "" || password == "" {
175
- errLog .Error ().Msg ("HARBOR_USERNAME or HARBOR_PASSWORD environment variable is not set" )
172
+ log .Error ().Msg ("HARBOR_USERNAME or HARBOR_PASSWORD environment variable is not set" )
176
173
return fmt .Errorf ("HARBOR_USERNAME or HARBOR_PASSWORD environment variable is not set" )
177
174
}
178
175
@@ -184,7 +181,7 @@ func CopyImage(ctx context.Context, imageName string) error {
184
181
// Pull the image with authentication
185
182
srcImage , err := crane .Pull (imageName , crane .WithAuth (auth ), crane .Insecure )
186
183
if err != nil {
187
- errLog .Error ().Msgf ("Failed to pull image: %v" , err )
184
+ log .Error ().Msgf ("Failed to pull image: %v" , err )
188
185
return fmt .Errorf ("failed to pull image: %w" , err )
189
186
} else {
190
187
log .Info ().Msg ("Image pulled successfully" )
@@ -193,7 +190,7 @@ func CopyImage(ctx context.Context, imageName string) error {
193
190
// Push the image to the destination registry
194
191
err = crane .Push (srcImage , destRef , crane .Insecure )
195
192
if err != nil {
196
- errLog .Error ().Msgf ("Failed to push image: %v" , err )
193
+ log .Error ().Msgf ("Failed to push image: %v" , err )
197
194
return fmt .Errorf ("failed to push image: %w" , err )
198
195
} else {
199
196
log .Info ().Msg ("Image pushed successfully" )
@@ -203,19 +200,9 @@ func CopyImage(ctx context.Context, imageName string) error {
203
200
// This is required because it is a temporary directory used by crane to pull and push images to and from
204
201
// And crane does not automatically clean it
205
202
if err := os .RemoveAll ("./local-oci-layout" ); err != nil {
206
- errLog .Error ().Msgf ("Failed to remove directory: %v" , err )
203
+ log .Error ().Msgf ("Failed to remove directory: %v" , err )
207
204
return fmt .Errorf ("failed to remove directory: %w" , err )
208
205
}
209
206
210
207
return nil
211
208
}
212
-
213
- // take only the parts after the hostname
214
- func removeHostName (imageName string ) string {
215
- parts := strings .Split (imageName , "/" )
216
- if len (parts ) > 1 {
217
- return strings .Join (parts [1 :], "/" )
218
- }
219
-
220
- return imageName
221
- }
0 commit comments