Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem using dictionary in lieu of k8s.WeightedPodAffinityTerm #874

Open
LS80 opened this issue Nov 20, 2022 · 4 comments
Open

Problem using dictionary in lieu of k8s.WeightedPodAffinityTerm #874

LS80 opened this issue Nov 20, 2022 · 4 comments
Labels
feature-request New/Enhanced functionality wanted jsii Stems from a jsii issue and requires its fix

Comments

@LS80
Copy link

LS80 commented Nov 20, 2022

The following example produces a valid pod manifest

from constructs import Construct
from cdk8s import App, Chart

from imports import k8s

class MyChart(Chart):
    def __init__(self, scope: Construct, id: str):
        super().__init__(scope, id)

        k8s.KubePod(self, "pod",
            metadata=k8s.ObjectMeta(name="pod-affinity"),
            spec=k8s.PodSpec(
                affinity=k8s.Affinity(
                    pod_anti_affinity=dict(
                        preferred_during_scheduling_ignored_during_execution=[
                            k8s.WeightedPodAffinityTerm(
                                weight=100,
                                pod_affinity_term=dict(
                                    label_selector=dict(match_labels=dict(foo="bar")),
                                    topology_key="topology.kubernetes.io/zone"
                                )
                            )
                        ]
                    )
                ),
                containers=[
                    k8s.Container(
                        name="pod-affinity",
                        image="registry.k8s.io/pause:2.0"
                    )
                ],
            )
        )

app = App()
MyChart(app, "pod")

But using a dictionary in place of k8s.WeightedPodAffinityTerm produces an error Missing required properties for k8s.WeightedPodAffinityTerm: 'podAffinityTerm'.

from constructs import Construct
from cdk8s import App, Chart

from imports import k8s

class MyChart(Chart):
    def __init__(self, scope: Construct, id: str):
        super().__init__(scope, id)

        k8s.KubePod(self, "pod",
            metadata=k8s.ObjectMeta(name="pod-affinity"),
            spec=k8s.PodSpec(
                affinity=k8s.Affinity(
                    pod_anti_affinity=dict(
                        preferred_during_scheduling_ignored_during_execution=[
                            dict(
                                weight=100,
                                pod_affinity_term=dict(
                                    label_selector=dict(match_labels=dict(foo="bar")),
                                    topology_key="topology.kubernetes.io/zone"
                                )
                            )
                        ]
                    )
                ),
                containers=[
                    k8s.Container(
                        name="pod-affinity",
                        image="registry.k8s.io/pause:2.0"
                    )
                ],
            )
        )

app = App()
MyChart(app, "pod")

app.synth()

I assume it's an example of the issue discussed in aws/jsii#1919 (Passing dicts in lieu of jsii structs does not consistently work).

@iliapolo iliapolo added the needs-triage Priority and effort undetermined yet label Feb 6, 2023
Copy link

github-actions bot commented Feb 6, 2024

This issue has not received any attention in 1 year and will be closed soon. If you want to keep it open, please leave a comment below @mentioning a maintainer.

@github-actions github-actions bot added the closing-soon Issue/PR will be closing soon if no response is provided label Feb 6, 2024
@LS80
Copy link
Author

LS80 commented Feb 6, 2024

@iliapolo is this something for the upstream https://github.com/aws/jsii to fix or can it be fixed here?

@github-actions github-actions bot removed the closing-soon Issue/PR will be closing soon if no response is provided label Feb 6, 2024
@iliapolo
Copy link
Member

iliapolo commented Jun 1, 2024

Interesting. This however does work:

from constructs import Construct
from cdk8s import App, Chart

from imports import k8s

class MyChart(Chart):
    def __init__(self, scope: Construct, id: str):
        super().__init__(scope, id)

        k8s.KubePod(self, "pod",
            metadata=k8s.ObjectMeta(name="pod-affinity"),
            spec=k8s.PodSpec(
                affinity=k8s.Affinity(
                    pod_anti_affinity=dict(
                        preferred_during_scheduling_ignored_during_execution=[
                            dict(
                                weight=100,
                                podAffinityTerm=dict(
                                    labelSelector=dict(match_labels=dict(foo="bar")),
                                    topologyKey="topology.kubernetes.io/zone"
                                )
                            )
                        ]
                    )
                ),
                containers=[
                    k8s.Container(
                        name="pod-affinity",
                        image="registry.k8s.io/pause:2.0"
                    )
                ],
            )
        )

app = App()
MyChart(app, "pod")

app.synth()

(Note the casing of properties inside the dict)

It looks like case conversion doesn't happen for literal dictionaries. It kind of makes sense because there's no apparent reason a literal dictionary should go through case conversion. jsii would have to detect that dictionary is being in lieu of a struct, right now it just passes it through. Definitely a jsii issue, i'll check with the team and update here.

Thanks for reporting!

@iliapolo iliapolo added bug Something isn't working effort/medium 1 week tops priority/p1 Should be on near term plans and removed bug Something isn't working effort/medium 1 week tops priority/p1 Should be on near term plans labels Jun 1, 2024
@iliapolo
Copy link
Member

iliapolo commented Jun 3, 2024

So not much we can do here, the behavior here is explained in this issue (see Passing dict in lieu of jsii structs does not consistently work). Keeping this open for discoverability, but this will have to wait for a jsii fix.

@iliapolo iliapolo added feature-request New/Enhanced functionality wanted jsii Stems from a jsii issue and requires its fix and removed needs-triage Priority and effort undetermined yet labels Jun 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request New/Enhanced functionality wanted jsii Stems from a jsii issue and requires its fix
Projects
None yet
Development

No branches or pull requests

2 participants