Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enabled CGO for Macos otelcol contrib #626

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .github/workflows/base-ci-goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ on:
goarch:
required: true
type: string
cgo:
required: true
type: string

jobs:
check-goreleaser:
Expand All @@ -32,7 +35,7 @@ jobs:
GOARCH: arm
- GOOS: windows
GOARCH: s390x
runs-on: ubuntu-22.04
runs-on: ${{ ( matrix.GOOS == 'darwin' && contains( fromJSON( inputs.cgo), 'darwin')) && 'macos-14' || 'ubuntu-22.04' }}

steps:
- name: Checkout
Expand All @@ -41,6 +44,7 @@ jobs:
fetch-depth: 0

- name: Setup QEMU
if: ${{ runner.os != 'macOS' }} # docker/setup-qemu-action action is not available on macos
uses: docker/setup-qemu-action@49b3bc8e6bdd4a60e6116a5414239cba5943d3cf # v3.2.0
with:
platforms: arm64,ppc64le,linux/arm/v7,s390x
Expand All @@ -53,6 +57,7 @@ jobs:

- name: Setup Docker Buildx
uses: docker/setup-buildx-action@c47758b77c9736f4b2ef4073d4d51994fabfe349 # v3.7.1
if: ${{ runner.os != 'macOS' }} # docker is not available on macos

- name: Setup Go
uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5.0.2
Expand Down
7 changes: 6 additions & 1 deletion .github/workflows/base-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ on:
goarch:
required: true
type: string
cgo:
required: true
type: string

jobs:
prepare:
Expand All @@ -32,7 +35,7 @@ jobs:
GOARCH: arm
- GOOS: windows
GOARCH: s390x
runs-on: ubuntu-22.04
runs-on: ${{ ( matrix.GOOS == 'darwin' && contains( fromJSON( inputs.cgo), 'darwin')) && 'macos-14' || 'ubuntu-22.04' }}

steps:
- uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0
Expand All @@ -44,10 +47,12 @@ jobs:
- uses: anchore/sbom-action/download-syft@61119d458adab75f756bc0b9e4bde25725f86a7a # v0.17.2

- uses: docker/setup-qemu-action@49b3bc8e6bdd4a60e6116a5414239cba5943d3cf # v3.2.0
if: ${{ runner.os != 'macOS' }} # docker/setup-qemu-action action is not available on macos
with:
platforms: arm64,ppc64le,linux/arm/v7,s390x

- uses: docker/setup-buildx-action@c47758b77c9736f4b2ef4073d4d51994fabfe349 # v3.7.1
if: ${{ runner.os != 'macOS' }}

- uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5.0.2
with:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ci-goreleaser-contrib.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
distribution: otelcol-contrib
goos: '[ "linux", "windows", "darwin" ]'
goarch: '[ "386", "amd64", "arm64", "ppc64le", "arm", "s390x" ]'
cgo: '[ "darwin" ]'
secrets: inherit

package-tests:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ci-goreleaser-core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
distribution: otelcol
goos: '[ "linux", "windows", "darwin" ]'
goarch: '[ "386", "amd64", "arm64", "ppc64le", "arm", "s390x" ]'
cgo: '[]'
secrets: inherit

package-tests:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ci-goreleaser-k8s.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ jobs:
distribution: otelcol-k8s
goos: '[ "linux" ]'
goarch: '[ "amd64", "arm64", "ppc64le", "s390x" ]'
cgo: '[]'
secrets: inherit
1 change: 1 addition & 0 deletions .github/workflows/release-contrib.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ jobs:
distribution: otelcol-contrib
goos: '[ "linux", "windows", "darwin" ]'
goarch: '[ "386", "amd64", "arm64", "ppc64le", "arm", "s390x" ]'
cgo: '[ "darwin" ]'
secrets: inherit
permissions: write-all
1 change: 1 addition & 0 deletions .github/workflows/release-core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ jobs:
distribution: otelcol
goos: '[ "linux", "windows", "darwin" ]'
goarch: '[ "386", "amd64", "arm64", "ppc64le", "arm", "s390x" ]'
cgo: '[]'
secrets: inherit
permissions: write-all
1 change: 1 addition & 0 deletions .github/workflows/release-k8s.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ jobs:
distribution: otelcol-k8s
goos: '[ "linux" ]'
goarch: '[ "amd64", "arm64", "ppc64le", "s390x" ]'
cgo: '[]'
secrets: inherit
permissions: write-all
73 changes: 62 additions & 11 deletions cmd/goreleaser/internal/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package internal

import (
"fmt"
"log"
"path"
"strings"

Expand All @@ -35,15 +36,15 @@ var (
ArmVersions = []string{"7"}
)

func Generate(dist string) config.Project {
func Generate(dist string, cgo string) config.Project {
return config.Project{
ProjectName: "opentelemetry-collector-releases",
Checksum: config.Checksum{
NameTemplate: fmt.Sprintf("{{ .ProjectName }}_%v_checksums.txt", dist),
},
Env: []string{"COSIGN_YES=true"},
Builds: Builds(dist),
Archives: Archives(dist),
Builds: Builds(dist, cgo),
Archives: Archives(dist, cgo),
MSI: WinPackages(dist),
NFPMs: Packages(dist),
Dockers: DockerImages(dist),
Expand All @@ -52,15 +53,22 @@ func Generate(dist string) config.Project {
DockerSigns: DockerSigns(),
SBOMs: SBOM(),
Version: 2,
Monorepo: config.Monorepo{
Monorepo: config.Monorepo{
TagPrefix: "v",
},
}
}

func Builds(dist string) []config.Build {
return []config.Build{
Build(dist),
func Builds(dist string, cgo string) []config.Build {
if len(cgo) == 0 {
return []config.Build{
Build(dist),
}
} else if cgo == "darwin" {
return BuildCGO(dist)
} else {
log.Fatal("Unsupport CGO OS version")
return []config.Build{}
}
}

Expand Down Expand Up @@ -90,19 +98,58 @@ func Build(dist string) config.Build {
}
}

func Archives(dist string) (r []config.Archive) {
func BuildCGO(dist string) []config.Build {
return []config.Build{
{
ID: dist,
Dir: "_build",
Binary: dist,
BuildDetails: config.BuildDetails{
Env: []string{"CGO_ENABLED=0"},
Flags: []string{"-trimpath"},
Ldflags: []string{"-s", "-w"},
},
Goos: []string{"linux", "windows"},
Goarch: Architectures,
Goarm: ArmVersions,
Ignore: []config.IgnoredBuild{
{Goos: "windows", Goarch: "arm"},
{Goos: "windows", Goarch: "arm64"},
{Goos: "windows", Goarch: "s390x"},
},
},
{
ID: getCgoDistId(dist),
Dir: "_build",
Binary: dist,
BuildDetails: config.BuildDetails{
Env: []string{"CGO_ENABLED=1", "CGO_LDFLAGS='-fstack-protector'"},
Flags: []string{"-trimpath", "-buildmode=pie"},
Ldflags: []string{"-s", "-w"},
},
Goos: []string{"darwin"},
Goarch: []string{"amd64", "arm64"},
},
}
}

func Archives(dist string, cgo string) (r []config.Archive) {
return []config.Archive{
Archive(dist),
Archive(dist, cgo),
}
}

// Archive configures a goreleaser archive (tarball).
// https://goreleaser.com/customization/archive/
func Archive(dist string) config.Archive {
func Archive(dist string, cgo string) config.Archive {
builds := []string{dist}
if len(cgo) > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the benefit of having cgo set as a string instead of a bool? Do we plan to have more conditional logic based on the content of cgo if we have more distributions built with cgo in the future?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put it to string in case we need other OS to support cgo in the future. I also wrote it in the cgoFlag description.

builds = append(builds, getCgoDistId(dist))
}
return config.Archive{
ID: dist,
NameTemplate: "{{ .Binary }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}{{ if .Mips }}_{{ .Mips }}{{ end }}",
Builds: []string{dist},
Builds: builds,
}
}

Expand Down Expand Up @@ -328,3 +375,7 @@ func SBOM() []config.SBOM {
},
}
}

func getCgoDistId(dist string) string {
return dist + "-cgo"
}
3 changes: 2 additions & 1 deletion cmd/goreleaser/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
)

var distFlag = flag.String("d", "", "Collector distributions to build")
var cgoFlag = flag.String("c", "", "Cgo enabled flag, followed by the OS that need to be supported, currently only darwin is supported, i.e. '-c darwin'")

func main() {
flag.Parse()
Expand All @@ -33,7 +34,7 @@ func main() {
log.Fatal("no distribution to build")
}

project := internal.Generate(*distFlag)
project := internal.Generate(*distFlag, *cgoFlag)

partial := map[string]any{
"partial": map[string]any{
Expand Down
25 changes: 18 additions & 7 deletions distributions/otelcol-contrib/.goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ msi:
builds:
- id: otelcol-contrib
goos:
- darwin
- linux
- windows
goarch:
Expand All @@ -27,12 +26,6 @@ builds:
goarm:
- "7"
ignore:
- goos: darwin
goarch: "386"
- goos: darwin
goarch: arm
- goos: darwin
goarch: s390x
- goos: windows
goarch: arm
- goos: windows
Expand All @@ -48,10 +41,28 @@ builds:
- -trimpath
env:
- CGO_ENABLED=0
- id: otelcol-contrib-cgo
goos:
- darwin
goarch:
- amd64
- arm64
dir: _build
binary: otelcol-contrib
ldflags:
- -s
- -w
flags:
- -trimpath
- -buildmode=pie
env:
- CGO_ENABLED=1
- CGO_LDFLAGS='-fstack-protector'
archives:
- id: otelcol-contrib
builds:
- otelcol-contrib
- otelcol-contrib-cgo
name_template: '{{ .Binary }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}{{ if .Mips }}_{{ .Mips }}{{ end }}'
nfpms:
- package_name: otelcol-contrib
Expand Down
6 changes: 5 additions & 1 deletion scripts/generate-goreleaser.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@ echo "Distributions to generate: $distributions";

for distribution in $(echo "$distributions" | tr "," "\n")
do
${GO} run cmd/goreleaser/main.go -d "${distribution}" > "./distributions/${distribution}/.goreleaser.yaml"
if [[ "${distribution}" == "otelcol-contrib" ]]; then
${GO} run cmd/goreleaser/main.go -d "${distribution}" -c "darwin" > "./distributions/${distribution}/.goreleaser.yaml"
else
${GO} run cmd/goreleaser/main.go -d "${distribution}" > "./distributions/${distribution}/.goreleaser.yaml"
fi
done