Skip to content

Commit

Permalink
Merge pull request #89 from sl1pm4t/fix-emptydir-runasuser
Browse files Browse the repository at this point in the history
Fix runAsUser=0 and empty_dir missing
  • Loading branch information
sl1pm4t authored Oct 5, 2021
2 parents 8528ea5 + be0c138 commit 4f5fd47
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 4 deletions.
6 changes: 3 additions & 3 deletions hcl_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (w *ObjectWalker) closeBlock() *hclBlock {
w.dst.AppendBlock(current.hcl)

} else {
if current.hasValue || current.isRequired() {
if current.hasValue || tfkschema.IncludedOnZero(w.currentBlock.fieldName) || current.isRequired() {
if !includeUnsupported && current.unsupported {
// don't append this block or child blocks
w.warn().
Expand Down Expand Up @@ -351,7 +351,7 @@ func (w *ObjectWalker) Primitive(v reflect.Value) error {
if !w.ignoreSliceElems && v.CanAddr() && v.CanInterface() {
w.debug(fmt.Sprintf("Primitive: %s = %v (%T)", w.field().Name, v.Interface(), v.Interface()))

if !IsZero(v) {
if !IsZero(v) || tfkschema.IncludedOnZero(w.field().Name) {
w.currentBlock.hasValue = true
w.currentBlock.SetAttributeValue(
tfkschema.ToTerraformAttributeName(w.field(), w.currentBlock.FullSchemaName()),
Expand All @@ -363,7 +363,7 @@ func (w *ObjectWalker) Primitive(v reflect.Value) error {
}

// Map is called everytime reflectwalk enters a Map
// Golang maps are usally output as HCL maps, but sometimes as sub-blocks.
// Golang maps are usually output as HCL maps, but sometimes as sub-blocks.
func (w *ObjectWalker) Map(m reflect.Value) error {
blockName := tfkschema.ToTerraformSubBlockName(w.field(), w.currentBlock.FullSchemaName())
hcl := hclwrite.NewBlock(blockName, nil)
Expand Down
5 changes: 5 additions & 0 deletions hcl_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ func TestWriteObject(t *testing.T) {
"kubernetes_storage_class",
0,
},
{
"replicationController",
"kubernetes_replication_controller",
0,
},
}

for _, tt := range tests {
Expand Down
13 changes: 13 additions & 0 deletions pkg/tfkschema/attr_overrides.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package tfkschema

// IncludedOnZero checks the attribute name against a lookup table to determine if it can be included
// when it is zero / empty.
func IncludedOnZero(attrName string) bool {
switch attrName {
case "EmptyDir":
return true
case "RunAsUser":
return true
}
return false
}
1 change: 1 addition & 0 deletions test-fixtures/deployment.tf.golden
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ resource "kubernetes_deployment" "backend_api" {
add = ["NET_BIND_SERVICE"]
drop = ["ALL"]
}
run_as_user = 0
}
}
container {
Expand Down
1 change: 1 addition & 0 deletions test-fixtures/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ spec:
- ALL
add:
- NET_BIND_SERVICE
runAsUser: 0
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
Expand Down
3 changes: 2 additions & 1 deletion test-fixtures/podNodeExporter.tf.golden
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ resource "kubernetes_pod" "node_exporter_7fth_7" {
termination_message_policy = "File"
image_pull_policy = "Always"
security_context {
privileged = true
privileged = true
run_as_user = 0
}
}
restart_policy = "Always"
Expand Down
85 changes: 85 additions & 0 deletions test-fixtures/replicationController.tf.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
resource "kubernetes_replication_controller" "es" {
metadata {
name = "es"
labels = { component = "elasticsearch" }
}
spec {
replicas = 1
template {
metadata {
labels = { component = "elasticsearch" }
}
spec {
volume {
name = "storage"
empty_dir {
}
}
init_container {
name = "init-sysctl"
image = "busybox"
command = ["sysctl", "-w", "vm.max_map_count=262144"]
image_pull_policy = "IfNotPresent"
security_context {
privileged = true
}
}
container {
name = "es"
image = "quay.io/pires/docker-elasticsearch-kubernetes:5.6.2"
port {
name = "http"
container_port = 9200
protocol = "TCP"
}
port {
name = "transport"
container_port = 9300
protocol = "TCP"
}
env {
name = "KUBERNETES_CA_CERTIFICATE_FILE"
value = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
}
env {
name = "NAMESPACE"
value_from {
field_ref {
field_path = "metadata.namespace"
}
}
}
env {
name = "CLUSTER_NAME"
value = "myesdb"
}
env {
name = "DISCOVERY_SERVICE"
value = "elasticsearch"
}
env {
name = "NODE_MASTER"
value = "true"
}
env {
name = "NODE_DATA"
value = "true"
}
env {
name = "HTTP_ENABLE"
value = "true"
}
volume_mount {
name = "storage"
mount_path = "/data"
}
security_context {
capabilities {
add = ["IPC_LOCK"]
}
}
}
}
}
}
}
File renamed without changes.

0 comments on commit 4f5fd47

Please sign in to comment.