Skip to content

Commit e48d9f2

Browse files
committed
Update observability func tests
Signed-off-by: João Vilaça <[email protected]>
1 parent 9262d8f commit e48d9f2

File tree

2 files changed

+31
-29
lines changed

2 files changed

+31
-29
lines changed

controllers/observability/pod_disruption_budget_at_limit.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ import (
1212
)
1313

1414
const (
15-
alertmanagerSvcHost = "https://alertmanager-main.openshift-monitoring.svc.cluster.local:9094"
16-
TlsCertPath = "/var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt"
15+
// ServiceAccountTlsCertPath is the path to the service account TLS certificate
16+
// used for TLS communication with the alertmanager API
17+
ServiceAccountTlsCertPath = "/var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt"
18+
19+
AlertmanagerSvcHost = "https://alertmanager-main.openshift-monitoring.svc.cluster.local:9094"
1720
)
1821

1922
func (r *Reconciler) ensurePodDisruptionBudgetAtLimitIsSilenced() error {
@@ -64,20 +67,28 @@ func (r *Reconciler) ensurePodDisruptionBudgetAtLimitIsSilenced() error {
6467
}
6568

6669
func (r *Reconciler) NewAlertmanagerApi() (*alertmanager.Api, error) {
67-
caCert, err := os.ReadFile(TlsCertPath)
70+
httpClient, err := NewHTTPClient()
6871
if err != nil {
69-
return nil, fmt.Errorf("failed to read ca cert: %w", err)
72+
return nil, fmt.Errorf("failed to create http client: %w", err)
7073
}
7174

72-
caCertPool := x509.NewCertPool()
73-
caCertPool.AppendCertsFromPEM(caCert)
75+
return alertmanager.NewAPI(*httpClient, AlertmanagerSvcHost, r.config.BearerToken), nil
76+
}
7477

75-
httpClient := http.Client{}
76-
httpClient.Transport = &http.Transport{
77-
TLSClientConfig: &tls.Config{RootCAs: caCertPool},
78+
func NewHTTPClient() (*http.Client, error) {
79+
caCert, err := os.ReadFile(ServiceAccountTlsCertPath)
80+
if err != nil {
81+
return nil, fmt.Errorf("failed to read service account TLS certificate: %w", err)
7882
}
7983

80-
return alertmanager.NewAPI(httpClient, alertmanagerSvcHost, r.config.BearerToken), nil
84+
caCertPool := x509.NewCertPool()
85+
caCertPool.AppendCertsFromPEM(caCert)
86+
87+
return &http.Client{
88+
Transport: &http.Transport{
89+
TLSClientConfig: &tls.Config{RootCAs: caCertPool},
90+
},
91+
}, nil
8192
}
8293

8394
func FindPodDisruptionBudgetAtLimitSilence(amSilences []alertmanager.Silence) *alertmanager.Silence {

tests/func-tests/observability_controller_test.go

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,20 @@ import (
1010

1111
v1 "k8s.io/api/core/v1"
1212
"sigs.k8s.io/controller-runtime/pkg/client"
13-
logf "sigs.k8s.io/controller-runtime/pkg/log"
14-
"sigs.k8s.io/controller-runtime/pkg/manager"
1513

16-
"github.com/kubevirt/hyperconverged-cluster-operator/controllers/commontestutils"
1714
"github.com/kubevirt/hyperconverged-cluster-operator/controllers/observability"
15+
"github.com/kubevirt/hyperconverged-cluster-operator/pkg/alertmanager"
1816
tests "github.com/kubevirt/hyperconverged-cluster-operator/tests/func-tests"
1917
)
2018

2119
const testName = "observability_controller"
2220

23-
var logger = logf.Log.WithName("observability-controller")
24-
25-
var _ = Describe("Observability Controller", Label(tests.OpenshiftLabel, "observability_controller"), func() {
26-
var r *observability.Reconciler
21+
var _ = Describe("Observability Controller", Label(tests.OpenshiftLabel, testName), func() {
22+
var cli client.Client
2723

2824
BeforeEach(func(ctx context.Context) {
29-
cli := tests.GetControllerRuntimeClient()
25+
cli = tests.GetControllerRuntimeClient()
3026
tests.FailIfNotOpenShift(ctx, cli, testName)
31-
32-
mgr, err := commontestutils.NewManagerMock(tests.GetClientConfig(), manager.Options{}, cli, logger)
33-
Expect(err).ToNot(HaveOccurred())
34-
35-
r = observability.NewReconciler(mgr, tests.InstallNamespace, nil)
3627
})
3728

3829
Context("PodDisruptionBudgetAtLimit", func() {
@@ -45,9 +36,10 @@ var _ = Describe("Observability Controller", Label(tests.OpenshiftLabel, "observ
4536
}
4637
})
4738

48-
It("should be silenced", func() {
49-
amApi, err := r.NewAlertmanagerApi()
39+
It("should be silenced", func(ctx context.Context) {
40+
httpClient, err := observability.NewHTTPClient()
5041
Expect(err).ToNot(HaveOccurred())
42+
amApi := alertmanager.NewAPI(*httpClient, observability.AlertmanagerSvcHost, tests.GetClientConfig().BearerToken)
5143

5244
amSilences, err := amApi.ListSilences()
5345
Expect(err).ToNot(HaveOccurred())
@@ -60,16 +52,15 @@ var _ = Describe("Observability Controller", Label(tests.OpenshiftLabel, "observ
6052
Expect(err).ToNot(HaveOccurred())
6153

6254
// Restart pod to force reconcile (reconcile periodicity is 1h)
63-
cli := tests.GetControllerRuntimeClient()
6455
var hcoPods v1.PodList
65-
err = cli.List(context.Background(), &hcoPods, &client.MatchingLabels{
56+
err = cli.List(ctx, &hcoPods, &client.MatchingLabels{
6657
"name": "hyperconverged-cluster-operator",
6758
})
6859
Expect(err).ToNot(HaveOccurred())
6960
Expect(hcoPods.Items).ToNot(BeEmpty())
7061

7162
for _, pod := range hcoPods.Items {
72-
err = cli.Delete(context.Background(), &pod)
63+
err = cli.Delete(ctx, &pod)
7364
Expect(err).ToNot(HaveOccurred())
7465
}
7566

@@ -85,7 +76,7 @@ var _ = Describe("Observability Controller", Label(tests.OpenshiftLabel, "observ
8576
})
8677

8778
func serviceAccountTlsCertPathExists() (bool, error) {
88-
_, err := os.Stat(observability.TlsCertPath)
79+
_, err := os.Stat(observability.ServiceAccountTlsCertPath)
8980
if errors.Is(err, os.ErrNotExist) {
9081
return false, nil
9182
}

0 commit comments

Comments
 (0)