Skip to content

Commit

Permalink
Add new fields to release metadata, fix issue with test suite (#1380)
Browse files Browse the repository at this point in the history
* extend helm_release.metadata with new computed fields

- last_deployed to implement #1374
- first_deployed and notes to extend functionality further

* add tests, fix uses of helm CLI in tests

tests that are using exec.Command to call helm CLI directly were expecting kubeconfig to be at KUBECONFIG, which may not be the same as KUBE_CONFIG_PATH

---------

Co-authored-by: Alex Pilon <[email protected]>
  • Loading branch information
danielskowronski and appilon authored Jun 11, 2024
1 parent c46979c commit 50ebe21
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .changelog/1380.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
`helm_release`: add new attributes metadata.last_deployed, metadata.first_deployed, metadata.notes
```
5 changes: 4 additions & 1 deletion docs/resources/release.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ Read-Only:
- `chart` (String)
- `name` (String)
- `namespace` (String)
- `revision` (Number)
- `first_deployed` (Number) timestamp when the release was first deployed
- `last_deployed` (Number) timestamp when the release was last deployed
- `revision` (Number) version of the release last deployed
- `notes` (String) rendered templates/NOTES.txt if available
- `values` (String)
- `version` (String)

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
require (
github.com/hashicorp/go-cty v1.4.1-0.20200723130312-85980079f637
github.com/hashicorp/terraform-plugin-docs v0.16.0
github.com/hashicorp/terraform-plugin-go v0.23.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0
github.com/hashicorp/terraform-plugin-testing v1.8.0
github.com/mitchellh/go-homedir v1.1.0
Expand Down Expand Up @@ -87,7 +88,6 @@ require (
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-exec v0.21.0 // indirect
github.com/hashicorp/terraform-json v0.22.1 // indirect
github.com/hashicorp/terraform-plugin-go v0.23.0 // indirect
github.com/hashicorp/terraform-plugin-log v0.9.0 // indirect
github.com/hashicorp/terraform-registry-address v0.2.3 // indirect
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions helm/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func buildChartRepository() {

// package all the charts
for _, c := range charts {
cmd := exec.Command("helm", "package", "-u",
cmd := exec.Command("helm", "--kubeconfig", os.Getenv("KUBE_CONFIG_PATH"), "package", "-u",
filepath.Join(testChartsPath, c.Name()),
"-d", testRepositoryDir)
out, err := cmd.CombinedOutput()
Expand All @@ -151,7 +151,7 @@ func buildChartRepository() {
}

// build the repository index
cmd := exec.Command("helm", "repo", "index", testRepositoryDir)
cmd := exec.Command("helm", "--kubeconfig", os.Getenv("KUBE_CONFIG_PATH"), "repo", "index", testRepositoryDir)
out, err := cmd.CombinedOutput()
if err != nil {
log.Println(string(out))
Expand Down
32 changes: 25 additions & 7 deletions helm/resource_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,21 @@ func resourceRelease() *schema.Resource {
Computed: true,
Description: "The version number of the application being deployed.",
},
"first_deployed": {
Type: schema.TypeInt,
Computed: true,
Description: "FirstDeployed is an int32 which represents timestamp when the release was first deployed.",
},
"last_deployed": {
Type: schema.TypeInt,
Computed: true,
Description: "LastDeployed is an int32 which represents timestamp when the release was last deployed.",
},
"notes": {
Type: schema.TypeString,
Computed: true,
Description: "Contains the rendered templates/NOTES.txt if available",
},
"values": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -1081,13 +1096,16 @@ func setReleaseAttributes(d *schema.ResourceData, r *release.Release, meta inter
}

return d.Set("metadata", []map[string]interface{}{{
"name": r.Name,
"revision": r.Version,
"namespace": r.Namespace,
"chart": r.Chart.Metadata.Name,
"version": r.Chart.Metadata.Version,
"app_version": r.Chart.Metadata.AppVersion,
"values": values,
"name": r.Name,
"revision": r.Version,
"namespace": r.Namespace,
"chart": r.Chart.Metadata.Name,
"version": r.Chart.Metadata.Version,
"app_version": r.Chart.Metadata.AppVersion,
"first_deployed": r.Info.FirstDeployed.Time.Unix(),
"last_deployed": r.Info.LastDeployed.Time.Unix(),
"notes": r.Info.Notes,
"values": values,
}})
}

Expand Down
12 changes: 9 additions & 3 deletions helm/resource_release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func TestAccResourceRelease_basic(t *testing.T) {
resource.TestCheckResourceAttr("helm_release.test", "metadata.0.chart", "test-chart"),
resource.TestCheckResourceAttr("helm_release.test", "metadata.0.version", "1.2.3"),
resource.TestCheckResourceAttr("helm_release.test", "metadata.0.app_version", "1.19.5"),
resource.TestMatchResourceAttr("helm_release.test", "metadata.0.first_deployed", regexp.MustCompile("[0-9]+")),
resource.TestMatchResourceAttr("helm_release.test", "metadata.0.last_deployed", regexp.MustCompile("[0-9]+")),
resource.TestMatchResourceAttr("helm_release.test", "metadata.0.notes", regexp.MustCompile(`^1. Get the application URL by running these commands:\n export POD_NAME=.*`)),
),
},
{
Expand Down Expand Up @@ -99,6 +102,9 @@ func TestAccResourceRelease_emptyVersion(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "metadata.0.chart", "test-chart"),
resource.TestCheckResourceAttr(resourceName, "metadata.0.version", "2.0.0"),
resource.TestCheckResourceAttr(resourceName, "metadata.0.app_version", "1.19.5"),
resource.TestMatchResourceAttr("helm_release.test", "metadata.0.first_deployed", regexp.MustCompile("[0-9]+")),
resource.TestMatchResourceAttr("helm_release.test", "metadata.0.last_deployed", regexp.MustCompile("[0-9]+")),
resource.TestMatchResourceAttr("helm_release.test", "metadata.0.notes", regexp.MustCompile(`^1. Get the application URL by running these commands:\n export POD_NAME=.*`)),
),
},
},
Expand Down Expand Up @@ -1504,7 +1510,7 @@ func TestAccResourceRelease_helm_repo_add(t *testing.T) {
defer deleteNamespace(t, namespace)

// add the repository with `helm repo add`
cmd := exec.Command("helm", "repo", "add", "hashicorp-test", testRepositoryURL)
cmd := exec.Command("helm", "--kubeconfig", os.Getenv("KUBE_CONFIG_PATH"), "repo", "add", "hashicorp-test", testRepositoryURL)
out, err := cmd.CombinedOutput()
t.Log(string(out))
if err != nil {
Expand Down Expand Up @@ -1555,7 +1561,7 @@ func TestAccResourceRelease_delete_regression(t *testing.T) {
{
PreConfig: func() {
// delete the release outside of terraform
cmd := exec.Command("helm", "delete", "--namespace", namespace, name)
cmd := exec.Command("helm", "--kubeconfig", os.Getenv("KUBE_CONFIG_PATH"), "delete", "--namespace", namespace, name)
out, err := cmd.CombinedOutput()
t.Log(string(out))
if err != nil {
Expand All @@ -1572,7 +1578,7 @@ func TestAccResourceRelease_delete_regression(t *testing.T) {
}

func getReleaseJSONManifest(namespace, name string) (string, error) {
cmd := exec.Command("helm", "get", "manifest", "--namespace", namespace, name)
cmd := exec.Command("helm", "--kubeconfig", os.Getenv("KUBE_CONFIG_PATH"), "get", "manifest", "--namespace", namespace, name)
manifest, err := cmd.Output()
if err != nil {
return "", err
Expand Down

0 comments on commit 50ebe21

Please sign in to comment.