Skip to content

Commit

Permalink
fix TLS setup when cluster driver notifies plugin with client updates
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandreLamarre committed Oct 27, 2023
1 parent 6323bd1 commit 2ce8d10
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package alerting_manager_test

import (
"testing"

_ "github.com/rancher/opni/pkg/test/setup"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestAlertingManager(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "AlertingManager Suite")
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func (a *AlertingClusterManager) InstallCluster(ctx context.Context, _ *emptypb.
lg.Error(fmt.Sprintf("%s", retryErr))
return nil, retryErr
}
a.notify(1)
return &emptypb.Empty{}, nil
}

Expand Down Expand Up @@ -343,6 +344,7 @@ func (a *AlertingClusterManager) notify(replicas int) {
alertingClient.WithQuerierAddress(
fmt.Sprintf("%s:3000", shared.AlertmanagerService),
),
alertingClient.WithTLSConfig(a.TlsConfig),
)
if err != nil {
panic(err)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package alerting_manager_test

import (
"crypto/tls"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/rancher/opni/pkg/alerting/client"
"github.com/rancher/opni/pkg/alerting/shared"
"github.com/rancher/opni/pkg/logger"
"github.com/rancher/opni/pkg/plugins/driverutil"
"github.com/rancher/opni/plugins/alerting/pkg/alerting/drivers/alerting_manager"
)

var _ = Describe("", Label("unit"), func() {
When("We register the alering cluster driver", func() {
It("should apply the tls config via driver options", func() {
tlsConfig := &tls.Config{}
opts := []driverutil.Option{
driverutil.NewOption("tlsConfig", tlsConfig),
}

options := alerting_manager.AlertingDriverOptions{
ConfigKey: shared.AlertManagerConfigKey,
InternalRoutingKey: shared.InternalRoutingConfigKey,
Logger: logger.NewPluginLogger().WithGroup("alerting").WithGroup("alerting-manager"),
}
driverutil.ApplyOptions(&options, opts...)
Expect(options.TlsConfig).NotTo(BeNil())
})

It("should apply cluster driver subscribers via driver options", func() {
subscriberA := make(chan client.AlertingClient)
subscriberB := make(chan client.AlertingClient)
opts := []driverutil.Option{
driverutil.NewOption("subscribers", []chan client.AlertingClient{subscriberA, subscriberB}),
}

options := alerting_manager.AlertingDriverOptions{
ConfigKey: shared.AlertManagerConfigKey,
InternalRoutingKey: shared.InternalRoutingConfigKey,
Logger: logger.NewPluginLogger().WithGroup("alerting").WithGroup("alerting-manager"),
}
driverutil.ApplyOptions(&options, opts...)
Expect(options.Subscribers).To(HaveLen(2))
})
})
})
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package alerting_manager

import (
"crypto/tls"
"log/slog"

alertingClient "github.com/rancher/opni/pkg/alerting/client"
"github.com/rancher/opni/pkg/alerting/shared"
"k8s.io/apimachinery/pkg/types"
"log/slog"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand All @@ -16,4 +18,5 @@ type AlertingDriverOptions struct {
InternalRoutingKey string `option:"internalRoutingKey"`
AlertingOptions *shared.AlertingClusterOptions `option:"alertingOptions"`
Subscribers []chan alertingClient.AlertingClient `option:"subscribers"`
TlsConfig *tls.Config `option:"tlsConfig"`
}
16 changes: 10 additions & 6 deletions plugins/alerting/pkg/alerting/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
)

func (p *Plugin) UseManagementAPI(client managementv1.ManagementClient) {
opt := &shared.AlertingClusterOptions{}
p.mgmtClient.Set(client)
cfg, err := client.GetConfig(context.Background(),
&emptypb.Empty{}, grpc.WaitForReady(true))
Expand All @@ -63,7 +64,7 @@ func (p *Plugin) UseManagementAPI(client managementv1.ManagementClient) {
os.Exit(1)
}
p.storageBackend.Set(backend)
opt := &shared.AlertingClusterOptions{
opt = &shared.AlertingClusterOptions{
Namespace: config.Spec.Alerting.Namespace,
WorkerNodesService: config.Spec.Alerting.WorkerNodeService,
WorkerNodePort: config.Spec.Alerting.WorkerPort,
Expand All @@ -74,13 +75,16 @@ func (p *Plugin) UseManagementAPI(client managementv1.ManagementClient) {
ConfigMap: config.Spec.Alerting.ConfigMap,
ManagementHookHandler: config.Spec.Alerting.ManagementHookHandler,
}
p.configureDriver(p.ctx,
driverutil.NewOption("alertingOptions", opt),
driverutil.NewOption("logger", p.logger.WithGroup("alerting-manager")),
driverutil.NewOption("subscribers", []chan alertingClient.AlertingClient{p.clusterNotifier}),
)

})
tlsConfig := p.loadCerts()
p.configureDriver(
p.ctx,
driverutil.NewOption("alertingOptions", opt),
driverutil.NewOption("logger", p.logger.WithGroup("alerting-manager")),
driverutil.NewOption("subscribers", []chan alertingClient.AlertingClient{p.clusterNotifier}),
driverutil.NewOption("tlsConfig", tlsConfig),
)
p.alertingTLSConfig.Set(tlsConfig)
go p.handleDriverNotifications()
go p.runSync()
Expand Down

0 comments on commit 2ce8d10

Please sign in to comment.