From 842be9a2576325270b8eadc6fb8656bb65891513 Mon Sep 17 00:00:00 2001 From: Pedro Aibar <75799784+motinsa@users.noreply.github.com> Date: Sun, 29 Dec 2024 20:27:23 +0100 Subject: [PATCH 1/2] Refactor: Encapsulate net opts loading in internalLabels Signed-off-by: Pedro Aibar <75799784+motinsa@users.noreply.github.com> --- pkg/cmd/container/create.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/pkg/cmd/container/create.go b/pkg/cmd/container/create.go index 37d625b1da5..1829d6681df 100644 --- a/pkg/cmd/container/create.go +++ b/pkg/cmd/container/create.go @@ -248,13 +248,7 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa envs = append(envs, "HOSTNAME="+netLabelOpts.Hostname) opts = append(opts, oci.WithEnv(envs)) - // TODO(aznashwan): more formal way to load net opts into internalLabels: - internalLabels.hostname = netLabelOpts.Hostname - internalLabels.ports = netLabelOpts.PortMappings - internalLabels.ipAddress = netLabelOpts.IPAddress - internalLabels.ip6Address = netLabelOpts.IP6Address - internalLabels.networks = netLabelOpts.NetworkSlice - internalLabels.macAddress = netLabelOpts.MACAddress + internalLabels.LoadNetOpts(netLabelOpts) // NOTE: OCI hooks are currently not supported on Windows so we skip setting them altogether. // The OCI hooks we define (whose logic can be found in pkg/ocihook) primarily @@ -732,6 +726,16 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO return containerd.WithAdditionalContainerLabels(m), nil } +// LoadNetOpts loads network options into InternalLabels. +func (il *internalLabels) LoadNetOpts(opts types.NetworkOptions) { + il.hostname = opts.Hostname + il.ports = opts.PortMappings + il.ipAddress = opts.IPAddress + il.ip6Address = opts.IP6Address + il.networks = opts.NetworkSlice + il.macAddress = opts.MACAddress +} + func dockercompatMounts(mountPoints []*mountutil.Processed) []dockercompat.MountPoint { result := make([]dockercompat.MountPoint, len(mountPoints)) for i := range mountPoints { From d6248ac109cde6dd057e66d7d3268d3d94353c56 Mon Sep 17 00:00:00 2001 From: Pedro Aibar <75799784+motinsa@users.noreply.github.com> Date: Thu, 2 Jan 2025 08:12:53 +0000 Subject: [PATCH 2/2] Refactor: Change function loadNetOpts from public to private Signed-off-by: Pedro Aibar --- pkg/cmd/container/create.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/container/create.go b/pkg/cmd/container/create.go index 1829d6681df..77b5656e098 100644 --- a/pkg/cmd/container/create.go +++ b/pkg/cmd/container/create.go @@ -248,7 +248,7 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa envs = append(envs, "HOSTNAME="+netLabelOpts.Hostname) opts = append(opts, oci.WithEnv(envs)) - internalLabels.LoadNetOpts(netLabelOpts) + internalLabels.loadNetOpts(netLabelOpts) // NOTE: OCI hooks are currently not supported on Windows so we skip setting them altogether. // The OCI hooks we define (whose logic can be found in pkg/ocihook) primarily @@ -726,8 +726,8 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO return containerd.WithAdditionalContainerLabels(m), nil } -// LoadNetOpts loads network options into InternalLabels. -func (il *internalLabels) LoadNetOpts(opts types.NetworkOptions) { +// loadNetOpts loads network options into InternalLabels. +func (il *internalLabels) loadNetOpts(opts types.NetworkOptions) { il.hostname = opts.Hostname il.ports = opts.PortMappings il.ipAddress = opts.IPAddress