Skip to content

Commit

Permalink
Add ability to manage fallback network config on PhotonOS (canonical#941
Browse files Browse the repository at this point in the history
)

Currently cloud-init generates fallback network config on various
scenarios.

For example:
1. When no DS found
2. There is no 'network' info given in DS metadata.
3. If a DS gives a network config once and upon reboot if DS doesn't
   give any network info, previously set network data will be
   overridden.

A newly introduced key in cloud.cfg.tmpl can be used to control this
behavior on PhotonOS.

Also, if OS comes with a set of default network files(configs), like in
PhotonOS, cloud-init should not overwrite them by default.

This change also includes some nitpicking changes of reorganizing few
config variables.

Signed-off-by: Shreenidhi Shedi <sshedi@vmware.com>
  • Loading branch information
sshedi authored and TheRealFalcon committed Aug 10, 2021
1 parent 3be4f60 commit 27e49b7
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 2 deletions.
15 changes: 15 additions & 0 deletions cloudinit/distros/photon.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#
# This file is part of cloud-init. See LICENSE file for license information.

from cloudinit import net
from cloudinit import util
from cloudinit import subp
from cloudinit import distros
Expand Down Expand Up @@ -54,6 +55,20 @@ def exec_cmd(self, cmd, capture=False):
util.logexc(LOG, 'Command %s failed', cmd)
return False, None, None

def generate_fallback_config(self):
key = 'disable_fallback_netcfg'
disable_fallback_netcfg = self._cfg.get(key, True)
LOG.debug('%s value is: %s', key, disable_fallback_netcfg)

if not disable_fallback_netcfg:
return net.generate_fallback_config()

LOG.info(
'Skipping generate_fallback_config. Rely on PhotonOS default '
'network config'
)
return None

def apply_locale(self, locale, out_fn=None):
# This has a dependancy on glibc-i18n, user need to manually install it
# and enable the option in cloud.cfg
Expand Down
10 changes: 8 additions & 2 deletions config/cloud.cfg.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ users:
- default
{% endif %}

# VMware guest customization.
{% if variant in ["photon"] %}
# VMware guest customization.
disable_vmware_customization: true
manage_etc_hosts: false
{% endif %}

# If this is set, 'root' will not be able to ssh in and they
Expand Down Expand Up @@ -306,10 +307,15 @@ system_info:
paths:
cloud_dir: /var/lib/cloud/
templates_dir: /etc/cloud/templates/
network:
renderers: ['networkd']

ssh_svcname: sshd

#manage_etc_hosts: true
# If set to true, cloud-init will not use fallback network config.
# In Photon, we have default network settings, hence if network settings are
# not explicitly given in metadata, don't use fallback network config.
disable_fallback_netcfg: true
{% endif %}
{% if variant in ["freebsd", "netbsd", "openbsd"] %}
network:
Expand Down
1 change: 1 addition & 0 deletions doc/rtd/topics/availability.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ OpenBSD and DragonFlyBSD:
- Gentoo Linux
- NetBSD
- OpenBSD
- Photon OS
- RHEL/CentOS
- SLES/openSUSE
- Ubuntu
Expand Down
7 changes: 7 additions & 0 deletions doc/rtd/topics/network-config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ interface given the information it has available.
Finally after selecting the "right" interface, a configuration is
generated and applied to the system.

.. note::

PhotonOS disables fallback networking configuration by default leaving
network unrendered when no other network config is provided.
If fallback config is still desired on PhotonOS, it can be enabled by
providing `disable_fallback_netcfg: false` in
`/etc/cloud/cloud.cfg:sys_config` settings.

Network Configuration Sources
=============================
Expand Down
51 changes: 51 additions & 0 deletions tests/unittests/test_distros/test_photon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# This file is part of cloud-init. See LICENSE file for license information.

from . import _get_distro
from cloudinit import util
from cloudinit.tests.helpers import mock
from cloudinit.tests.helpers import CiTestCase

SYSTEM_INFO = {
'paths': {
'cloud_dir': '/var/lib/cloud/',
'templates_dir': '/etc/cloud/templates/',
},
'network': {'renderers': 'networkd'},
}


class TestPhoton(CiTestCase):
with_logs = True
distro = _get_distro('photon', SYSTEM_INFO)
expected_log_line = 'Rely on PhotonOS default network config'

def test_network_renderer(self):
self.assertEqual(self.distro._cfg['network']['renderers'], 'networkd')

def test_get_distro(self):
self.assertEqual(self.distro.osfamily, 'photon')

def test_write_hostname(self):
hostname = 'myhostname'
hostfile = self.tmp_path('hostfile')
self.distro._write_hostname(hostname, hostfile)
self.assertEqual(hostname + '\n', util.load_file(hostfile))

@mock.patch('cloudinit.net.generate_fallback_config')
def test_fallback_netcfg(self, m_fallback_cfg):

key = 'disable_fallback_netcfg'
# Don't use fallback if no setting given
self.logs.truncate(0)
assert(self.distro.generate_fallback_config() is None)
self.assertIn(self.expected_log_line, self.logs.getvalue())

self.logs.truncate(0)
self.distro._cfg[key] = True
assert(self.distro.generate_fallback_config() is None)
self.assertIn(self.expected_log_line, self.logs.getvalue())

self.logs.truncate(0)
self.distro._cfg[key] = False
assert(self.distro.generate_fallback_config() is not None)
self.assertNotIn(self.expected_log_line, self.logs.getvalue())

0 comments on commit 27e49b7

Please sign in to comment.