Skip to content

Commit

Permalink
fix: compat with 1.18 for notify module
Browse files Browse the repository at this point in the history
  • Loading branch information
choujimmy committed May 11, 2020
1 parent aa7020f commit 1f02146
Show file tree
Hide file tree
Showing 39 changed files with 311 additions and 263 deletions.
11 changes: 6 additions & 5 deletions cmd/tke-notify-api/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package app

import (
"context"
"fmt"

v1 "tkestack.io/tke/api/platform/v1"
Expand All @@ -43,8 +44,8 @@ func CreateServerChain(cfg *config.Config) (*genericapiserver.GenericAPIServer,
return nil, err
}

apiServer.GenericAPIServer.AddPostStartHookOrDie("start-notify-api-server-informers", func(context genericapiserver.PostStartHookContext) error {
cfg.VersionedSharedInformerFactory.Start(context.StopCh)
apiServer.GenericAPIServer.AddPostStartHookOrDie("start-notify-api-server-informers", func(ctx genericapiserver.PostStartHookContext) error {
cfg.VersionedSharedInformerFactory.Start(ctx.StopCh)

// Store notify api address in configmap named tke-notify-api; It is used by prometheus addon for alertmanager to send alarms
notifyAPIAddress := fmt.Sprintf("https://%s:%d", cfg.ExternalHost, cfg.ExternalPort)
Expand All @@ -58,20 +59,20 @@ func CreateServerChain(cfg *config.Config) (*genericapiserver.GenericAPIServer,
Annotations: map[string]string{NotifyAPIAddressKey: notifyAPIAddress},
},
}
cm, err := cfg.PlatformClient.ConfigMaps().Get(NotifyApiConfigMapName, metav1.GetOptions{})
cm, err := cfg.PlatformClient.ConfigMaps().Get(context.Background(), NotifyApiConfigMapName, metav1.GetOptions{})
if err == nil && cm != nil {
v, ok := cm.Annotations[NotifyAPIAddressKey]
if !ok || v != notifyConfigMap.Annotations[NotifyAPIAddressKey] {
notifyConfigMap.ResourceVersion = cm.ResourceVersion
_, err = cfg.PlatformClient.ConfigMaps().Update(notifyConfigMap)
_, err = cfg.PlatformClient.ConfigMaps().Update(context.Background(), notifyConfigMap, metav1.UpdateOptions{})
if err != nil {
log.Warnf("failed to update configmap for tke-notify-api due to %v", err)
return err
}
}
return nil
}
cm, err = cfg.PlatformClient.ConfigMaps().Create(notifyConfigMap)
cm, err = cfg.PlatformClient.ConfigMaps().Create(context.Background(), notifyConfigMap, metav1.CreateOptions{})
if err != nil {
log.Warnf("failed to create configmap for tke-notify-api due to %v", err)
return err
Expand Down
2 changes: 1 addition & 1 deletion cmd/tke-platform-api/app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func buildWebhookAuditBackend(o apiserveroptions.AuditWebhookOptions) (audit.Bac
return nil, nil
}
groupVersion, _ := schema.ParseGroupVersion(o.GroupVersionString)
webhook, err := pluginwebhook.NewBackend(o.ConfigFile, groupVersion, o.InitialBackoff)
webhook, err := pluginwebhook.NewBackend(o.ConfigFile, groupVersion, o.InitialBackoff, nil)
if err != nil {
return nil, fmt.Errorf("initializing audit webhook: %v", err)
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/monitor/services/alertmanager/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ package alertmanager

import (
"bytes"
"context"

"github.com/pkg/errors"
alertconfig "github.com/prometheus/alertmanager/config"
"tkestack.io/tke/pkg/util/log"
)

func (h *processor) Create(clusterName string, alertValue string, entity *alertconfig.Route) error {
func (h *processor) Create(ctx context.Context, clusterName string, alertValue string, entity *alertconfig.Route) error {
h.Lock()
defer h.Unlock()

Expand All @@ -37,7 +39,7 @@ func (h *processor) Create(clusterName string, alertValue string, entity *alertc
return errors.New("empty alertValue")
}

routeOp, err := h.loadConfig(clusterName)
routeOp, err := h.loadConfig(ctx, clusterName)
if err != nil {
return errors.Wrapf(err, "route operator not found")
}
Expand All @@ -55,7 +57,7 @@ func (h *processor) Create(clusterName string, alertValue string, entity *alertc
return errors.Wrapf(err, "failed to save")
}

err = h.saveConfig(clusterName, output.String())
err = h.saveConfig(ctx, clusterName, output.String())
if err != nil {
return errors.Wrapf(err, "failed to save configmap")
}
Expand Down
22 changes: 11 additions & 11 deletions pkg/monitor/services/alertmanager/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
package alertmanager

import (
"context"
"reflect"
"testing"

alertmanager_config "tkestack.io/tke/pkg/platform/controller/addon/prometheus"

alert_config "github.com/prometheus/alertmanager/config"
alertconfig "github.com/prometheus/alertmanager/config"
"gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
alertmanagerconfig "tkestack.io/tke/pkg/platform/controller/addon/prometheus"
)

func TestProcessor_Create(t *testing.T) {
Expand All @@ -37,8 +37,8 @@ func TestProcessor_Create(t *testing.T) {
}

t.Logf("With no alert label")
route := &alert_config.Route{}
err = p.Create(clusterName, "test", route)
route := &alertconfig.Route{}
err = p.Create(context.Background(), clusterName, "test", route)
if err == nil {
t.Errorf("creation should failed")
return
Expand All @@ -48,7 +48,7 @@ func TestProcessor_Create(t *testing.T) {
"alert": "test",
}
t.Logf("With duplicate match label")
err = p.Create(clusterName, "test", route)
err = p.Create(context.Background(), clusterName, "test", route)
if err == nil {
t.Errorf("creation should failed")
return
Expand All @@ -60,22 +60,22 @@ func TestProcessor_Create(t *testing.T) {
}

t.Logf("With correct label")
err = p.Create(clusterName, "abc", route)
err = p.Create(context.Background(), clusterName, "abc", route)
if err != nil {
t.Errorf("creation should success, code: %s", err)
return
}

t.Logf("Validate persistent data")
configMap, err := k8sClient.CoreV1().ConfigMaps(metav1.NamespaceSystem).Get(alertmanager_config.AlertManagerConfigMap, metav1.GetOptions{})
configMap, err := k8sClient.CoreV1().ConfigMaps(metav1.NamespaceSystem).Get(context.Background(), alertmanagerconfig.AlertManagerConfigMap, metav1.GetOptions{})
if err != nil {
t.Errorf("can't get persistent data, %v", err)
return
}

targetConfig := &alert_config.Config{}
expectConfig := &alert_config.Config{}
_ = yaml.Unmarshal([]byte(configMap.Data[alertmanager_config.AlertManagerConfigName]), targetConfig)
targetConfig := &alertconfig.Config{}
expectConfig := &alertconfig.Config{}
_ = yaml.Unmarshal([]byte(configMap.Data[alertmanagerconfig.AlertManagerConfigName]), targetConfig)
_ = yaml.Unmarshal([]byte(exampleAlertConfig), expectConfig)

expectConfig.Route.Routes = append(expectConfig.Route.Routes, route)
Expand Down
8 changes: 5 additions & 3 deletions pkg/monitor/services/alertmanager/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ package alertmanager

import (
"bytes"
"context"

"tkestack.io/tke/pkg/util/log"

"github.com/pkg/errors"
)

func (h *processor) Delete(clusterName string, alertValue string) error {
func (h *processor) Delete(ctx context.Context, clusterName string, alertValue string) error {
h.Lock()
defer h.Unlock()

Expand All @@ -37,7 +39,7 @@ func (h *processor) Delete(clusterName string, alertValue string) error {
return errors.New("empty alertValue")
}

routeOp, err := h.loadConfig(clusterName)
routeOp, err := h.loadConfig(ctx, clusterName)
if err != nil {
return errors.Wrapf(err, "route operator not found")
}
Expand All @@ -54,7 +56,7 @@ func (h *processor) Delete(clusterName string, alertValue string) error {
return errors.Wrapf(err, "failed to save")
}

err = h.saveConfig(clusterName, output.String())
err = h.saveConfig(ctx, clusterName, output.String())
if err != nil {
return errors.Wrapf(err, "failed to save configmap")
}
Expand Down
18 changes: 9 additions & 9 deletions pkg/monitor/services/alertmanager/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
package alertmanager

import (
"context"
"reflect"
"testing"

alertmanager_config "tkestack.io/tke/pkg/platform/controller/addon/prometheus"

alert_config "github.com/prometheus/alertmanager/config"
alertconfig "github.com/prometheus/alertmanager/config"
"gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
alertmanagerconfig "tkestack.io/tke/pkg/platform/controller/addon/prometheus"
)

func TestProcessor_Delete(t *testing.T) {
Expand All @@ -37,29 +37,29 @@ func TestProcessor_Delete(t *testing.T) {
}

t.Logf("With non-existed label")
err = p.Delete(clusterName, "non-exist-label")
err = p.Delete(context.Background(), clusterName, "non-exist-label")
if err == nil {
t.Errorf("delete should failed")
return
}

t.Logf("With correct label")
err = p.Delete(clusterName, "test")
err = p.Delete(context.Background(), clusterName, "test")
if err != nil {
t.Errorf("delete should success, code: %s", err)
return
}

t.Logf("Validate persistent data")
configMap, err := k8sClient.CoreV1().ConfigMaps(metav1.NamespaceSystem).Get(alertmanager_config.AlertManagerConfigMap, metav1.GetOptions{})
configMap, err := k8sClient.CoreV1().ConfigMaps(metav1.NamespaceSystem).Get(context.Background(), alertmanagerconfig.AlertManagerConfigMap, metav1.GetOptions{})
if err != nil {
t.Errorf("can't get persistent data, %v", err)
return
}

targetConfig := &alert_config.Config{}
expectConfig := &alert_config.Config{}
_ = yaml.Unmarshal([]byte(configMap.Data[alertmanager_config.AlertManagerConfigName]), targetConfig)
targetConfig := &alertconfig.Config{}
expectConfig := &alertconfig.Config{}
_ = yaml.Unmarshal([]byte(configMap.Data[alertmanagerconfig.AlertManagerConfigName]), targetConfig)
_ = yaml.Unmarshal([]byte(exampleAlertConfig), expectConfig)

expectConfig.Route.Routes = expectConfig.Route.Routes[1:]
Expand Down
6 changes: 4 additions & 2 deletions pkg/monitor/services/alertmanager/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
package alertmanager

import (
"context"

"github.com/pkg/errors"
"github.com/prometheus/alertmanager/config"
"tkestack.io/tke/pkg/util/log"
)

func (h *processor) Get(clusterName string, alertValue string) (*config.Route, error) {
func (h *processor) Get(ctx context.Context, clusterName string, alertValue string) (*config.Route, error) {
h.Lock()
defer h.Unlock()

Expand All @@ -36,7 +38,7 @@ func (h *processor) Get(clusterName string, alertValue string) (*config.Route, e
return nil, errors.New("empty alertValue")
}

routeOp, err := h.loadConfig(clusterName)
routeOp, err := h.loadConfig(ctx, clusterName)
if err != nil {
return nil, errors.Wrapf(err, "route operator not found")
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/monitor/services/alertmanager/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
package alertmanager

import (
"context"
"reflect"
"testing"

alert_config "github.com/prometheus/alertmanager/config"
alertconfig "github.com/prometheus/alertmanager/config"
"gopkg.in/yaml.v2"
)

Expand All @@ -34,20 +35,20 @@ func TestProcessor_Get(t *testing.T) {
}

t.Logf("With non-existed label")
_, err = p.Get(clusterName, "non-exist-label")
_, err = p.Get(context.Background(), clusterName, "non-exist-label")
if err == nil {
t.Errorf("get should failed")
return
}

t.Logf("With correct label")
targetRoute, err := p.Get(clusterName, "test")
targetRoute, err := p.Get(context.Background(), clusterName, "test")
if err != nil {
t.Errorf("get should success, code: %s", err)
return
}

expectConfig := &alert_config.Config{}
expectConfig := &alertconfig.Config{}
_ = yaml.Unmarshal([]byte(exampleAlertConfig), expectConfig)

if !reflect.DeepEqual(targetRoute, expectConfig.Route.Routes[0]) {
Expand Down
13 changes: 6 additions & 7 deletions pkg/monitor/services/alertmanager/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ package alertmanager
import (
"time"

"tkestack.io/tke/pkg/monitor/services"
"tkestack.io/tke/pkg/monitor/util"
alertmanager_config "tkestack.io/tke/pkg/platform/controller/addon/prometheus"
"tkestack.io/tke/pkg/util/log"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
"tkestack.io/tke/pkg/monitor/services"
"tkestack.io/tke/pkg/monitor/util"
alertmanagerconfig "tkestack.io/tke/pkg/platform/controller/addon/prometheus"
"tkestack.io/tke/pkg/util/log"
)

const (
Expand Down Expand Up @@ -89,10 +88,10 @@ func createProcessorServer() (kubernetes.Interface, services.RouteProcessor, str
k8sClient := fake.NewSimpleClientset()
configMap := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: alertmanager_config.AlertManagerConfigMap,
Name: alertmanagerconfig.AlertManagerConfigMap,
},
Data: map[string]string{
alertmanager_config.AlertManagerConfigName: exampleAlertConfig,
alertmanagerconfig.AlertManagerConfigName: exampleAlertConfig,
},
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/monitor/services/alertmanager/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,22 @@
package alertmanager

import (
"context"

"github.com/pkg/errors"
"github.com/prometheus/alertmanager/config"
"tkestack.io/tke/pkg/util/log"
)

func (h *processor) List(clusterName string) ([]*config.Route, error) {
func (h *processor) List(ctx context.Context, clusterName string) ([]*config.Route, error) {
h.Lock()
defer h.Unlock()

if clusterName == "" {
return nil, errors.New("empty clusterName")
}

routeOp, err := h.loadConfig(clusterName)
routeOp, err := h.loadConfig(ctx, clusterName)
if err != nil {
return nil, errors.Wrapf(err, "route operator not found")
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/monitor/services/alertmanager/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
package alertmanager

import (
"context"
"reflect"
"testing"

alert_config "github.com/prometheus/alertmanager/config"
alertconfig "github.com/prometheus/alertmanager/config"
"gopkg.in/yaml.v2"
)

Expand All @@ -34,13 +35,13 @@ func TestProcessor_List(t *testing.T) {
}

t.Logf("List all routes")
targetRoutes, err := p.List(clusterName)
targetRoutes, err := p.List(context.Background(), clusterName)
if err != nil {
t.Errorf("list should success, code: %s", err)
return
}

expectConfig := &alert_config.Config{}
expectConfig := &alertconfig.Config{}
_ = yaml.Unmarshal([]byte(exampleAlertConfig), expectConfig)

if !reflect.DeepEqual(targetRoutes, expectConfig.Route.Routes) {
Expand Down
Loading

0 comments on commit 1f02146

Please sign in to comment.