From 0c6801bd73628b6985b43698c906fb8b9da7f62c Mon Sep 17 00:00:00 2001 From: Nghia Tran Date: Tue, 14 May 2024 15:59:55 -0700 Subject: [PATCH 1/2] add ldflags Signed-off-by: Nghia Tran --- docs/resources/build.md | 1 + docs/resources/image.md | 1 + docs/resources/resolve.md | 1 + internal/provider/const.go | 2 ++ internal/provider/resource_ko_build.go | 24 +++++++++++++++++++++--- internal/provider/resource_ko_image.go | 7 +++++++ internal/provider/resource_ko_resolve.go | 7 +++++++ 7 files changed, 40 insertions(+), 3 deletions(-) diff --git a/docs/resources/build.md b/docs/resources/build.md index 63999773..cd1bb930 100644 --- a/docs/resources/build.md +++ b/docs/resources/build.md @@ -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 | [/[/]][,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). diff --git a/docs/resources/image.md b/docs/resources/image.md index 5e52cf7f..62d54c86 100644 --- a/docs/resources/image.md +++ b/docs/resources/image.md @@ -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 | [/[/]][,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). diff --git a/docs/resources/resolve.md b/docs/resources/resolve.md index 7e86e5be..76716f19 100644 --- a/docs/resources/resolve.md +++ b/docs/resources/resolve.md @@ -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 | [/[/]][,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. diff --git a/internal/provider/const.go b/internal/provider/const.go index 248a17f7..414719b8 100644 --- a/internal/provider/const.go +++ b/internal/provider/const.go @@ -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 { diff --git a/internal/provider/resource_ko_build.go b/internal/provider/resource_ko_build.go index 38a3ada2..6c07dd34 100644 --- a/internal/provider/resource_ko_build.go +++ b/internal/provider/resource_ko_build.go @@ -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. + }, }, } } @@ -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 ( @@ -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)) @@ -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{})), } } @@ -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 { diff --git a/internal/provider/resource_ko_image.go b/internal/provider/resource_ko_image.go index 79174a61..d120f29c 100644 --- a/internal/provider/resource_ko_image.go +++ b/internal/provider/resource_ko_image.go @@ -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. + }, }, } } diff --git a/internal/provider/resource_ko_resolve.go b/internal/provider/resource_ko_resolve.go index c6d85851..f46989ca 100644 --- a/internal/provider/resource_ko_resolve.go +++ b/internal/provider/resource_ko_resolve.go @@ -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. + }, }, } } From 663d82522456b1c17a074a445d2f6f52b9a33781 Mon Sep 17 00:00:00 2001 From: Nghia Tran Date: Tue, 14 May 2024 17:43:48 -0700 Subject: [PATCH 2/2] add test coverage Signed-off-by: Nghia Tran --- internal/provider/resource_ko_build_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/provider/resource_ko_build_test.go b/internal/provider/resource_ko_build_test.go index b948846a..4fe22980 100644 --- a/internal/provider/resource_ko_build_test.go +++ b/internal/provider/resource_ko_build_test.go @@ -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( @@ -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(