mirror of
https://github.com/siderolabs/talos.git
synced 2025-08-19 13:41:13 +02:00
This change is only moving packages and updating import paths. Goal: expose `internal/pkg/provision` as `pkg/provision` to enable other projects to import Talos provisioning library. As cluster checks are almost always required as part of provisioning process, package `internal/pkg/cluster` was also made public as `pkg/cluster`. Other changes were direct dependencies discovered by `importvet` which were updated. Public packages (useful, general purpose packages with stable API): * `internal/pkg/conditions` -> `pkg/conditions` * `internal/pkg/tail` -> `pkg/tail` Private packages (used only on provisioning library internally): * `internal/pkg/inmemhttp` -> `pkg/provision/internal/inmemhttp` * `internal/pkg/kernel/vmlinuz` -> `pkg/provision/internal/vmlinuz` * `internal/pkg/cniutils` -> `pkg/provision/internal/cniutils` Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
88 lines
3.0 KiB
Go
88 lines
3.0 KiB
Go
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
// Package cniutils provides helper functions to parse CNI results.
|
|
package cniutils
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/containernetworking/cni/pkg/types/current"
|
|
)
|
|
|
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License"). You may
|
|
// not use this file except in compliance with the License. A copy of the
|
|
// License is located at
|
|
//
|
|
// http://aws.amazon.com/apache2.0/
|
|
//
|
|
// or in the "license" file accompanying this file. This file is distributed
|
|
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
|
|
// express or implied. See the License for the specific language governing
|
|
// permissions and limitations under the License.
|
|
|
|
// FilterBySandbox returns scans the provided list of interfaces and returns two lists: the first
|
|
// are a list of interfaces with the provided sandboxID, the second are the other interfaces not
|
|
// in that sandboxID.
|
|
func FilterBySandbox(sandbox string, ifaces ...*current.Interface) (in, out []*current.Interface) {
|
|
for _, iface := range ifaces {
|
|
if iface.Sandbox == sandbox {
|
|
in = append(in, iface)
|
|
} else {
|
|
out = append(out, iface)
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// IfacesWithName scans the provided list of ifaces and returns the ones with the provided name.
|
|
func IfacesWithName(name string, ifaces ...*current.Interface) []*current.Interface {
|
|
var foundIfaces []*current.Interface
|
|
|
|
for _, iface := range ifaces {
|
|
if iface.Name == name {
|
|
foundIfaces = append(foundIfaces, iface)
|
|
}
|
|
}
|
|
|
|
return foundIfaces
|
|
}
|
|
|
|
// VMTapPair takes a CNI result and returns the vm iface and the tap iface corresponding
|
|
// to the provided vmID. See the vmconf package docs for details on the expected
|
|
// vm and tap iface configurations.
|
|
func VMTapPair(result *current.Result, vmID string) (vmIface, tapIface *current.Interface, err error) {
|
|
vmIfaces, otherIfaces := FilterBySandbox(vmID, result.Interfaces...)
|
|
if len(vmIfaces) > 1 {
|
|
return nil, nil, fmt.Errorf(
|
|
"expected to find at most 1 interface in sandbox %q, but instead found %d",
|
|
vmID, len(vmIfaces))
|
|
} else if len(vmIfaces) == 0 {
|
|
return nil, nil, fmt.Errorf("no pseudo-device for %s", vmID)
|
|
}
|
|
|
|
vmIface = vmIfaces[0]
|
|
|
|
// As specified in the package docstring, the vm interface is given the same name as the
|
|
// corresponding tap device outside the VM. The tap device, however, will be in a sandbox
|
|
// corresponding to a network namespace path.
|
|
tapName := vmIface.Name
|
|
|
|
tapIfaces := IfacesWithName(tapName, otherIfaces...)
|
|
if len(tapIfaces) > 1 {
|
|
return nil, nil, fmt.Errorf(
|
|
"expected to find at most 1 interface with name %q, but instead found %d",
|
|
tapName, len(tapIfaces))
|
|
} else if len(tapIfaces) == 0 {
|
|
return nil, nil, fmt.Errorf("device not found: %s", tapName)
|
|
}
|
|
|
|
tapIface = tapIfaces[0]
|
|
|
|
return vmIface, tapIface, nil
|
|
}
|