Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve mapping from k8s endpoints to polaris instance #62

Merged
merged 1 commit into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
v12 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
coreinformers "k8s.io/client-go/informers/core/v1"
Expand Down Expand Up @@ -824,18 +823,6 @@ func (p *PolarisController) processSyncInstance(service *v1.Service) (err error)
return err
}

selector := labels.NewSelector()
for k, v := range service.Spec.Selector {
reqiure, _ := labels.NewRequirement(k, selection.Equals, []string{v})
selector.Add(*reqiure)
}

pods, err := p.podLister.Pods(service.GetNamespace()).List(selector)
if err != nil {
log.Errorf("Get endpoint of service %s error %v, ignore", serviceMsg, err)
return err
}

/*
1. 先获取当前service Endpoint中的IP信息,IP:Port:Weight
2. 获取北极星中注册的Service,经过过滤,获取对应的 IP:Port:Weight
Expand All @@ -848,7 +835,7 @@ func (p *PolarisController) processSyncInstance(service *v1.Service) (err error)
return err
}
ipPortMap := getCustomWeight(service, serviceMsg)
specIPs := address.GetAddressMapFromEndpoints(service, endpoint, pods, ipPortMap)
specIPs := address.GetAddressMapFromEndpoints(service, endpoint, p.podLister, ipPortMap)
currentIPs := address.GetAddressMapFromPolarisInstance(instances, p.config.PolarisController.ClusterName)
addIns, deleteIns, updateIns := p.CompareInstance(service, specIPs, currentIPs)

Expand Down
32 changes: 16 additions & 16 deletions pkg/util/address/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/polarismesh/polaris-controller/pkg/util"
"github.com/polarismesh/polaris-go/pkg/model"
v1 "k8s.io/api/core/v1"
corelisters "k8s.io/client-go/listers/core/v1"
)

// Address 记录IP端口信息
Expand All @@ -53,20 +54,14 @@ type InstanceSet map[string]*Address
// "port": "80"
// }
// }]
func GetAddressMapFromEndpoints(service *v1.Service, endpoint *v1.Endpoints, pods []*v1.Pod,
indexPortMap util.IndexPortMap) InstanceSet {
func GetAddressMapFromEndpoints(service *v1.Service, endpoint *v1.Endpoints,
podLister corelisters.PodLister, indexPortMap util.IndexPortMap) InstanceSet {
instanceSet := make(InstanceSet)
workloadKind := service.GetAnnotations()[util.WorkloadKind]
defaultWeight := util.GetWeightFromService(service)
hasIndex := workloadKind == "statefulset" || workloadKind == "statefulsetplus" ||
workloadKind == "StatefulSetPlus" || workloadKind == "StatefulSet"

podMap := make(map[string]*v1.Pod, len(pods))
for i := range pods {
pod := pods[i]
podMap[pod.Status.PodIP] = pod
}

res, _ := json.Marshal(endpoint)
log.Infof("get endpoints %s", string(res))

Expand All @@ -80,7 +75,7 @@ func GetAddressMapFromEndpoints(service *v1.Service, endpoint *v1.Endpoints, pod
}
}

pod, ok := podMap[readyAds.IP]
pod, err := podLister.Pods(readyAds.TargetRef.Namespace).Get(readyAds.TargetRef.Name)

for _, port := range subset.Ports {
ipPort := fmt.Sprintf("%s-%d", readyAds.IP, port.Port)
Expand All @@ -102,11 +97,13 @@ func GetAddressMapFromEndpoints(service *v1.Service, endpoint *v1.Endpoints, pod
Protocol: port.Name,
}

if ok {
address.Metadata = pod.Labels
instanceSet[ipPort] = address

if err != nil {
continue
}

instanceSet[ipPort] = address
address.Metadata = pod.Labels
}
}

Expand All @@ -120,7 +117,8 @@ func GetAddressMapFromEndpoints(service *v1.Service, endpoint *v1.Endpoints, pod
}
}

pod, ok := podMap[notReadyAds.IP]
pod, err := podLister.Pods(notReadyAds.TargetRef.Namespace).
Get(notReadyAds.TargetRef.Name)

for _, port := range subset.Ports {
ipPort := fmt.Sprintf("%s-%d", notReadyAds.IP, port.Port)
Expand All @@ -141,11 +139,13 @@ func GetAddressMapFromEndpoints(service *v1.Service, endpoint *v1.Endpoints, pod
Protocol: port.Name,
}

if ok {
address.Metadata = pod.Labels
instanceSet[ipPort] = address

if err != nil {
continue
}

instanceSet[ipPort] = address
address.Metadata = pod.Labels
}
}
}
Expand Down