mirror of
https://github.com/coreos/prometheus-operator.git
synced 2026-02-05 06:45:27 +01:00
Enable depguard in more test framework files
Signed-off-by: Arthur Silva Sens <arthur.sens@coralogix.com>
This commit is contained in:
@@ -49,6 +49,13 @@ linters-settings:
|
||||
- "**/versionutil/*.go"
|
||||
- "**/webconfig/*.go"
|
||||
- "**/e2e/*.go"
|
||||
- "**/framework/admission_webhooks.go"
|
||||
- "**/framework/alertmanager.go"
|
||||
- "**/framework/cluster_role_binding.go"
|
||||
- "**/framework/cluster_role.go"
|
||||
- "**/framework/configmap.go"
|
||||
- "**/framework/context.go"
|
||||
- "**/framework/crd.go"
|
||||
- "**/framework/framework.go"
|
||||
deny:
|
||||
- pkg: "github.com/pkg/errors"
|
||||
|
||||
@@ -16,8 +16,8 @@ package framework
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
v1 "k8s.io/api/admissionregistration/v1"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
@@ -27,7 +27,7 @@ import (
|
||||
func (f *Framework) createOrUpdateMutatingHook(ctx context.Context, certBytes []byte, namespace, source string) (FinalizerFn, error) {
|
||||
hook, err := parseMutatingHookYaml(source)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "Failed parsing mutating webhook")
|
||||
return nil, fmt.Errorf("Failed parsing mutating webhook: %w", err)
|
||||
}
|
||||
|
||||
hook.Webhooks[0].ClientConfig.Service.Namespace = namespace
|
||||
@@ -35,14 +35,14 @@ func (f *Framework) createOrUpdateMutatingHook(ctx context.Context, certBytes []
|
||||
|
||||
h, err := f.KubeClient.AdmissionregistrationV1().MutatingWebhookConfigurations().Get(ctx, hook.Name, metav1.GetOptions{})
|
||||
if err != nil && !apierrors.IsNotFound(err) {
|
||||
return nil, errors.Wrapf(err, "failed to get mutating webhook %s", hook.Name)
|
||||
return nil, fmt.Errorf("failed to get mutating webhook %s: %w", hook.Name, err)
|
||||
}
|
||||
|
||||
if apierrors.IsNotFound(err) {
|
||||
// MutatingWebhookConfiguration doesn't exists -> Create
|
||||
_, err = f.KubeClient.AdmissionregistrationV1().MutatingWebhookConfigurations().Create(ctx, hook, metav1.CreateOptions{})
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to create mutating webhook %s", hook.Name)
|
||||
return nil, fmt.Errorf("failed to create mutating webhook %s: %w", hook.Name, err)
|
||||
}
|
||||
} else {
|
||||
// must set this field from existing MutatingWebhookConfiguration to prevent update fail
|
||||
@@ -51,7 +51,7 @@ func (f *Framework) createOrUpdateMutatingHook(ctx context.Context, certBytes []
|
||||
// MutatingWebhookConfiguration already exists -> Update
|
||||
_, err = f.KubeClient.AdmissionregistrationV1().MutatingWebhookConfigurations().Update(ctx, hook, metav1.UpdateOptions{})
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to update mutating webhook %s", hook.Name)
|
||||
return nil, fmt.Errorf("failed to update mutating webhook %s: %w", hook.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ func (f *Framework) createOrUpdateMutatingHook(ctx context.Context, certBytes []
|
||||
func (f *Framework) createOrUpdateValidatingHook(ctx context.Context, certBytes []byte, namespace, source string) (FinalizerFn, error) {
|
||||
hook, err := parseValidatingHookYaml(source)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "Failed parsing validating webhook")
|
||||
return nil, fmt.Errorf("Failed parsing validating webhook: %w", err)
|
||||
}
|
||||
|
||||
hook.Webhooks[0].ClientConfig.Service.Namespace = namespace
|
||||
@@ -71,14 +71,14 @@ func (f *Framework) createOrUpdateValidatingHook(ctx context.Context, certBytes
|
||||
|
||||
h, err := f.KubeClient.AdmissionregistrationV1().ValidatingWebhookConfigurations().Get(ctx, hook.Name, metav1.GetOptions{})
|
||||
if err != nil && !apierrors.IsNotFound(err) {
|
||||
return nil, errors.Wrapf(err, "failed to get validating webhook %s", hook.Name)
|
||||
return nil, fmt.Errorf("failed to get validating webhook %s: %w", hook.Name, err)
|
||||
}
|
||||
|
||||
if apierrors.IsNotFound(err) {
|
||||
// ValidatingWebhookConfiguration doesn't exists -> Create
|
||||
_, err = f.KubeClient.AdmissionregistrationV1().ValidatingWebhookConfigurations().Create(ctx, hook, metav1.CreateOptions{})
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to create validating webhook %s", hook.Name)
|
||||
return nil, fmt.Errorf("failed to create validating webhook %s: %w", hook.Name, err)
|
||||
}
|
||||
} else {
|
||||
// must set this field from existing ValidatingWebhookConfiguration to prevent update fail
|
||||
@@ -87,7 +87,7 @@ func (f *Framework) createOrUpdateValidatingHook(ctx context.Context, certBytes
|
||||
// ValidatingWebhookConfiguration already exists -> Update
|
||||
_, err = f.KubeClient.AdmissionregistrationV1().ValidatingWebhookConfigurations().Update(ctx, hook, metav1.UpdateOptions{})
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to update validating webhook %s", hook.Name)
|
||||
return nil, fmt.Errorf("failed to update validating webhook %s: %w", hook.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,7 +112,7 @@ func parseValidatingHookYaml(source string) (*v1.ValidatingWebhookConfiguration,
|
||||
|
||||
resource := v1.ValidatingWebhookConfiguration{}
|
||||
if err := yaml.NewYAMLOrJSONDecoder(manifest, 100).Decode(&resource); err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to decode file %s", source)
|
||||
return nil, fmt.Errorf("failed to decode file %s: %w", source, err)
|
||||
}
|
||||
|
||||
return &resource, nil
|
||||
@@ -126,7 +126,7 @@ func parseMutatingHookYaml(source string) (*v1.MutatingWebhookConfiguration, err
|
||||
|
||||
resource := v1.MutatingWebhookConfiguration{}
|
||||
if err := yaml.NewYAMLOrJSONDecoder(manifest, 100).Decode(&resource); err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to decode file %s", source)
|
||||
return nil, fmt.Errorf("failed to decode file %s: %w", source, err)
|
||||
}
|
||||
|
||||
return &resource, nil
|
||||
|
||||
@@ -24,7 +24,6 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/prometheus/alertmanager/api/v2/client/silence"
|
||||
"github.com/prometheus/alertmanager/api/v2/models"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
@@ -84,7 +83,7 @@ func (f *Framework) CreateAlertmanagerConfig(ctx context.Context, ns, name strin
|
||||
}
|
||||
subRouteJSON, err := json.Marshal(subRoute)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to marshal subroute")
|
||||
return nil, fmt.Errorf("failed to marshal subroute: %w", err)
|
||||
}
|
||||
|
||||
amConfig := &monitoringv1alpha1.AlertmanagerConfig{
|
||||
@@ -187,17 +186,17 @@ func (f *Framework) CreateAlertmanagerAndWaitUntilReady(ctx context.Context, a *
|
||||
amConfigSecretName := fmt.Sprintf("alertmanager-%s", a.Name)
|
||||
s, err := f.AlertmanagerConfigSecret(a.Namespace, amConfigSecretName)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, fmt.Sprintf("making alertmanager config secret %v failed", amConfigSecretName))
|
||||
return nil, fmt.Errorf("making alertmanager config secret %v failed: %w", amConfigSecretName, err)
|
||||
}
|
||||
|
||||
_, err = f.KubeClient.CoreV1().Secrets(a.Namespace).Create(ctx, s, metav1.CreateOptions{})
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, fmt.Sprintf("creating alertmanager config secret %v failed", s.Name))
|
||||
return nil, fmt.Errorf("creating alertmanager config secret %v failed: %w", s.Name, err)
|
||||
}
|
||||
|
||||
a, err = f.MonClientV1.Alertmanagers(a.Namespace).Create(ctx, a, metav1.CreateOptions{})
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, fmt.Sprintf("creating alertmanager %v failed", a.Name))
|
||||
return nil, fmt.Errorf("creating alertmanager %v failed: %w", a.Name, err)
|
||||
}
|
||||
|
||||
return a, f.WaitForAlertmanagerReady(ctx, a)
|
||||
@@ -224,7 +223,7 @@ func (f *Framework) WaitForAlertmanagerReady(ctx context.Context, a *monitoringv
|
||||
},
|
||||
5*time.Minute,
|
||||
); err != nil {
|
||||
return errors.Wrapf(err, "alertmanager %v/%v failed to become available", a.Namespace, a.Name)
|
||||
return fmt.Errorf("alertmanager %v/%v failed to become available: %w", a.Namespace, a.Name, err)
|
||||
}
|
||||
|
||||
// Check that all pods report the expected number of peers.
|
||||
@@ -233,11 +232,9 @@ func (f *Framework) WaitForAlertmanagerReady(ctx context.Context, a *monitoringv
|
||||
for i := 0; i < replicas; i++ {
|
||||
name := fmt.Sprintf("alertmanager-%v-%v", a.Name, strconv.Itoa(i))
|
||||
if err := f.WaitForAlertmanagerPodInitialized(ctx, a.Namespace, name, replicas, a.Spec.ForceEnableClusterMode, isAMHTTPS); err != nil {
|
||||
return errors.Wrap(err,
|
||||
fmt.Sprintf(
|
||||
"failed to wait for an Alertmanager cluster (%s) with %d instances to become ready",
|
||||
name, replicas,
|
||||
),
|
||||
return fmt.Errorf(
|
||||
"failed to wait for an Alertmanager cluster (%s) with %d instances to become ready: %w",
|
||||
name, replicas, err,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -248,7 +245,7 @@ func (f *Framework) WaitForAlertmanagerReady(ctx context.Context, a *monitoringv
|
||||
func (f *Framework) PatchAlertmanagerAndWaitUntilReady(ctx context.Context, name, ns string, spec monitoringv1.AlertmanagerSpec) (*monitoringv1.Alertmanager, error) {
|
||||
a, err := f.PatchAlertmanager(ctx, name, ns, spec)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to patch Alertmanager %s/%s", ns, name)
|
||||
return nil, fmt.Errorf("failed to patch Alertmanager %s/%s: %w", ns, name, err)
|
||||
}
|
||||
|
||||
err = f.WaitForAlertmanagerReady(ctx, a)
|
||||
@@ -270,7 +267,7 @@ func (f *Framework) PatchAlertmanager(ctx context.Context, name, ns string, spec
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to marshal Alertmanager spec")
|
||||
return nil, fmt.Errorf("failed to marshal Alertmanager spec: %w", err)
|
||||
}
|
||||
|
||||
p, err := f.MonClientV1.Alertmanagers(ns).Patch(
|
||||
@@ -305,11 +302,11 @@ func (f *Framework) ScaleAlertmanagerAndWaitUntilReady(ctx context.Context, name
|
||||
func (f *Framework) DeleteAlertmanagerAndWaitUntilGone(ctx context.Context, ns, name string) error {
|
||||
_, err := f.MonClientV1.Alertmanagers(ns).Get(ctx, name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return errors.Wrap(err, fmt.Sprintf("requesting Alertmanager tpr %v failed", name))
|
||||
return fmt.Errorf("requesting Alertmanager tpr %v failed: %w", name, err)
|
||||
}
|
||||
|
||||
if err := f.MonClientV1.Alertmanagers(ns).Delete(ctx, name, metav1.DeleteOptions{}); err != nil {
|
||||
return errors.Wrap(err, fmt.Sprintf("deleting Alertmanager tpr %v failed", name))
|
||||
return fmt.Errorf("deleting Alertmanager tpr %v failed: %w", name, err)
|
||||
}
|
||||
|
||||
if err := f.WaitForPodsReady(
|
||||
@@ -319,7 +316,7 @@ func (f *Framework) DeleteAlertmanagerAndWaitUntilGone(ctx context.Context, ns,
|
||||
0,
|
||||
alertmanager.ListOptions(name),
|
||||
); err != nil {
|
||||
return errors.Wrap(err, fmt.Sprintf("waiting for Alertmanager tpr (%s) to vanish timed out", name))
|
||||
return fmt.Errorf("waiting for Alertmanager tpr (%s) to vanish timed out: %w", name, err)
|
||||
}
|
||||
|
||||
err = f.KubeClient.CoreV1().Secrets(ns).Delete(ctx, fmt.Sprintf("alertmanager-%s", name), metav1.DeleteOptions{})
|
||||
@@ -469,7 +466,7 @@ func (f *Framework) WaitForAlertmanagerFiringAlert(ctx context.Context, ns, svcN
|
||||
}
|
||||
|
||||
if err := json.NewDecoder(bytes.NewBuffer(resp)).Decode(&alerts); err != nil {
|
||||
return false, errors.Wrap(err, "failed to decode alerts from Alertmanager API")
|
||||
return false, fmt.Errorf("failed to decode alerts from Alertmanager API: %w", err)
|
||||
}
|
||||
|
||||
if len(alerts) != 1 {
|
||||
|
||||
@@ -16,12 +16,12 @@ package framework
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
@@ -36,7 +36,7 @@ import (
|
||||
func (f *Framework) GetCRD(ctx context.Context, name string) (*v1.CustomResourceDefinition, error) {
|
||||
crd, err := f.APIServerClient.ApiextensionsV1().CustomResourceDefinitions().Get(ctx, name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "unable to get CRD with name %v", name)
|
||||
return nil, fmt.Errorf("unable to get CRD with name %v: %w", name, err)
|
||||
}
|
||||
return crd, nil
|
||||
}
|
||||
@@ -45,7 +45,7 @@ func (f *Framework) GetCRD(ctx context.Context, name string) (*v1.CustomResource
|
||||
func (f *Framework) ListCRDs(ctx context.Context) (*v1.CustomResourceDefinitionList, error) {
|
||||
crds, err := f.APIServerClient.ApiextensionsV1().CustomResourceDefinitions().List(ctx, metav1.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "unable to list CRDs")
|
||||
return nil, fmt.Errorf("unable to list CRDs: %w", err)
|
||||
}
|
||||
return crds, nil
|
||||
}
|
||||
@@ -54,14 +54,14 @@ func (f *Framework) ListCRDs(ctx context.Context) (*v1.CustomResourceDefinitionL
|
||||
func (f *Framework) CreateOrUpdateCRD(ctx context.Context, crd *v1.CustomResourceDefinition) error {
|
||||
c, err := f.APIServerClient.ApiextensionsV1().CustomResourceDefinitions().Get(ctx, crd.Name, metav1.GetOptions{})
|
||||
if err != nil && !apierrors.IsNotFound(err) {
|
||||
return errors.Wrapf(err, "getting CRD: %s", crd.Spec.Names.Kind)
|
||||
return fmt.Errorf("getting CRD %s: %w", crd.Spec.Names.Kind, err)
|
||||
}
|
||||
|
||||
if apierrors.IsNotFound(err) {
|
||||
// CRD doesn't exists -> Create
|
||||
_, err := f.APIServerClient.ApiextensionsV1().CustomResourceDefinitions().Create(ctx, crd, metav1.CreateOptions{})
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "create CRD: %s", crd.Spec.Names.Kind)
|
||||
return fmt.Errorf("create CRD %s: %w", crd.Spec.Names.Kind, err)
|
||||
}
|
||||
} else {
|
||||
// must set this field from existing CRD to prevent update fail
|
||||
@@ -70,7 +70,7 @@ func (f *Framework) CreateOrUpdateCRD(ctx context.Context, crd *v1.CustomResourc
|
||||
// CRD already exists -> Update
|
||||
_, err := f.APIServerClient.ApiextensionsV1().CustomResourceDefinitions().Update(ctx, crd, metav1.UpdateOptions{})
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "update CRD: %s", crd.Spec.Names.Kind)
|
||||
return fmt.Errorf("update CRD %s: %w", crd.Spec.Names.Kind, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
@@ -79,7 +79,7 @@ func (f *Framework) CreateOrUpdateCRD(ctx context.Context, crd *v1.CustomResourc
|
||||
func (f *Framework) DeleteCRD(ctx context.Context, name string) error {
|
||||
err := f.APIServerClient.ApiextensionsV1().CustomResourceDefinitions().Delete(ctx, name, metav1.DeleteOptions{})
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unable to delete CRD with name %v", name)
|
||||
return fmt.Errorf("unable to delete CRD with name %v: %w", name, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -89,18 +89,18 @@ func (f *Framework) DeleteCRD(ctx context.Context, name string) error {
|
||||
func (f *Framework) MakeCRD(source string) (*v1.CustomResourceDefinition, error) {
|
||||
manifest, err := SourceToIOReader(source)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "get manifest from source: %s", source)
|
||||
return nil, fmt.Errorf("get manifest from source %s: %w", source, err)
|
||||
}
|
||||
|
||||
content, err := io.ReadAll(manifest)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "get manifest content")
|
||||
return nil, fmt.Errorf("get manifest content: %w", err)
|
||||
}
|
||||
|
||||
crd := v1.CustomResourceDefinition{}
|
||||
err = yaml.Unmarshal(content, &crd)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "unmarshal CRD asset file: %s", source)
|
||||
return nil, fmt.Errorf("unmarshal CRD asset file %s: %w", source, err)
|
||||
}
|
||||
|
||||
return &crd, nil
|
||||
@@ -116,12 +116,15 @@ func WaitForCRDReady(listFunc func(opts metav1.ListOptions) (runtime.Object, err
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
return false, errors.Wrap(err, "failed to list CRD")
|
||||
return false, fmt.Errorf("failed to list CRD: %w", err)
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
|
||||
return errors.Wrap(err, "timed out waiting for Custom Resource")
|
||||
if err != nil {
|
||||
return fmt.Errorf("timed out waiting for Custom Resource: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateCRDAndWaitUntilReady creates a Custom Resource Definition from yaml
|
||||
@@ -133,7 +136,7 @@ func (f *Framework) CreateOrUpdateCRDAndWaitUntilReady(ctx context.Context, crdN
|
||||
|
||||
crd, err := f.MakeCRD(assetPath)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "create CRD: %s from manifest: %s", crdName, assetPath)
|
||||
return fmt.Errorf("create CRD: %s from manifest: %s: %w", crdName, assetPath, err)
|
||||
}
|
||||
|
||||
crd.ObjectMeta.Name = crd.Spec.Names.Plural + "." + group
|
||||
@@ -141,12 +144,12 @@ func (f *Framework) CreateOrUpdateCRDAndWaitUntilReady(ctx context.Context, crdN
|
||||
|
||||
err = f.CreateOrUpdateCRD(ctx, crd)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "create CRD %s on the apiserver", crdName)
|
||||
return fmt.Errorf("create CRD %s on the apiserver: %w", crdName, err)
|
||||
}
|
||||
|
||||
err = WaitForCRDReady(listFunc)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "%s CRD not ready", crdName)
|
||||
return fmt.Errorf("%s CRD not ready: %w", crdName, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user