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

Support default value for any type #883

Open
LittleWat opened this issue Dec 14, 2023 · 6 comments
Open

Support default value for any type #883

LittleWat opened this issue Dec 14, 2023 · 6 comments
Assignees
Labels
question Further information is requested

Comments

@LittleWat
Copy link

LittleWat commented Dec 14, 2023

Describe the problem/challenge you have

I want set the default value for any type but I can't.

Here is the example.

  • schema.yaml
#@data/values-schema
---
test:
  #@schema/type any=True
  optional_config:
    param1: "a"
    param2: "b"
  • template.yaml
#@ load("@ytt:data", "data")

conf: #@ data.values.test.optional_config
  • value.yaml
#@data/values
---
test:
  optional_config:
    param3: "c"
$  ytt -f value.yaml -f schema.yaml -f template.yaml
ytt: Error: Overlaying data values (in following order: value.yaml):
  Document on line value.yaml:2:
    Map item (key 'test') on line value.yaml:3:
      Map item (key 'optional_config') on line value.yaml:4:
        Map item (key 'param3') on line value.yaml:5:
          Expected number of matched nodes to be 1, but was 0

Describe the solution you'd like

In the example, the expected output is:

conf:
    param1: a
    param2: b
    param3: c

I hope this is supported 🙏

Or I would be glad if you could share the workarounds for this 🙇‍♂️


Vote on this request

This is an invitation to the community to vote on issues, to help us prioritize our backlog. Use the "smiley face" up to the right of this comment to vote.

👍 "I would like to see this addressed as soon as possible"
👎 "There are other more important things to focus on right now"

We are also happy to receive and review Pull Requests if you want to help working on this issue.

@LittleWat LittleWat added carvel triage This issue has not yet been triaged for relevance enhancement This issue is a feature request labels Dec 14, 2023
@LittleWat
Copy link
Author

LittleWat commented Dec 14, 2023

I found the workaround for this. It was necessary to write the Starlark code, though.

  • schema.yaml
#@data/values-schema
---
test:
  #@schema/type any=True
  optional_config: null

_default_optional_config:
  param1: a
  param2: b
  • template.yaml
#@ load("@ytt:data", "data")
#@ load("@ytt:struct", "struct")

#@ def combine_with_default(user_config, default_config):
#@  result = struct.decode(default_config)
#@  if user_config:
#@      result.update(struct.decode(user_config))
#@  end
#@  return result
#@ end

conf: #@ combine_with_default(data.values.test.optional_config, data.values._default_optional_config)
  • value.yaml
#@data/values
---
test:
  optional_config:
    param1: "aaa"
    param3: "c"
    parma4:
      param4_4: "ddd"

and here is the execution result:

$  ytt -f schema.yaml -f template.yaml -f value.yaml
conf:
  param1: aaa
  param2: b
  param3: c
  parma4:
    param4_4: ddd

@prembhaskal prembhaskal self-assigned this Dec 15, 2023
@prembhaskal
Copy link
Member

@LittleWat What you are trying to achieve can be easily done using an overlay

  • template.yml
#@ load("@ytt:data", "data")

conf: #@ data.values.test.optional_config
  • default-schema.yml
#@data/values-schema
---
test:
  #@schema/type any=True
  optional_config:
    param1: a
    param2: b
  • val.yml
#@ load("@ytt:overlay", "overlay")

#@overlay/match by=overlay.all
---
conf:
  #@overlay/match missing_ok=True
    param3: c

@prembhaskal
Copy link
Member

you can even overlay data values instead of overlaying final results using below variant of val.yml

  • val.yml (overlay data-values)
#@ load("@ytt:overlay", "overlay")
#@data/values

#@overlay/match by=overlay.all
---
test:
  optional_config:
  #@overlay/match missing_ok=True
    param3: c
    

@prembhaskal
Copy link
Member

Also feel free to use / explore carvel channel on kubernetes slack. A question similar to yours is here - https://kubernetes.slack.com/archives/CH8KCCKA5/p1675159628085549

@prembhaskal prembhaskal added question Further information is requested and removed enhancement This issue is a feature request labels Dec 15, 2023
@renuy renuy removed the carvel triage This issue has not yet been triaged for relevance label Jan 9, 2024
@LittleWat
Copy link
Author

LittleWat commented Mar 1, 2024

@prembhaskal Hi, thank you for your quick replies!

I see! #@overlay function can be used here. However, in our use-case, we want to let our customers configure just #@data/values file to abstract(simplify) the k8s configuration. It would be necessary to let our customers understand the logic behind it when using the #@overlay function, which is not so ideal. But it might be necessary. Thanks anyway!!

@prembhaskal
Copy link
Member

hmm understood, so you want to hide the complexity of overlay from end user, but still allow them to pass in the extra configurations.
I think we can do that by making overlay to read from another data values file. something like below (a pseudo code btw)

#@ load("@ytt:overlay", "overlay")
#@load("@ytt:data", "data")
#@data/values

#@overlay/match by=overlay.all
---
test:
  optional_config:
  #@overlay/match missing_ok=True
    #@ data.other_optional_config

I can give it a try later today.

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
Status: No status
Development

No branches or pull requests

3 participants