From 898ab042ab612e9b624371c5264c33aae96b51d0 Mon Sep 17 00:00:00 2001 From: Matthias Bertschy Date: Mon, 4 Mar 2024 14:19:38 +0100 Subject: [PATCH] use clusterName and account from InClusterConfig Signed-off-by: Matthias Bertschy --- adapters/httpendpoint/v1/adapter.go | 14 +++++++------- cmd/client/main.go | 3 +-- config/config.go | 16 ++-------------- config/config_test.go | 5 +---- configuration/client/config.json | 3 --- tests/synchronizer_integration_test.go | 7 +++---- 6 files changed, 14 insertions(+), 34 deletions(-) diff --git a/adapters/httpendpoint/v1/adapter.go b/adapters/httpendpoint/v1/adapter.go index 434f486..3b2f03a 100644 --- a/adapters/httpendpoint/v1/adapter.go +++ b/adapters/httpendpoint/v1/adapter.go @@ -20,7 +20,7 @@ import ( type Adapter struct { callbacks domain.Callbacks - cfg config.HTTPEndpoint + cfg config.Config clients map[string]adapters.Client httpMux *http.ServeMux httpServer *http.Server @@ -28,14 +28,14 @@ type Adapter struct { isStarted bool } -func NewHTTPEndpointAdapter(cfg config.HTTPEndpoint) *Adapter { +func NewHTTPEndpointAdapter(cfg config.Config) *Adapter { httpMux := http.NewServeMux() httpMux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) }) server := &http.Server{ - Addr: fmt.Sprintf(":%s", cfg.ServerPort), + Addr: fmt.Sprintf(":%s", cfg.HTTPEndpoint.ServerPort), ReadTimeout: 5 * time.Second, WriteTimeout: 10 * time.Second, IdleTimeout: 120 * time.Second, @@ -54,7 +54,7 @@ func NewHTTPEndpointAdapter(cfg config.HTTPEndpoint) *Adapter { // ensure that the Adapter struct satisfies the adapters.Adapter interface at compile-time var _ adapters.Adapter = (*Adapter)(nil) -func (a *Adapter) GetConfig() config.HTTPEndpoint { +func (a *Adapter) GetConfig() config.Config { return a.cfg } @@ -210,7 +210,7 @@ func (a *Adapter) Start(ctx context.Context) error { // In order to validate the kind is supported by resources list in the config we will build a map of supported verbs, group, version and resource // build the map: a.supportedPaths = map[domain.Strategy]map[string]map[string]map[string]bool{} - for _, resource := range a.cfg.Resources { + for _, resource := range a.cfg.HTTPEndpoint.Resources { lowerCaseStrategy := domain.Strategy(strings.ToLower(string(resource.Strategy))) if _, ok := a.supportedPaths[lowerCaseStrategy]; !ok { a.supportedPaths[lowerCaseStrategy] = map[string]map[string]map[string]bool{} @@ -230,7 +230,7 @@ func (a *Adapter) Start(ctx context.Context) error { logger.L().Ctx(ctx).Info("httpendpoint server stopped") }() a.isStarted = true - logger.L().Ctx(ctx).Info("httpendpoint server started", helpers.String("port", a.cfg.ServerPort)) + logger.L().Ctx(ctx).Info("httpendpoint server started", helpers.String("port", a.cfg.HTTPEndpoint.ServerPort)) return nil } @@ -242,5 +242,5 @@ func (a *Adapter) Stop(ctx context.Context) error { } func (a *Adapter) IsRelated(ctx context.Context, id domain.ClientIdentifier) bool { - return a.cfg.Account == id.Account && a.cfg.ClusterName == id.Cluster + return a.cfg.InCluster.Account == id.Account && a.cfg.InCluster.ClusterName == id.Cluster } diff --git a/cmd/client/main.go b/cmd/client/main.go index 2761c57..116d9a2 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -72,7 +72,7 @@ func main() { adapters = append(adapters, inClusterAdapter) } if err := cfg.HTTPEndpoint.ValidateConfig(); err == nil { - httpEndpointAdapter := httpendpoint.NewHTTPEndpointAdapter(cfg.HTTPEndpoint) + httpEndpointAdapter := httpendpoint.NewHTTPEndpointAdapter(cfg) adapters = append(adapters, httpEndpointAdapter) } if len(adapters) == 0 { @@ -154,6 +154,5 @@ func updateClusterName(cfg *config.Config) { if present && clusterName != "" { logger.L().Debug("cluster name from env", helpers.String("clusterName", clusterName)) cfg.InCluster.ClusterName = clusterName - cfg.HTTPEndpoint.ClusterName = clusterName } } diff --git a/config/config.go b/config/config.go index 5835ab1..28a90c0 100644 --- a/config/config.go +++ b/config/config.go @@ -43,11 +43,8 @@ type InCluster struct { } type HTTPEndpoint struct { - ServerPort string `mapstructure:"serverPort"` - ClusterName string `mapstructure:"clusterName"` - Account string `mapstructure:"account"` - AccessKey string `mapstructure:"accessKey"` - Resources []Resource `mapstructure:"resources"` + ServerPort string `mapstructure:"serverPort"` + Resources []Resource `mapstructure:"resources"` } type Resource struct { @@ -148,15 +145,6 @@ func (c *InCluster) ValidateConfig() error { } func (c *HTTPEndpoint) ValidateConfig() error { - if c.AccessKey == "" { - return fmt.Errorf("access key is missing") - } - if c.Account == "" { - return fmt.Errorf("account is missing") - } - if c.ClusterName == "" { - return fmt.Errorf("cluster name is missing") - } if c.ServerPort == "" { return fmt.Errorf("server port is missing") } diff --git a/config/config_test.go b/config/config_test.go index a35f8f4..e169bfe 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -77,10 +77,7 @@ func TestLoadConfig(t *testing.T) { }, }, HTTPEndpoint: HTTPEndpoint{ - ServerPort: "8089", - ClusterName: "cluster-1", - Account: "11111111-2222-3333-4444-11111111", - AccessKey: "xxxxxxxx-1111-1111-1111-xxxxxxxx", + ServerPort: "8089", Resources: []Resource{ {Group: "test-ks", Version: "v1", Resource: "alerts", Strategy: "copy"}, }, diff --git a/configuration/client/config.json b/configuration/client/config.json index 6a53a33..3ee50ad 100644 --- a/configuration/client/config.json +++ b/configuration/client/config.json @@ -33,9 +33,6 @@ }, "httpEndpoint": { "serverPort": "8089", - "clusterName": "cluster-1", - "account": "11111111-2222-3333-4444-11111111", - "accessKey": "xxxxxxxx-1111-1111-1111-xxxxxxxx", "resources": [ { "group": "test-ks", diff --git a/tests/synchronizer_integration_test.go b/tests/synchronizer_integration_test.go index a32798f..73312c0 100644 --- a/tests/synchronizer_integration_test.go +++ b/tests/synchronizer_integration_test.go @@ -531,9 +531,8 @@ func createAndStartSynchronizerClient(t *testing.T, cluster *TestKubernetesClust newConn := func() (net.Conn, error) { return clientConn, nil } - httpAdapterConf := clientCfg.HTTPEndpoint - httpAdapterConf.ServerPort = httpAdapterPort - httpAdapter := httpendpoint.NewHTTPEndpointAdapter(httpAdapterConf) + clientCfg.HTTPEndpoint.ServerPort = httpAdapterPort + httpAdapter := httpendpoint.NewHTTPEndpointAdapter(clientCfg) ctx, cancel := context.WithCancel(cluster.ctx) @@ -1213,7 +1212,7 @@ func TestSynchronizer_TC13_HTTPEndpoint(t *testing.T) { require.NoError(t, err) // http.DefaultClient.Timeout = 10 * time.Second for httpAdapterIdx := range td.clusters { - syncHTTPAdpaterPort := td.clusters[httpAdapterIdx].syncHTTPAdpater.GetConfig().ServerPort + syncHTTPAdpaterPort := td.clusters[httpAdapterIdx].syncHTTPAdpater.GetConfig().HTTPEndpoint.ServerPort // check that the endpoint is up for i := 0; i < 10; i++ { resp, err := http.Get(fmt.Sprintf("http://localhost:%s/readyz", syncHTTPAdpaterPort))