-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MKS kubeconfig data source (#178)
* Add MKS kubeconfig data source For #145 * Fix due to recommendations * Use sensitive fields in mks kubeconfig data source
- Loading branch information
Showing
12 changed files
with
375 additions
and
42 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
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,107 @@ | ||
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, | ||
Sensitive: true, | ||
}, | ||
"server": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Sensitive: true, | ||
}, | ||
"cluster_ca_cert": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Sensitive: true, | ||
}, | ||
"client_cert": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Sensitive: true, | ||
}, | ||
"client_key": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Sensitive: 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 | ||
} |
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,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)) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
55 changes: 55 additions & 0 deletions
55
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.
Oops, something went wrong.
Oops, something went wrong.