Skip to content

Commit

Permalink
use clusterName and account from InClusterConfig
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Bertschy <matthias.bertschy@gmail.com>
  • Loading branch information
matthyx committed Mar 4, 2024
1 parent 3a29952 commit 898ab04
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 34 deletions.
14 changes: 7 additions & 7 deletions adapters/httpendpoint/v1/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ import (

type Adapter struct {
callbacks domain.Callbacks
cfg config.HTTPEndpoint
cfg config.Config
clients map[string]adapters.Client
httpMux *http.ServeMux
httpServer *http.Server
supportedPaths map[domain.Strategy]map[string]map[string]map[string]bool
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,
Expand All @@ -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
}

Expand Down Expand Up @@ -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{}
Expand All @@ -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
}

Expand All @@ -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
}
3 changes: 1 addition & 2 deletions cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
}
16 changes: 2 additions & 14 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
}
Expand Down
5 changes: 1 addition & 4 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
},
Expand Down
3 changes: 0 additions & 3 deletions configuration/client/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 3 additions & 4 deletions tests/synchronizer_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 898ab04

Please sign in to comment.