1
0
Fork 0
mirror of https://github.com/prometheus-operator/prometheus-operator.git synced 2025-04-21 03:38:43 +00:00

crd: Enrich Prometheus operator CRD registration error handling

If a user:
1. deploys the Prometheus operator without CRD validation
2. creates an invalid CRD instance (e.g. Servicemonitor)
3. restarts the Prometheus operator
it will crash-loop with a difficult to understand error message, failing
to parse the invalid CRD instance.

This patch improves the error feedback to the user, to identify the
invalid CRD more easily. The Prometheus operator will keep
crash-looping to enforce a valid CRD state on startup.
This commit is contained in:
Max Leonard Inden 2018-04-11 11:38:46 +02:00
parent 45d89a0e52
commit a036337c9b
No known key found for this signature in database
GPG key ID: 5403C5464810BC26
3 changed files with 22 additions and 8 deletions
pkg
alertmanager
k8sutil
prometheus

View file

@ -554,5 +554,8 @@ func (c *Operator) createCRDs() error {
}
// We have to wait for the CRDs to be ready. Otherwise the initial watch may fail.
return k8sutil.WaitForCRDReady(c.mclient.MonitoringV1().Alertmanagers(c.config.Namespace).List)
return errors.Wrap(
k8sutil.WaitForCRDReady(c.mclient.MonitoringV1().Alertmanagers(c.config.Namespace).List),
"waiting for Alertmanager crd failed",
)
}

View file

@ -41,7 +41,7 @@ var CustomResourceDefinitionTypeMeta metav1.TypeMeta = metav1.TypeMeta{
APIVersion: "apiextensions.k8s.io/v1beta1",
}
// WaitForCRDReady waits for a third party resource to be available for use.
// WaitForCRDReady waits for a custom resource definition to be available for use.
func WaitForCRDReady(listFunc func(opts metav1.ListOptions) (runtime.Object, error)) error {
err := wait.Poll(3*time.Second, 10*time.Minute, func() (bool, error) {
_, err := listFunc(metav1.ListOptions{})
@ -51,7 +51,7 @@ func WaitForCRDReady(listFunc func(opts metav1.ListOptions) (runtime.Object, err
return false, nil
}
}
return false, err
return false, errors.Wrap(err, "failed to list CRD")
}
return true, nil
})

View file

@ -37,6 +37,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/rest"
@ -1079,10 +1080,20 @@ func (c *Operator) createCRDs() error {
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.
err := k8sutil.WaitForCRDReady(c.mclient.MonitoringV1().Prometheuses(c.config.Namespace).List)
if err != nil {
return err
crdListFuncs := []struct {
name string
listFunc func(opts metav1.ListOptions) (runtime.Object, error)
}{
{"Prometheus", c.mclient.MonitoringV1().Prometheuses(c.config.Namespace).List},
{"ServiceMonitor", c.mclient.MonitoringV1().ServiceMonitors(c.config.Namespace).List},
}
return k8sutil.WaitForCRDReady(c.mclient.MonitoringV1().ServiceMonitors(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
}