forked from kubermatic/terraform-provider-kubermatic
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
168 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
page_title: "MetaKube: metakube_k8s_version" | ||
--- | ||
|
||
# metakube_k8s_version | ||
|
||
Get the latest supported kubernetes version matching `major` & `minor`. | ||
|
||
## Example Usage | ||
|
||
Get the latest supported patch of Kubernetes v1.21.x: | ||
|
||
```hcl | ||
data "metakube_k8s_version" "example" { | ||
major = "1" | ||
minor = "21" | ||
} | ||
resource "metakube_cluster" "foo" { | ||
# ... | ||
spec { | ||
version = data.metakube_k8s_version.example.version | ||
# ... | ||
} | ||
# ... | ||
} | ||
``` | ||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `major` - (Optional) Major version, defaults to the latest available. | ||
* `minor` - (Optional) Minor version, cannot be specified without `major`, defaults to the latest available. | ||
|
||
## Attributes Reference | ||
|
||
The only attribute exported is: | ||
* `version`: The latest Kubernetes version supported by MetaKube matching `major` and `minor`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package metakube | ||
|
||
import ( | ||
"context" | ||
"golang.org/x/mod/semver" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
||
"github.com/syseleven/go-metakube/client/versions" | ||
) | ||
|
||
func dataSourceMetakubeK8sClusterVersion() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourceMetakubeK8sClusterVersionRead, | ||
Schema: map[string]*schema.Schema{ | ||
"major": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "Kubernetes cluster major version", | ||
}, | ||
"minor": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
RequiredWith: []string{"major"}, | ||
Description: "Kubernetes cluster minor version", | ||
}, | ||
"version": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "The latest version of kubernetes cluster that satisfies specification and supported by MetaKube", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceMetakubeK8sClusterVersionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
k := meta.(*metakubeProviderMeta) | ||
|
||
partialVersionSpec := "" | ||
if v, ok := d.GetOk("major"); ok { | ||
partialVersionSpec = v.(string) | ||
} | ||
if v, ok := d.GetOk("minor"); ok { | ||
partialVersionSpec += "." + v.(string) | ||
} | ||
|
||
p := versions.NewGetMasterVersionsParams().WithContext(ctx) | ||
r, err := k.client.Versions.GetMasterVersions(p, k.auth) | ||
if err != nil { | ||
return diag.Errorf("%s", stringifyResponseError(err)) | ||
} | ||
|
||
var all []string | ||
for _, item := range r.Payload { | ||
if item != nil { | ||
all = append(all, item.Version.(string)) | ||
} | ||
} | ||
|
||
var available []string | ||
for _, v := range all { | ||
if strings.Index(v, partialVersionSpec) == 0 { | ||
available = append(available, v) | ||
} | ||
} | ||
|
||
if len(available) == 0 { | ||
return diag.Errorf("found following versions but did not match specification: %s", strings.Join(all, " ")) | ||
} | ||
|
||
latest := available[0] | ||
for _, v := range available { | ||
if semver.Compare("v"+v, "v"+latest) > 0 { | ||
latest = v | ||
} | ||
} | ||
|
||
d.SetId(latest) | ||
d.Set("version", latest) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters