Skip to content

Commit

Permalink
Add MKS kubeconfig data source
Browse files Browse the repository at this point in the history
  • Loading branch information
dkder3k committed Dec 7, 2021
1 parent 0a3a4ce commit 08bb77f
Show file tree
Hide file tree
Showing 12 changed files with 407 additions and 55 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ require (
github.com/selectel/dbaas-go v0.4.0
github.com/selectel/domains-go v0.3.0
github.com/selectel/go-selvpcclient v1.12.0
github.com/selectel/mks-go v0.6.0
github.com/selectel/mks-go v0.9.0
github.com/stretchr/testify v1.7.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ github.com/selectel/go-selvpcclient v1.12.0 h1:LsT074HOVF1dWYapsAWjaaJDQhmDPpcsV
github.com/selectel/go-selvpcclient v1.12.0/go.mod h1:HNteVXevZMjUCRR6lImTsGZZSTeKu89S/qbEDWDqmgc=
github.com/selectel/mks-go v0.6.0 h1:5ZlqvwlVrjrrbNygsiFfUGZTyTPzPGGo8aYJkbtrYV0=
github.com/selectel/mks-go v0.6.0/go.mod h1:OrlLnGes+HK7HNxUab/8Rll5iT0XQQnx4+dW1Ysph2o=
github.com/selectel/mks-go v0.9.0 h1:j0uvdUkbKtQmNWU5cjadsplPbCggWvTETAUHJQMWawU=
github.com/selectel/mks-go v0.9.0/go.mod h1:FcFqF3WvZIhztyAt1+ZySKf0zWmCEvg9e2gRwxVyQOw=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
Expand Down
101 changes: 101 additions & 0 deletions selectel/data_source_selectel_mks_kubeconfig_v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package selectel

import (
"context"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/selectel/go-selvpcclient/selvpcclient/resell/v2/tokens"
v1 "github.com/selectel/mks-go/pkg/v1"
"github.com/selectel/mks-go/pkg/v1/cluster"
)

func dataSourceMKSKubeconfigV1() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceMKSKubeconfigV1Read,
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"cluster_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"region": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
ru1Region,
ru2Region,
ru3Region,
ru7Region,
ru8Region,
ru9Region,
}, false),
},
"raw_config": {
Type: schema.TypeString,
Computed: true,
},
"server": {
Type: schema.TypeString,
Computed: true,
},
"cluster_ca_cert": {
Type: schema.TypeString,
Computed: true,
},
"client_cert": {
Type: schema.TypeString,
Computed: true,
},
"client_key": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceMKSKubeconfigV1Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
resellV2Client := config.resellV2Client()
tokenOpts := tokens.TokenOpts{
ProjectID: d.Get("project_id").(string),
}

log.Print(msgCreate(objectToken, tokenOpts))
token, _, err := tokens.Create(ctx, resellV2Client, tokenOpts)
if err != nil {
return diag.FromErr(errCreatingObject(objectToken, err))
}

region := d.Get("region").(string)
endpoint := getMKSClusterV1Endpoint(region)
mksClient := v1.NewMKSClientV1(token.ID, endpoint)

clusterID := d.Get("cluster_id").(string)

mksCluster, _, err := cluster.Get(ctx, mksClient, clusterID)
if err != nil {
return diag.FromErr(errGettingObject(objectCluster, clusterID, err))
}

parsedKubeconfig, _, err := cluster.GetParsedKubeconfig(ctx, mksClient, mksCluster.ID)
if err != nil {
return diag.FromErr(errGettingObject(objectKubeConfig, clusterID, err))
}

d.SetId(clusterID)
d.Set("raw_config", parsedKubeconfig.KubeconfigRaw)
d.Set("server", parsedKubeconfig.Server)
d.Set("cluster_ca_cert", parsedKubeconfig.ClusterCA)
d.Set("client_cert", parsedKubeconfig.ClientCert)
d.Set("client_key", parsedKubeconfig.ClientKey)
return nil
}
72 changes: 72 additions & 0 deletions selectel/data_source_selectel_mks_kubeconfig_v1_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package selectel

import (
"errors"
"fmt"
"testing"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccMKSKubeconfigV1DataSourceBasic(t *testing.T) {
projectName := acctest.RandomWithPrefix("tf-acc")
clusterName := acctest.RandomWithPrefix("tf-acc-cl")
kubeVersion := testAccMKSClusterV1GetDefaultKubeVersion(t)
maintenanceWindowStart := testAccMKSClusterV1GetMaintenanceWindowStart(12 * time.Hour)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccSelectelPreCheck(t) },
ProviderFactories: testAccProviders,
CheckDestroy: testAccCheckVPCV2ProjectDestroy,
Steps: []resource.TestStep{
{
Config: testAccMKSKubeconfigV1Basic(projectName, clusterName, kubeVersion, maintenanceWindowStart),
Check: resource.ComposeTestCheckFunc(
testAccCheckMKSKubeconfigV1("data.selectel_mks_kubeconfig_v1.kubeconfig_tf_acc_test_1"),
),
},
},
})
}

func testAccCheckMKSKubeconfigV1(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("can't find kubeconfig data source: %s", name)
}

if _, ok = rs.Primary.Attributes["raw_config"]; !ok {
return errors.New("empty 'raw_config' field in kubeconfigs data source")
}
if _, ok = rs.Primary.Attributes["server"]; !ok {
return errors.New("empty 'server' field in kubeconfigs data source")
}
if _, ok = rs.Primary.Attributes["cluster_ca_cert"]; !ok {
return errors.New("empty 'cluster_ca_cert' field in kubeconfigs data source")
}
if _, ok = rs.Primary.Attributes["client_cert"]; !ok {
return errors.New("empty 'client_cert' field in kubeconfigs data source")
}
if _, ok = rs.Primary.Attributes["client_key"]; !ok {
return errors.New("empty 'client_key' field in kubeconfigs data source")
}

return nil
}
}

func testAccMKSKubeconfigV1Basic(projectName, clusterName, kubeVersion, maintenanceWindowStart string) string {
return fmt.Sprintf(`
%s
data "selectel_mks_kubeconfig_v1" "kubeconfig_tf_acc_test_1" {
cluster_id = "${selectel_mks_cluster_v1.cluster_tf_acc_test_1.id}"
project_id = "${selectel_vpc_project_v2.project_tf_acc_test_1.id}"
region = "ru-3"
}
`, testAccMKSClusterV1Basic(projectName, clusterName, kubeVersion, maintenanceWindowStart))
}
2 changes: 2 additions & 0 deletions selectel/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
objectUser = "user"
objectVRRPSubnet = "VRRP subnet"
objectCluster = "cluster"
objectKubeConfig = "kubeconfig"
objectNodegroup = "nodegroup"
objectDomain = "domain"
objectRecord = "record"
Expand Down Expand Up @@ -73,6 +74,7 @@ func Provider() *schema.Provider {
"selectel_dbaas_flavor_v1": dataSourceDBaaSFlavorV1(),
"selectel_dbaas_configuration_parameter_v1": dataSourceDBaaSConfigurationParameterV1(),
"selectel_dbaas_prometheus_metric_token_v1": dataSourceDBaaSPrometheusMetricTokenV1(),
"selectel_mks_kubeconfig_v1": dataSourceMKSKubeconfigV1(),
},
ResourcesMap: map[string]*schema.Resource{
"selectel_vpc_floatingip_v2": resourceVPCFloatingIPV2(),
Expand Down
45 changes: 32 additions & 13 deletions vendor/github.com/selectel/mks-go/pkg/v1/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion vendor/github.com/selectel/mks-go/pkg/v1/cluster/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions vendor/github.com/selectel/mks-go/pkg/v1/cluster/requests.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 08bb77f

Please sign in to comment.