37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
configgenerator.go 3.09 KB
一键复制 编辑 原始数据 按行查看 历史
Dan Ramich 提交于 2019-06-12 13:38 . goimport linting changes
package configsyncer
import (
"sort"
"strings"
"github.com/rancher/norman/controller"
loggingconfig "github.com/rancher/rancher/pkg/controllers/user/logging/config"
"github.com/rancher/rancher/pkg/controllers/user/logging/generator"
"github.com/rancher/rancher/pkg/project"
v1 "github.com/rancher/types/apis/core/v1"
mgmtv3 "github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
func NewConfigGenerator(clusterName string, projectLoggingLister mgmtv3.ProjectLoggingLister, namespaceLister v1.NamespaceLister) *ConfigGenerator {
return &ConfigGenerator{
clusterName: clusterName,
projectLoggingLister: projectLoggingLister,
namespaceLister: namespaceLister,
}
}
type ConfigGenerator struct {
clusterName string
projectLoggingLister mgmtv3.ProjectLoggingLister
namespaceLister v1.NamespaceLister
}
func (s *ConfigGenerator) GenerateClusterLoggingConfig(clusterLogging *mgmtv3.ClusterLogging, systemProjectID, certDir string) ([]byte, error) {
if clusterLogging == nil {
return []byte{}, nil
}
var excludeNamespaces string
if clusterLogging.Spec.IncludeSystemComponent != nil && !*clusterLogging.Spec.IncludeSystemComponent {
var err error
if excludeNamespaces, err = s.addExcludeNamespaces(systemProjectID); err != nil {
return nil, err
}
}
buf, err := generator.GenerateClusterConfig(clusterLogging.Spec, excludeNamespaces, certDir)
if err != nil {
return nil, err
}
return buf, nil
}
func (s *ConfigGenerator) GenerateProjectLoggingConfig(projectLoggings []*mgmtv3.ProjectLogging, systemProjectID, certDir string) ([]byte, error) {
if len(projectLoggings) == 0 {
allProjectLoggings, err := s.projectLoggingLister.List("", labels.NewSelector())
if err != nil {
return nil, errors.Wrapf(err, "List project loggings failed")
}
for _, logging := range allProjectLoggings {
if controller.ObjectInCluster(s.clusterName, logging) {
projectLoggings = append(projectLoggings, logging)
}
}
if len(projectLoggings) == 0 {
return []byte{}, nil
}
}
sort.Slice(projectLoggings, func(i, j int) bool {
return projectLoggings[i].Name < projectLoggings[j].Name
})
namespaces, err := s.namespaceLister.List(metav1.NamespaceAll, labels.NewSelector())
if err != nil {
return nil, errors.Wrap(err, "list namespace failed")
}
buf, err := generator.GenerateProjectConfig(projectLoggings, namespaces, systemProjectID, certDir)
if err != nil {
return nil, err
}
return buf, nil
}
func (s *ConfigGenerator) addExcludeNamespaces(systemProjectID string) (string, error) {
namespaces, err := s.namespaceLister.List(metav1.NamespaceAll, labels.NewSelector())
if err != nil {
return "", errors.Wrapf(err, "list namespace failed")
}
var systemNamespaces []string
for _, v := range namespaces {
if v.Annotations[project.ProjectIDAnn] == systemProjectID {
namespacePattern := loggingconfig.GetNamespacePattern(v.Name)
systemNamespaces = append(systemNamespaces, namespacePattern)
}
}
return strings.Join(systemNamespaces, "|"), nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.6-rc1

搜索帮助

0d507c66 1850385 C8b1a773 1850385