代码拉取完成,页面将自动刷新
package networkpolicy
import (
"fmt"
"sort"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
knetworkingv1 "k8s.io/api/networking/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
type serviceHandler struct {
npmgr *netpolMgr
clusterLister v3.ClusterLister
clusterNamespace string
}
func (sh *serviceHandler) Sync(key string, service *corev1.Service) (runtime.Object, error) {
if service == nil || service.DeletionTimestamp != nil {
return nil, nil
}
disabled, err := isNetworkPolicyDisabled(sh.clusterNamespace, sh.clusterLister)
if err != nil {
return nil, err
}
if disabled {
return nil, nil
}
moved, err := isNamespaceMoved(service.Namespace, sh.npmgr.nsLister)
if err != nil {
return nil, err
}
if moved {
return nil, nil
}
logrus.Debugf("serviceHandler: Sync: %+v", *service)
return nil, sh.npmgr.nodePortsUpdateHandler(service, sh.clusterNamespace)
}
func (npmgr *netpolMgr) nodePortsUpdateHandler(service *corev1.Service, clusterNamespace string) error {
systemNamespaces, _, err := npmgr.getSystemNSInfo(clusterNamespace)
if err != nil {
return fmt.Errorf("netpolMgr: hostPortsUpdateHandler: getSystemNamespaces: err=%v", err)
}
policyName := getNodePortsPolicyName(service)
if _, ok := systemNamespaces[service.Namespace]; ok {
npmgr.delete(service.Namespace, policyName)
return nil
}
np := generateServiceNetworkPolicy(service, policyName)
hasNodePorts := false
for _, port := range service.Spec.Ports {
if port.NodePort != 0 {
tp := port.TargetPort
proto := corev1.Protocol(port.Protocol)
p := knetworkingv1.NetworkPolicyPort{
Protocol: &proto,
Port: &tp,
}
np.Spec.Ingress[0].Ports = append(np.Spec.Ingress[0].Ports, p)
hasNodePorts = true
}
}
// sort ports so it always appears in a certain order
sort.Slice(np.Spec.Ingress[0].Ports, func(i, j int) bool {
return portToString(np.Spec.Ingress[0].Ports[i]) < portToString(np.Spec.Ingress[0].Ports[j])
})
if hasNodePorts {
logrus.Debugf("netpolMgr: nodePortsUpdateHandler: service=%+v has node ports, hence programming np=%+v", *service, *np)
return npmgr.program(np)
}
return nil
}
func getNodePortsPolicyName(service *corev1.Service) string {
return "np-" + service.Name
}
func generateServiceNetworkPolicy(service *corev1.Service, policyName string) *knetworkingv1.NetworkPolicy {
np := &knetworkingv1.NetworkPolicy{
ObjectMeta: v1.ObjectMeta{
Name: policyName,
Namespace: service.Namespace,
OwnerReferences: []v1.OwnerReference{
{
APIVersion: "v1",
Kind: "Service",
UID: service.UID,
Name: service.Name,
},
},
},
Spec: knetworkingv1.NetworkPolicySpec{
PodSelector: v1.LabelSelector{
MatchLabels: service.Spec.Selector,
},
Ingress: []knetworkingv1.NetworkPolicyIngressRule{
{
From: []knetworkingv1.NetworkPolicyPeer{},
Ports: []knetworkingv1.NetworkPolicyPort{},
},
},
},
}
return np
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。