代码拉取完成,页面将自动刷新
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。