You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|`--registry <registry_name>`| Set the name of the Azure Container Registry. |
20
-
|`--repository <repository_name>`| Set the name of the repository in your Azure Container Registry. |
21
-
|`--subscription <subscription_id>`| Set the ID of the Azure subscription. If not specified, the default one will be used. |
22
-
|`--timestamp <cutoff_timestamp>`| Set the cutoff timestamp. All images before this timestamp will be deleted. Default: 01/01/2024. |
23
-
|`--delay <delay_in_seconds>`| Set the delay (in seconds) between deletion requests. Default: 1 second. |
24
-
|`--contexts <context_list>`| Comma-separated list of Kubernetes contexts. The deletion process will not start if any 'imageToDelete' is running in a cluster from the context list. |
25
-
|`--dry-run`| Perform a dry run, printing the tags to be deleted but do not delete them. |
If you want to use the `--contexts` option, you need to share your local kubeconfig file with the Docker container, to allow Azure ACR Purge to access your Kubernetes contexts:
returntime.Now(), fmt.Errorf("invalid duration type. Please use 's' for seconds, 'm' for minutes, 'h' for hours, or 'd' for days")
122
+
}
45
123
}
46
124
47
125
funcmain() {
48
126
registryName:=flag.String("registry", "", "Name of the Azure Container Registry")
49
127
repositoryName:=flag.String("repository", "", "Name of the repository in your registry")
50
128
subscriptionId:=flag.String("subscription", "", "ID of the subscription. If not specified it will use the default one")
51
129
contexts:=flag.String("contexts", "", "Comma-separated list of Kubernetes contexts. The deletion process will not start if any 'imageToDelete' is running in a cluster from the context list")
52
-
deletionCutoffTimestamp:=flag.String("timestamp", "01/01/2024", "All Images before the timestamp will get deleted")
53
-
delay:=flag.Float64("delay", 1, "Delay between deletion requests")
130
+
allContexts:=flag.Bool("all-contexts", false, "The deletion process will not start if any 'imageToDelete' is running in a cluster from your kubeconfig contexts")
131
+
ago:=flag.String("ago", "360d", "Time duration in the past. Expects a number followed by a duration type: 's' for seconds, 'm' for minutes, 'h' for hours, 'd' for days.")
54
132
dryRunMode:=flag.Bool("dry-run", false, "Perform a dry run, print tags to be deleted but do not delete them")
55
133
flag.Parse()
56
134
@@ -59,47 +137,73 @@ func main() {
59
137
return
60
138
}
61
139
140
+
if*ago=="" {
141
+
log.Println("You must provide a duration ago")
142
+
return
143
+
}
144
+
62
145
if*subscriptionId!="" {
63
146
_, err:=exec.Command("bash", "-c", fmt.Sprintf("az account set --subscription %s", *subscriptionId)).Output()
64
147
iferr!=nil {
65
148
log.Println("Failed to set az subscription: ", err)
log.Fatalf("Error: The Image %s is running in one of your cluster. Please reconsider your deletion timestamp. \n", image)
234
+
log.Fatalf("Error: %s", err)
235
+
return
127
236
}
128
237
}
129
238
130
-
if*dryRunMode {
131
-
log.Printf("[DRY-RUN] Docker Image %s with tags %s would get deleted. Created Time: %s \n", *repositoryName, strings.Join(metadata.Tags, ","), metadata.CreatedTime)
132
-
continue
133
-
}
134
-
135
-
iflen(metadata.Digest) >0 {
136
-
imagesToDelete=append(imagesToDelete, metadata)
137
-
}
239
+
log.Printf("[DRY-RUN] Docker Image %s with tags %s would get deleted. Created Time: %s \n", *repositoryName, strings.Join(metadata.Tags, ","), metadata.CreatedTime)
240
+
totals["images"]++
241
+
totals["bytes"] +=metadata.ImageSize
138
242
}
139
243
140
-
iflen(imagesToDelete) ==0 {
141
-
return
142
-
}
244
+
log.Printf("Found %d docker images with approximately %.2f GB worth of data to delete.", totals["images"], float64(totals["bytes"])/float64(bytesToGB))
143
245
144
-
amountImages:=0
145
-
for_, imageToDelete:=rangeimagesToDelete {
146
-
log.Printf("Docker Image %s with tags %s will get deleted. Created Time: %s \n", *repositoryName, strings.Join(imageToDelete.Tags, ","), imageToDelete.CreatedTime)
0 commit comments