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

Add Jsonnet native functions #702

Merged
merged 13 commits into from
Jul 19, 2024
Merged

Add Jsonnet native functions #702

merged 13 commits into from
Jul 19, 2024

Conversation

fujiwara
Copy link
Contributor

@fujiwara fujiwara commented Jun 14, 2024

This PR introduces Jsonnet native functions!

Add functions to go-jsonnet's native function, similar to those previously provided by text/template functions.

Examples

# ecspresso.yml
region: '{{ must_env `AWS_REGION` }}'
cluster: '{{ env `CLUSTER` `default` }}'
# ...
// ecspresso.jsonnet
local env = std.native('env');
local must_env = std.native('must_env');
{
  region: must_env('AWS_REGION'),
  cluster: env('CLUSTER', 'default'),
  // ...
}
// ecs-service-def.jsonnet
local env = std.native('env');
local tfstate = std.native('tfstate');
local vpc_tfstate = std.native('vpc_tfstate'); // func_prefix: vpc_
{
  desiredCount: std.parseInt(env('DESIRED_COUNT', '1')),
  loadBalancers: [
    {
      containerName: 'nginx',
      containerPort: 80,
      targetGroupArn: tfstate('aws_lb_target_group.nginx.arn'),
    },
  ],
  networkConfiguration: {
    awsvpcConfiguration: {
      securityGroups: [ vpc_tfstate('aws_security_group.foo.id') ],
      subnets: [
        vpc_tfstate('aws_subnet.az-a.id'),
        vpc_tfstate('aws_subnet.az-b.id'),
      ],
    },
  },

  // ...
// ecs-task-def.jsonnet
local ssm = std.native('ssm');
local secretsmanager_arn = std.native('secretsmanager_arn');
{
  containerDefinitions: [
    {
      environment: [
        {
          name: 'FOO',
          value: ssm('/ecspresso-test/foo'),
        },
      ],
      secrets: [
        {
          name: 'BAZ',
          valueFrom: secretsmanager_arn('ecspresso-test/baz'),
        },
      ],
//....

Add functions

  • env(name, default)
  • must_env(name)
  • ssm(name)
  • ssm_list(name, index)
  • secretsmanager_arn(id)
  • cfn_export(name)
  • cfn_output(stackName, outputKey)
  • tfstate(addr)

Limitations

env and must_env are available at first (for reading a config file).
Other functions will be available to load task definitions or service definitions.

This limitation is same to text/template functions.

@fujiwara fujiwara added the v2.4 label Jun 14, 2024
@fujiwara fujiwara changed the title Feature/jsonnet native funcs Add Jsonnet native functions Jul 19, 2024
@fujiwara fujiwara merged commit 9ae0cfa into v2 Jul 19, 2024
4 checks passed
@fujiwara fujiwara deleted the feature/jsonnet-native-funcs branch July 19, 2024 16:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant