Skip to content

Commit

Permalink
Merge pull request #386 from hhyasdf/release/v0.8.2
Browse files Browse the repository at this point in the history
[CHERRY PICK] release for v0.8.2
  • Loading branch information
mars1024 authored Jun 19, 2023
2 parents bff9076 + e5884fd commit bc62c0c
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 78 deletions.
52 changes: 35 additions & 17 deletions cmd/cni/cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@ import (
"runtime"
"strings"

"github.com/vishvananda/netlink"

networkingv1 "github.com/alibaba/hybridnet/pkg/apis/networking/v1"

"github.com/alibaba/hybridnet/pkg/request"

"github.com/containernetworking/cni/pkg/skel"
"github.com/containernetworking/cni/pkg/types"
"github.com/containernetworking/cni/pkg/types/current"
"github.com/containernetworking/cni/pkg/version"
"github.com/containernetworking/plugins/pkg/ns"
"github.com/vishvananda/netlink"

networkingv1 "github.com/alibaba/hybridnet/pkg/apis/networking/v1"
"github.com/alibaba/hybridnet/pkg/request"
)

func init() {
Expand All @@ -59,6 +58,12 @@ func cmdAdd(args *skel.CmdArgs) error {
return err
}

var netNs ns.NetNS
if netNs, err = ns.GetNS(args.Netns); err != nil {
return fmt.Errorf("unable to open netns %q: %v", args.Netns, err)
}
defer netNs.Close()

podName, err := parseValueFromArgs("K8S_POD_NAME", args.Args)
if err != nil {
return err
Expand All @@ -80,19 +85,21 @@ func cmdAdd(args *skel.CmdArgs) error {
return err
}

result, err := generateCNIResult(cniVersion, response)
result, err := generateCNIResult(cniVersion, response, args.IfName, netNs)
if err != nil {
return fmt.Errorf("generate cni result failed: %v", err)
}

return types.PrintResult(result, cniVersion)
}

func generateCNIResult(cniVersion string, cniResponse *request.PodResponse) (*current.Result, error) {
func generateCNIResult(cniVersion string, cniResponse *request.PodResponse, ifName string, netNs ns.NetNS) (*current.Result, error) {
result := &current.Result{CNIVersion: cniVersion}
result.IPs = []*current.IPConfig{}
result.Routes = []*types.Route{}
result.Interfaces = []*current.Interface{}

// fulfill ips and routes by daemon response
for _, address := range cniResponse.IPAddress {
ipAddr, mask, _ := net.ParseCIDR(address.IP)
ip := current.IPConfig{}
Expand Down Expand Up @@ -127,17 +134,28 @@ func generateCNIResult(cniVersion string, cniResponse *request.PodResponse) (*cu
result.Routes = append(result.Routes, &route)
}

// for chained cni plugins
hostVeth, err := netlink.LinkByName(cniResponse.HostInterface)
if err != nil {
return nil, fmt.Errorf("failed to lookup host veth %q: %v", cniResponse.HostInterface, err)
}
// fetch interface info by name in container namespace
if err := netNs.Do(func(_ ns.NetNS) error {
createdInterface, err := netlink.LinkByName(ifName)
if err != nil {
return fmt.Errorf("unable to get created interface %q: %v", ifName, err)
}

result.Interfaces = append(result.Interfaces, &current.Interface{
Name: ifName,
Mac: createdInterface.Attrs().HardwareAddr.String(),
Sandbox: netNs.Path(),
})

hostIface := &current.Interface{}
hostIface.Name = hostVeth.Attrs().Name
hostIface.Mac = hostVeth.Attrs().HardwareAddr.String()
return nil
}); err != nil {
return nil, err
}

result.Interfaces = []*current.Interface{hostIface}
// bind ips with created interface
for _, ip := range result.IPs {
ip.Interface = current.Int(0)
}

return result, nil
}
Expand Down
24 changes: 1 addition & 23 deletions pkg/apis/networking/v1/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (

"github.com/gogf/gf/container/gset"

"github.com/alibaba/hybridnet/pkg/constants"
"github.com/alibaba/hybridnet/pkg/utils"
)

Expand Down Expand Up @@ -281,33 +280,15 @@ func Intersect(rangeA *AddressRange, rangeB *AddressRange) bool {
return false
}

// IsLegacyModel will show whether IPInstance has switched to new version
// TODO: legacy mode, to be removed in the next major version
func IsLegacyModel(ipInstance *IPInstance) bool {
return len(ipInstance.Spec.Binding.ReferredObject.Kind) == 0
}

func IsReserved(ipInstance *IPInstance) bool {
if IsLegacyModel(ipInstance) {
return len(ipInstance.Status.NodeName) == 0
}

return len(ipInstance.Spec.Binding.NodeName) == 0
}

func FetchBindingPodName(ipInstance *IPInstance) string {
if IsLegacyModel(ipInstance) {
return ipInstance.Labels[constants.LabelPod]
}

return ipInstance.Spec.Binding.PodName
}

func FetchBindingNodeName(ipInstance *IPInstance) string {
if IsLegacyModel(ipInstance) {
return ipInstance.Labels[constants.LabelNode]
}

return ipInstance.Spec.Binding.NodeName
}

Expand All @@ -316,10 +297,7 @@ func IsValidIPInstance(ipInstance *IPInstance) bool {
return false
}

if IsLegacyModel(ipInstance) {
return len(ipInstance.Status.Phase) > 0
}

// old version IPInstance is no longer valid
return len(ipInstance.Spec.Binding.ReferredObject.Kind) > 0
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/controllers/networking/networkstatus_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"context"
"time"

"github.com/alibaba/hybridnet/pkg/utils/transform"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -276,7 +278,7 @@ var _ = Describe("Network status controller integration test suite", func() {
context.Background(),
&networkingv1.IPInstance{},
client.MatchingLabels{
constants.LabelPod: pod.Name,
constants.LabelPod: transform.TransferPodNameForLabelValue(pod.Name),
},
client.InNamespace("default"),
)).NotTo(HaveOccurred())
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/networking/pod_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result
if metav1.GetControllerOf(pod) == nil {
var ipInstanceList = &networkingv1.IPInstanceList{}
if err = r.List(ctx, ipInstanceList,
client.MatchingLabels{constants.LabelPod: pod.Name},
client.MatchingLabels{constants.LabelPod: transform.TransferPodNameForLabelValue(pod.Name)},
client.InNamespace(pod.Namespace),
); err != nil {
return ctrl.Result{}, wrapError("failed to list ip instance for pod", err)
Expand Down
19 changes: 10 additions & 9 deletions pkg/controllers/networking/pod_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
networkingv1 "github.com/alibaba/hybridnet/pkg/apis/networking/v1"
"github.com/alibaba/hybridnet/pkg/constants"
"github.com/alibaba/hybridnet/pkg/controllers/utils"
"github.com/alibaba/hybridnet/pkg/utils/transform"
//+kubebuilder:scaffold:imports
)

Expand Down Expand Up @@ -181,7 +182,7 @@ var _ = Describe("Pod controller integration test suite", func() {
context.Background(),
&networkingv1.IPInstance{},
client.MatchingLabels{
constants.LabelPod: podName,
constants.LabelPod: transform.TransferPodNameForLabelValue(podName),
},
client.InNamespace("default"),
)).NotTo(HaveOccurred())
Expand Down Expand Up @@ -706,7 +707,7 @@ var _ = Describe("Pod controller integration test suite", func() {
context.Background(),
&networkingv1.IPInstance{},
client.MatchingLabels{
constants.LabelPod: podName,
constants.LabelPod: transform.TransferPodNameForLabelValue(podName),
},
client.InNamespace("default"),
)).NotTo(HaveOccurred())
Expand Down Expand Up @@ -855,7 +856,7 @@ var _ = Describe("Pod controller integration test suite", func() {
context.Background(),
&networkingv1.IPInstance{},
client.MatchingLabels{
constants.LabelPod: podName,
constants.LabelPod: transform.TransferPodNameForLabelValue(podName),
},
client.InNamespace("default"),
)).NotTo(HaveOccurred())
Expand Down Expand Up @@ -1117,7 +1118,7 @@ var _ = Describe("Pod controller integration test suite", func() {
context.Background(),
&networkingv1.IPInstance{},
client.MatchingLabels{
constants.LabelPod: podName,
constants.LabelPod: transform.TransferPodNameForLabelValue(podName),
},
client.InNamespace("default"),
)).NotTo(HaveOccurred())
Expand Down Expand Up @@ -1270,7 +1271,7 @@ var _ = Describe("Pod controller integration test suite", func() {
context.Background(),
&networkingv1.IPInstance{},
client.MatchingLabels{
constants.LabelPod: podName,
constants.LabelPod: transform.TransferPodNameForLabelValue(podName),
},
client.InNamespace("default"),
)).NotTo(HaveOccurred())
Expand Down Expand Up @@ -1463,7 +1464,7 @@ var _ = Describe("Pod controller integration test suite", func() {
context.Background(),
&networkingv1.IPInstance{},
client.MatchingLabels{
constants.LabelPod: podName,
constants.LabelPod: transform.TransferPodNameForLabelValue(podName),
},
client.InNamespace("default"),
)).NotTo(HaveOccurred())
Expand Down Expand Up @@ -1725,7 +1726,7 @@ var _ = Describe("Pod controller integration test suite", func() {
context.Background(),
&networkingv1.IPInstance{},
client.MatchingLabels{
constants.LabelPod: podName,
constants.LabelPod: transform.TransferPodNameForLabelValue(podName),
},
client.InNamespace("default"),
)).NotTo(HaveOccurred())
Expand Down Expand Up @@ -1909,7 +1910,7 @@ var _ = Describe("Pod controller integration test suite", func() {
context.Background(),
&networkingv1.IPInstance{},
client.MatchingLabels{
constants.LabelPod: podName,
constants.LabelPod: transform.TransferPodNameForLabelValue(podName),
},
client.InNamespace("default"),
)).NotTo(HaveOccurred())
Expand Down Expand Up @@ -2080,7 +2081,7 @@ var _ = Describe("Pod controller integration test suite", func() {
context.Background(),
&networkingv1.IPInstance{},
client.MatchingLabels{
constants.LabelPod: podName,
constants.LabelPod: transform.TransferPodNameForLabelValue(podName),
},
client.InNamespace("default"),
)).NotTo(HaveOccurred())
Expand Down
25 changes: 5 additions & 20 deletions pkg/controllers/networking/pod_ip_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,30 +68,15 @@ func NewPodIPCache(ctx context.Context, c client.Reader, logger logr.Logger) (Po
}

for _, ip := range ipList.Items {
if networkingv1.IsLegacyModel(&ip) {
return nil, fmt.Errorf("get legacy model ip instance, if this happens more than once, " +
"please check if the networking CRD yamls is updated to the latest v0.5 version")
if !networkingv1.IsValidIPInstance(&ip) {
return nil, fmt.Errorf("get legacy model ip instance, " +
"please check if the networking CRD yamls is updated to the latest v0.5 version and manager should also be " +
"updated to v0.5.x at first to update IPInstances")
}

podName := networkingv1.FetchBindingPodName(&ip)
if len(podName) != 0 {
var podUID types.UID

if len(ip.Spec.Binding.PodUID) != 0 {
podUID = ip.Spec.Binding.PodUID
} else if !networkingv1.IsReserved(&ip) {
// TODO: no longer need to get pod if all the ip instances is updated to the v1.2 version
pod, err := controllerutils.GetPod(ctx, c, podName, ip.Namespace)
if err != nil {
if err = client.IgnoreNotFound(err); err != nil {
return nil, fmt.Errorf("unable to get Pod %v for IPInstance %v: %v", podName, ip.Name, err)
}
}

if pod != nil {
podUID = pod.UID
}
}
podUID := ip.Spec.Binding.PodUID

var recordedIPInstances []string
if cache.podToIP[namespacedKey(podName, ip.Namespace)] == nil {
Expand Down
4 changes: 3 additions & 1 deletion pkg/controllers/networking/subnetstatus_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"fmt"
"time"

"github.com/alibaba/hybridnet/pkg/utils/transform"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -360,7 +362,7 @@ var _ = Describe("Subnet status controller integration test suite", func() {
context.Background(),
&networkingv1.IPInstance{},
client.MatchingLabels{
constants.LabelPod: pod.Name,
constants.LabelPod: transform.TransferPodNameForLabelValue(pod.Name),
},
client.InNamespace("default"),
)).NotTo(HaveOccurred())
Expand Down
4 changes: 3 additions & 1 deletion pkg/controllers/utils/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"context"
"fmt"

"github.com/alibaba/hybridnet/pkg/utils/transform"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/labels"
Expand Down Expand Up @@ -271,7 +273,7 @@ func ListAllocatedIPInstances(ctx context.Context, c client.Reader, opts ...clie
func ListAllocatedIPInstancesOfPod(ctx context.Context, c client.Reader, pod *corev1.Pod) (ips []*networkingv1.IPInstance, err error) {
return ListAllocatedIPInstances(ctx, c,
client.MatchingLabels{
constants.LabelPod: pod.Name,
constants.LabelPod: transform.TransferPodNameForLabelValue(pod.Name),
},
client.InNamespace(pod.Namespace),
)
Expand Down
8 changes: 5 additions & 3 deletions pkg/ipam/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"context"
"fmt"

"github.com/alibaba/hybridnet/pkg/utils/transform"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -141,7 +143,7 @@ func (s *crdStore) DeCouple(ctx context.Context, pod *corev1.Pod) (err error) {
if err = s.List(ctx,
ipInstanceList,
client.MatchingLabels{
constants.LabelPod: pod.Name,
constants.LabelPod: transform.TransferPodNameForLabelValue(pod.Name),
},
client.InNamespace(pod.Namespace),
); err != nil {
Expand Down Expand Up @@ -170,7 +172,7 @@ func (s *crdStore) IPReserve(ctx context.Context, pod *corev1.Pod, opts ...ipamt
if err = s.List(ctx,
ipInstanceList,
client.MatchingLabels{
constants.LabelPod: pod.Name,
constants.LabelPod: transform.TransferPodNameForLabelValue(pod.Name),
},
client.InNamespace(pod.Namespace),
); err != nil {
Expand Down Expand Up @@ -327,7 +329,7 @@ func assembleIPInstance(ipIns *networkingv1.IPInstance, ip *ipamtypes.IP, pod *c
ipIns.Labels[constants.LabelSubnet] = ip.Subnet
ipIns.Labels[constants.LabelNetwork] = ip.Network
ipIns.Labels[constants.LabelNode] = pod.Spec.NodeName
ipIns.Labels[constants.LabelPod] = pod.Name
ipIns.Labels[constants.LabelPod] = transform.TransferPodNameForLabelValue(pod.Name)
ipIns.Labels[constants.LabelPodUID] = string(pod.UID)

// additional labels will be patched
Expand Down
Loading

0 comments on commit bc62c0c

Please sign in to comment.