37 Star 403 Fork 75

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
pod_endpoints.go 2.08 KB
一键复制 编辑 原始数据 按行查看 历史
package endpoints
import (
"strings"
"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) error {
if obj == nil && !strings.HasSuffix(key, allEndpoints) {
return 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 err
}
} else {
pods = append(pods, obj)
}
workloadsToUpdate := map[string]*workloadutil.Workload{}
if err != nil {
return err
}
for _, pod := range pods {
if pod.Spec.NodeName != "" && podHasHostPort(pod) {
workloads, err := c.workloadController.GetWorkloadsMatchingLabels(pod.Namespace, pod.Labels)
if err != nil {
return 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
}
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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.1.2-rc13

搜索帮助