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

ocf-kubernetes-deploy: create k8s secrets from file templates #146

Merged
merged 1 commit into from
Apr 29, 2020

Conversation

cg505
Copy link
Member

@cg505 cg505 commented Apr 24, 2020

Fixes #145.

This will look for files like kubernetes/secrets/<secret_name>/*.erb, and create corresponding Kubernetes secrets containing these files as key-value pairs. This allows us to template files to include injected secrets and then mount the Kubernetes secret resource as a volume into a container.

This allows files like homeserver.yaml in matrix to be stored in the git repo for matrix, and just have the secret portions injected (given the correct volume setup in the deployment yaml).

@cg505 cg505 requested review from ja5087, dkess and fydai April 24, 2020 06:17
Copy link
Member

@dkess dkess left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a comment about building yaml, but this whole design looks suspicious to me. You shouldn't need to template Kubernetes resource or even write any part of them in this script-- that's what kubernetes-deploy (or krane render) handles for you.

Instead, the secret resources should be defined in their respective repos as erb files, with templates for where the actual secret data goes. ocf-kubernetes-deploy should read the secrets and add them to the bindings dict so they can get filled in.

EDIT: ok, I kind of get this, since we don't want to make an inner template of the entire synapse config. I guess this is fine, as long as we don't lean on it too much and avoid it when possible. We want repos to be explicit about what Kubernetes resources they're creating, and this change makes the guarantee a bit muddy, but it's probably worth it.

Comment on lines 17 to 30
SECRET_RESOURCE_TEMPLATE = '''
apiVersion: v1
kind: Secret
metadata:
name: {name}
type: Opaque
stringData:
{string_data}
'''

STRINGDATA_SECRET_TEMPLATE = '''
{name}: |
{value}
'''
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of templating yaml, you should build up a json object with standard Python types (dicts, lists, strings) and then convert it to json at the end. All json is valid yaml anyways so there's no need to touch yaml here.

Copy link
Member Author

@cg505 cg505 Apr 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that we aren't creating a yaml file, we are creating an erb file. That is to say, we are NOT doing json or yaml manipulation; fundamentally we are doing string manipulation to create an erb file that will be templated into a yaml file. Working in json creates an additional level of conversion that we have to make sure not to break.

A concrete example of what this means:

Say we decide to use a partial in our secret template. ocf-kubernetes-deploy reads our template and stuff it into a json object. Then we dump the json to a file. Now, the " characters in the partial tag in the template have been escaped by the json parser, and the generated ocf-kubernetes-deploy-secret-xyz.json.erb is not valid erb (even though the secret template is valid erb).

This reasoning is definitely a little unintuitive, but it is principled. That said, the template system I used is a little messy and it would be great if there was a better way to do this.

This will look for files like kubernetes/secrets/<secret_name>/*.erb,
and create corresponding Kubernetes secrets containing these files as
key-value pairs. This allows us to template files to include injected
secrets and then mount the Kubernetes secret resource as a volume into
a container.
@cg505 cg505 force-pushed the k8s-secret-files branch from bb6e017 to 7e79a15 Compare April 26, 2020 01:07
@cg505 cg505 mentioned this pull request Apr 26, 2020
5 tasks
@cg505 cg505 merged commit a1f5667 into master Apr 29, 2020
@cg505 cg505 deleted the k8s-secret-files branch April 29, 2020 00:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ocf-kubernetes-deploy: allow for templating large config files with secrets
4 participants