mirror of
https://github.com/prometheus-operator/prometheus-operator.git
synced 2025-04-21 03:38:43 +00:00
Merge pull request #1416 from brancz/update-validation
*: Update CRDs on boot
This commit is contained in:
commit
1afb7d850a
2 changed files with 48 additions and 27 deletions
pkg
|
@ -38,6 +38,7 @@ import (
|
|||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/fields"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
|
@ -534,13 +535,6 @@ func (c *Operator) destroyAlertmanager(key string) error {
|
|||
}
|
||||
|
||||
func (c *Operator) createCRDs() error {
|
||||
_, aErr := c.mclient.MonitoringV1().Alertmanagers(c.config.Namespace).List(metav1.ListOptions{})
|
||||
if aErr == nil {
|
||||
// If Alertmanager objects are already registered, we won't attempt to
|
||||
// do so again.
|
||||
return nil
|
||||
}
|
||||
|
||||
crds := []*extensionsobj.CustomResourceDefinition{
|
||||
k8sutil.NewCustomResourceDefinition(c.config.CrdKinds.Alertmanager, c.config.CrdGroup, c.config.Labels.LabelsMap, c.config.EnableValidation),
|
||||
}
|
||||
|
@ -548,15 +542,38 @@ func (c *Operator) createCRDs() error {
|
|||
crdClient := c.crdclient.ApiextensionsV1beta1().CustomResourceDefinitions()
|
||||
|
||||
for _, crd := range crds {
|
||||
if _, err := crdClient.Create(crd); err != nil && !apierrors.IsAlreadyExists(err) {
|
||||
return errors.Wrapf(err, "Creating CRD: %s", crd.Spec.Names.Kind)
|
||||
oldCRD, err := crdClient.Get(crd.Name, metav1.GetOptions{})
|
||||
if err != nil && !apierrors.IsNotFound(err) {
|
||||
return errors.Wrapf(err, "getting CRD: %s", crd.Spec.Names.Kind)
|
||||
}
|
||||
if apierrors.IsNotFound(err) {
|
||||
if _, err := crdClient.Create(crd); err != nil {
|
||||
return errors.Wrapf(err, "creating CRD: %s", crd.Spec.Names.Kind)
|
||||
}
|
||||
level.Info(c.logger).Log("msg", "CRD created", "crd", crd.Spec.Names.Kind)
|
||||
}
|
||||
if err == nil {
|
||||
crd.ResourceVersion = oldCRD.ResourceVersion
|
||||
if _, err := crdClient.Update(crd); err != nil {
|
||||
return errors.Wrapf(err, "creating CRD: %s", crd.Spec.Names.Kind)
|
||||
}
|
||||
level.Info(c.logger).Log("msg", "CRD updated", "crd", crd.Spec.Names.Kind)
|
||||
}
|
||||
level.Info(c.logger).Log("msg", "CRD created", "crd", crd.Spec.Names.Kind)
|
||||
}
|
||||
|
||||
// We have to wait for the CRDs to be ready. Otherwise the initial watch may fail.
|
||||
return errors.Wrap(
|
||||
k8sutil.WaitForCRDReady(c.mclient.MonitoringV1().Alertmanagers(c.config.Namespace).List),
|
||||
"waiting for Alertmanager crd failed",
|
||||
)
|
||||
crdListFuncs := []struct {
|
||||
name string
|
||||
listFunc func(opts metav1.ListOptions) (runtime.Object, error)
|
||||
}{
|
||||
{"Alertmanager", c.mclient.MonitoringV1().Alertmanagers(c.config.Namespace).List},
|
||||
}
|
||||
|
||||
for _, crdListFunc := range crdListFuncs {
|
||||
err := k8sutil.WaitForCRDReady(crdListFunc.listFunc)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "waiting for %v crd failed", crdListFunc.name)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -1105,15 +1105,6 @@ func (c *Operator) selectServiceMonitors(p *monitoringv1.Prometheus) (map[string
|
|||
}
|
||||
|
||||
func (c *Operator) createCRDs() error {
|
||||
_, pErr := c.mclient.MonitoringV1().Prometheuses(c.config.Namespace).List(metav1.ListOptions{})
|
||||
_, sErr := c.mclient.MonitoringV1().ServiceMonitors(c.config.Namespace).List(metav1.ListOptions{})
|
||||
_, rErr := c.mclient.MonitoringV1().RuleFiles(c.config.Namespace).List(metav1.ListOptions{})
|
||||
if pErr == nil && sErr == nil && rErr == nil {
|
||||
// If Prometheus, RuleFile and ServiceMonitor objects are already registered, we
|
||||
// won't attempt to do so again.
|
||||
return nil
|
||||
}
|
||||
|
||||
crds := []*extensionsobj.CustomResourceDefinition{
|
||||
k8sutil.NewCustomResourceDefinition(c.config.CrdKinds.Prometheus, c.config.CrdGroup, c.config.Labels.LabelsMap, c.config.EnableValidation),
|
||||
k8sutil.NewCustomResourceDefinition(c.config.CrdKinds.ServiceMonitor, c.config.CrdGroup, c.config.Labels.LabelsMap, c.config.EnableValidation),
|
||||
|
@ -1123,10 +1114,23 @@ func (c *Operator) createCRDs() error {
|
|||
crdClient := c.crdclient.ApiextensionsV1beta1().CustomResourceDefinitions()
|
||||
|
||||
for _, crd := range crds {
|
||||
if _, err := crdClient.Create(crd); err != nil && !apierrors.IsAlreadyExists(err) {
|
||||
return errors.Wrapf(err, "creating CRD: %s", crd.Spec.Names.Kind)
|
||||
oldCRD, err := crdClient.Get(crd.Name, metav1.GetOptions{})
|
||||
if err != nil && !apierrors.IsNotFound(err) {
|
||||
return errors.Wrapf(err, "getting CRD: %s", crd.Spec.Names.Kind)
|
||||
}
|
||||
if apierrors.IsNotFound(err) {
|
||||
if _, err := crdClient.Create(crd); err != nil {
|
||||
return errors.Wrapf(err, "creating CRD: %s", crd.Spec.Names.Kind)
|
||||
}
|
||||
level.Info(c.logger).Log("msg", "CRD created", "crd", crd.Spec.Names.Kind)
|
||||
}
|
||||
if err == nil {
|
||||
crd.ResourceVersion = oldCRD.ResourceVersion
|
||||
if _, err := crdClient.Update(crd); err != nil {
|
||||
return errors.Wrapf(err, "creating CRD: %s", crd.Spec.Names.Kind)
|
||||
}
|
||||
level.Info(c.logger).Log("msg", "CRD updated", "crd", crd.Spec.Names.Kind)
|
||||
}
|
||||
level.Info(c.logger).Log("msg", "CRD created", "crd", crd.Spec.Names.Kind)
|
||||
}
|
||||
|
||||
crdListFuncs := []struct {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue