Checks whether any kubernetes_network_policy does not target any pod. Validates that spec.pod_selector.match_labels contains concrete label key/value pairs that match at least one pod’s metadata.labels and not unresolved references like kubernetes_<resource>.<name>. If no pod matches the selector or the label value appears to be a reference, the rule reports an IncorrectValue issue.
Compliant Code Examples
resource"kubernetes_network_policy""example2"{metadata{name="terraform-example-network-policy"namespace="default"}spec{pod_selector{match_expressions{key="name"operator="In"values=["webfront","api"]}match_labels={app="ngnix2"}}ingress{ports{port="http"protocol="TCP"}ports{port="8125"protocol="UDP"}from{namespace_selector{match_labels={name="default"}}}from{ip_block{cidr="10.0.0.0/8"except=["10.0.0.0/24","10.0.1.0/24",]}}}egress{} # single empty rule to allow all egress traffic
policy_types=["Ingress","Egress"]}}resource"kubernetes_pod""test2"{metadata{name="terraform-example"labels={app="ngnix2"}}spec{container{image="nginx:1.7.9"name="example"env{name="environment"value="test"}port{container_port=8080}liveness_probe{http_get{path="/nginx_status"port=80http_header{name="X-Custom-Header"value="Awesome"}}initial_delay_seconds=3period_seconds=3}}dns_config{nameservers=["1.1.1.1","8.8.8.8","9.9.9.9"]searches=["example.com"]option{name="ndots"value=1}option{name="use-vc"}}dns_policy="None"}}resource"kubernetes_network_policy""example222"{metadata{name="terraform-example-network-policy"namespace="default"}spec{pod_selector{match_expressions{key="name"operator="In"values=["webfront","api"]}match_labels={app="kubernetes_pod.test2.metadata.0.labels.app"}}ingress{ports{port="http"protocol="TCP"}ports{port="8125"protocol="UDP"}from{namespace_selector{match_labels={name="default"}}}from{ip_block{cidr="10.0.0.0/8"except=["10.0.0.0/24","10.0.1.0/24",]}}}egress{} # single empty rule to allow all egress traffic
policy_types=["Ingress","Egress"]}}
# Negative: the NetworkPolicy targets pods with `app = "ngnix-v1"`, and a
# matching pod is declared via the current `kubernetes_pod_v1` provider
# resource. `findTargettedPod` must recognize `_v1` pods in addition to
# `kubernetes_pod`.
resource"kubernetes_network_policy""example_v1"{metadata{name="terraform-example-network-policy-v1"namespace="default"}spec{pod_selector{match_labels={app="ngnix-v1"}}policy_types=["Ingress"]}}resource"kubernetes_pod_v1""test_v1"{metadata{name="terraform-example-v1"labels={app="ngnix-v1"}}spec{container{image="nginx:1.7.9"name="example"}}}
Non-Compliant Code Examples
resource"kubernetes_network_policy""example"{metadata{name="terraform-example-network-policy"namespace="default"}spec{pod_selector{match_expressions{key="name"operator="In"values=["webfront","api"]}match_labels={app="ngnix"}}ingress{ports{port="http"protocol="TCP"}ports{port="8125"protocol="UDP"}from{namespace_selector{match_labels={name="default"}}}from{ip_block{cidr="10.0.0.0/8"except=["10.0.0.0/24","10.0.1.0/24",]}}}egress{} # single empty rule to allow all egress traffic
policy_types=["Ingress","Egress"]}}
# Reproduces a false-negative scenario: the NetworkPolicy targets pods with
# `app = "ngnix"`, but the only resource in the module that carries a matching
# label is a non-Kubernetes resource (a Teleport app). The rule must still
# flag the NetworkPolicy because no `kubernetes_pod` targets it.
resource"kubernetes_network_policy""positive2"{metadata{name="terraform-example-network-policy-2"namespace="default"}spec{pod_selector{match_labels={app="ngnix"}}policy_types=["Ingress"]}}resource"teleport_app""decoy"{version="v3"metadata={name="not-a-pod"labels={app="ngnix"}}spec={uri="tcp://example.com:443"}}
1
2
rulesets:- Terraform / Kubernetes # Rules to enforce / Kubernetes.
맞춤형 데모 요청
Datadog 시작하기
Ask AI
AI-generated responses may be inaccurate. Verify important info.