Skip to content

Commit

Permalink
feat: Allow aliyun ds to fetch data in init-local (#4590)
Browse files Browse the repository at this point in the history
  • Loading branch information
ld9379435 authored Dec 1, 2023
1 parent 30761d5 commit fe144a5
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
22 changes: 21 additions & 1 deletion cloudinit/sources/DataSourceAliYun.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# This file is part of cloud-init. See LICENSE file for license information.

import logging
from typing import List

from cloudinit import dmi, sources
from cloudinit.event import EventScope, EventType
from cloudinit.sources import DataSourceEc2 as EC2
from cloudinit.sources import DataSourceHostname

LOG = logging.getLogger(__name__)

ALIYUN_PRODUCT = "Alibaba Cloud ECS"


Expand All @@ -23,6 +27,10 @@ class DataSourceAliYun(EC2.DataSourceEc2):
def imdsv2_token_put_header(self):
return "X-aliyun-ecs-metadata-token"

def __init__(self, sys_cfg, distro, paths):
super(DataSourceAliYun, self).__init__(sys_cfg, distro, paths)
self.default_update_events[EventScope.NETWORK].add(EventType.BOOT)

def get_hostname(self, fqdn=False, resolve_ip=False, metadata_only=False):
hostname = self.metadata.get("hostname")
is_default = False
Expand Down Expand Up @@ -61,12 +69,24 @@ def parse_public_keys(public_keys):
return keys


class DataSourceAliYunLocal(DataSourceAliYun):
"""Datasource run at init-local which sets up network to query metadata.
In init-local, no network is available. This subclass sets up minimal
networking with dhclient on a viable nic so that it can talk to the
metadata service. If the metadata service provides network configuration
then render the network configuration for that instance based on metadata.
"""

perform_dhcp_setup = True


# Used to match classes to dependencies
datasources = [
(DataSourceAliYunLocal, (sources.DEP_FILESYSTEM,)), # Run at init-local
(DataSourceAliYun, (sources.DEP_FILESYSTEM, sources.DEP_NETWORK)),
]


# Return a list of data sources that match this set of dependencies
def get_datasource_list(depends):
return sources.list_from_depends(depends, datasources)
47 changes: 47 additions & 0 deletions tests/unittests/sources/test_aliyun.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,53 @@ def test_with_mock_server(self, m_is_aliyun, m_resolv):
"metadata (http://100.100.100.200)", self.ds.subplatform
)

@mock.patch("cloudinit.net.ephemeral.EphemeralIPv6Network")
@mock.patch("cloudinit.net.ephemeral.EphemeralIPv4Network")
@mock.patch("cloudinit.sources.DataSourceEc2.util.is_resolvable")
@mock.patch("cloudinit.sources.DataSourceAliYun._is_aliyun")
@mock.patch("cloudinit.net.find_fallback_nic")
@mock.patch("cloudinit.net.ephemeral.maybe_perform_dhcp_discovery")
@mock.patch("cloudinit.sources.DataSourceEc2.util.is_FreeBSD")
def test_aliyun_local_with_mock_server(
self,
m_is_bsd,
m_dhcp,
m_fallback_nic,
m_is_aliyun,
m_resolva,
m_net4,
m_net6,
):
m_is_aliyun.return_value = True
m_fallback_nic.return_value = "eth9"
m_dhcp.return_value = [
{
"interface": "eth9",
"fixed-address": "192.168.2.9",
"routers": "192.168.2.1",
"subnet-mask": "255.255.255.0",
"broadcast-address": "192.168.2.255",
}
]
m_is_bsd.return_value = False
cfg = {"datasource": {"AliYun": {"timeout": "1", "max_wait": "1"}}}
distro = mock.MagicMock()
paths = helpers.Paths({"run_dir": self.tmp_dir()})
self.ds = ay.DataSourceAliYunLocal(cfg, distro, paths)
self.regist_default_server()
ret = self.ds.get_data()
self.assertEqual(True, ret)
self.assertEqual(1, m_is_aliyun.call_count)
self._test_get_data()
self._test_get_sshkey()
self._test_get_iid()
self._test_host_name()
self.assertEqual("aliyun", self.ds.cloud_name)
self.assertEqual("ec2", self.ds.platform)
self.assertEqual(
"metadata (http://100.100.100.200)", self.ds.subplatform
)

@mock.patch("cloudinit.sources.DataSourceAliYun._is_aliyun")
def test_returns_false_when_not_on_aliyun(self, m_is_aliyun):
"""If is_aliyun returns false, then get_data should return False."""
Expand Down
1 change: 1 addition & 0 deletions tests/unittests/sources/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from tests.unittests import helpers as test_helpers

DEFAULT_LOCAL = [
AliYun.DataSourceAliYunLocal,
Azure.DataSourceAzure,
CloudSigma.DataSourceCloudSigma,
ConfigDrive.DataSourceConfigDrive,
Expand Down
1 change: 1 addition & 0 deletions tools/.github-cla-signers
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ kallioli
klausenbusk
KsenijaS
landon912
ld9379435
licebmi
linitio
lkundrak
Expand Down

0 comments on commit fe144a5

Please sign in to comment.