37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
servicehandler.go 3.00 KB
一键复制 编辑 原始数据 按行查看 历史
Darren Shepherd 提交于 2018-11-06 10:06 . Change return type
package networkpolicy
import (
"fmt"
"sort"
"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"
"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
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.0-rc15

搜索帮助

0d507c66 1850385 C8b1a773 1850385