1
0
Fork 0
mirror of https://github.com/prometheus-operator/prometheus-operator.git synced 2025-04-21 11:48:53 +00:00

pkg/*: renamings and reformatting

This commit is contained in:
Sergiusz Urbaniak 2020-08-31 13:02:55 +02:00
parent 0c9283465a
commit 27c1680975
7 changed files with 120 additions and 100 deletions

View file

@ -55,8 +55,8 @@ type Operator struct {
mclient monitoringclient.Interface
logger log.Logger
alrtInfs *informers.InformersForResource
ssetInfs *informers.InformersForResource
alrtInfs *informers.ForResource
ssetInfs *informers.ForResource
queue workqueue.RateLimitingInterface
@ -117,27 +117,30 @@ func New(ctx context.Context, c prometheusoperator.Config, logger log.Logger, r
o.alrtInfs, err = informers.NewInformersForResource(
informers.NewMonitoringInformerFactories(
o.config.Namespaces.AlertmanagerAllowList, o.config.Namespaces.DenyList,
mclient, resyncPeriod,
o.config.Namespaces.AlertmanagerAllowList,
o.config.Namespaces.DenyList,
mclient,
resyncPeriod,
func(options *metav1.ListOptions) {
options.LabelSelector = o.config.AlertManagerSelector
},
),
monitoringv1.SchemeGroupVersion.WithResource(monitoringv1.AlertmanagerName),
)
if err != nil {
return nil, errors.Wrap(err, "error creating alertmanager informers")
}
o.ssetInfs, err = informers.NewInformersForResource(
informers.NewKubeInformerFactories(
o.config.Namespaces.AlertmanagerAllowList, o.config.Namespaces.DenyList,
o.kclient, resyncPeriod, nil,
o.config.Namespaces.AlertmanagerAllowList,
o.config.Namespaces.DenyList,
o.kclient,
resyncPeriod,
nil,
),
appsv1.SchemeGroupVersion.WithResource("statefulsets"),
)
if err != nil {
return nil, errors.Wrap(err, "error creating statefulset informers")
}
@ -151,7 +154,7 @@ func (c *Operator) waitForCacheSync(stopc <-chan struct{}) error {
for _, infs := range []struct {
name string
informersForResource *informers.InformersForResource
informersForResource *informers.ForResource
}{
{"Alertmanager", c.alrtInfs},
{"StatefulSet", c.ssetInfs},
@ -434,9 +437,7 @@ func (c *Operator) sync(ctx context.Context, key string) error {
return errors.Wrap(err, "retrieving statefulset failed")
}
exists := !apierrors.IsNotFound(err)
if !exists {
if apierrors.IsNotFound(err) {
sset, err := makeStatefulSet(am, nil, c.config)
if err != nil {
return errors.Wrap(err, "making the statefulset, to create, failed")

View file

@ -18,10 +18,8 @@ import (
"strings"
"time"
"github.com/prometheus-operator/prometheus-operator/pkg/listwatch"
"github.com/pkg/errors"
"github.com/prometheus-operator/prometheus-operator/pkg/listwatch"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@ -32,24 +30,24 @@ import (
"k8s.io/client-go/tools/cache"
)
type GenericInformer interface {
type InformLister interface {
Informer() cache.SharedIndexInformer
Lister() cache.GenericLister
}
// InformerFactoriesForNamespaces is a simple way to combine several shared informers into a single struct with unified listing power
type InformerFactoriesForNamespaces interface {
ForResource(namespace string, resource schema.GroupVersionResource) (GenericInformer, error)
// FactoriesForNamespaces is a way to combine several shared informers into a single struct with unified listing power.
type FactoriesForNamespaces interface {
ForResource(namespace string, resource schema.GroupVersionResource) (InformLister, error)
Namespaces() sets.String
}
type InformersForResource struct {
informers []GenericInformer
type ForResource struct {
informers []InformLister
}
func NewInformersForResource(ifs InformerFactoriesForNamespaces, resource schema.GroupVersionResource) (*InformersForResource, error) {
func NewInformersForResource(ifs FactoriesForNamespaces, resource schema.GroupVersionResource) (*ForResource, error) {
namespaces := ifs.Namespaces().List()
var informers []GenericInformer
var informers []InformLister
for _, ns := range namespaces {
informer, err := ifs.ForResource(ns, resource)
@ -59,34 +57,34 @@ func NewInformersForResource(ifs InformerFactoriesForNamespaces, resource schema
informers = append(informers, informer)
}
return &InformersForResource{
return &ForResource{
informers: informers,
}, nil
}
func (w *InformersForResource) Start(stopCh <-chan struct{}) {
func (w *ForResource) Start(stopCh <-chan struct{}) {
for _, i := range w.informers {
go i.Informer().Run(stopCh)
}
}
func (w *InformersForResource) GetInformers() []GenericInformer {
func (w *ForResource) GetInformers() []InformLister {
return w.informers
}
func (w *InformersForResource) AddEventHandler(handler cache.ResourceEventHandler) {
func (w *ForResource) AddEventHandler(handler cache.ResourceEventHandler) {
for _, i := range w.informers {
i.Informer().AddEventHandler(handler)
}
}
func (w *InformersForResource) AddEventHandlerWithResyncPeriod(handler cache.ResourceEventHandler, resyncPeriod time.Duration) {
func (w *ForResource) AddEventHandlerWithResyncPeriod(handler cache.ResourceEventHandler, resyncPeriod time.Duration) {
for _, i := range w.informers {
i.Informer().AddEventHandlerWithResyncPeriod(handler, resyncPeriod)
}
}
func (w *InformersForResource) HasSynced() bool {
func (w *ForResource) HasSynced() bool {
for _, i := range w.informers {
if !i.Informer().HasSynced() {
return false
@ -96,7 +94,7 @@ func (w *InformersForResource) HasSynced() bool {
return true
}
func (w *InformersForResource) List(selector labels.Selector) ([]runtime.Object, error) {
func (w *ForResource) List(selector labels.Selector) ([]runtime.Object, error) {
var ret []runtime.Object
for _, inf := range w.informers {
@ -110,7 +108,7 @@ func (w *InformersForResource) List(selector labels.Selector) ([]runtime.Object,
return ret, nil
}
func (w *InformersForResource) ListAllByNamespace(namespace string, selector labels.Selector, appendFn cache.AppendFunc) error {
func (w *ForResource) ListAllByNamespace(namespace string, selector labels.Selector, appendFn cache.AppendFunc) error {
for _, inf := range w.informers {
err := cache.ListAllByNamespace(inf.Informer().GetIndexer(), namespace, selector, appendFn)
if err != nil {
@ -121,17 +119,15 @@ func (w *InformersForResource) ListAllByNamespace(namespace string, selector lab
return nil
}
func (w *InformersForResource) Get(name string) (runtime.Object, error) {
func (w *ForResource) Get(name string) (runtime.Object, error) {
var err error
for _, inf := range w.informers {
var ret runtime.Object
ret, err = inf.Lister().Get(name)
if apierrors.IsNotFound(err) {
continue
}
if err != nil {
return nil, err
}
@ -154,25 +150,25 @@ func newInformerOptions(allowedNamespaces, deniedNamespaces map[string]struct{},
return func(options *v1.ListOptions) {
tweaks(options)
denyListTweak(options, deniedNamespaces)
denyNamespacesTweak(options, deniedNamespaces)
}, namespaces
}
for ns, _ := range allowedNamespaces {
for ns := range allowedNamespaces {
namespaces = append(namespaces, ns)
}
return tweaks, namespaces
}
func denyListTweak(options *metav1.ListOptions, namespaces map[string]struct{}) {
func denyNamespacesTweak(options *metav1.ListOptions, namespaces map[string]struct{}) {
if len(namespaces) == 0 {
return
}
var denied []string
for ns, _ := range namespaces {
for ns := range namespaces {
denied = append(denied, "metadata.namespace!="+ns)
}

View file

@ -27,7 +27,7 @@ import (
func NewKubeInformerFactories(
allowNamespaces, denyNamespaces map[string]struct{},
kubeClient kubernetes.Interface, defaultResync time.Duration, tweakListOptions func(*metav1.ListOptions),
) InformerFactoriesForNamespaces {
) FactoriesForNamespaces {
tweaks, namespaces := newInformerOptions(
allowNamespaces, denyNamespaces, tweakListOptions,
)
@ -49,6 +49,6 @@ func (i kubeInformersForNamespaces) Namespaces() sets.String {
return sets.StringKeySet(i)
}
func (i kubeInformersForNamespaces) ForResource(namespace string, resource schema.GroupVersionResource) (GenericInformer, error) {
func (i kubeInformersForNamespaces) ForResource(namespace string, resource schema.GroupVersionResource) (InformLister, error) {
return i[namespace].ForResource(resource)
}

View file

@ -27,7 +27,7 @@ import (
func NewMonitoringInformerFactories(
allowNamespaces, denyNamespaces map[string]struct{},
monitoringClient monitoring.Interface, defaultResync time.Duration, tweakListOptions func(*metav1.ListOptions),
) InformerFactoriesForNamespaces {
) FactoriesForNamespaces {
tweaks, namespaces := newInformerOptions(
allowNamespaces, denyNamespaces, tweakListOptions,
)
@ -49,6 +49,6 @@ func (i monitoringInformersForNamespaces) Namespaces() sets.String {
return sets.StringKeySet(i)
}
func (i monitoringInformersForNamespaces) ForResource(namespace string, resource schema.GroupVersionResource) (GenericInformer, error) {
func (i monitoringInformersForNamespaces) ForResource(namespace string, resource schema.GroupVersionResource) (InformLister, error) {
return i[namespace].ForResource(resource)
}

View file

@ -75,7 +75,7 @@ func NewFilteredUnprivilegedNamespaceListWatchFromClient(l log.Logger, c cache.G
}
fieldSelector := options.FieldSelector
denyListTweak(options, deniedNamespaces)
denyNamesTweak(options, deniedNamespaces)
options.FieldSelector = strings.Join([]string{options.FieldSelector, fieldSelector}, ",")
}
@ -292,14 +292,14 @@ func IdenticalNamespaces(a, b map[string]struct{}) bool {
return true
}
func denyListTweak(options *metav1.ListOptions, namespaces map[string]struct{}) {
if len(namespaces) == 0 {
func denyNamesTweak(options *metav1.ListOptions, names map[string]struct{}) {
if len(names) == 0 {
return
}
var denied []string
for ns, _ := range namespaces {
for ns := range names {
denied = append(denied, "metadata.name!="+ns)
}

View file

@ -63,14 +63,14 @@ type Operator struct {
nsPromInf cache.SharedIndexInformer
nsMonInf cache.SharedIndexInformer
promInfs *informers.InformersForResource
smonInfs *informers.InformersForResource
pmonInfs *informers.InformersForResource
probeInfs *informers.InformersForResource
ruleInfs *informers.InformersForResource
cmapInfs *informers.InformersForResource
secrInfs *informers.InformersForResource
ssetInfs *informers.InformersForResource
promInfs *informers.ForResource
smonInfs *informers.ForResource
pmonInfs *informers.ForResource
probeInfs *informers.ForResource
ruleInfs *informers.ForResource
cmapInfs *informers.ForResource
secrInfs *informers.ForResource
ssetInfs *informers.ForResource
queue workqueue.RateLimitingInterface
@ -250,15 +250,16 @@ func New(ctx context.Context, conf Config, logger log.Logger, r prometheus.Regis
c.promInfs, err = informers.NewInformersForResource(
informers.NewMonitoringInformerFactories(
c.config.Namespaces.PrometheusAllowList, c.config.Namespaces.DenyList,
mclient, resyncPeriod,
c.config.Namespaces.PrometheusAllowList,
c.config.Namespaces.DenyList,
mclient,
resyncPeriod,
func(options *metav1.ListOptions) {
options.LabelSelector = c.config.PromSelector
},
),
monitoringv1.SchemeGroupVersion.WithResource(monitoringv1.PrometheusName),
)
if err != nil {
return nil, errors.Wrap(err, "error creating prometheus informers")
}
@ -271,87 +272,104 @@ func New(ctx context.Context, conf Config, logger log.Logger, r prometheus.Regis
c.smonInfs, err = informers.NewInformersForResource(
informers.NewMonitoringInformerFactories(
c.config.Namespaces.AllowList, c.config.Namespaces.DenyList,
mclient, resyncPeriod, nil,
c.config.Namespaces.AllowList,
c.config.Namespaces.DenyList,
mclient,
resyncPeriod,
nil,
),
monitoringv1.SchemeGroupVersion.WithResource(monitoringv1.ServiceMonitorName),
)
if err != nil {
return nil, errors.Wrap(err, "error creating servicemonitor informers")
}
c.pmonInfs, err = informers.NewInformersForResource(
informers.NewMonitoringInformerFactories(
c.config.Namespaces.AllowList, c.config.Namespaces.DenyList,
mclient, resyncPeriod, nil,
c.config.Namespaces.AllowList,
c.config.Namespaces.DenyList,
mclient,
resyncPeriod,
nil,
),
monitoringv1.SchemeGroupVersion.WithResource(monitoringv1.PodMonitorName),
)
if err != nil {
return nil, errors.Wrap(err, "error creating podmonitor informers")
}
c.probeInfs, err = informers.NewInformersForResource(
informers.NewMonitoringInformerFactories(
c.config.Namespaces.AllowList, c.config.Namespaces.DenyList,
mclient, resyncPeriod, nil,
c.config.Namespaces.AllowList,
c.config.Namespaces.DenyList,
mclient,
resyncPeriod,
nil,
),
monitoringv1.SchemeGroupVersion.WithResource(monitoringv1.ProbeName),
)
if err != nil {
return nil, errors.Wrap(err, "error creating podmonitor informers")
return nil, errors.Wrap(err, "error creating probe informers")
}
c.ruleInfs, err = informers.NewInformersForResource(
informers.NewMonitoringInformerFactories(
c.config.Namespaces.AllowList, c.config.Namespaces.DenyList,
mclient, resyncPeriod, nil,
c.config.Namespaces.AllowList,
c.config.Namespaces.DenyList,
mclient,
resyncPeriod,
nil,
),
monitoringv1.SchemeGroupVersion.WithResource(monitoringv1.PrometheusRuleName),
)
if err != nil {
return nil, errors.Wrap(err, "error creating podmonitor informers")
return nil, errors.Wrap(err, "error creating prometheusrule informers")
}
c.cmapInfs, err = informers.NewInformersForResource(
informers.NewKubeInformerFactories(
c.config.Namespaces.AllowList, c.config.Namespaces.DenyList,
c.kclient, resyncPeriod,
c.config.Namespaces.AllowList,
c.config.Namespaces.DenyList,
c.kclient,
resyncPeriod,
func(options *metav1.ListOptions) {
options.LabelSelector = labelPrometheusName
},
),
v1.SchemeGroupVersion.WithResource(string(v1.ResourceConfigMaps)),
)
if err != nil {
return nil, errors.Wrap(err, "error creating configmap informers")
}
c.secrInfs, err = informers.NewInformersForResource(
informers.NewKubeInformerFactories(
c.config.Namespaces.PrometheusAllowList, c.config.Namespaces.DenyList,
c.kclient, resyncPeriod, func(options *metav1.ListOptions) {
c.config.Namespaces.PrometheusAllowList,
c.config.Namespaces.DenyList,
c.kclient,
resyncPeriod,
func(options *metav1.ListOptions) {
options.FieldSelector = strings.Join([]string{options.FieldSelector, secretListWatchSelector.String()}, ",")
},
),
v1.SchemeGroupVersion.WithResource(string(v1.ResourceSecrets)),
)
if err != nil {
return nil, errors.Wrap(err, "error creating configmap informers")
return nil, errors.Wrap(err, "error creating secrets informers")
}
c.ssetInfs, err = informers.NewInformersForResource(
informers.NewKubeInformerFactories(
c.config.Namespaces.PrometheusAllowList, c.config.Namespaces.DenyList,
c.kclient, resyncPeriod, nil,
c.config.Namespaces.PrometheusAllowList,
c.config.Namespaces.DenyList,
c.kclient,
resyncPeriod,
nil,
),
appsv1.SchemeGroupVersion.WithResource("statefulsets"),
)
if err != nil {
return nil, errors.Wrap(err, "error creating configmap informers")
return nil, errors.Wrap(err, "error creating statefulset informers")
}
newNamespaceInformer := func(o *Operator, allowList map[string]struct{}) cache.SharedIndexInformer {
@ -391,7 +409,7 @@ func (c *Operator) waitForCacheSync(ctx context.Context) error {
for _, infs := range []struct {
name string
informersForResource *informers.InformersForResource
informersForResource *informers.ForResource
}{
{"Prometheus", c.promInfs},
{"ServiceMonitor", c.smonInfs},

View file

@ -60,10 +60,10 @@ type Operator struct {
mclient monitoringclient.Interface
logger log.Logger
thanosRulerInfs *informers.InformersForResource
cmapInfs *informers.InformersForResource
ruleInfs *informers.InformersForResource
ssetInfs *informers.InformersForResource
thanosRulerInfs *informers.ForResource
cmapInfs *informers.ForResource
ruleInfs *informers.ForResource
ssetInfs *informers.ForResource
nsThanosRulerInf cache.SharedIndexInformer
nsRuleInf cache.SharedIndexInformer
@ -138,54 +138,60 @@ func New(ctx context.Context, conf prometheusoperator.Config, logger log.Logger,
o.cmapInfs, err = informers.NewInformersForResource(
informers.NewKubeInformerFactories(
o.config.Namespaces.ThanosRulerAllowList, o.config.Namespaces.DenyList,
o.kclient, resyncPeriod,
o.config.Namespaces.ThanosRulerAllowList,
o.config.Namespaces.DenyList,
o.kclient,
resyncPeriod,
func(options *metav1.ListOptions) {
options.LabelSelector = labelThanosRulerName
},
),
v1.SchemeGroupVersion.WithResource(string(v1.ResourceConfigMaps)),
)
if err != nil {
return nil, errors.Wrap(err, "error creating configmap informers")
}
o.thanosRulerInfs, err = informers.NewInformersForResource(
informers.NewMonitoringInformerFactories(
o.config.Namespaces.ThanosRulerAllowList, o.config.Namespaces.DenyList,
mclient, resyncPeriod,
o.config.Namespaces.ThanosRulerAllowList,
o.config.Namespaces.DenyList,
mclient,
resyncPeriod,
func(options *metav1.ListOptions) {
options.LabelSelector = o.config.ThanosRulerSelector
},
),
monitoringv1.SchemeGroupVersion.WithResource(monitoringv1.ThanosRulerName),
)
if err != nil {
return nil, errors.Wrap(err, "error creating thanosruler informers")
}
o.ruleInfs, err = informers.NewInformersForResource(
informers.NewMonitoringInformerFactories(
o.config.Namespaces.AllowList, o.config.Namespaces.DenyList,
mclient, resyncPeriod, nil,
o.config.Namespaces.AllowList,
o.config.Namespaces.DenyList,
mclient,
resyncPeriod,
nil,
),
monitoringv1.SchemeGroupVersion.WithResource(monitoringv1.PrometheusRuleName),
)
if err != nil {
return nil, errors.Wrap(err, "error creating prometheusrule informers")
}
o.ssetInfs, err = informers.NewInformersForResource(
informers.NewKubeInformerFactories(
o.config.Namespaces.ThanosRulerAllowList, o.config.Namespaces.DenyList,
o.kclient, resyncPeriod, nil,
o.config.Namespaces.ThanosRulerAllowList,
o.config.Namespaces.DenyList,
o.kclient,
resyncPeriod,
nil,
),
appsv1.SchemeGroupVersion.WithResource("statefulsets"),
)
if err != nil {
return nil, errors.Wrap(err, "error creating statefulset informers")
}
@ -227,7 +233,7 @@ func (o *Operator) waitForCacheSync(stopc <-chan struct{}) error {
for _, infs := range []struct {
name string
informersForResource *informers.InformersForResource
informersForResource *informers.ForResource
}{
{"ThanosRuler", o.thanosRulerInfs},
{"ConfigMap", o.cmapInfs},
@ -578,7 +584,6 @@ func (o *Operator) processNextWorkItem(ctx context.Context) bool {
func (o *Operator) sync(ctx context.Context, key string) error {
trobj, err := o.thanosRulerInfs.Get(key)
if apierrors.IsNotFound(err) {
// Dependent resources are cleaned up by K8s via OwnerReferences
return nil