代码拉取完成,页面将自动刷新
package networkpolicy
import (
"fmt"
"reflect"
"github.com/rancher/norman/clientbase"
"github.com/rancher/norman/condition"
typescorev1 "github.com/rancher/types/apis/core/v1"
"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"
"k8s.io/client-go/kubernetes"
)
type mgr struct {
pnpLister v3.ProjectNetworkPolicyLister
pnpClient *clientbase.ObjectClient
nsLister typescorev1.NamespaceLister
nsClient *clientbase.ObjectClient
K8sClient kubernetes.Interface
}
type projectSyncer struct {
pnpLister v3.ProjectNetworkPolicyLister
pnpClient *clientbase.ObjectClient
projClient *clientbase.ObjectClient
}
// 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
}
pcopy := p.DeepCopyObject()
pcopy, err := ps.createDefaultNetworkPolicy(pcopy)
if err != nil {
return err
}
// update if it has changed
if pcopy != nil && !reflect.DeepEqual(p, pcopy) {
_, err = ps.projClient.Update(p.Name, pcopy)
if err != nil {
return err
}
}
return nil
}
func (ps *projectSyncer) createDefaultNetworkPolicy(p runtime.Object) (runtime.Object, error) {
return 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("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("Successfully created default network policy for project: %v", projectName)
}
}
return p, nil
})
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。