Skip to content

Commit bc62c0c

Browse files
authored
Merge pull request #386 from hhyasdf/release/v0.8.2
[CHERRY PICK] release for v0.8.2
2 parents bff9076 + e5884fd commit bc62c0c

File tree

12 files changed

+85
-78
lines changed

12 files changed

+85
-78
lines changed

cmd/cni/cni.go

+35-17
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,15 @@ import (
2424
"runtime"
2525
"strings"
2626

27-
"github.com/vishvananda/netlink"
28-
29-
networkingv1 "github.com/alibaba/hybridnet/pkg/apis/networking/v1"
30-
31-
"github.com/alibaba/hybridnet/pkg/request"
32-
3327
"github.com/containernetworking/cni/pkg/skel"
3428
"github.com/containernetworking/cni/pkg/types"
3529
"github.com/containernetworking/cni/pkg/types/current"
3630
"github.com/containernetworking/cni/pkg/version"
31+
"github.com/containernetworking/plugins/pkg/ns"
32+
"github.com/vishvananda/netlink"
33+
34+
networkingv1 "github.com/alibaba/hybridnet/pkg/apis/networking/v1"
35+
"github.com/alibaba/hybridnet/pkg/request"
3736
)
3837

3938
func init() {
@@ -59,6 +58,12 @@ func cmdAdd(args *skel.CmdArgs) error {
5958
return err
6059
}
6160

61+
var netNs ns.NetNS
62+
if netNs, err = ns.GetNS(args.Netns); err != nil {
63+
return fmt.Errorf("unable to open netns %q: %v", args.Netns, err)
64+
}
65+
defer netNs.Close()
66+
6267
podName, err := parseValueFromArgs("K8S_POD_NAME", args.Args)
6368
if err != nil {
6469
return err
@@ -80,19 +85,21 @@ func cmdAdd(args *skel.CmdArgs) error {
8085
return err
8186
}
8287

83-
result, err := generateCNIResult(cniVersion, response)
88+
result, err := generateCNIResult(cniVersion, response, args.IfName, netNs)
8489
if err != nil {
8590
return fmt.Errorf("generate cni result failed: %v", err)
8691
}
8792

8893
return types.PrintResult(result, cniVersion)
8994
}
9095

91-
func generateCNIResult(cniVersion string, cniResponse *request.PodResponse) (*current.Result, error) {
96+
func generateCNIResult(cniVersion string, cniResponse *request.PodResponse, ifName string, netNs ns.NetNS) (*current.Result, error) {
9297
result := &current.Result{CNIVersion: cniVersion}
9398
result.IPs = []*current.IPConfig{}
9499
result.Routes = []*types.Route{}
100+
result.Interfaces = []*current.Interface{}
95101

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

130-
// for chained cni plugins
131-
hostVeth, err := netlink.LinkByName(cniResponse.HostInterface)
132-
if err != nil {
133-
return nil, fmt.Errorf("failed to lookup host veth %q: %v", cniResponse.HostInterface, err)
134-
}
137+
// fetch interface info by name in container namespace
138+
if err := netNs.Do(func(_ ns.NetNS) error {
139+
createdInterface, err := netlink.LinkByName(ifName)
140+
if err != nil {
141+
return fmt.Errorf("unable to get created interface %q: %v", ifName, err)
142+
}
143+
144+
result.Interfaces = append(result.Interfaces, &current.Interface{
145+
Name: ifName,
146+
Mac: createdInterface.Attrs().HardwareAddr.String(),
147+
Sandbox: netNs.Path(),
148+
})
135149

136-
hostIface := &current.Interface{}
137-
hostIface.Name = hostVeth.Attrs().Name
138-
hostIface.Mac = hostVeth.Attrs().HardwareAddr.String()
150+
return nil
151+
}); err != nil {
152+
return nil, err
153+
}
139154

140-
result.Interfaces = []*current.Interface{hostIface}
155+
// bind ips with created interface
156+
for _, ip := range result.IPs {
157+
ip.Interface = current.Int(0)
158+
}
141159

142160
return result, nil
143161
}

pkg/apis/networking/v1/utils.go

+1-23
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626

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

29-
"github.com/alibaba/hybridnet/pkg/constants"
3029
"github.com/alibaba/hybridnet/pkg/utils"
3130
)
3231

@@ -281,33 +280,15 @@ func Intersect(rangeA *AddressRange, rangeB *AddressRange) bool {
281280
return false
282281
}
283282

284-
// IsLegacyModel will show whether IPInstance has switched to new version
285-
// TODO: legacy mode, to be removed in the next major version
286-
func IsLegacyModel(ipInstance *IPInstance) bool {
287-
return len(ipInstance.Spec.Binding.ReferredObject.Kind) == 0
288-
}
289-
290283
func IsReserved(ipInstance *IPInstance) bool {
291-
if IsLegacyModel(ipInstance) {
292-
return len(ipInstance.Status.NodeName) == 0
293-
}
294-
295284
return len(ipInstance.Spec.Binding.NodeName) == 0
296285
}
297286

298287
func FetchBindingPodName(ipInstance *IPInstance) string {
299-
if IsLegacyModel(ipInstance) {
300-
return ipInstance.Labels[constants.LabelPod]
301-
}
302-
303288
return ipInstance.Spec.Binding.PodName
304289
}
305290

306291
func FetchBindingNodeName(ipInstance *IPInstance) string {
307-
if IsLegacyModel(ipInstance) {
308-
return ipInstance.Labels[constants.LabelNode]
309-
}
310-
311292
return ipInstance.Spec.Binding.NodeName
312293
}
313294

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

319-
if IsLegacyModel(ipInstance) {
320-
return len(ipInstance.Status.Phase) > 0
321-
}
322-
300+
// old version IPInstance is no longer valid
323301
return len(ipInstance.Spec.Binding.ReferredObject.Kind) > 0
324302
}
325303

pkg/controllers/networking/networkstatus_controller_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"context"
2121
"time"
2222

23+
"github.com/alibaba/hybridnet/pkg/utils/transform"
24+
2325
. "github.com/onsi/ginkgo"
2426
. "github.com/onsi/gomega"
2527
corev1 "k8s.io/api/core/v1"
@@ -276,7 +278,7 @@ var _ = Describe("Network status controller integration test suite", func() {
276278
context.Background(),
277279
&networkingv1.IPInstance{},
278280
client.MatchingLabels{
279-
constants.LabelPod: pod.Name,
281+
constants.LabelPod: transform.TransferPodNameForLabelValue(pod.Name),
280282
},
281283
client.InNamespace("default"),
282284
)).NotTo(HaveOccurred())

pkg/controllers/networking/pod_controller.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result
121121
if metav1.GetControllerOf(pod) == nil {
122122
var ipInstanceList = &networkingv1.IPInstanceList{}
123123
if err = r.List(ctx, ipInstanceList,
124-
client.MatchingLabels{constants.LabelPod: pod.Name},
124+
client.MatchingLabels{constants.LabelPod: transform.TransferPodNameForLabelValue(pod.Name)},
125125
client.InNamespace(pod.Namespace),
126126
); err != nil {
127127
return ctrl.Result{}, wrapError("failed to list ip instance for pod", err)

pkg/controllers/networking/pod_controller_test.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
networkingv1 "github.com/alibaba/hybridnet/pkg/apis/networking/v1"
3838
"github.com/alibaba/hybridnet/pkg/constants"
3939
"github.com/alibaba/hybridnet/pkg/controllers/utils"
40+
"github.com/alibaba/hybridnet/pkg/utils/transform"
4041
//+kubebuilder:scaffold:imports
4142
)
4243

@@ -181,7 +182,7 @@ var _ = Describe("Pod controller integration test suite", func() {
181182
context.Background(),
182183
&networkingv1.IPInstance{},
183184
client.MatchingLabels{
184-
constants.LabelPod: podName,
185+
constants.LabelPod: transform.TransferPodNameForLabelValue(podName),
185186
},
186187
client.InNamespace("default"),
187188
)).NotTo(HaveOccurred())
@@ -706,7 +707,7 @@ var _ = Describe("Pod controller integration test suite", func() {
706707
context.Background(),
707708
&networkingv1.IPInstance{},
708709
client.MatchingLabels{
709-
constants.LabelPod: podName,
710+
constants.LabelPod: transform.TransferPodNameForLabelValue(podName),
710711
},
711712
client.InNamespace("default"),
712713
)).NotTo(HaveOccurred())
@@ -855,7 +856,7 @@ var _ = Describe("Pod controller integration test suite", func() {
855856
context.Background(),
856857
&networkingv1.IPInstance{},
857858
client.MatchingLabels{
858-
constants.LabelPod: podName,
859+
constants.LabelPod: transform.TransferPodNameForLabelValue(podName),
859860
},
860861
client.InNamespace("default"),
861862
)).NotTo(HaveOccurred())
@@ -1117,7 +1118,7 @@ var _ = Describe("Pod controller integration test suite", func() {
11171118
context.Background(),
11181119
&networkingv1.IPInstance{},
11191120
client.MatchingLabels{
1120-
constants.LabelPod: podName,
1121+
constants.LabelPod: transform.TransferPodNameForLabelValue(podName),
11211122
},
11221123
client.InNamespace("default"),
11231124
)).NotTo(HaveOccurred())
@@ -1270,7 +1271,7 @@ var _ = Describe("Pod controller integration test suite", func() {
12701271
context.Background(),
12711272
&networkingv1.IPInstance{},
12721273
client.MatchingLabels{
1273-
constants.LabelPod: podName,
1274+
constants.LabelPod: transform.TransferPodNameForLabelValue(podName),
12741275
},
12751276
client.InNamespace("default"),
12761277
)).NotTo(HaveOccurred())
@@ -1463,7 +1464,7 @@ var _ = Describe("Pod controller integration test suite", func() {
14631464
context.Background(),
14641465
&networkingv1.IPInstance{},
14651466
client.MatchingLabels{
1466-
constants.LabelPod: podName,
1467+
constants.LabelPod: transform.TransferPodNameForLabelValue(podName),
14671468
},
14681469
client.InNamespace("default"),
14691470
)).NotTo(HaveOccurred())
@@ -1725,7 +1726,7 @@ var _ = Describe("Pod controller integration test suite", func() {
17251726
context.Background(),
17261727
&networkingv1.IPInstance{},
17271728
client.MatchingLabels{
1728-
constants.LabelPod: podName,
1729+
constants.LabelPod: transform.TransferPodNameForLabelValue(podName),
17291730
},
17301731
client.InNamespace("default"),
17311732
)).NotTo(HaveOccurred())
@@ -1909,7 +1910,7 @@ var _ = Describe("Pod controller integration test suite", func() {
19091910
context.Background(),
19101911
&networkingv1.IPInstance{},
19111912
client.MatchingLabels{
1912-
constants.LabelPod: podName,
1913+
constants.LabelPod: transform.TransferPodNameForLabelValue(podName),
19131914
},
19141915
client.InNamespace("default"),
19151916
)).NotTo(HaveOccurred())
@@ -2080,7 +2081,7 @@ var _ = Describe("Pod controller integration test suite", func() {
20802081
context.Background(),
20812082
&networkingv1.IPInstance{},
20822083
client.MatchingLabels{
2083-
constants.LabelPod: podName,
2084+
constants.LabelPod: transform.TransferPodNameForLabelValue(podName),
20842085
},
20852086
client.InNamespace("default"),
20862087
)).NotTo(HaveOccurred())

pkg/controllers/networking/pod_ip_cache.go

+5-20
Original file line numberDiff line numberDiff line change
@@ -68,30 +68,15 @@ func NewPodIPCache(ctx context.Context, c client.Reader, logger logr.Logger) (Po
6868
}
6969

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

7677
podName := networkingv1.FetchBindingPodName(&ip)
7778
if len(podName) != 0 {
78-
var podUID types.UID
79-
80-
if len(ip.Spec.Binding.PodUID) != 0 {
81-
podUID = ip.Spec.Binding.PodUID
82-
} else if !networkingv1.IsReserved(&ip) {
83-
// TODO: no longer need to get pod if all the ip instances is updated to the v1.2 version
84-
pod, err := controllerutils.GetPod(ctx, c, podName, ip.Namespace)
85-
if err != nil {
86-
if err = client.IgnoreNotFound(err); err != nil {
87-
return nil, fmt.Errorf("unable to get Pod %v for IPInstance %v: %v", podName, ip.Name, err)
88-
}
89-
}
90-
91-
if pod != nil {
92-
podUID = pod.UID
93-
}
94-
}
79+
podUID := ip.Spec.Binding.PodUID
9580

9681
var recordedIPInstances []string
9782
if cache.podToIP[namespacedKey(podName, ip.Namespace)] == nil {

pkg/controllers/networking/subnetstatus_controller_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"fmt"
2222
"time"
2323

24+
"github.com/alibaba/hybridnet/pkg/utils/transform"
25+
2426
. "github.com/onsi/ginkgo"
2527
. "github.com/onsi/gomega"
2628
corev1 "k8s.io/api/core/v1"
@@ -360,7 +362,7 @@ var _ = Describe("Subnet status controller integration test suite", func() {
360362
context.Background(),
361363
&networkingv1.IPInstance{},
362364
client.MatchingLabels{
363-
constants.LabelPod: pod.Name,
365+
constants.LabelPod: transform.TransferPodNameForLabelValue(pod.Name),
364366
},
365367
client.InNamespace("default"),
366368
)).NotTo(HaveOccurred())

pkg/controllers/utils/client.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"context"
2121
"fmt"
2222

23+
"github.com/alibaba/hybridnet/pkg/utils/transform"
24+
2325
corev1 "k8s.io/api/core/v1"
2426
"k8s.io/apimachinery/pkg/api/errors"
2527
"k8s.io/apimachinery/pkg/labels"
@@ -271,7 +273,7 @@ func ListAllocatedIPInstances(ctx context.Context, c client.Reader, opts ...clie
271273
func ListAllocatedIPInstancesOfPod(ctx context.Context, c client.Reader, pod *corev1.Pod) (ips []*networkingv1.IPInstance, err error) {
272274
return ListAllocatedIPInstances(ctx, c,
273275
client.MatchingLabels{
274-
constants.LabelPod: pod.Name,
276+
constants.LabelPod: transform.TransferPodNameForLabelValue(pod.Name),
275277
},
276278
client.InNamespace(pod.Namespace),
277279
)

pkg/ipam/store/store.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"context"
2121
"fmt"
2222

23+
"github.com/alibaba/hybridnet/pkg/utils/transform"
24+
2325
corev1 "k8s.io/api/core/v1"
2426
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2527
"k8s.io/apimachinery/pkg/types"
@@ -141,7 +143,7 @@ func (s *crdStore) DeCouple(ctx context.Context, pod *corev1.Pod) (err error) {
141143
if err = s.List(ctx,
142144
ipInstanceList,
143145
client.MatchingLabels{
144-
constants.LabelPod: pod.Name,
146+
constants.LabelPod: transform.TransferPodNameForLabelValue(pod.Name),
145147
},
146148
client.InNamespace(pod.Namespace),
147149
); err != nil {
@@ -170,7 +172,7 @@ func (s *crdStore) IPReserve(ctx context.Context, pod *corev1.Pod, opts ...ipamt
170172
if err = s.List(ctx,
171173
ipInstanceList,
172174
client.MatchingLabels{
173-
constants.LabelPod: pod.Name,
175+
constants.LabelPod: transform.TransferPodNameForLabelValue(pod.Name),
174176
},
175177
client.InNamespace(pod.Namespace),
176178
); err != nil {
@@ -327,7 +329,7 @@ func assembleIPInstance(ipIns *networkingv1.IPInstance, ip *ipamtypes.IP, pod *c
327329
ipIns.Labels[constants.LabelSubnet] = ip.Subnet
328330
ipIns.Labels[constants.LabelNetwork] = ip.Network
329331
ipIns.Labels[constants.LabelNode] = pod.Spec.NodeName
330-
ipIns.Labels[constants.LabelPod] = pod.Name
332+
ipIns.Labels[constants.LabelPod] = transform.TransferPodNameForLabelValue(pod.Name)
331333
ipIns.Labels[constants.LabelPodUID] = string(pod.UID)
332334

333335
// additional labels will be patched

0 commit comments

Comments
 (0)