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

support env for CGO_ENABLED=1 #163

Merged
merged 4 commits into from
May 29, 2024
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ jobs:
fail-fast: false
matrix:
terraform:
- '1.5.*'
- '1.6.*'
- '1.7.*'
- '1.8.*'
steps:
- uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
- uses: actions/setup-go@cdcb36043654635271a94b9a6d1392de5bb323a7 # v5.0.1
Expand Down
5 changes: 5 additions & 0 deletions cmd/test-cgo/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

import "C"

func main() {}
1 change: 1 addition & 0 deletions docs/resources/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Sample resource in the Terraform provider scaffolding.
### Optional

- `base_image` (String) base image to use
- `env` (List of String) Extra environment variables to pass to the go build
- `ldflags` (List of String) Extra ldflags to pass to the go build
- `platforms` (List of String) Which platform to use when pulling a multi-platform base. Format: all | <os>[/<arch>[/<variant>]][,platform]*
- `repo` (String) Container repository to publish images to. If set, this overrides the provider's docker_repo, and the image name will be exactly the specified `repo`, without the importpath appended.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/ko-build/terraform-provider-ko

go 1.22
go 1.22.3

require (
github.com/aws/aws-lambda-go v1.47.0
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const (
RepoKey = "repo"
// Ldflags is used for common "ldflags" resource attribute
LdflagsKey = "ldflags"
// EnvKey is used for common "env" resource attribute
EnvKey = "env"
)

func StringSlice(in []interface{}) []string {
Expand Down
21 changes: 15 additions & 6 deletions internal/provider/resource_ko_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ func resourceBuild() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString},
ForceNew: true, // Any time this changes, don't try to update in-place, just create it.
},
EnvKey: {
Description: "Extra environment variables to pass to the go build",
Optional: true,
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
ForceNew: true, // Any time this changes, don't try to update in-place, just create it.
},
},
}
}
Expand All @@ -128,6 +135,7 @@ type buildOptions struct {
auth *authn.Basic
bare bool // If true, use the "bare" namer that doesn't append the importpath.
ldflags []string // Extra ldflags to pass to the go build.
env []string // Extra environment variables to pass to the go build.
}

var (
Expand All @@ -146,6 +154,11 @@ func (o *buildOptions) makeBuilder(ctx context.Context) (*build.Caching, error)
bo := []build.Option{
build.WithTrimpath(true),
build.WithPlatforms(o.platforms...),
build.WithConfig(map[string]build.Config{
o.ip: {
Ldflags: o.ldflags,
Env: o.env,
}}),
build.WithBaseImages(func(_ context.Context, _ string) (name.Reference, build.Result, error) {
ref, err := name.ParseReference(o.baseImage)
if err != nil {
Expand Down Expand Up @@ -180,12 +193,7 @@ func (o *buildOptions) makeBuilder(ctx context.Context) (*build.Caching, error)
return nil, nil, fmt.Errorf("unexpected base image media type: %s", desc.MediaType)
}),
}
if len(o.ldflags) > 0 {
bo = append(bo, build.WithConfig(map[string]build.Config{
o.ip: {
Ldflags: o.ldflags,
}}))
}

switch o.sbom {
case "spdx":
bo = append(bo, build.WithSPDX(version))
Expand Down Expand Up @@ -297,6 +305,7 @@ func fromData(d *schema.ResourceData, po *Opts) buildOptions {
auth: po.auth,
bare: bare,
ldflags: toStringSlice(d.Get("ldflags").([]interface{})),
env: toStringSlice(d.Get("env").([]interface{})),
}
}

Expand Down
16 changes: 16 additions & 0 deletions internal/provider/resource_ko_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,22 @@ func TestAccResourceKoBuild(t *testing.T) {
}},
})

resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Steps: []resource.TestStep{{
Config: `
resource "ko_build" "foo" {
importpath = "github.com/ko-build/terraform-provider-ko/cmd/test-cgo"
env = ["CGO_ENABLED=1"]
}
`,
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr("ko_build.foo", "image_ref",
regexp.MustCompile("^"+url+"/github.com/ko-build/terraform-provider-ko/cmd/test-cgo@sha256:")),
),
}},
})

for _, sbom := range []string{"spdx", "cyclonedx", "go.version-m", "none"} {
resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Expand Down
1 change: 1 addition & 0 deletions tools/tools.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build tools
// +build tools

package tools
Expand Down
Loading