-
Notifications
You must be signed in to change notification settings - Fork 165
/
configure.go
217 lines (193 loc) · 6.43 KB
/
configure.go
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package internal
// This file is a script which generates the .goreleaser.yaml file for all
// supported OpenTelemetry Collector distributions.
//
// Run it with `make generate-goreleaser`.
import (
"fmt"
"path"
"strings"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/nfpm/v2/files"
)
var (
ImagePrefixes = []string{"otel", "ghcr.io/open-telemetry/opentelemetry-collector-releases"}
Architectures = []string{"386", "amd64", "arm64", "ppc64le"}
)
func Generate(imagePrefixes []string, dists []string) config.Project {
return config.Project{
ProjectName: "opentelemetry-collector-releases",
Checksum: config.Checksum{
NameTemplate: "{{ .ProjectName }}_checksums.txt",
},
Builds: Builds(dists),
Archives: Archives(dists),
NFPMs: Packages(dists),
Dockers: DockerImages(imagePrefixes, dists),
DockerManifests: DockerManifests(imagePrefixes, dists),
}
}
func Builds(dists []string) (r []config.Build) {
for _, dist := range dists {
r = append(r, Build(dist))
}
return
}
// Build configures a goreleaser build.
// https://goreleaser.com/customization/build/
func Build(dist string) config.Build {
return config.Build{
ID: dist,
Dir: path.Join("distributions", dist, "_build"),
Binary: dist,
BuildDetails: config.BuildDetails{
Env: []string{"CGO_ENABLED=0"},
Flags: []string{"-trimpath"},
Ldflags: []string{"-s", "-w"},
},
Goos: []string{"darwin", "linux", "windows"},
Goarch: Architectures,
Ignore: []config.IgnoredBuild{
{Goos: "darwin", Goarch: "386"},
{Goos: "windows", Goarch: "arm64"},
},
}
}
func Archives(dists []string) (r []config.Archive) {
for _, dist := range dists {
r = append(r, Archive(dist))
}
return
}
// Archive configures a goreleaser archive (tarball).
// https://goreleaser.com/customization/archive/
func Archive(dist string) config.Archive {
return config.Archive{
ID: dist,
NameTemplate: "{{ .Binary }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}{{ if .Mips }}_{{ .Mips }}{{ end }}",
Builds: []string{dist},
}
}
func Packages(dists []string) (r []config.NFPM) {
for _, dist := range dists {
r = append(r, Package(dist))
}
return
}
// Package configures goreleaser to build a system package.
// https://goreleaser.com/customization/nfpm/
func Package(dist string) config.NFPM {
return config.NFPM{
ID: dist,
Builds: []string{dist},
Formats: []string{"apk", "deb", "rpm"},
License: "Apache 2.0",
Description: fmt.Sprintf("OpenTelemetry Collector - %s", dist),
Maintainer: "The OpenTelemetry Collector maintainers <[email protected]>",
NFPMOverridables: config.NFPMOverridables{
PackageName: dist,
Scripts: config.NFPMScripts{
PreInstall: path.Join("distributions", dist, "preinstall.sh"),
PostInstall: path.Join("distributions", dist, "postinstall.sh"),
PreRemove: path.Join("distributions", dist, "preremove.sh"),
},
Contents: files.Contents{
{
Source: path.Join("distributions", dist, fmt.Sprintf("%s.service", dist)),
Destination: path.Join("/lib", "systemd", "system", fmt.Sprintf("%s.service", dist)),
},
{
Source: path.Join("distributions", dist, fmt.Sprintf("%s.conf", dist)),
Destination: path.Join("/etc", dist, fmt.Sprintf("%s.conf", dist)),
Type: "config|noreplace",
},
{
Source: path.Join("configs", fmt.Sprintf("%s.yaml", dist)),
Destination: path.Join("/etc", dist, "config.yaml"),
Type: "config",
},
},
},
}
}
func DockerImages(imagePrefixes, dists []string) (r []config.Docker) {
for _, dist := range dists {
for _, arch := range Architectures {
r = append(r, DockerImage(imagePrefixes, dist, arch))
}
}
return
}
// DockerImage configures goreleaser to build a container image.
// https://goreleaser.com/customization/docker/
func DockerImage(imagePrefixes []string, dist, arch string) config.Docker {
var imageTemplates []string
for _, prefix := range imagePrefixes {
imageTemplates = append(
imageTemplates,
fmt.Sprintf("%s/%s:{{ .Version }}-%s", prefix, imageName(dist), arch),
fmt.Sprintf("%s/%s:latest-%s", prefix, imageName(dist), arch),
)
}
label := func(name, template string) string {
return fmt.Sprintf("--label=org.opencontainers.image.%s={{%s}}", name, template)
}
return config.Docker{
ImageTemplates: imageTemplates,
Dockerfile: path.Join("distributions", dist, "Dockerfile"),
Use: "buildx",
BuildFlagTemplates: []string{
"--pull",
fmt.Sprintf("--platform=linux/%s", arch),
label("created", ".Date"),
label("name", ".ProjectName"),
label("revision", ".FullCommit"),
label("version", ".Version"),
label("source", ".GitURL"),
},
Files: []string{path.Join("configs", fmt.Sprintf("%s.yaml", dist))},
Goos: "linux",
Goarch: arch,
}
}
func DockerManifests(imagePrefixes, dists []string) (r []config.DockerManifest) {
for _, dist := range dists {
for _, prefix := range imagePrefixes {
r = append(r, DockerManifest(prefix, `{{ .Version }}`, dist))
r = append(r, DockerManifest(prefix, "latest", dist))
}
}
return
}
// DockerManifest configures goreleaser to build a multi-arch container image manifest.
// https://goreleaser.com/customization/docker_manifest/
func DockerManifest(prefix, version, dist string) config.DockerManifest {
var imageTemplates []string
for _, arch := range Architectures {
imageTemplates = append(
imageTemplates,
fmt.Sprintf("%s/%s:%s-%s", prefix, imageName(dist), version, arch),
)
}
return config.DockerManifest{
NameTemplate: fmt.Sprintf("%s/%s:%s", prefix, imageName(dist), version),
ImageTemplates: imageTemplates,
}
}
// imageName translates a distribution name to a container image name.
func imageName(dist string) string {
return strings.Replace(dist, "otelcol", "opentelemetry-collector", 1)
}