37 Star 407 Fork 75

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
projectsyncer.go 2.53 KB
一键复制 编辑 原始数据 按行查看 历史
kinarashah 提交于 2018-07-31 14:12 . add support to turn off networkpolicy
package networkpolicy
import (
"fmt"
"reflect"
"github.com/rancher/norman/condition"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
)
type projectSyncer struct {
pnpLister v3.ProjectNetworkPolicyLister
pnpClient v3.ProjectNetworkPolicyInterface
projClient v3.ProjectInterface
clusterLister v3.ClusterLister
clusterNamespace string
}
// Sync is responsible for creating a default ProjectNetworkPolicy for
// every project created. There is no need to worry about clean up, as
// this pnp object is tied to the namespace of the project, it's deleted
// automatically.
func (ps *projectSyncer) Sync(key string, p *v3.Project) error {
if p == nil || p.DeletionTimestamp != nil {
return nil
}
disabled, err := isNetworkPolicyDisabled(ps.clusterNamespace, ps.clusterLister)
if err != nil {
return err
}
if disabled {
return nil
}
updated, err := ps.createDefaultNetworkPolicy(p)
if err != nil {
return err
}
// update if it has changed
if updated != nil && !reflect.DeepEqual(p, updated) {
_, err = ps.projClient.Update(updated)
if err != nil {
return err
}
}
return nil
}
func (ps *projectSyncer) createDefaultNetworkPolicy(p *v3.Project) (*v3.Project, error) {
updated, err := v3.DefaultNetworkPolicyCreated.Do(p, func() (runtime.Object, error) {
o, err := meta.Accessor(p)
if err != nil {
return p, condition.Error("MissingMetadata", err)
}
projectName := o.GetName()
defaultPolicyName := "pnp-" + projectName
existingPolicies, err := ps.pnpLister.List(defaultPolicyName, labels.Everything())
if err != nil {
logrus.Errorf("projectSyncer: createDefaultNetworkPolicy: error fetching existing project network policy: %v", err)
return p, err
}
if len(existingPolicies) == 0 {
pnpDesc := fmt.Sprintf("Default network policy for project %v", projectName)
_, err = ps.pnpClient.Create(&v3.ProjectNetworkPolicy{
ObjectMeta: v1.ObjectMeta{
Name: defaultPolicyName,
Namespace: projectName,
},
Spec: v3.ProjectNetworkPolicySpec{
Description: pnpDesc,
ProjectName: o.GetNamespace() + ":" + projectName,
},
})
if err == nil {
logrus.Infof("projectSyncer: createDefaultNetworkPolicy: successfully created default network policy for project: %v", projectName)
}
}
return p, nil
})
if err != nil {
return p, err
}
return updated.(*v3.Project), nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.0.14-rc2

搜索帮助