dazzling-angle-45051
03/23/2022, 8:36 PMwith kubernetes.core.v1.Namespace(project):
acme_corp.MyResource(f"{project}-{stack}-myresource")
prehistoric-activity-61023
03/23/2022, 8:43 PMns = kubernetes.core.v1.Namespace(..)
deployment = kubernetes.core.v1.Secret(
...
metadata=kubernetes.meta.v1.ObjectMetaArgs(
namespace=namespace.metadata.name,
...
),
)
depends_on
is a last resort option for me when the dependency cannot be detected automaticallyInput[str]
and pass the resource name similar to the example abovedazzling-angle-45051
03/23/2022, 9:05 PMprehistoric-activity-61023
03/23/2022, 9:08 PMns = kubernetes.core.v1.Namespace(..)
acme_corp.MyResource(
f"{project}-{stack}-myresource",
namespace=ns.metadata.name,
)
dazzling-angle-45051
03/23/2022, 9:10 PMprehistoric-activity-61023
03/23/2022, 9:12 PMdazzling-angle-45051
03/23/2022, 9:12 PMnamespace
as a depends_on
to the resources that I'm creating within MyResource
, but still, it doesn't assign the same namespace nameprehistoric-activity-61023
03/23/2022, 9:13 PM__init__
fragment and the part where you create some another k8s resourcedazzling-angle-45051
03/23/2022, 9:15 PMimport pulumi
import pulumi_kubernetes as kubernetes
import acme_corp
project = pulumi.get_project()
stack = pulumi.get_stack()
namespace = kubernetes.core.v1.Namespace(project)
acme_corp.MyResource(
f"{project}-{stack}-resource", namespace=namespace
)
in `acme_corp`:
import pulumi
import pulumi_aws as aws
import pulumi_kubernetes as kubernetes
class MyResource(pulumi.ComponentResource):
def __init__(self, name: str, namespace, opts=None):
super().__init__("acme_corp:MyResource", name, None, opts)
aws.s3.Bucket(name, acl="private", opts=pulumi.ResourceOptions(parent=self))
resource = kubernetes.yaml.ConfigFile(
f"{name}-resource",
"./yaml/resource.yaml",
opts=pulumi.ResourceOptions(parent=self, depends_on=[namespace]),
)
prehistoric-activity-61023
03/23/2022, 9:16 PM{project}-{random_suffix}
? If you want to fully control the name of the namespace, you have to set the metadata
field (see my example above).dazzling-angle-45051
03/23/2022, 9:17 PMproject-123
while the resource tries to be deployed in project-456
(that doesn't exist)prehistoric-activity-61023
03/23/2022, 9:18 PMdepends_on
so it should actually wait until the namespace is created. However, you don’t set the metadata on the resource itself.dazzling-angle-45051
03/23/2022, 9:19 PMprehistoric-activity-61023
03/23/2022, 9:19 PMtransformations
./yaml/resource.yaml
?dazzling-angle-45051
03/23/2022, 9:21 PMprehistoric-activity-61023
03/23/2022, 9:21 PMmetadata
field already set or notdazzling-angle-45051
03/23/2022, 9:22 PMapiVersion: batch/v1beta1
kind: CronJob
metadata:
name: whatever
spec:
schedule: "0 */1 * * *"
concurrencyPolicy: Forbid
suspend: false
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 1
jobTemplate:
spec:
template:
spec:
restartPolicy: OnFailure
containers:
- name: whatever
image: whatever
prehistoric-activity-61023
03/23/2022, 9:22 PMtransformations
) it once it’s read using kubernetes.yaml.ConfigFile
and add/modify namespace
parameterConfigFile
has some dedicated support for overriding namespace (it could have, let me check)dazzling-angle-45051
03/23/2022, 9:27 PMdef set_namespace(obj, opts):
obj["metadata"]["namespace"] = namespace.id
cronjob = kubernetes.yaml.ConfigFile(
f"{name}-cronjob",
"./yaml/cronjob.yaml",
opts=pulumi.ResourceOptions(parent=self, depends_on=[namespace]),
transformations=[set_namespace],
)
this works, but sounds super overkillprehistoric-activity-61023
03/23/2022, 9:27 PMdef set_namespace(name: str) -> Callable[[Any, pulumi.ResourceOptions], None]:
def inner(obj, opts):
obj["metadata"]["namespace"] = name
and use this transformation function while creating the resource:
resource = kubernetes.yaml.ConfigFile(
f"{name}-resource",
"./yaml/resource.yaml",
opts=pulumi.ResourceOptions(parent=self, depends_on=[namespace]),
transformations=[set_namespace(namespace.metadata.name)],
)
set_namespace
function so it can be reused by other modulesdepends_on
transformations
fielddazzling-angle-45051
03/23/2022, 9:30 PMprehistoric-activity-61023
03/23/2022, 9:30 PMnamespace.metadata.name
is not a simple str
but rather pulumi.Output[str]
so it can do “some magic” to make these dependencies autodiscoverablenamespace
in kubernetes.yaml.ConfigFile
as it seems like a very common usecase and creating a transformation function for that, indeed seems like an overkilldazzling-angle-45051
03/23/2022, 9:34 PMprehistoric-activity-61023
03/23/2022, 9:34 PMdazzling-angle-45051
03/23/2022, 9:35 PM