@@ -43,21 +43,52 @@ func ParseCode(u string) (Code, bool) {
4343// Build builds the checktype defined in a directory. It builds the binary and
4444// the docker image of the checktype and returns the name of the docker image
4545// built.
46- func (c Code ) Build (logger log.Logger ) (string , error ) {
47- modified , err := c .isModified (logger )
46+ func (c Code ) Build (debug bool , logger log.Logger ) (string , error ) {
47+ modified , err := c .isModified (debug , logger )
4848 if err != nil {
4949 return "" , err
5050 }
5151 if ! modified {
52- logger .Infof ("No changes in checktype in dir %s, reusing image %s" , string (c ), c .imageName ())
53- return c .imageName (), nil
52+ logger .Infof ("No changes in checktype in dir %s, reusing image %s" , string (c ), c .imageName (debug ))
53+ return c .imageName (debug ), nil
5454 }
5555 logger .Infof ("Compiling checktype in dir %s" , c )
5656 dir := string (c )
5757 // Run go build in the checktype dir.
5858 if err := goBuildDir (dir ); err != nil {
5959 return "" , err
6060 }
61+ dockerfile := "Dockerfile"
62+
63+ // In case of debug, we need to add the delve debugger to the image.
64+ // We create a new Dockerfile with the necessary changes.
65+ // The original Dockerfile is kept in the dir but excluded from the Tar file.
66+ // The new Dockerfile is removed after the image is built.
67+ if debug {
68+ b , err := os .ReadFile (path .Join (dir , "Dockerfile" ))
69+ if err != nil {
70+ return "" , err
71+ }
72+ dockerfile = "Dockerfile.debug"
73+ add := fmt .Sprintf (`
74+ RUN apk add --no-cache go
75+ RUN go install github.com/go-delve/delve/cmd/dlv@latest
76+ EXPOSE 2345
77+ ENTRYPOINT /root/go/bin/dlv --listen=:2345 --headless=true --api-version=2 --log exec %s
78+ ` , filepath .Base (dir ))
79+
80+ b = append (b , []byte (add )... )
81+ if err := os .WriteFile (path .Join (dir , dockerfile ), b , 0644 ); err != nil {
82+ return "" , err
83+ }
84+
85+ defer func () {
86+ if err := os .Remove (path .Join (dir , dockerfile )); err != nil {
87+ logger .Errorf ("error removing %s: %v" , dockerfile , err )
88+ }
89+ }()
90+ }
91+
6192 // Build a Tar file with the docker image contents.
6293 logger .Infof ("Building image for checktype in dir %s" , dir )
6394 contents , err := buildTarFromDir (dir )
@@ -72,8 +103,8 @@ func (c Code) Build(logger log.Logger) (string, error) {
72103 t := modif .Format (time .RFC822 )
73104 logger .Debugf ("Last modified time for checktype in dir %s is %s" , dir , t )
74105 labels := map [string ]string {modifTimeLabel : t }
75- image := c .imageName ()
76- r , err := buildDockerdImage (contents , []string {image }, labels )
106+ image := c .imageName (debug )
107+ r , err := buildDockerdImage (contents , []string {image }, labels , dockerfile )
77108 if err != nil {
78109 return "" , err
79110 }
@@ -82,19 +113,19 @@ func (c Code) Build(logger log.Logger) (string, error) {
82113 return image , nil
83114}
84115
85- func (c Code ) isModified (logger log.Logger ) (bool , error ) {
86- labels , err := imageInfo (c .imageName ())
116+ func (c Code ) isModified (debug bool , logger log.Logger ) (bool , error ) {
117+ labels , err := imageInfo (c .imageName (debug ))
87118 if err != nil {
88119 return false , err
89120 }
90121 imageTimeS , ok := labels [modifTimeLabel ]
91122 if ! ok {
92- logger .Infof ("Image %s does not contain the label %s" , c .imageName (), modifTimeLabel )
123+ logger .Infof ("Image %s does not contain the label %s" , c .imageName (debug ), modifTimeLabel )
93124 return true , nil
94125 }
95126 _ , err = time .Parse (time .RFC822 , imageTimeS )
96127 if err != nil {
97- logger .Infof ("invalid time, %+w defined in the label %s of the image %s" , err , modifTimeLabel , c .imageName ())
128+ logger .Infof ("invalid time, %+w defined in the label %s of the image %s" , err , modifTimeLabel , c .imageName (debug ))
98129 return true , nil
99130 }
100131 dirTime , err := c .lastModified (logger )
@@ -142,14 +173,16 @@ func (c Code) lastModified(logger log.Logger) (time.Time, error) {
142173 return * latest , nil
143174}
144175
145- func (c Code ) imageName () string {
146- dir := string (c )
147- image := path .Base (dir )
148- return fmt .Sprintf ("%s-%s" , image , "local" )
176+ func (c Code ) imageName (debug bool ) string {
177+ base := path .Base (string (c ))
178+ if debug {
179+ return fmt .Sprintf ("%s-local-debug" , base )
180+ }
181+ return fmt .Sprintf ("%s-local" , base )
149182}
150183
151184func goBuildDir (dir string ) error {
152- args := []string {"build" , "-a " , "-ldflags" , "-extldflags -static " , "." }
185+ args := []string {"build" , "-gcflags " , "all=-N -l " , "." }
153186 cmd := exec .Command ("go" , args ... )
154187 cmd .Env = os .Environ ()
155188 cmd .Env = append (cmd .Env , "GOOS=linux" , "CGO_ENABLED=0" )
0 commit comments