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

How do use a map value? #45

Closed
ghost opened this issue Nov 17, 2020 · 5 comments
Closed

How do use a map value? #45

ghost opened this issue Nov 17, 2020 · 5 comments
Labels
question Further information is requested

Comments

@ghost
Copy link

ghost commented Nov 17, 2020

Hello @TomWright,

I have another question, I do have this code to create the yaml file:

echo '' | \
    dasel put string -p yaml 'apiVersion' 'cilium.io/v2' | \
    dasel put string -p yaml 'kind' 'CiliumNetworkPolicy' | \
    dasel put string -p yaml 'metadata.name' 'allow-kube-dns' | \
    dasel put string -p yaml 'spec.endpointSelector.matchLabels' {} | \
    dasel put string -p yaml 'spec.egress.[0].toEndpoints.[0].matchLabels.k8s:io\.kubernetes\.pod\.namespace' 'kube-system' | \
    dasel put string -p yaml 'spec.egress.[0].toEndpoints.[0].matchLabels.k8s-app' 'kube-dns' | \
    dasel put string -p yaml 'spec.egress.[0].toPorts.[0].ports.[0].port' '53' | \
    dasel put string -p yaml 'spec.egress.[0].toPorts.[0].ports.[0].protocol' 'UDP' 

This is the output:

apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
  name: allow-kube-dns
spec:
  egress:
  - toEndpoints:
    - matchLabels:
        k8s-app: kube-dns
        k8s:io.kubernetes.pod.namespace: kube-system
    toPorts:
    - ports:
      - port: "53"
        protocol: UDP
  endpointSelector:
    matchLabels: '{}'

But when applying this there is an error:

error: error validating "tt.yaml": error validating data: ValidationError(CiliumNetworkPolicy.spec.endpointSelector.matchLabels): invalid type for io.cilium.v2.CiliumNetworkPolicy.spec.endpointSelector.matchLabels: got "string", expected "map"; if you choose to ignore these errors, turn validation off with --validate=false

When I remove the single quotes arond the {} at matchLabels, it does work. How can I achieve this ?

  endpointSelector:
    matchLabels: {}

The docs state string, bool or int, but no other options.

Thanks again

@ghost ghost added the question Further information is requested label Nov 17, 2020
@TomWright
Copy link
Owner

This put is causing that entry to appear:

dasel put string -p yaml 'spec.endpointSelector.matchLabels' {}

Dasel doesn't currently support putting empty values.

I would argue that this item should just be omitted since it doesn't contain any values, giving you the following YAML:

kind: CiliumNetworkPolicy
metadata:
  name: allow-kube-dns
spec:
  egress:
  - toEndpoints:
    - matchLabels:
        k8s-app: kube-dns
        k8s:io.kubernetes.pod.namespace: kube-system
    toPorts:
    - ports:
      - port: "53"
        protocol: UDP

@TomWright
Copy link
Owner

Alternatively you could use dasel put object -p yaml 'spec.endpointSelector.matchLabels'.

You will currently receive an error stating that type is a required flag, but I can remove that requirement.

@ghost
Copy link
Author

ghost commented Nov 17, 2020

Hi @TomWright,

Could be great, when possible, to use { } would be great

@TomWright
Copy link
Owner

No worries, that's now supported as of v1.6.1 🙂

Notice that I used put object with no types/values rather than put string {}:

echo '' | \
    dasel put string -p yaml 'apiVersion' 'cilium.io/v2' | \
    dasel put string -p yaml 'kind' 'CiliumNetworkPolicy' | \
    dasel put string -p yaml 'metadata.name' 'allow-kube-dns' | \
    dasel put object -p yaml 'spec.endpointSelector.matchLabels' | \
    dasel put string -p yaml 'spec.egress.[0].toEndpoints.[0].matchLabels.k8s:io\.kubernetes\.pod\.namespace' 'kube-system' | \
    dasel put string -p yaml 'spec.egress.[0].toEndpoints.[0].matchLabels.k8s-app' 'kube-dns' | \
    dasel put string -p yaml 'spec.egress.[0].toPorts.[0].ports.[0].port' '53' | \
    dasel put string -p yaml 'spec.egress.[0].toPorts.[0].ports.[0].protocol' 'UDP' 
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
  name: allow-kube-dns
spec:
  egress:
  - toEndpoints:
    - matchLabels:
        k8s-app: kube-dns
        k8s:io.kubernetes.pod.namespace: kube-system
    toPorts:
    - ports:
      - port: "53"
        protocol: UDP
  endpointSelector:
    matchLabels: {}

@ghost
Copy link
Author

ghost commented Nov 17, 2020

Looking great, first test are positive. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant