37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
pod_endpoints.go 2.15 KB
一键复制 编辑 原始数据 按行查看 历史
Darren Shepherd 提交于 2018-11-06 10:06 . Change return type
package endpoints
import (
"strings"
"k8s.io/apimachinery/pkg/runtime"
"fmt"
workloadutil "github.com/rancher/rancher/pkg/controllers/user/workload"
"github.com/rancher/types/apis/core/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
)
// This controller is responsible for monitoring pods having host port set
// and sending an update event to the corresponding workload
// so the endpoints can be reconciled for those
type PodsController struct {
podLister v1.PodLister
workloadController workloadutil.CommonController
}
func (c *PodsController) sync(key string, obj *corev1.Pod) (runtime.Object, error) {
if obj == nil && !strings.HasSuffix(key, allEndpoints) {
return nil, nil
}
var pods []*corev1.Pod
var err error
if strings.HasSuffix(key, allEndpoints) {
namespace := ""
if !strings.EqualFold(key, allEndpoints) {
namespace = strings.TrimSuffix(key, fmt.Sprintf("/%s", allEndpoints))
}
pods, err = c.podLister.List(namespace, labels.NewSelector())
if err != nil {
return nil, err
}
} else {
pods = append(pods, obj)
}
workloadsToUpdate := map[string]*workloadutil.Workload{}
if err != nil {
return nil, err
}
for _, pod := range pods {
if pod.Spec.NodeName != "" && podHasHostPort(pod) {
workloads, err := c.workloadController.GetWorkloadsMatchingLabels(pod.Namespace, pod.Labels)
if err != nil {
return nil, err
}
for _, w := range workloads {
existingPublicEps := getPublicEndpointsFromAnnotations(w.Annotations)
found := false
for _, ep := range existingPublicEps {
if ep.PodName == pod.Name {
found = true
break
}
}
// push changes only when
// a) the workload doesn't have the pod's endpoint for active pod
// b) pod is removed
if found == (pod.DeletionTimestamp != nil) {
workloadsToUpdate[key] = w
}
}
}
}
// push changes to workload
for _, w := range workloadsToUpdate {
c.workloadController.EnqueueWorkload(w)
}
return nil, nil
}
func podHasHostPort(obj *corev1.Pod) bool {
for _, c := range obj.Spec.Containers {
for _, p := range c.Ports {
if p.HostPort != 0 {
return true
}
}
}
return false
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.0-alpha3

搜索帮助

0d507c66 1850385 C8b1a773 1850385