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

Add ldflags support #153

Merged
merged 2 commits into from
May 15, 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
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
- `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.
- `sbom` (String) The SBOM media type to use (none will disable SBOM synthesis and upload, also supports: spdx, cyclonedx, go.version-m).
Expand Down
1 change: 1 addition & 0 deletions docs/resources/image.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ resource "ko_image" "example" {
### Optional

- `base_image` (String) base image to use
- `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.
- `sbom` (String) The SBOM media type to use (none will disable SBOM synthesis and upload, also supports: spdx, cyclonedx, go.version-m).
Expand Down
1 change: 1 addition & 0 deletions docs/resources/resolve.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ output "manifests" {
### Optional

- `base_image` (String)
- `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]*
- `push` (Boolean) Push images to KO_DOCKER_REPO
- `recursive` (Boolean) Process the directory used in -f, --filename recursively. Useful when you want to manage related manifests organized within the same directory.
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const (
ManifestsKey = "manifests"
// RepoKey is used for common "repo" resource attribute
RepoKey = "repo"
// Ldflags is used for common "ldflags" resource attribute
LdflagsKey = "ldflags"
)

func StringSlice(in []interface{}) []string {
Expand Down
24 changes: 21 additions & 3 deletions internal/provider/resource_ko_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ func resourceBuild() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
LdflagsKey: {
Description: "Extra ldflags 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 @@ -119,7 +126,8 @@ type buildOptions struct {
baseImage string
sbom string
auth *authn.Basic
bare bool // If true, use the "bare" namer that doesn't append the importpath.
bare bool // If true, use the "bare" namer that doesn't append the importpath.
ldflags []string // Extra ldflags to pass to the go build.
}

var (
Expand Down Expand Up @@ -172,6 +180,12 @@ 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 @@ -277,11 +291,12 @@ func fromData(d *schema.ResourceData, po *Opts) buildOptions {
ip: d.Get("importpath").(string),
workingDir: d.Get("working_dir").(string),
imageRepo: repo,
platforms: toStringSlice(d.Get("platforms").([]interface{})),
platforms: defaultPlatform(toStringSlice(d.Get("platforms").([]interface{}))),
baseImage: getString(d, BaseImageKey, po.bo.BaseImage),
sbom: d.Get("sbom").(string),
auth: po.auth,
bare: bare,
ldflags: toStringSlice(d.Get("ldflags").([]interface{})),
}
}

Expand All @@ -292,11 +307,14 @@ func getString(d *schema.ResourceData, key string, defaultVal string) string {
return defaultVal
}

func toStringSlice(in []interface{}) []string {
func defaultPlatform(in []string) []string {
if len(in) == 0 {
return []string{"linux/amd64"}
}
return in
}

func toStringSlice(in []interface{}) []string {
out := make([]string, len(in))
for i, ii := range in {
if s, ok := ii.(string); ok {
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/resource_ko_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func TestAccResourceKoBuild(t *testing.T) {
resource "ko_build" "foo" {
importpath = "github.com/ko-build/terraform-provider-ko/cmd/test"
platforms = ["all"]
ldflags = ["-s", "-w"]
}
`,
Check: resource.ComposeTestCheckFunc(
Expand Down Expand Up @@ -209,6 +210,7 @@ func TestAccResourceKoBuild_ProviderRepo(t *testing.T) {
Config: `
resource "ko_build" "foo" {
importpath = "github.com/ko-build/terraform-provider-ko/cmd/test"
ldflags = ["-s", "-w"]
}
`,
Check: resource.ComposeTestCheckFunc(
Expand Down
7 changes: 7 additions & 0 deletions internal/provider/resource_ko_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ func resourceImage() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
LdflagsKey: {
Description: "Extra ldflags 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 Down
7 changes: 7 additions & 0 deletions internal/provider/resource_ko_resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ func resolveConfig() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
LdflagsKey: {
Description: "Extra ldflags 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 Down
Loading