coreos-cloudinit, /etc/environment, and a general way of string substitution

216 views
Skip to first unread message

xie...@gmail.com

unread,
Apr 20, 2015, 7:26:29 AM4/20/15
to coreo...@googlegroups.com
Dear all,

Recently I was trying to build an OEM version of CoreOS to work best for the cloud platform running by the company I work for. Basically we have in our ability to offer customer free MPLS VPN across regions simply by having an extra NIC, there is absolutely no setup/config needed from the user.

Now current user of CoreOS has the ability to use $public_ipv4 and $private_ipv4 in their cloud-config.yaml and this will be replaced by coreos-cloudinit as the code shown here:

// TODO(jonboulle): this is getting unwieldy, should be able to simplify the interface somehow
func NewEnvironment(root, configRoot, workspace, sshKeyName string, metadata datasource.Metadata) *Environment {
 firstNonNull := func(ip net.IP, env string) string {
           if ip == nil {
                 return env
             }
              return ip.String()
     }
      substitutions := map[string]string{
            "$public_ipv4":  firstNonNull(metadata.PublicIPv4, os.Getenv("COREOS_PUBLIC_IPV4")),
           "$private_ipv4": firstNonNull(metadata.PrivateIPv4, os.Getenv("COREOS_PRIVATE_IPV4")),
         "$public_ipv6":  firstNonNull(metadata.PublicIPv6, os.Getenv("COREOS_PUBLIC_IPV6")),
           "$private_ipv6": firstNonNull(metadata.PrivateIPv6, os.Getenv("COREOS_PRIVATE_IPV6")),
 }
      return &Environment{root, configRoot, workspace, sshKeyName, substitutions}
}
https://github.com/coreos/coreos-cloudinit/blob/4eaaa5c9273a0ce557d424f5da676777bef53e8e/initialize/env.go

I would like to have an extra $vpn_ipv4 that will get replaced when coreos-cloudinit is called. But I also understand this is not meaningful to anyone else, so I thought, could this substitution business be turned into a general way.

One example could be: we give coreos-cloudinit an extra parameter to allow it to read a list from an external file (which could live under /usr/share/oem), whatever is specified there will get replaced by using /etc/environment.

In this way, anyone can easily extend the support of string substitution without needing to make above code messier. In the end, why should  we hard code any string in the code? I see this as a 'service' coreos-cloudinit provides, which should really handle general cases.

And this also fits the 'OEM' thinking, in that if I need a new $vpn_ipv4, or $vpn_gateway, I can just leave my list in ../oem and make sure I have correct values in /etc/environment, therefore avoid making PR to add something only useful to me, considering how much 'single purpose' code currently is already there.

Thoughts?

Best regards,

Dong Xie

Alex Crawford

unread,
Apr 22, 2015, 7:39:01 PM4/22/15
to coreo...@googlegroups.com
On 04/20, xie...@gmail.com wrote:
> Dear all,
>
> Recently I was trying to build an OEM version of CoreOS to work best for
> the cloud platform running by the company I work for. Basically we have in
> our ability to offer customer free MPLS VPN across regions simply by having
> an extra NIC, there is absolutely no setup/config needed from the user.
>
> Now current user of CoreOS has the ability to use $public_ipv4 and
> $private_ipv4 in their cloud-config.yaml and this will be replaced by
> coreos-cloudinit as the code shown here:
>
> // TODO(jonboulle): this is getting unwieldy, should be able to simplify
> the interface somehowfunc NewEnvironment(root, configRoot, workspace,
> sshKeyName string, metadata datasource.Metadata) *Environment {
> firstNonNull := func(ip net.IP, env string) string { if ip == nil
> { return env } return ip.String
> () } substitutions := map[string]string{ "
My suggestion moving foward is to have your OEM cloud-config write a
file containing any metadata you want to expose Then ask your users to
source that environment in their services (assuming they need it) and
use the variable directly. For example:

OEM cloud-config:

write_files:
- path: /run/my-cloud-provider/environment
content: MYCLOUDPROVIDER_IPV4_VPN=10.0.1.2


User cloud-config

coreos:
units:
- name: etcd.service
drop-ins:
- name: args.conf
contents: |
[Service]
EnvironmentFile=/etc/environment
EnvironmentFile=/run/my-cloud-provider/environment
ExecStart=/usr/bin/etcd \
-advertise-client-urls=http://${COREOS_PUBLIC_IPV4}:2379 \
-initial-advertise-peer-urls=http://${MYCLOUDPROVIDER_IPV4_VPN}:2380 \
-listen-client-urls=http://0.0.0.0:2379 \
-listen-peer-urls=http://${MYCLOUDPROVIDER_IPV4_VPN}:2380

-Alex
Reply all
Reply to author
Forward
0 new messages