Skip to content

Commit

Permalink
feat: add provider config generation (#20)
Browse files Browse the repository at this point in the history
* feat: add provider config generation

* feat: add gpg figning to goreleaser
  • Loading branch information
eddycharly committed Nov 7, 2020
1 parent 7034eff commit 70ab6ce
Show file tree
Hide file tree
Showing 15 changed files with 179 additions and 54 deletions.
17 changes: 11 additions & 6 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,24 @@ jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Unshallow
run: git fetch --prune --unshallow
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.13
- name: Checkout code
uses: actions/checkout@v2
- uses: actions/cache@v2.1.2
with:
path: /home/runner/go/pkg/mod
key: go-mod
- name: Import GPG key
id: import_gpg
uses: paultyng/ghaction-import-gpg@v2.1.0
env:
GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }}
PASSPHRASE: ${{ secrets.PASSPHRASE }}
- name: Create release
uses: goreleaser/goreleaser-action@v2.2.1
env:
GPG_FINGERPRINT: ${{ steps.import_gpg.outputs.fingerprint }}
GITHUB_TOKEN: ${{ secrets.GORELEASER_TOKEN }}
with:
version: latest
Expand Down
18 changes: 13 additions & 5 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,17 @@ archives:
checksum:
name_template: '{{ .ProjectName }}_{{ .Version }}_SHA256SUMS'
algorithm: sha256
signs:
- artifacts: checksum
args:
# if you are using this is a GitHub action or some other automated pipeline, you
# need to pass the batch flag to indicate its not interactive.
- "--batch"
- "--local-user"
- "{{ .Env.GPG_FINGERPRINT }}" # set this environment variable for your signing key
- "--output"
- "${signature}"
- "--detach-sign"
- "${artifact}"
changelog:
sort: asc
filters:
exclude:
- "^docs:"
- "^test:"
skip: true
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ terraform {
provider "kops" {
state_store = "s3://cluster.example.com"
// optionally use an AWS profile
aws_profile = "example_profile"
// optionally set up your cloud provider access config
aws {
profile = "example_profile"
}
}
```

Expand All @@ -104,7 +106,7 @@ provider "kops" {
```hcl
resource "kops_cluster" "cluster" {
name = "cluster.example.com"
admin_ssh_key = file("path_to_ssh public key file")
admin_ssh_key = file("path to ssh public key file")
cloud_provider = "aws"
kubernetes_version = "stable"
dns_zone = "example.com"
Expand Down
5 changes: 5 additions & 0 deletions docs/AwsConfig.generated.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# kops_aws_config

| attribute | type | optional | required | computed |
| --- | --- | --- | --- | --- |
| `profile` | String | | :white_check_mark: | |
6 changes: 6 additions & 0 deletions docs/ProviderConfig.generated.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# kops_provider_config

| attribute | type | optional | required | computed |
| --- | --- | --- | --- | --- |
| `state_store` | String | :white_check_mark: | | |
| `aws` | [AwsConfig](./AwsConfig.generated.md) | | :white_check_mark: | |
5 changes: 4 additions & 1 deletion examples/aws-profile/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ terraform {

provider "kops" {
state_store = "s3://cluster.example.com"
aws_profile = "profile"

aws {
profile = "profile"
}
}
4 changes: 4 additions & 0 deletions hack/gen-structures/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,10 @@ func build(i interface{}, o ...func(o options) options) {
}

func main() {
build(api.ProviderConfig{},
required("StateStore"),
)
build(api.AwsConfig{})
build(api.Cluster{},
required("Name", "AdminSshKey", "CloudProvider", "Subnet", "NetworkID", "Topology", "EtcdCluster", "Networking", "InstanceGroup"),
computed("MasterPublicName", "MasterInternalName", "ConfigBase", "NetworkCIDR", "NonMasqueradeCIDR", "IAM"),
Expand Down
5 changes: 5 additions & 0 deletions pkg/api/AwsConfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package api

type AwsConfig struct {
Profile string
}
6 changes: 6 additions & 0 deletions pkg/api/ProviderConfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package api

type ProviderConfig struct {
StateStore string
Aws *AwsConfig
}
39 changes: 13 additions & 26 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"

"github.com/eddycharly/terraform-provider-kops/pkg/structures"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/kops/pkg/client/simple"
Expand All @@ -18,38 +19,24 @@ A valid value follows the format s3://<bucket>.
Trailing slash will be trimmed.`
)

type config struct {
stateStore string
clientset simple.Clientset
}

func ConfigureProvider(data *schema.ResourceData) (interface{}, error) {
profile := data.Get("aws_profile").(string)

if profile != "" {
os.Setenv("AWS_SDK_LOAD_CONFIG", "1")
os.Setenv("AWS_PROFILE", profile)
func ConfigureProvider(d *schema.ResourceData) (interface{}, error) {
providerConfig := structures.ExpandProviderConfig(d.Get("").(map[string]interface{}))
if providerConfig.Aws != nil {
if providerConfig.Aws.Profile != "" {
os.Setenv("AWS_SDK_LOAD_CONFIG", "1")
os.Setenv("AWS_PROFILE", providerConfig.Aws.Profile)
}
}

registryPath := data.Get("state_store").(string)

basePath, err := vfs.Context.BuildVfsPath(registryPath)
basePath, err := vfs.Context.BuildVfsPath(providerConfig.StateStore)
if err != nil {
return nil, fmt.Errorf("error building path for %q: %v", registryPath, err)
return nil, fmt.Errorf("error building path for %q: %v", providerConfig.StateStore, err)
}

if !vfs.IsClusterReadable(basePath) {
return nil, field.Invalid(field.NewPath("State Store"), registryPath, invalidStateError)
return nil, field.Invalid(field.NewPath("State Store"), providerConfig.StateStore, invalidStateError)
}

clientset := vfsclientset.NewVFSClientset(basePath)

return &config{
clientset: clientset,
stateStore: registryPath,
}, nil
return vfsclientset.NewVFSClientset(basePath), nil
}

func Clientset(in interface{}) simple.Clientset {
return in.(*config).clientset
return in.(simple.Clientset)
}
15 changes: 2 additions & 13 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,13 @@ import (
"github.com/eddycharly/terraform-provider-kops/pkg/config"
"github.com/eddycharly/terraform-provider-kops/pkg/datasources"
"github.com/eddycharly/terraform-provider-kops/pkg/resources"
"github.com/eddycharly/terraform-provider-kops/pkg/schemas"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func NewProvider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"state_store": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("KOPS_STATE_STORE", nil),
Description: "Location of state storage.",
},
"aws_profile": {
Type: schema.TypeString,
Optional: true,
Description: "AWS profile.",
},
},
Schema: schemas.ProviderConfig().Schema,
DataSourcesMap: map[string]*schema.Resource{
"kops_cluster": datasources.Cluster(),
},
Expand Down
13 changes: 13 additions & 0 deletions pkg/schemas/AwsConfig.generated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package schemas

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func AwsConfig() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"profile": OptionalString(),
},
}
}
14 changes: 14 additions & 0 deletions pkg/schemas/ProviderConfig.generated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package schemas

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func ProviderConfig() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"state_store": RequiredString(),
"aws": OptionalStruct(AwsConfig()),
},
}
}
24 changes: 24 additions & 0 deletions pkg/structures/AwsConfig.generated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package structures

import (
"github.com/eddycharly/terraform-provider-kops/pkg/api"
)

func ExpandAwsConfig(in map[string]interface{}) api.AwsConfig {
if in == nil {
panic("expand AwsConfig failure, in is nil")
}
return api.AwsConfig{
Profile: func(in interface{}) string {
return string(ExpandString(in))
}(in["profile"]),
}
}

func FlattenAwsConfig(in api.AwsConfig) map[string]interface{} {
return map[string]interface{}{
"profile": func(in string) interface{} {
return FlattenString(string(in))
}(in.Profile),
}
}
54 changes: 54 additions & 0 deletions pkg/structures/ProviderConfig.generated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package structures

import (
"github.com/eddycharly/terraform-provider-kops/pkg/api"
)

func ExpandProviderConfig(in map[string]interface{}) api.ProviderConfig {
if in == nil {
panic("expand ProviderConfig failure, in is nil")
}
return api.ProviderConfig{
StateStore: func(in interface{}) string {
return string(ExpandString(in))
}(in["state_store"]),
Aws: func(in interface{}) *api.AwsConfig {
return func(in interface{}) *api.AwsConfig {
if in == nil {
return nil
}
if _, ok := in.([]interface{}); ok && len(in.([]interface{})) == 0 {
return nil
}
return func(in api.AwsConfig) *api.AwsConfig {
return &in
}(func(in interface{}) api.AwsConfig {
if len(in.([]interface{})) == 0 || in.([]interface{})[0] == nil {
return api.AwsConfig{}
}
return (ExpandAwsConfig(in.([]interface{})[0].(map[string]interface{})))
}(in))
}(in)
}(in["aws"]),
}
}

func FlattenProviderConfig(in api.ProviderConfig) map[string]interface{} {
return map[string]interface{}{
"state_store": func(in string) interface{} {
return FlattenString(string(in))
}(in.StateStore),
"aws": func(in *api.AwsConfig) interface{} {
return func(in *api.AwsConfig) interface{} {
if in == nil {
return nil
}
return func(in api.AwsConfig) interface{} {
return func(in api.AwsConfig) []map[string]interface{} {
return []map[string]interface{}{FlattenAwsConfig(in)}
}(in)
}(*in)
}(in)
}(in.Aws),
}
}

0 comments on commit 70ab6ce

Please sign in to comment.