Skip to content

Commit 320f46f

Browse files
fix(darwin): remove the need for cgo when building a darwin binary on linux (cherry-pick #23507) (#23526)
Signed-off-by: rumstead <[email protected]> Co-authored-by: rumstead <[email protected]>
1 parent d69639a commit 320f46f

File tree

6 files changed

+89
-40
lines changed

6 files changed

+89
-40
lines changed

.goreleaser.yaml

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ builds:
2121
- -X github.com/argoproj/argo-cd/v3/common.gitCommit={{ .FullCommit }}
2222
- -X github.com/argoproj/argo-cd/v3/common.gitTreeState={{ .Env.GIT_TREE_STATE }}
2323
- -X github.com/argoproj/argo-cd/v3/common.kubectlVersion={{ .Env.KUBECTL_VERSION }}
24-
- '{{ if or (eq .Runtime.Goos "linux") (eq .Runtime.Goos "windows") }}-extldflags="-static"{{ end }}'
24+
- -extldflags="-static"
2525
goos:
2626
- linux
2727
- windows
@@ -42,15 +42,6 @@ builds:
4242
goarch: ppc64le
4343
- goos: windows
4444
goarch: arm64
45-
overrides:
46-
- goos: darwin
47-
goarch: amd64
48-
env:
49-
- CGO_ENABLED=1
50-
- goos: darwin
51-
goarch: arm64
52-
env:
53-
- CGO_ENABLED=1
5445

5546
archives:
5647
- id: argocd-archive

cmd/argocd-k8s-auth/commands/azure.go renamed to cmd/argocd-k8s-auth/commands/azure_cgo.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build !darwin || (cgo && darwin)
2+
13
package commands
24

35
import (
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//go:build darwin && !cgo
2+
3+
// Package commands
4+
// This file is used when the GOOS is darwin and CGO is not enabled.
5+
// It provides a no-op implementation of newAzureCommand to allow goreleaser to build
6+
// a darwin binary on a linux machine.
7+
package commands
8+
9+
import (
10+
"log"
11+
12+
"github.com/spf13/cobra"
13+
14+
"github.com/argoproj/argo-cd/v3/util/workloadidentity"
15+
)
16+
17+
func newAzureCommand() *cobra.Command {
18+
command := &cobra.Command{
19+
Use: "azure",
20+
Run: func(c *cobra.Command, _ []string) {
21+
log.Fatalf(workloadidentity.CGOError)
22+
},
23+
}
24+
return command
25+
}

util/workloadidentity/workloadidentity.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
package workloadidentity
22

33
import (
4-
"context"
54
"time"
6-
7-
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
8-
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
9-
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
105
)
116

127
const (
@@ -22,34 +17,9 @@ type TokenProvider interface {
2217
GetToken(scope string) (*Token, error)
2318
}
2419

25-
type WorkloadIdentityTokenProvider struct {
26-
tokenCredential azcore.TokenCredential
27-
}
28-
2920
// Used to propagate initialization error if any
3021
var initError error
3122

32-
func NewWorkloadIdentityTokenProvider() TokenProvider {
33-
cred, err := azidentity.NewDefaultAzureCredential(&azidentity.DefaultAzureCredentialOptions{})
34-
initError = err
35-
return WorkloadIdentityTokenProvider{tokenCredential: cred}
36-
}
37-
38-
func (c WorkloadIdentityTokenProvider) GetToken(scope string) (*Token, error) {
39-
if initError != nil {
40-
return nil, initError
41-
}
42-
43-
token, err := c.tokenCredential.GetToken(context.Background(), policy.TokenRequestOptions{
44-
Scopes: []string{scope},
45-
})
46-
if err != nil {
47-
return nil, err
48-
}
49-
50-
return &Token{AccessToken: token.Token, ExpiresOn: token.ExpiresOn}, nil
51-
}
52-
5323
func CalculateCacheExpiryBasedOnTokenExpiry(tokenExpiry time.Time) time.Duration {
5424
// Calculate the cache expiry as 5 minutes before the token expires
5525
cacheExpiry := time.Until(tokenExpiry) - time.Minute*5
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//go:build !darwin || (cgo && darwin)
2+
3+
package workloadidentity
4+
5+
import (
6+
"context"
7+
8+
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
9+
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
10+
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
11+
)
12+
13+
type WorkloadIdentityTokenProvider struct {
14+
tokenCredential azcore.TokenCredential
15+
}
16+
17+
func NewWorkloadIdentityTokenProvider() TokenProvider {
18+
cred, err := azidentity.NewDefaultAzureCredential(&azidentity.DefaultAzureCredentialOptions{})
19+
initError = err
20+
return WorkloadIdentityTokenProvider{tokenCredential: cred}
21+
}
22+
23+
func (c WorkloadIdentityTokenProvider) GetToken(scope string) (*Token, error) {
24+
if initError != nil {
25+
return nil, initError
26+
}
27+
28+
token, err := c.tokenCredential.GetToken(context.Background(), policy.TokenRequestOptions{
29+
Scopes: []string{scope},
30+
})
31+
if err != nil {
32+
return nil, err
33+
}
34+
35+
return &Token{AccessToken: token.Token, ExpiresOn: token.ExpiresOn}, nil
36+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//go:build darwin && !cgo
2+
3+
// Package workloadidentity
4+
// This file is used when the GOOS is darwin and CGO is not enabled.
5+
// It provides a no-op implementation of the WorkloadIdentityTokenProvider to allow goreleaser to build
6+
// a darwin binary on a linux machine.
7+
package workloadidentity
8+
9+
import (
10+
"errors"
11+
)
12+
13+
type WorkloadIdentityTokenProvider struct {
14+
}
15+
16+
const CGOError = "CGO is not enabled, cannot use workload identity token provider"
17+
18+
// Code that does not require CGO
19+
func NewWorkloadIdentityTokenProvider() TokenProvider {
20+
panic(CGOError)
21+
}
22+
23+
func (c WorkloadIdentityTokenProvider) GetToken(scope string) (*Token, error) {
24+
return nil, errors.New(CGOError)
25+
}

0 commit comments

Comments
 (0)