Skip to content

Instantly share code, notes, and snippets.

@Dentrax
Last active August 14, 2022 20:41
Show Gist options
  • Save Dentrax/bab093c4d8348c21da65be7e34c75efc to your computer and use it in GitHub Desktop.
Save Dentrax/bab093c4d8348c21da65be7e34c75efc to your computer and use it in GitHub Desktop.
demo: delve-entrypoint-mutating
package main
import (
"archive/tar"
"bytes"
"fmt"
"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/tarball"
"io"
"log"
"os"
)
func main() {
log.Printf("pulling the image...")
img, err := crane.Pull("registry.trendyol.com/platform/base/poc/simple-golang-app:latest")
if err != nil {
panic(err)
}
log.Printf("creating layer...")
l, err := createLayer("./dlv")
if err != nil {
panic(err)
}
log.Printf("appending layer...")
i, err := mutate.AppendLayers(img, l)
if err != nil {
panic(err)
}
log.Printf("tagging layer...")
tag, err := name.NewTag("furkanturkal/delve:foo")
if err != nil {
panic(err)
}
log.Printf("image %s pushed to the registry succesfully\n", tag.String())
cfg, err := i.ConfigFile()
if err != nil {
log.Fatalf("getting config: %v", err)
}
cfg = cfg.DeepCopy()
fmt.Printf("%+v", cfg.Config.Entrypoint)
cfg.Config.Entrypoint = []string{"./dlv", "--listen=:40000", "--headless=true", "--api-version=2", "exec", cfg.Config.Entrypoint[0]}
li, err := mutate.Config(i, cfg.Config)
if err != nil {
log.Fatalf("mutating config: %v", err)
}
fmt.Printf("update entrypoint to: %+v", cfg.Config.Entrypoint)
log.Printf("pushing layer...")
//if s, err := daemon.Write(tag, newImg); err != nil {
// panic(err)
//} else {
// fmt.Println(s)
if err := crane.Push(li, tag.String()); err != nil {
panic(err)
}
log.Printf("DONE")
}
func createLayer(fpath string) (v1.Layer, error) {
var b bytes.Buffer
tw := tar.NewWriter(&b)
file, err := os.Open(fpath)
if err != nil {
return nil, err
}
defer file.Close()
stat, err := file.Stat()
if err != nil {
return nil, err
}
header := &tar.Header{
Name: "/src/" + fpath,
Size: stat.Size(),
Mode: int64(stat.Mode()),
ModTime: stat.ModTime(),
}
header.Typeflag = tar.TypeReg
err = tw.WriteHeader(header)
if err != nil {
return nil, err
}
_, err = io.Copy(tw, file)
if err != nil {
return nil, err
}
if err := tw.Close(); err != nil {
return nil, fmt.Errorf("failed to finish tar: %w", err)
}
return tarball.LayerFromReader(&b)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment