Skip to content

Commit 0d66ef9

Browse files
committed
Allow kustomization plugin to run in-cluster
Ensure that plugin can run in cluster
1 parent 1090145 commit 0d66ef9

File tree

4 files changed

+117
-8
lines changed

4 files changed

+117
-8
lines changed

.github/workflows/main.yml

+56-1
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,55 @@ jobs:
162162
- name: 'Terraform Apply'
163163
run: terraform apply --auto-approve
164164

165+
int_test_in_cluster:
166+
runs-on: ubuntu-latest
167+
needs: [compile_provider]
168+
169+
steps:
170+
- name: 'Checkout'
171+
uses: actions/checkout@v1
172+
173+
- name: 'Setup Kind'
174+
uses: engineerd/[email protected]
175+
with:
176+
version: "v0.11.0"
177+
178+
- name: 'Download terraform-plugins'
179+
uses: actions/download-artifact@v2
180+
with:
181+
name: terraform-plugins
182+
path: terraform.d/plugins
183+
184+
- name: Create terraform pod
185+
run: kubectl apply -f tests/in-cluster.yaml
186+
187+
- name: Wait for pod to be ready
188+
run: kubectl wait pod/terraform --for condition=Ready --timeout 60s
189+
190+
- name: 'Ensure provider is executable'
191+
run: chmod +x terraform.d/plugins/registry.terraform.io/kbst/kustomization/1.0.0/linux_amd64/terraform-provider-kustomization_v1.0.0
192+
193+
- name: Make provider directory
194+
run: kubectl exec terraform -- mkdir terraform.d
195+
196+
- name: Copy provider
197+
run: kubectl cp terraform.d/plugins terraform:terraform.d/plugins
198+
199+
- name: Copy test file
200+
run: kubectl cp test.tf terraform:test.tf
201+
202+
- name: Copy kustomize directory
203+
run: kubectl cp kustomize terraform:kustomize
204+
205+
- name: Create provider file
206+
run: kubectl exec -it terraform -- sh -c 'echo "provider \"kustomization\" {\n kubeconfig_incluster = true\n}" > provider.tf'
207+
208+
- name: 'Terraform Init'
209+
run: kubectl exec -it terraform -- terraform init
210+
211+
- name: 'Terraform Apply'
212+
run: kubectl exec -it terraform -- terraform apply --auto-approve
213+
165214
int_test_state_import:
166215
runs-on: ubuntu-latest
167216
needs: [compile_provider]
@@ -285,7 +334,13 @@ jobs:
285334
286335
goreleaser:
287336
runs-on: ubuntu-latest
288-
needs: [tf_tests, int_test_kubeconfig_path, int_test_kubeconfig_raw, int_test_state_import, int_test_kubestack_kind]
337+
needs:
338+
- tf_tests
339+
- int_test_kubeconfig_path
340+
- int_test_kubeconfig_raw
341+
- int_test_in_cluster
342+
- int_test_state_import
343+
- int_test_kubestack_kind
289344
if: startsWith(github.ref, 'refs/tags/v')
290345
steps:
291346
- name: Checkout

docs/index.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,24 @@ terraform {
2727
}
2828
2929
provider "kustomization" {
30-
# one of kubeconfig_path or kubeconfig_raw is required
30+
# one of kubeconfig_path, kubeconfig_raw or kubeconfig_incluster must be set
3131
3232
# kubeconfig_path = "~/.kube/config"
3333
# can also be set using KUBECONFIG_PATH environment variable
3434
3535
# kubeconfig_raw = data.template_file.kubeconfig.rendered
3636
# kubeconfig_raw = yamlencode(local.kubeconfig)
37+
38+
# kubeconfig_incluster = true
3739
}
3840
3941
```
4042

4143
## Argument Reference
4244

43-
- `kubeconfig_path` - (One of `kubeconfig_path` or `kubeconfig_raw` required) Path to a kubeconfig file. Can be set using `KUBECONFIG_PATH` environment variable.
44-
- `kubeconfig_raw` - (One of `kubeconfig_path` or `kubeconfig_raw` required) Raw kubeconfig file. If `kubeconfig_raw` is set, `kubeconfig_path` is ignored.
45+
- `kubeconfig_path` - Path to a kubeconfig file. Can be set using `KUBECONFIG_PATH` environment variable.
46+
- `kubeconfig_raw` - Raw kubeconfig file. If `kubeconfig_raw` is set, `kubeconfig_path` is ignored.
47+
- `kubeconfig_incluster` - Set to `true` when running inside a kubernetes cluster.
4548
- `context` - (Optional) Context to use in kubeconfig with multiple contexts, if not specified the default context is used.
4649

4750
## Imports

kustomize/provider.go

+18-4
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,20 @@ func Provider() *schema.Provider {
4747
Type: schema.TypeString,
4848
Optional: true,
4949
DefaultFunc: schema.EnvDefaultFunc("KUBECONFIG_PATH", nil),
50-
ExactlyOneOf: []string{"kubeconfig_path", "kubeconfig_raw"},
51-
Description: fmt.Sprintf("Path to a kubeconfig file. Can be set using KUBECONFIG_PATH env var. Either kubeconfig_path or kubeconfig_raw is required."),
50+
ExactlyOneOf: []string{"kubeconfig_path", "kubeconfig_raw", "kubeconfig_incluster"},
51+
Description: "Path to a kubeconfig file. Can be set using KUBECONFIG_PATH env var",
5252
},
5353
"kubeconfig_raw": {
5454
Type: schema.TypeString,
5555
Optional: true,
56-
ExactlyOneOf: []string{"kubeconfig_path", "kubeconfig_raw"},
57-
Description: "Raw kube config. If kubeconfig_raw is set, kubeconfig_path is ignored.",
56+
ExactlyOneOf: []string{"kubeconfig_path", "kubeconfig_raw", "kubeconfig_incluster"},
57+
Description: "Raw kube config. If kubeconfig_raw is set, KUBECONFIG_PATH is ignored.",
58+
},
59+
"kubeconfig_incluster": {
60+
Type: schema.TypeBool,
61+
Optional: true,
62+
ExactlyOneOf: []string{"kubeconfig_path", "kubeconfig_raw", "kubeconfig_incluster"},
63+
Description: "Set to true when running inside a kubernetes cluster. If kubeconfig_incluster is set, KUBECONFIG_PATH is ignored.",
5864
},
5965
"context": {
6066
Type: schema.TypeString,
@@ -71,6 +77,7 @@ func Provider() *schema.Provider {
7177

7278
raw := d.Get("kubeconfig_raw").(string)
7379
path := d.Get("kubeconfig_path").(string)
80+
incluster := d.Get("kubeconfig_incluster").(bool)
7481
context := d.Get("context").(string)
7582

7683
if raw != "" {
@@ -92,6 +99,13 @@ func Provider() *schema.Provider {
9299
}
93100
}
94101

102+
if incluster {
103+
config, err = rest.InClusterConfig()
104+
if err != nil {
105+
return nil, fmt.Errorf("provider kustomization: couldn't load in cluster config: %s", err)
106+
}
107+
}
108+
95109
// empty default config required to support
96110
// using a cluster resource or data source
97111
// that may not exist yet, to configure the provider

tests/in-cluster.yaml

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
apiVersion: v1
2+
kind: ServiceAccount
3+
metadata:
4+
name: terraform
5+
namespace: default
6+
---
7+
apiVersion: rbac.authorization.k8s.io/v1
8+
kind: ClusterRoleBinding
9+
metadata:
10+
name: terraform-admin
11+
roleRef:
12+
apiGroup: rbac.authorization.k8s.io
13+
kind: ClusterRole
14+
name: cluster-admin
15+
subjects:
16+
- apiGroup: ""
17+
kind: ServiceAccount
18+
name: terraform
19+
namespace: default
20+
---
21+
apiVersion: v1
22+
kind: Pod
23+
metadata:
24+
labels:
25+
run: terraform
26+
name: terraform
27+
namespace: default
28+
spec:
29+
containers:
30+
- command:
31+
- sleep
32+
- "100000"
33+
image: hashicorp/terraform:1.0.5
34+
imagePullPolicy: IfNotPresent
35+
name: terraform
36+
workingDir: /terraform
37+
serviceAccount: terraform

0 commit comments

Comments
 (0)