Skip to content

Commit

Permalink
Merge pull request #114 from steadybit/feat/discovery-sdk
Browse files Browse the repository at this point in the history
feat/discovery sdk
  • Loading branch information
joshiste authored Nov 26, 2023
2 parents 6b69ba1 + 15828e6 commit 3abdd7b
Show file tree
Hide file tree
Showing 21 changed files with 602 additions and 872 deletions.
12 changes: 0 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ jobs:
with:
fetch-depth: 0

- name: Remove non-semver tags (from helmchart) for goreleaser to work properly
run: |
git tag -d $(git tag -l | grep -v "^v[0-9]*.[0-9]*.[0-9]*")
- uses: actions/setup-go@v4
with:
go-version: '1.21'
Expand Down Expand Up @@ -75,10 +71,6 @@ jobs:
with:
fetch-depth: 0

- name: Remove non-semver tags (from helmchart) for goreleaser to work properly
run: |
git tag -d $(git tag -l | grep -v "^v[0-9]*.[0-9]*.[0-9]*")
- name: Set up QEMU
uses: docker/setup-qemu-action@v3

Expand Down Expand Up @@ -134,10 +126,6 @@ jobs:
with:
fetch-depth: 0

- name: Remove non-semver tags (from helmchart) for goreleaser to work properly
run: |
git tag -d $(git tag -l | grep -v "^v[0-9]*.[0-9]*.[0-9]*")
- uses: actions/setup-go@v4
with:
go-version: '1.21'
Expand Down
6 changes: 6 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ before:
hooks:
- go mod download
- make licenses-report

release:
prerelease: "false"

git:
ignore_tags:
- steadybit-extension-aws-*

builds:
- binary: extension-aws
env:
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## next

- use discovery_kit_sdk for discoveries

## v2.2.15

Expand Down
52 changes: 19 additions & 33 deletions extaz/availablity_zone_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,40 @@ import (
"github.com/rs/zerolog/log"
"github.com/steadybit/discovery-kit/go/discovery_kit_api"
"github.com/steadybit/discovery-kit/go/discovery_kit_commons"
"github.com/steadybit/discovery-kit/go/discovery_kit_sdk"
"github.com/steadybit/extension-aws/config"
"github.com/steadybit/extension-aws/utils"
extension_kit "github.com/steadybit/extension-kit"
"github.com/steadybit/extension-kit/extbuild"
"github.com/steadybit/extension-kit/exthttp"
"github.com/steadybit/extension-kit/extutil"
"net/http"
"os"
"time"
)

type azDiscovery struct {
}

var (
targets *[]discovery_kit_api.Target
discoveryError *extension_kit.ExtensionError
_ discovery_kit_sdk.TargetDescriber = (*azDiscovery)(nil)
)

func RegisterDiscoveryHandlers(stopCh chan os.Signal) {
exthttp.RegisterHttpHandler("/az/discovery", exthttp.GetterAsHandler(getAZDiscoveryDescription))
exthttp.RegisterHttpHandler("/az/discovery/target-description", exthttp.GetterAsHandler(getAZTargetDescription))
exthttp.RegisterHttpHandler("/az/discovery/discovered-targets", getAZDiscoveryResults)
utils.StartDiscoveryTask(
stopCh,
"availability zone",
time.Duration(config.Config.DiscoveryIntervalZone)*time.Second,
getTargetsForAccount,
func(updatedTargets []discovery_kit_api.Target, err *extension_kit.ExtensionError) {
targets = &updatedTargets
discoveryError = err
})
func NewAzDiscovery(ctx context.Context) discovery_kit_sdk.TargetDiscovery {
discovery := &azDiscovery{}
return discovery_kit_sdk.NewCachedTargetDiscovery(discovery,
discovery_kit_sdk.WithRefreshTargetsNow(),
discovery_kit_sdk.WithRefreshTargetsInterval(ctx, time.Duration(config.Config.DiscoveryIntervalZone)*time.Second),
)
}

func getAZDiscoveryDescription() discovery_kit_api.DiscoveryDescription {
func (a *azDiscovery) Describe() discovery_kit_api.DiscoveryDescription {
return discovery_kit_api.DiscoveryDescription{
Id: azTargetType,
RestrictTo: extutil.Ptr(discovery_kit_api.LEADER),
Discover: discovery_kit_api.DescribingEndpointReferenceWithCallInterval{
Method: "GET",
Path: "/az/discovery/discovered-targets",
CallInterval: extutil.Ptr(fmt.Sprintf("%ds", config.Config.DiscoveryIntervalZone)),
},
}
}

func getAZTargetDescription() discovery_kit_api.TargetDescription {
func (a *azDiscovery) DescribeTarget() discovery_kit_api.TargetDescription {
return discovery_kit_api.TargetDescription{
Id: azTargetType,
Label: discovery_kit_api.PluralLabel{One: "Availability Zone", Other: "Availability Zones"},
Expand All @@ -80,28 +70,24 @@ func getAZTargetDescription() discovery_kit_api.TargetDescription {
}
}

func getAZDiscoveryResults(w http.ResponseWriter, _ *http.Request, _ []byte) {
if discoveryError != nil {
exthttp.WriteError(w, *discoveryError)
} else {
exthttp.WriteBody(w, discovery_kit_api.DiscoveryData{Targets: targets})
}
func (a *azDiscovery) DiscoverTargets(ctx context.Context) ([]discovery_kit_api.Target, error) {
return utils.ForEveryAccount(utils.Accounts, getTargetsForAccount, ctx, "availability zone")
}

func getTargetsForAccount(account *utils.AwsAccount, ctx context.Context) (*[]discovery_kit_api.Target, error) {
func getTargetsForAccount(account *utils.AwsAccount, ctx context.Context) ([]discovery_kit_api.Target, error) {
client := ec2.NewFromConfig(account.AwsConfig)
result, err := GetAllAvailabilityZones(ctx, client, account.AccountNumber)
result, err := getAllAvailabilityZones(ctx, client, account.AccountNumber)
if err != nil {
return nil, err
}
return &result, nil
return result, nil
}

type AZDescribeAvailabilityZonesApi interface {
DescribeAvailabilityZones(ctx context.Context, params *ec2.DescribeAvailabilityZonesInput, optFns ...func(*ec2.Options)) (*ec2.DescribeAvailabilityZonesOutput, error)
}

func GetAllAvailabilityZones(ctx context.Context, ec2Api AZDescribeAvailabilityZonesApi, awsAccountNumber string) ([]discovery_kit_api.Target, error) {
func getAllAvailabilityZones(ctx context.Context, ec2Api AZDescribeAvailabilityZonesApi, awsAccountNumber string) ([]discovery_kit_api.Target, error) {
result := make([]discovery_kit_api.Target, 0, 20)

output, err := ec2Api.DescribeAvailabilityZones(ctx, &ec2.DescribeAvailabilityZonesInput{
Expand Down
10 changes: 5 additions & 5 deletions extaz/availablity_zone_discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type ec2ClientMock struct {
}

func (m *ec2ClientMock) DescribeAvailabilityZones(ctx context.Context, params *ec2.DescribeAvailabilityZonesInput, optFns ...func(*ec2.Options)) (*ec2.DescribeAvailabilityZonesOutput, error) {
args := m.Called(ctx, params)
args := m.Called(ctx, params, optFns)
if args.Get(0) == nil {
return nil, args.Error(1)
}
Expand All @@ -39,10 +39,10 @@ func TestGetAllAvailabilityZones(t *testing.T) {
},
},
}
mockedApi.On("DescribeAvailabilityZones", mock.Anything, mock.Anything).Return(&mockedReturnValue, nil)
mockedApi.On("DescribeAvailabilityZones", mock.Anything, mock.Anything, mock.Anything).Return(&mockedReturnValue, nil)

// When
targets, err := GetAllAvailabilityZones(context.Background(), mockedApi, "42")
targets, err := getAllAvailabilityZones(context.Background(), mockedApi, "42")

// Then
assert.Equal(t, nil, err)
Expand All @@ -63,10 +63,10 @@ func TestGetAllAvailabilityZonesError(t *testing.T) {
// Given
mockedApi := new(ec2ClientMock)

mockedApi.On("DescribeAvailabilityZones", mock.Anything, mock.Anything).Return(nil, errors.New("expected"))
mockedApi.On("DescribeAvailabilityZones", mock.Anything, mock.Anything, mock.Anything).Return(nil, errors.New("expected"))

// When
_, err := GetAllAvailabilityZones(context.Background(), mockedApi, "42")
_, err := getAllAvailabilityZones(context.Background(), mockedApi, "42")

// Then
assert.Equal(t, err.Error(), "expected")
Expand Down
Loading

0 comments on commit 3abdd7b

Please sign in to comment.