Skip to content

Commit 16bfa42

Browse files
committed
parse podNetworking to podNetworks
1 parent fd6b172 commit 16bfa42

File tree

16 files changed

+383
-320
lines changed

16 files changed

+383
-320
lines changed

cmd/terway-controlplane/terway-controlplane.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ import (
2121
"math/rand"
2222
"time"
2323

24-
"github.com/AliyunContainerService/terway/pkg/aliyun/client"
24+
aliyun "github.com/AliyunContainerService/terway/pkg/aliyun/client"
2525
"github.com/AliyunContainerService/terway/pkg/aliyun/credential"
2626
"github.com/AliyunContainerService/terway/pkg/apis/crds"
2727
networkv1beta1 "github.com/AliyunContainerService/terway/pkg/apis/network.alibabacloud.com/v1beta1"
28+
"github.com/AliyunContainerService/terway/pkg/backoff"
2829
"github.com/AliyunContainerService/terway/pkg/cert"
2930
register "github.com/AliyunContainerService/terway/pkg/controller"
3031
_ "github.com/AliyunContainerService/terway/pkg/controller/all"
@@ -35,7 +36,6 @@ import (
3536
"github.com/AliyunContainerService/terway/pkg/utils"
3637
"github.com/AliyunContainerService/terway/pkg/version"
3738
"github.com/AliyunContainerService/terway/types/controlplane"
38-
3939
"k8s.io/apimachinery/pkg/runtime"
4040
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
4141
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
@@ -60,14 +60,25 @@ func init() {
6060

6161
func main() {
6262
rand.Seed(time.Now().UnixNano())
63+
var (
64+
configFilePath string
65+
credentialFilePath string
66+
)
67+
flag.StringVar(&configFilePath, "config", "/etc/config/ctrl-config.yaml", "config file for controlplane")
68+
flag.StringVar(&credentialFilePath, "credential", "/etc/credential/ctrl-secret.yaml", "secret file for controlplane")
69+
6370
flag.Parse()
6471

6572
ctrl.SetLogger(klogr.New())
6673
log.Info(version.Version)
6774

68-
cfg := controlplane.GetConfig()
69-
log.Info("using config", "config", cfg)
75+
ctx := ctrl.SetupSignalHandler()
7076

77+
cfg, err := controlplane.ParseAndValidate(configFilePath, credentialFilePath)
78+
if err != nil {
79+
panic(err)
80+
}
81+
backoff.OverrideBackoff(cfg.BackoffOverride)
7182
utils.SetStsKinds(cfg.CustomStatefulWorkloadKinds)
7283

7384
restConfig := ctrl.GetConfigOrDie()
@@ -76,7 +87,9 @@ func main() {
7687
restConfig.UserAgent = version.UA
7788
utils.RegisterClients(restConfig)
7889

79-
err := crds.RegisterCRDs()
90+
log.Info("using config", "config", cfg)
91+
92+
err = crds.RegisterCRDs()
8093
if err != nil {
8194
panic(err)
8295
}
@@ -91,13 +104,11 @@ func main() {
91104
panic(err)
92105
}
93106

94-
aliyunClient, err := client.New(clientSet, flowcontrol.NewTokenBucketRateLimiter(cfg.ReadOnlyQPS, cfg.ReadOnlyBurst), flowcontrol.NewTokenBucketRateLimiter(cfg.MutatingQPS, cfg.MutatingBurst))
107+
aliyunClient, err := aliyun.New(clientSet, flowcontrol.NewTokenBucketRateLimiter(cfg.ReadOnlyQPS, cfg.ReadOnlyBurst), flowcontrol.NewTokenBucketRateLimiter(cfg.MutatingQPS, cfg.MutatingBurst))
95108
if err != nil {
96109
panic(err)
97110
}
98111

99-
ctx := ctrl.SetupSignalHandler()
100-
101112
mgr, err := ctrl.NewManager(restConfig, ctrl.Options{
102113
Scheme: scheme,
103114
HealthProbeBindAddress: cfg.HealthzBindAddress,

daemon/daemon.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/AliyunContainerService/terway/pkg/utils"
3030
"github.com/AliyunContainerService/terway/rpc"
3131
"github.com/AliyunContainerService/terway/types"
32+
"github.com/AliyunContainerService/terway/types/daemon"
3233

3334
"github.com/containernetworking/cni/libcni"
3435
containertypes "github.com/containernetworking/cni/pkg/types"
@@ -1347,7 +1348,7 @@ func newNetworkService(configFilePath, kubeconfig, master, daemonMode string) (r
13471348

13481349
var err error
13491350

1350-
globalConfig, err := types.GetConfigFromFileWithMerge(configFilePath, nil)
1351+
globalConfig, err := daemon.GetConfigFromFileWithMerge(configFilePath, nil)
13511352
if err != nil {
13521353
return nil, err
13531354
}
@@ -1364,7 +1365,7 @@ func newNetworkService(configFilePath, kubeconfig, master, daemonMode string) (r
13641365
dynamicCfg = ""
13651366
}
13661367

1367-
config, err := types.GetConfigFromFileWithMerge(configFilePath, []byte(dynamicCfg))
1368+
config, err := daemon.GetConfigFromFileWithMerge(configFilePath, []byte(dynamicCfg))
13681369
if err != nil {
13691370
return nil, fmt.Errorf("failed parse config: %v", err)
13701371
}
@@ -1594,7 +1595,7 @@ func restoreLocalENIRes(ecs ipam.API, k8s Kubernetes, resourceDB storage.Storage
15941595
}
15951596

15961597
// setup default value
1597-
func setDefault(cfg *types.Configure) error {
1598+
func setDefault(cfg *daemon.Config) error {
15981599
if cfg.EniCapRatio == 0 {
15991600
cfg.EniCapRatio = 1
16001601
}
@@ -1611,7 +1612,7 @@ func setDefault(cfg *types.Configure) error {
16111612
return nil
16121613
}
16131614

1614-
func validateConfig(cfg *types.Configure) error {
1615+
func validateConfig(cfg *daemon.Config) error {
16151616
switch cfg.IPStack {
16161617
case "", string(types.IPStackIPv4), string(types.IPStackDual):
16171618
default:
@@ -1621,7 +1622,7 @@ func validateConfig(cfg *types.Configure) error {
16211622
return nil
16221623
}
16231624

1624-
func getPoolConfig(cfg *types.Configure, ipamType types.IPAMType) (*types.PoolConfig, error) {
1625+
func getPoolConfig(cfg *daemon.Config, ipamType types.IPAMType) (*types.PoolConfig, error) {
16251626
poolConfig := &types.PoolConfig{
16261627
MaxPoolSize: cfg.MaxPoolSize,
16271628
MinPoolSize: cfg.MinPoolSize,

daemon/k8s.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/AliyunContainerService/terway/pkg/utils"
2222
"github.com/AliyunContainerService/terway/pkg/version"
2323
"github.com/AliyunContainerService/terway/types"
24+
"github.com/AliyunContainerService/terway/types/daemon"
2425

2526
"github.com/pkg/errors"
2627
log "github.com/sirupsen/logrus"
@@ -269,7 +270,7 @@ func (k *k8s) WaitTrunkReady() (string, error) {
269270
}
270271

271272
// newK8S return Kubernetes service by pod spec and daemon mode
272-
func newK8S(master, kubeconfig string, daemonMode string, globalConfig *types.Configure) (Kubernetes, error) {
273+
func newK8S(master, kubeconfig string, daemonMode string, globalConfig *daemon.Config) (Kubernetes, error) {
273274

274275
k8sRestConfig, err := clientcmd.BuildConfigFromFlags(master, kubeconfig)
275276
if err != nil {

pkg/controller/common/match.go

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -18,70 +18,11 @@ package common
1818

1919
import (
2020
"context"
21-
"fmt"
2221

2322
"github.com/AliyunContainerService/terway/pkg/apis/network.alibabacloud.com/v1beta1"
24-
"github.com/AliyunContainerService/terway/pkg/utils"
25-
26-
corev1 "k8s.io/api/core/v1"
27-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28-
"k8s.io/apimachinery/pkg/labels"
2923
"sigs.k8s.io/controller-runtime/pkg/client"
3024
)
3125

32-
// MatchOnePodNetworking will range all podNetworking and try to found a matched podNetworking for this pod
33-
// for stateless pod Fixed ip config is never matched
34-
func MatchOnePodNetworking(pod *corev1.Pod, ns *corev1.Namespace, networkings []v1beta1.PodNetworking) (*v1beta1.PodNetworking, error) {
35-
podLabels := labels.Set(pod.Labels)
36-
nsLabels := labels.Set(ns.Labels)
37-
for _, podNetworking := range networkings {
38-
if podNetworking.Status.Status != v1beta1.NetworkingStatusReady {
39-
continue
40-
}
41-
if !utils.IsStsPod(pod) {
42-
// for fixed ip , only match sts pod
43-
if podNetworking.Spec.AllocationType.Type == v1beta1.IPAllocTypeFixed {
44-
continue
45-
}
46-
}
47-
48-
matchOne := false
49-
if podNetworking.Spec.Selector.PodSelector != nil {
50-
ok, err := PodMatchSelector(podNetworking.Spec.Selector.PodSelector, podLabels)
51-
if err != nil {
52-
return nil, fmt.Errorf("error match pod selector, %w", err)
53-
}
54-
if !ok {
55-
continue
56-
}
57-
matchOne = true
58-
}
59-
if podNetworking.Spec.Selector.NamespaceSelector != nil {
60-
ok, err := PodMatchSelector(podNetworking.Spec.Selector.NamespaceSelector, nsLabels)
61-
if err != nil {
62-
return nil, fmt.Errorf("error match namespace selector, %w", err)
63-
}
64-
if !ok {
65-
continue
66-
}
67-
matchOne = true
68-
}
69-
if matchOne {
70-
return &podNetworking, nil
71-
}
72-
}
73-
return nil, nil
74-
}
75-
76-
// PodMatchSelector pod is selected by selector
77-
func PodMatchSelector(labelSelector *metav1.LabelSelector, l labels.Set) (bool, error) {
78-
selector, err := metav1.LabelSelectorAsSelector(labelSelector)
79-
if err != nil {
80-
return false, err
81-
}
82-
return selector.Matches(l), nil
83-
}
84-
8526
// UpdatePodENI update cr
8627
func UpdatePodENI(ctx context.Context, c client.Client, update *v1beta1.PodENI) (*v1beta1.PodENI, error) {
8728
var err error
@@ -107,16 +48,3 @@ func UpdatePodENIStatus(ctx context.Context, c client.Client, update *v1beta1.Po
10748
}
10849
return nil, err
10950
}
110-
111-
// PatchPodENIStatus set cr status
112-
func PatchPodENIStatus(ctx context.Context, c client.Client, update, old *v1beta1.PodENI) (*v1beta1.PodENI, error) {
113-
var err error
114-
for i := 0; i < 2; i++ {
115-
err = c.Status().Patch(ctx, update, client.MergeFrom(old))
116-
if err != nil {
117-
continue
118-
}
119-
return update, nil
120-
}
121-
return nil, err
122-
}

pkg/controller/node/node_controller.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/AliyunContainerService/terway/pkg/controller/vswitch"
1616
"github.com/AliyunContainerService/terway/types"
1717
"github.com/AliyunContainerService/terway/types/controlplane"
18+
"github.com/AliyunContainerService/terway/types/daemon"
1819
"k8s.io/client-go/util/retry"
1920

2021
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
@@ -209,7 +210,7 @@ func (m *ReconcileNode) ensureTrunkENI(ctx context.Context, node *corev1.Node) (
209210
l.Info("no eni found, will create trunk eni")
210211

211212
// create and attach eni
212-
eniConfig, err := common.ConfigFromConfigMConfigFromConfigMap(ctx, m.client, node.Name)
213+
eniConfig, err := daemon.ConfigFromConfigMap(ctx, m.client, node.Name)
213214
if err != nil {
214215
return reconcile.Result{}, err
215216
}
@@ -284,7 +285,7 @@ func (m *ReconcileNode) initENIManagerForNode(ctx context.Context, node *corev1.
284285
return err
285286
}
286287

287-
eniConfig, err := common.ConfigFromConfigMConfigFromConfigMap(ctx, m.client, node.Name)
288+
eniConfig, err := daemon.ConfigFromConfigMap(ctx, m.client, node.Name)
288289
if err != nil {
289290
return err
290291
}

pkg/controller/pod-eni/eni_controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,8 @@ func (m *ReconcilePodENI) gcCRPodENIs(ctx context.Context) {
595595
ll.V(5).Info("update pod lastSeen to now")
596596
update := podENI.DeepCopy()
597597
update.Status.PodLastSeen = metav1.Now()
598-
_, err = common.PatchPodENIStatus(ctx, m.client, update, &podENI)
598+
599+
err = m.client.Status().Patch(ctx, update, client.MergeFrom(&podENI))
599600
if err != nil {
600601
ll.Error(err, "error update timestamp")
601602
}

pkg/controller/pod-networking/networking_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//go:build test_env
2-
// +build test_env
32

43
package podnetworking
54

pkg/controller/pod-networking/suite_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//go:build test_env
2-
// +build test_env
32

43
package podnetworking
54

@@ -12,6 +11,7 @@ import (
1211
networkv1beta1 "github.com/AliyunContainerService/terway/pkg/apis/network.alibabacloud.com/v1beta1"
1312
register "github.com/AliyunContainerService/terway/pkg/controller"
1413
"github.com/AliyunContainerService/terway/pkg/controller/vswitch"
14+
"github.com/AliyunContainerService/terway/types/controlplane"
1515

1616
"github.com/aliyun/alibaba-cloud-sdk-go/services/vpc"
1717
. "github.com/onsi/ginkgo"
@@ -74,7 +74,12 @@ var _ = BeforeSuite(func() {
7474
},
7575
},
7676
}
77-
err = register.Controllers[controllerName](k8sManager, fakeClient, vsw)
77+
err = register.Controllers[controllerName].Creator(k8sManager, &register.ControllerCtx{
78+
Config: &controlplane.Config{},
79+
VSwitchPool: vsw,
80+
AliyunClient: fakeClient,
81+
DelegateClient: fakeClient,
82+
})
7883
Expect(err).ToNot(HaveOccurred())
7984

8085
go func() {

pkg/controller/pod/pod_controller.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import (
4949
)
5050

5151
const controllerName = "pod"
52+
const defaultInterface = "eth0"
5253

5354
func init() {
5455
register.Add(controllerName, func(mgr manager.Manager, ctrlCtx *register.ControllerCtx) error {
@@ -471,7 +472,7 @@ func (m *ReconcilePod) reConfig(ctx context.Context, pod *corev1.Pod, prePodENI
471472
for i, n := range anno.PodNetworks {
472473
name := n.Interface
473474
if name == "" {
474-
name = "eth0"
475+
name = defaultInterface
475476
}
476477
targets[name] = i
477478
}
@@ -481,7 +482,7 @@ func (m *ReconcilePod) reConfig(ctx context.Context, pod *corev1.Pod, prePodENI
481482
alloc := update.Spec.Allocations[i]
482483
name := alloc.Interface
483484
if name == "" {
484-
name = "eth0"
485+
name = defaultInterface
485486
}
486487

487488
if _, ok := targets[name]; ok {

pkg/controller/pod/pod_controller_default.go

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,66 @@ package pod
44

55
import (
66
"context"
7+
"fmt"
78

89
"github.com/AliyunContainerService/terway/pkg/apis/network.alibabacloud.com/v1beta1"
10+
"github.com/AliyunContainerService/terway/pkg/controller/common"
11+
"github.com/AliyunContainerService/terway/pkg/controller/vswitch"
912
"github.com/AliyunContainerService/terway/types/controlplane"
1013
)
1114

1215
// ParsePodNetworksFromAnnotation parse alloc
1316
func (m *ReconcilePod) ParsePodNetworksFromAnnotation(ctx context.Context, zoneID string, anno *controlplane.PodNetworksAnnotation) ([]*v1beta1.Allocation, error) {
14-
return nil, nil
17+
if zoneID == "" {
18+
return nil, fmt.Errorf("zoneID is empty")
19+
}
20+
var allocs []*v1beta1.Allocation
21+
22+
for _, c := range anno.PodNetworks {
23+
ifName := c.Interface
24+
if ifName == "" {
25+
ifName = defaultInterface
26+
}
27+
28+
if len(c.VSwitchOptions) == 0 || len(c.SecurityGroupIDs) == 0 {
29+
return nil, fmt.Errorf("vSwitchOptions or securityGroupIDs is missing")
30+
}
31+
32+
alloc := &v1beta1.Allocation{
33+
ENI: v1beta1.ENI{
34+
SecurityGroupIDs: c.SecurityGroupIDs,
35+
},
36+
Interface: ifName,
37+
ExtraConfig: map[string]string{},
38+
}
39+
40+
ctx := common.WithCtx(ctx, alloc)
41+
42+
// allow config route without
43+
realClient, _, err := common.Became(ctx, m.aliyun)
44+
if err != nil {
45+
return nil, err
46+
}
47+
48+
sw, err := m.swPool.GetOne(ctx, realClient, zoneID, c.VSwitchOptions, &vswitch.SelectOptions{
49+
IgnoreZone: false,
50+
})
51+
if err != nil {
52+
return nil, err
53+
}
54+
alloc.ENI.VSwitchID = sw.ID
55+
alloc.IPv4CIDR = sw.IPv4CIDR
56+
alloc.IPv6CIDR = sw.IPv6CIDR
57+
var routes []v1beta1.Route
58+
for _, r := range c.ExtraRoutes {
59+
routes = append(routes, v1beta1.Route{Dst: r.Dst})
60+
}
61+
alloc.ExtraRoutes = routes
62+
63+
allocs = append(allocs, alloc)
64+
}
65+
66+
return allocs, nil
1567
}
1668

1769
func (m *ReconcilePod) PostENICreate(ctx context.Context, alloc *v1beta1.Allocation) error {

0 commit comments

Comments
 (0)