From 91db60e71b330c4d936c51087fd6a16fd086edd0 Mon Sep 17 00:00:00 2001 From: chenshuoshi Date: Tue, 8 Dec 2020 15:24:17 +0800 Subject: [PATCH] #152: add k8s.aliyun.com/eip-charge-type annotation --- daemon/eip.go | 2 +- daemon/k8s.go | 17 +++++++++++++---- pkg/aliyun/ecs.go | 2 +- pkg/aliyun/eip.go | 5 +++-- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/daemon/eip.go b/daemon/eip.go index 8dfaefa1..36c3ac20 100644 --- a/daemon/eip.go +++ b/daemon/eip.go @@ -55,7 +55,7 @@ func (e *eipResourceManager) Allocate(context *networkContext, prefer string) (t return nil, fmt.Errorf("pod network mode not support EIP associate") } - eipInfo, err := e.ecs.AllocateEipAddress(context.pod.EipInfo.PodEipBandWidth, context.pod.EipInfo.PodEipID, eniID, eniIP, e.allowEipRob) + eipInfo, err := e.ecs.AllocateEipAddress(context.pod.EipInfo.PodEipBandWidth, context.pod.EipInfo.PodEipChargeType, context.pod.EipInfo.PodEipID, eniID, eniIP, e.allowEipRob) if err != nil { return nil, errors.Errorf("error allocate eip info: %v", err) } diff --git a/daemon/k8s.go b/daemon/k8s.go index aed4458a..c5c0c366 100644 --- a/daemon/k8s.go +++ b/daemon/k8s.go @@ -20,6 +20,7 @@ import ( "github.com/AliyunContainerService/terway/deviceplugin" "github.com/AliyunContainerService/terway/pkg/storage" + "github.com/denverdino/aliyungo/common" "github.com/pkg/errors" log "github.com/sirupsen/logrus" "gopkg.in/yaml.v2" @@ -50,10 +51,11 @@ const ( ) type podEipInfo struct { - PodEip bool - PodEipID string - PodEipIP string - PodEipBandWidth int + PodEip bool + PodEipID string + PodEipIP string + PodEipBandWidth int + PodEipChargeType common.InternetChargeType } type podInfo struct { @@ -312,6 +314,7 @@ const podEgressBandwidth = "k8s.aliyun.com/egress-bandwidth" const podWithEip = "k8s.aliyun.com/pod-with-eip" const eciWithEip = "k8s.aliyun.com/eci-with-eip" // to adopt ask annotation const podEipBandwidth = "k8s.aliyun.com/eip-bandwidth" +const podEipChargeType = "k8s.aliyun.com/eip-charge-type" const podEciEipInstanceID = "k8s.aliyun.com/eci-eip-instanceid" // to adopt ask annotation const podPodEipInstanceID = "k8s.aliyun.com/pod-eip-instanceid" const podEipAddress = "k8s.aliyun.com/allocated-eipAddress" @@ -384,10 +387,12 @@ func convertPod(daemonMode string, pod *corev1.Pod) *podInfo { if eipAnnotation, ok := podAnnotation[podWithEip]; ok && eipAnnotation == conditionTrue { pi.EipInfo.PodEip = true pi.EipInfo.PodEipBandWidth = 5 + pi.EipInfo.PodEipChargeType = common.PayByTraffic } if eipAnnotation, ok := podAnnotation[eciWithEip]; ok && eipAnnotation == conditionTrue { pi.EipInfo.PodEip = true pi.EipInfo.PodEipBandWidth = 5 + pi.EipInfo.PodEipChargeType = common.PayByTraffic } if eipAnnotation, ok := podAnnotation[podEipBandwidth]; ok { @@ -399,6 +404,10 @@ func convertPod(daemonMode string, pod *corev1.Pod) *podInfo { } } + if eipAnnotation, ok := podAnnotation[podEipChargeType]; ok { + pi.EipInfo.PodEipChargeType = common.InternetChargeType(eipAnnotation) + } + if eipAnnotation, ok := podAnnotation[podEciEipInstanceID]; ok && eipAnnotation != "" { pi.EipInfo.PodEip = true pi.EipInfo.PodEipID = eipAnnotation diff --git a/pkg/aliyun/ecs.go b/pkg/aliyun/ecs.go index d593756c..8ddf4388 100644 --- a/pkg/aliyun/ecs.go +++ b/pkg/aliyun/ecs.go @@ -45,7 +45,7 @@ type ECS interface { GetAttachedSecurityGroup(instanceID string) (string, error) DescribeVSwitch(vSwitch string) (availIPCount int, err error) // EIP - AllocateEipAddress(bandwidth int, eipID, eniID string, eniIP net.IP, allowRob bool) (*types.EIP, error) + AllocateEipAddress(bandwidth int, chargeType common.InternetChargeType, eipID, eniID string, eniIP net.IP, allowRob bool) (*types.EIP, error) UnassociateEipAddress(eipID, eniID, eniIP string) error ReleaseEipAddress(eipID, eniID string, eniIP net.IP) error QueryEniIDByIP(address net.IP) (string, error) diff --git a/pkg/aliyun/eip.go b/pkg/aliyun/eip.go index 24363e0e..d89128c8 100644 --- a/pkg/aliyun/eip.go +++ b/pkg/aliyun/eip.go @@ -7,13 +7,14 @@ import ( "github.com/AliyunContainerService/terway/pkg/metric" "github.com/AliyunContainerService/terway/types" + "github.com/denverdino/aliyungo/common" "github.com/denverdino/aliyungo/ecs" "github.com/pkg/errors" "github.com/sirupsen/logrus" "k8s.io/apimachinery/pkg/util/wait" ) -func (e *ecsImpl) AllocateEipAddress(bandwidth int, eipID, eniID string, eniIP net.IP, allowRob bool) (*types.EIP, error) { +func (e *ecsImpl) AllocateEipAddress(bandwidth int, chargeType common.InternetChargeType, eipID, eniID string, eniIP net.IP, allowRob bool) (*types.EIP, error) { var ( eipInfo *types.EIP err error @@ -23,7 +24,7 @@ func (e *ecsImpl) AllocateEipAddress(bandwidth int, eipID, eniID string, eniIP n start := time.Now() var eipAddressStr, allocationID string eipAddressStr, allocationID, err = e.clientSet.Vpc().AllocateEipAddress( - &ecs.AllocateEipAddressArgs{RegionId: e.region, Bandwidth: bandwidth}, + &ecs.AllocateEipAddressArgs{RegionId: e.region, Bandwidth: bandwidth, InternetChargeType: chargeType}, ) metric.OpenAPILatency.WithLabelValues("AllocateEipAddress", fmt.Sprint(err != nil)).Observe(metric.MsSince(start)) logrus.Debugf("AllocateEipAddress: %v, %v, %v", eipAddressStr, allocationID, err)