-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-containerfiles.go
More file actions
160 lines (142 loc) · 5.46 KB
/
generate-containerfiles.go
File metadata and controls
160 lines (142 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"bytes"
"flag" // Import flag package
"fmt"
"log"
"os"
"path/filepath"
"strings"
"text/template"
)
// Variables set by ldflags
var (
commit string
date string
version string
)
type Config struct {
BaseImage string
CachingMode string
UseUPX bool
GoVersion string
AthensProxyURL string
HttpProxyURL string
GoBuildCommand string
// Add fields for ARGs if needed in template
MYPATH string
COMMIT string
DATE string
VERSION string
// etc.
}
func main() {
// --- Flags ---
tmplPath := flag.String("template", "build/containers/go_nix_simple_refactor/Containerfile.tmpl", "Path to the Containerfile template")
outputPath := flag.String("output", "build/containers/go_nix_simple_refactor", "Directory to save generated Containerfiles")
athensURL := flag.String("athens-url", "http://localhost:8888", "Athens proxy URL")
httpProxyURL := flag.String("http-proxy-url", "http://localhost:3128", "HTTP proxy URL")
// athensURL := flag.String("athens-url", "http://hp4.home:8888", "Athens proxy URL")
// httpProxyURL := flag.String("http-proxy-url", "http://hp4.home:3128", "HTTP proxy URL")
goVersion := flag.String("go-version", "1.24.2", "Go version for builder image")
// Add flags for MYPATH, COMMIT, DATE, VERSION if they need to be passed to the template
// Example:
// mypathArg := flag.String("mypath", ".", "Value for MYPATH ARG")
flag.Parse()
log.Printf("Generator version: %s, commit: %s, built: %s", version, commit, date)
// --- Template Parsing ---
tmpl, err := template.ParseFiles(*tmplPath)
if err != nil {
log.Fatalf("Error parsing template %s: %v", *tmplPath, err)
}
// --- Define Options for Combinations ---
baseImages := []string{"gcr.io/distroless/static-debian12", "scratch"}
cachingModes := []string{"docker", "athens", "http", "none"}
useUPXOptions := []bool{false, true}
var configs []Config
for _, base := range baseImages {
for _, cache := range cachingModes {
for _, upx := range useUPXOptions {
// --- Skip invalid or undesired combinations ---
// // Example: UPX might not make sense with certain cache modes,
// // or you might only want UPX for distroless. Adjust as needed.
// if upx && base == "scratch" {
// // Example: Let's say we only want UPX for distroless for now
// // log.Printf("Skipping combination: Base=%s, Cache=%s, UPX=%t", base, cache, upx)
// // continue
// }
// if cache != "default" && upx {
// // Example: Let's say UPX only applies to the 'default' cache build for simplicity
// // log.Printf("Skipping combination: Base=%s, Cache=%s, UPX=%t", base, cache, upx)
// // continue
// }
// // Add any other skipping logic here if necessary
// --- Create Config for this combination ---
cfg := Config{
BaseImage: base,
CachingMode: cache,
UseUPX: upx,
GoVersion: *goVersion,
AthensProxyURL: *athensURL, // Set URLs regardless, template/build command logic uses them conditionally
HttpProxyURL: *httpProxyURL,
// Add ARGs if needed: VERSION: version, COMMIT: commit, DATE: date, MYPATH: *mypathArg ...
}
configs = append(configs, cfg)
}
}
}
log.Printf("Generated %d configuration combinations.", len(configs))
// --- Generation Loop ---
for i := range configs { // Use index to modify cfg in place
cfg := &configs[i] // Get pointer to modify original config
// --- Determine Go Build Command ---
var buildCmdBuilder strings.Builder
buildCmdBuilder.WriteString("RUN ") // Start the RUN command
switch cfg.CachingMode {
case "docker":
buildCmdBuilder.WriteString("--mount=type=cache,target=/go/pkg/mod \\\n --mount=type=cache,target=/root/.cache/go-build \\\n ")
case "athens":
buildCmdBuilder.WriteString(fmt.Sprintf("GOPROXY=%s,https://proxy.golang.org,direct \\\n ", cfg.AthensProxyURL))
case "http":
buildCmdBuilder.WriteString(fmt.Sprintf("HTTP_PROXY=%s \\\n HTTPS_PROXY=%s \\\n ", cfg.HttpProxyURL, cfg.HttpProxyURL))
case "none":
// No extra flags needed for the RUN line itself
}
// Append the common part of the go build command
buildCmdBuilder.WriteString("CGO_ENABLED=0 go build \\\n")
buildCmdBuilder.WriteString(" -trimpath \\\n")
buildCmdBuilder.WriteString(" -tags=netgo,osusergo \\\n")
buildCmdBuilder.WriteString(" -ldflags=\"-s -w\" \\\n")
buildCmdBuilder.WriteString(" -o /go/bin/go_nix_simple \\\n")
buildCmdBuilder.WriteString(" ./cmd/go_nix_simple/go_nix_simple.go")
cfg.GoBuildCommand = buildCmdBuilder.String()
// --- End Go Build Command Determination ---
// --- Generate Filename ---
// ***** ADD THESE LINES BACK *****
baseName := "distroless"
if cfg.BaseImage == "scratch" {
baseName = "scratch"
}
packerName := "noupx"
if cfg.UseUPX {
packerName = "upx"
}
// ********************************
filename := fmt.Sprintf("Containerfile.%s.%s.%s", baseName, cfg.CachingMode, packerName)
fullPath := filepath.Join(*outputPath, filename) // Use flag value
// --- Execute Template ---
var buf bytes.Buffer
err = tmpl.Execute(&buf, cfg) // Pass pointer to config
if err != nil {
log.Printf("Error executing template for %s: %v", filename, err)
continue // Skip to next config on template error
}
// --- Write File ---
err = os.WriteFile(fullPath, buf.Bytes(), 0644)
if err != nil {
log.Printf("Error writing file %s: %v", fullPath, err)
continue // Skip to next config on write error
}
log.Printf("Generated: %s", fullPath)
}
}