Skip to content

Commit

Permalink
feat: add http endpoint to alter update/replacement config
Browse files Browse the repository at this point in the history
  • Loading branch information
joshiste committed Jul 10, 2024
1 parent 4856510 commit 178f7d2
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 14 deletions.
49 changes: 35 additions & 14 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
"strings"
)

var (
Config Specification
)

// Specification is the configuration specification for the extension. Configuration values can be applied
// through environment variables. Learn more through the documentation of the envconfig package.
// https://github.com/kelseyhightower/envconfig
Expand All @@ -29,7 +33,7 @@ type Specification struct {
ContainerPerPod int `json:"containerPerPod" split_words:"true" required:"false" default:"2"`

AttributeUpdates AttributeUpdateSpecifications `split_words:"true" required:"false" default:"[]"`
TargetReplacements TargetReplacementsSpecifications `split_words:"true" required:"false" default:""`
TargetReplacements TargetReplacementsSpecifications `split_words:"true" required:"false" default:"[]"`

DiscoveryAttributesExcludesContainer []string `json:"discoveryAttributesExcludesContainer" split_words:"true" required:"false"`
DiscoveryAttributesExcludesEc2 []string `json:"discoveryAttributesExcludesEc2" split_words:"true" required:"false"`
Expand Down Expand Up @@ -64,7 +68,13 @@ func IsPodZero() bool {
}

type AttributeUpdateSpecifications []AttributeUpdateSpecification
type TargetReplacementsSpecifications []TargetReplacementsSpecification

type AttributeUpdateSpecification struct {
Type string `json:"type" split_words:"true"`
AttributeName string `json:"attributeName" split_words:"true"`
Rate float64 `json:"rate" split_words:"true"`
Interval int `json:"interval" split_words:"true"`
}

func (s *AttributeUpdateSpecifications) Decode(value string) error {
var specs []AttributeUpdateSpecification
Expand All @@ -76,15 +86,8 @@ func (s *AttributeUpdateSpecifications) Decode(value string) error {
return nil
}

type AttributeUpdateSpecification struct {
Type string `json:"type" split_words:"true"`
AttributeName string `json:"attributeName" split_words:"true"`
Rate float64 `json:"rate" split_words:"true"`
Interval int `json:"interval" split_words:"true"`
}

func (s *TargetReplacementsSpecification) Decode(value string) error {
var spec TargetReplacementsSpecification
func (s *AttributeUpdateSpecification) Decode(value string) error {
var spec AttributeUpdateSpecification
err := json.Unmarshal([]byte(value), &spec)
if err != nil {
return fmt.Errorf("invalid json: %w", err)
Expand All @@ -93,15 +96,33 @@ func (s *TargetReplacementsSpecification) Decode(value string) error {
return nil
}

type TargetReplacementsSpecifications []TargetReplacementsSpecification

type TargetReplacementsSpecification struct {
Type string `json:"type" split_words:"true"`
Count int `json:"count" split_words:"true"`
Interval int `json:"interval" split_words:"true"`
}

var (
Config Specification
)
func (s *TargetReplacementsSpecifications) Decode(value string) error {
var specs []TargetReplacementsSpecification
err := json.Unmarshal([]byte(value), &specs)
if err != nil {
return fmt.Errorf("invalid json: %w", err)
}
*s = specs
return nil
}

func (s *TargetReplacementsSpecification) Decode(value string) error {
var spec TargetReplacementsSpecification
err := json.Unmarshal([]byte(value), &spec)
if err != nil {
return fmt.Errorf("invalid json: %w", err)
}
*s = spec
return nil
}

func ParseConfiguration() {
err := envconfig.Process("steadybit_extension", &Config)
Expand Down
38 changes: 38 additions & 0 deletions extloadtest/discovery.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2024 Steadybit GmbH

package extloadtest

import (
"context"
"encoding/json"
"fmt"
"github.com/procyon-projects/chrono"
"github.com/rs/zerolog/log"
"github.com/steadybit/action-kit/go/action_kit_api/v2"
"github.com/steadybit/action-kit/go/action_kit_sdk"
"github.com/steadybit/discovery-kit/go/discovery_kit_api"
"github.com/steadybit/discovery-kit/go/discovery_kit_sdk"
"github.com/steadybit/extension-kit/exthttp"
"github.com/steadybit/extension-kit/extutil"
"github.com/steadybit/extension-loadtest/config"
"net/http"
Expand Down Expand Up @@ -302,3 +308,35 @@ func (t *TargetData) RegisterRecreateActions() {
},
))
}

func (t *TargetData) RegisterConfigUpdateHandlers() {
exthttp.RegisterHttpHandler("/config/targetReplacements", t.updateConfigHandler(&config.Config.TargetReplacements))
exthttp.RegisterHttpHandler("/config/attributeUpdates", t.updateConfigHandler(&config.Config.AttributeUpdates))
}

func (t *TargetData) updateConfigHandler(config interface{}) exthttp.Handler {
return func(w http.ResponseWriter, r *http.Request, body []byte) {
if r.Method == http.MethodPost {
clone := extutil.JsonMangle(config)
err := json.Unmarshal(body, clone)
if err != nil {
w.WriteHeader(400)
_, _ = w.Write([]byte(err.Error()))
}
config = clone
t.rescheduleUpdates()
exthttp.WriteBody(w, config)
} else if r.Method == http.MethodGet {
exthttp.WriteBody(w, config)
} else {
w.WriteHeader(405)
}
}
}

func (t *TargetData) rescheduleUpdates() {
log.Info().Msg("Stopping all scheduled updates")
scheduler.Shutdown()
scheduler = chrono.NewDefaultTaskScheduler()
t.ScheduleUpdates()
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func main() {
targetData.ScheduleUpdates()
targetData.RegisterDiscoveries()
targetData.RegisterRecreateActions()
targetData.RegisterConfigUpdateHandlers()

discovery_kit_sdk.Register(extloadtest.NewEnrichmentRuleProvider())

Expand Down

0 comments on commit 178f7d2

Please sign in to comment.