optimize dockerfile generation (#284)

This commit is contained in:
Kevin Wan
2020-12-12 16:53:06 +08:00
committed by GitHub
parent f919bc6713
commit acd48f0abb
5 changed files with 23 additions and 13 deletions

View File

@@ -29,11 +29,16 @@ type Docker struct {
ExeFile string ExeFile string
HasPort bool HasPort bool
Port int Port int
HasArgs bool
Argument string Argument string
} }
func DockerCommand(c *cli.Context) error { func DockerCommand(c *cli.Context) (err error) {
defer func() {
if err == nil {
fmt.Println(aurora.Green("Done."))
}
}()
goFile := c.String("go") goFile := c.String("go")
if len(goFile) == 0 { if len(goFile) == 0 {
return errors.New("-go can't be empty") return errors.New("-go can't be empty")
@@ -60,7 +65,6 @@ func DockerCommand(c *cli.Context) error {
projDir, ok := util.FindProjectPath(goFile) projDir, ok := util.FindProjectPath(goFile)
if ok { if ok {
fmt.Printf("Hint: run \"docker build ...\" command in dir %q\n", projDir) fmt.Printf("Hint: run \"docker build ...\" command in dir %q\n", projDir)
fmt.Println(aurora.Green("Done."))
} }
return nil return nil
@@ -135,7 +139,6 @@ func generateDockerfile(goFile string, port int, args ...string) error {
ExeFile: util.FileNameWithoutExt(filepath.Base(goFile)), ExeFile: util.FileNameWithoutExt(filepath.Base(goFile)),
HasPort: port > 0, HasPort: port > 0,
Port: port, Port: port,
HasArgs: builder.Len() > 0,
Argument: builder.String(), Argument: builder.String(),
}) })
} }

View File

@@ -22,7 +22,7 @@ ADD go.mod .
ADD go.sum . ADD go.sum .
RUN go mod download RUN go mod download
COPY . . COPY . .
{{if .HasArgs}}COPY {{.GoRelPath}}/etc /app/etc {{if .Argument}}COPY {{.GoRelPath}}/etc /app/etc
{{end}}RUN go build -ldflags="-s -w" -o /app/{{.ExeFile}} {{.GoRelPath}}/{{.GoFile}} {{end}}RUN go build -ldflags="-s -w" -o /app/{{.ExeFile}} {{.GoRelPath}}/{{.GoFile}}
@@ -32,8 +32,8 @@ RUN apk update --no-cache && apk add --no-cache ca-certificates tzdata
ENV TZ Asia/Shanghai ENV TZ Asia/Shanghai
WORKDIR /app WORKDIR /app
COPY --from=builder /app/{{.ExeFile}} /app/{{.ExeFile}} COPY --from=builder /app/{{.ExeFile}} /app/{{.ExeFile}}{{if .Argument}}
{{if .HasArgs}}COPY --from=builder /app/etc /app/etc{{end}} COPY --from=builder /app/etc /app/etc{{end}}
{{if .HasPort}} {{if .HasPort}}
EXPOSE {{.Port}} EXPOSE {{.Port}}
{{end}} {{end}}

View File

@@ -246,9 +246,8 @@ var (
Required: true, Required: true,
}, },
cli.StringFlag{ cli.StringFlag{
Name: "secret", Name: "secret",
Usage: "the image pull secret", Usage: "the secret to image pull from registry",
Required: true,
}, },
cli.IntFlag{ cli.IntFlag{
Name: "requestCpu", Name: "requestCpu",

View File

@@ -47,9 +47,9 @@ spec:
volumeMounts: volumeMounts:
- name: timezone - name: timezone
mountPath: /etc/localtime mountPath: /etc/localtime
imagePullSecrets: {{if .Secret}}imagePullSecrets:
- name: {{.Secret}} - name: {{.Secret}}
volumes: {{end}}volumes:
- name: timezone - name: timezone
hostPath: hostPath:
path: /usr/share/zoneinfo/Asia/Shanghai path: /usr/share/zoneinfo/Asia/Shanghai

View File

@@ -2,8 +2,10 @@ package kube
import ( import (
"errors" "errors"
"fmt"
"text/template" "text/template"
"github.com/logrusorgru/aurora"
"github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@@ -53,7 +55,7 @@ func DeploymentCommand(c *cli.Context) error {
defer out.Close() defer out.Close()
t := template.Must(template.New("deploymentTemplate").Parse(text)) t := template.Must(template.New("deploymentTemplate").Parse(text))
return t.Execute(out, Deployment{ err = t.Execute(out, Deployment{
Name: c.String("name"), Name: c.String("name"),
Namespace: c.String("namespace"), Namespace: c.String("namespace"),
Image: c.String("image"), Image: c.String("image"),
@@ -70,6 +72,12 @@ func DeploymentCommand(c *cli.Context) error {
MinReplicas: c.Int("minReplicas"), MinReplicas: c.Int("minReplicas"),
MaxReplicas: c.Int("maxReplicas"), MaxReplicas: c.Int("maxReplicas"),
}) })
if err != nil {
return err
}
fmt.Println(aurora.Green("Done."))
return nil
} }
func Category() string { func Category() string {