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

kubernetes_server_version data source #2306

Merged
merged 9 commits into from
Aug 9, 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
3 changes: 3 additions & 0 deletions .changelog/2306.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:feature
New data source: `kubernetes_server_version`
```
29 changes: 29 additions & 0 deletions docs/data-sources/server_version.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
subcategory: "core/v1"
page_title: "Kubernetes: kubernetes_server_version"
description: |-
Retrieves and parses the server's version (git version).
---

# kubernetes_server_version

This data source reads the versioning information of the server and makes specific attributes available to Terraform. Read more at [version info reference](https://pkg.go.dev/k8s.io/apimachinery/pkg/version#Info)

<!-- schema generated by tfplugindocs -->
## Schema

### Read-Only

- `build_date` (String) Kubernetes server build date
- `compiler` (String) Compiler used to build Kubernetes
- `git_commit` (String) Git commit SHA
- `git_tree_state` (String) Git commit tree state
- `git_version` (String) Composite version and git commit sha
- `go_version` (String) Go compiler version
- `id` (String) The ID of this resource.
- `major` (String) Major Kubernetes version
- `minor` (String) Minor Kubernetes version
- `platform` (String) Platform
- `version` (String) Composite Kubernetes server version


99 changes: 99 additions & 0 deletions kubernetes/data_source_kubernetes_server_version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package kubernetes

import (
"context"

gversion "github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceKubernetesServerVersion() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceKubernetesServerVersionRead,
Description: "This data source reads the versioning information of the server and makes specific attributes available to Terraform. Read more at [version info reference](https://pkg.go.dev/k8s.io/apimachinery/pkg/version#Info)",
Schema: map[string]*schema.Schema{
"version": {
Type: schema.TypeString,
Description: "Composite Kubernetes server version",
Computed: true,
},
"build_date": {
Type: schema.TypeString,
Description: "Kubernetes server build date",
Computed: true,
},
"compiler": {
Type: schema.TypeString,
Description: "Compiler used to build Kubernetes",
Computed: true,
},
"git_commit": {
Type: schema.TypeString,
Description: "Git commit SHA",
Computed: true,
},
"git_tree_state": {
Type: schema.TypeString,
Description: "Git commit tree state",
Computed: true,
},
"git_version": {
Type: schema.TypeString,
Description: "Composite version and git commit sha",
Computed: true,
},
"major": {
Type: schema.TypeString,
Description: "Major Kubernetes version",
Computed: true,
},
"minor": {
Type: schema.TypeString,
Description: "Minor Kubernetes version",
Computed: true,
},
"platform": {
Type: schema.TypeString,
Description: "Platform",
Computed: true,
},
"go_version": {
Type: schema.TypeString,
Description: "Go compiler version",
Computed: true,
},
},
}
}

func dataSourceKubernetesServerVersionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn, err := meta.(KubeClientsets).MainClientset()
if err != nil {
return diag.FromErr(err)
}
sv, err := conn.ServerVersion()
if err != nil {
return diag.FromErr(err)
}

gv, err := gversion.NewVersion(sv.String())
if err != nil {
return diag.FromErr(err)
}
d.SetId(gv.String())
d.Set("version", gv.String())
daemon4d-us marked this conversation as resolved.
Show resolved Hide resolved
d.Set("build_date", sv.BuildDate)
d.Set("compiler", sv.Compiler)
d.Set("git_commit", sv.GitCommit)
d.Set("git_tree_state", sv.GitTreeState)
d.Set("git_version", sv.GitVersion)
d.Set("go_version", sv.GoVersion)
d.Set("major", sv.Major)
d.Set("minor", sv.Minor)
d.Set("platform", sv.Platform)
return nil
}
61 changes: 61 additions & 0 deletions kubernetes/data_source_kubernetes_server_version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package kubernetes

import (
"fmt"
"testing"

gversion "github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccKubernetesDataSourceServerVersion_basic(t *testing.T) {
dataSourceName := "data.kubernetes_server_version.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccKubernetesDataSourceServerVersionConfig_basic(),
Check: func(st *terraform.State) error {
meta := testAccProvider.Meta()
if meta == nil {
return fmt.Errorf("Provider not initialized, unable to check cluster version")
}
conn, err := meta.(KubeClientsets).MainClientset()
if err != nil {
return err
}
ver, err := conn.ServerVersion()
if err != nil {
return err
}
gver, err := gversion.NewVersion(ver.String())
if err != nil {
return err
}
return resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "version", gver.String()),
resource.TestCheckResourceAttr(dataSourceName, "build_date", ver.BuildDate),
resource.TestCheckResourceAttr(dataSourceName, "compiler", ver.Compiler),
resource.TestCheckResourceAttr(dataSourceName, "git_commit", ver.GitCommit),
resource.TestCheckResourceAttr(dataSourceName, "git_tree_state", ver.GitTreeState),
resource.TestCheckResourceAttr(dataSourceName, "git_version", ver.GitVersion),
resource.TestCheckResourceAttr(dataSourceName, "major", ver.Major),
resource.TestCheckResourceAttr(dataSourceName, "minor", ver.Minor),
resource.TestCheckResourceAttr(dataSourceName, "platform", ver.Platform),
resource.TestCheckResourceAttr(dataSourceName, "go_version", ver.GoVersion),
)(st)
},
},
},
})
}

func testAccKubernetesDataSourceServerVersionConfig_basic() string {
return `data "kubernetes_server_version" "test" {}`
}
1 change: 1 addition & 0 deletions kubernetes/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ func Provider() *schema.Provider {
"kubernetes_persistent_volume_claim": dataSourceKubernetesPersistentVolumeClaimV1(),
"kubernetes_persistent_volume_claim_v1": dataSourceKubernetesPersistentVolumeClaimV1(),
"kubernetes_nodes": dataSourceKubernetesNodes(),
"kubernetes_server_version": dataSourceKubernetesServerVersion(),

// networking
"kubernetes_ingress": dataSourceKubernetesIngress(),
Expand Down
12 changes: 12 additions & 0 deletions templates/data-sources/server_version.md.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
subcategory: "core/v1"
page_title: "Kubernetes: kubernetes_server_version"
description: |-
Retrieves and parses the server's version (git version).
---

# {{ .Name }}

{{ .Description }}

{{ .SchemaMarkdown }}
Loading