YZ yaratti ama benimde cok hosuma gitti not almak istedim.
Konu su; bir docker image kullanarak yeni bir docker image olusturuyorum ancak annotationlarinida aktariyorum hatta ekleme de yapacagim.
EXISTING_JSON=$(skopeo inspect "docker://$TARGET_IMAGE" --raw | jq -c '.annotations // {}')
ANNOTATION_ARGS=()
while IFS= read -r entry; do
key="${entry%%=*}"
val="${entry#*=}"
ANNOTATION_ARGS+=(--annotation "index:${key}=${val}")
done < <(echo "$EXISTING_JSON" | jq -r 'to_entries[] | "\(.key)=\(.value|tostring)"')
ANNOTATION_ARGS+=(--annotation "index:de.buyukburc.hardened.synced_at=$UPDATED_AT_UTC")
ANNOTATION_ARGS+=(--annotation "index:de.buyukburc.hardened.source_image=$SOURCE_IMAGE")
docker buildx imagetools create --tag "$TARGET_IMAGE" "${ANNOTATION_ARGS[@]}" "$TARGET_IMAGE"
simdi bu dostumuzu parcalayalim.
ANNOTATION_ARGS=()
Bos bir dizi olustur ANNOTATION_ARGS adinda
Skopeo
skopeo podman kaynakli bir OCI Image toolu ve cok faydali bir arkadas, “–raw” ile ayni mimaride olmasa bile image inceleyebiliyoruz. jq filtresi ile de sadece annotations kismini cikariyoruz.
jq -c ‘.annotations // {}’
burada su kisim ilginc jq -c ‘.annotations // {}’
// -> eger sol taraftan birsey gelmez ise (sol taraf .annotations) sag tarafi kullan
{} -> bos json objecti
-c –> ciktiyi tek satirda ver.
yani annotations olmasa bile bos bir json objecti gönder ki ilerde loopa girince null üzerinde loop yapilamaz hatasi almasin.
simdi EXISTING_JSON icinde json olarak data var
echo “$EXISTING_JSON” | jq -r ‘to_entries[] | “\(.key)=\(.value|tostring)”‘
-r –> ” lari kaldir
to_entries[] –> object i ciftler halinde listeye cevir
{key:"color", value:"red"}{key:"size", value:"big"}
\(.key)=\(.value|tostring) –> her birini araya = koyarak text haline getir.
color=red
size=big
while IFS= read -r entry; do
IFS= (bosluk) –> space/tab gibi beyaz yerlerde ayrim yapma , her satiri oldugu gibi al.
read -r –> escape ve backslash karakterlerini isleme
Böylece gelen satiri process etmeden direk loopta olduug gibi okuyor.
okudugu her bir satir da entry adi ile loop ta isleniyor.
key=”${entry%%=}” val=”${entry#=}”
simdi loop ta entry olarak “color=red” geldi. sagdan sola expansion yapiyoruz
key="${entry%%=*}"
=* –> = ve sonra gelen hersey
%% –> önceki ile yakalanandan sona kadar herseyi sil
burada “=red”
sonuc color
key=color
val="${entry#*=}"
entryi al “color=red” hala ayni looptayiz
*= –> = dahil = e kadar olan herseyi al
# –> bastan itibaren bir öncekinde yakalanan herseyi sil.
burada “color=”
sonuc red
val=red
ANNOTATION_ARGS+=(–annotation “index:${key}=${val}”)
Annotation args dizisine ekle
ANNOTATION_ARGS+=(–annotation “index:de.buyukburc.hardened.synced_at=$UPDATED_AT_UTC”)
Diziye istedigimiz diger manual bilgileride bu sekilde ekliyoruz.
docker buildx imagetools create –tag “$TARGET_IMAGE” “${ANNOTATION_ARGS[@]}” “$TARGET_IMAGE”
bu sekilde annotations ile beraber docker imagi olusturuyoruz ancak bu image ilk image daki mimarileri koruyor.
skopeo inspect --raw "docker://$TARGET_IMAGE" | jq '.manifests[].platform'
{
"architecture": "arm64",
"os": "linux"
}
{
"architecture": "amd64",
"os": "linux"
}