Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add aws support #640

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions cmd/cloudprober.go
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why this file is included in this change?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's imports tool, placing the internal package in the top section, seems like a good change but I can remove if you want

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, actually flag is in the middle for a reason. It's required for Google's importing tools to work properly. Please drop this file.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package main

import (
"context"
"flag"
"fmt"
"log/slog"
"os"
Expand All @@ -31,8 +32,6 @@ import (
"syscall"
"time"

"flag"

"github.com/cloudprober/cloudprober"
"github.com/cloudprober/cloudprober/config"
"github.com/cloudprober/cloudprober/config/runconfig"
Expand All @@ -41,7 +40,7 @@ import (
)

var (
versionFlag = flag.Bool("version", false, "Print version and exit")
versionFlag = flag.Bool(" version", false, "Print version and exit")
rabunkosar-dd marked this conversation as resolved.
Show resolved Hide resolved
buildInfoFlag = flag.Bool("buildinfo", false, "Print build info and exit")
stopTime = flag.Duration("stop_time", 0, "How long to wait for cleanup before process exits on SIGINT and SIGTERM")
cpuprofile = flag.String("cpuprof", "", "Write cpu profile to file")
Expand Down
14 changes: 9 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ require (
cloud.google.com/go/logging v1.8.1
cloud.google.com/go/pubsub v1.33.0
github.com/Masterminds/sprig/v3 v3.2.3
github.com/aws/aws-sdk-go-v2 v1.16.10
github.com/aws/aws-sdk-go-v2 v1.23.0
github.com/aws/aws-sdk-go-v2/config v1.15.9
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.11
github.com/aws/aws-sdk-go-v2/service/cloudwatch v1.18.3
github.com/aws/aws-sdk-go-v2/service/ec2 v1.136.0
github.com/aws/aws-sdk-go-v2/service/elasticache v1.32.2
github.com/aws/aws-sdk-go-v2/service/rds v1.63.0
github.com/fullstorydev/grpcurl v1.8.7
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/google/uuid v1.3.1
Expand Down Expand Up @@ -41,13 +44,14 @@ require (
github.com/apache/arrow/go/v12 v12.0.0 // indirect
github.com/apache/thrift v0.16.0 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.12.12 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.17 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.11 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.3 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.3 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.18 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.11 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.10.1 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.10.3 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.11.15 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.16.12 // indirect
github.com/aws/smithy-go v1.12.1 // indirect
github.com/aws/smithy-go v1.17.0 // indirect
github.com/bufbuild/protocompile v0.4.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4 // indirect
Expand Down
23 changes: 18 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ github.com/apache/arrow/go/v12 v12.0.0/go.mod h1:d+tV/eHZZ7Dz7RPrFKtPK02tpr+c9/P
github.com/apache/thrift v0.16.0 h1:qEy6UW60iVOlUy+b9ZR0d5WzUWYGOo4HfopoyBaNmoY=
github.com/apache/thrift v0.16.0/go.mod h1:PHK3hniurgQaNMZYaCLEqXKsYK8upmhPbmdP2FXSqgU=
github.com/aws/aws-sdk-go-v2 v1.16.4/go.mod h1:ytwTPBG6fXTZLxxeeCCWj2/EMYp/xDUgX+OET6TLNNU=
github.com/aws/aws-sdk-go-v2 v1.16.10 h1:+yDD0tcuHRQZgqONkpDwzepqmElQaSlFPymHRHR9mrc=
github.com/aws/aws-sdk-go-v2 v1.16.10/go.mod h1:WTACcleLz6VZTp7fak4EO5b9Q4foxbn+8PIz3PmyKlo=
github.com/aws/aws-sdk-go-v2 v1.23.0 h1:PiHAzmiQQr6JULBUdvR8fKlA+UPKLT/8KbiqpFBWiAo=
github.com/aws/aws-sdk-go-v2 v1.23.0/go.mod h1:i1XDttT4rnf6vxc9AuskLc6s7XBee8rlLilKlc03uAA=
github.com/aws/aws-sdk-go-v2/config v1.15.9 h1:TK5yNEnFDQ9iaO04gJS/3Y+eW8BioQiCUafW75/Wc3Q=
github.com/aws/aws-sdk-go-v2/config v1.15.9/go.mod h1:rv/l/TbZo67kp99v/3Kb0qV6Fm1KEtKyruEV2GvVfgs=
github.com/aws/aws-sdk-go-v2/credentials v1.12.4/go.mod h1:7g+GGSp7xtR823o1jedxKmqRZGqLdoHQfI4eFasKKxs=
Expand All @@ -72,28 +73,40 @@ github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.5/go.mod h1:WAPnuhG5IQ/i6DET
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.11 h1:zZHPdM2x09/0F8D7XyVvQnP2/jaW7bEMmtcSCPYq/iI=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.11/go.mod h1:38Asv/UyQbDNpSXCurZRlDMjzIl6J+wUe8vY3TtUuzA=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.11/go.mod h1:tmUB6jakq5DFNcXsXOA/ZQ7/C8VnSKYkx58OI7Fh79g=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.17 h1:U8DZvyFFesBmK62dYC6BRXm4Cd/wPP3aPcecu3xv/F4=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.17/go.mod h1:6qtGip7sJEyvgsLjphRZWF9qPe3xJf1mL/MM01E35Wc=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.3 h1:DUwbD79T8gyQ23qVXFUthjzVMTviSHi3y4z58KvghhM=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.3/go.mod h1:7sGSz1JCKHWWBHq98m6sMtWQikmYPpxjqOydDemiVoM=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.5/go.mod h1:fV1AaS2gFc1tM0RCb015FJ0pvWVUfJZANzjwoO4YakM=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.11 h1:GMp98usVW5tzQhxd26KWhoNQPlR2noIlfbzqjVGBhLU=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.11/go.mod h1:cYAfnB+9ZkmZWpQWmPDsuIGm4EA+6k2ZVtxKjw/XJBY=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.3 h1:AplLJCtIaUZDCbr6+gLYdsYNxne4iuaboJhVt9d+WXI=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.3/go.mod h1:ify42Rb7nKeDDPkFjKn7q1bPscVPu/+gmHH8d2c+anU=
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.12/go.mod h1:00c7+ALdPh4YeEUPXJzyU0Yy01nPGOq2+9rUaz05z9g=
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.18 h1:/spg6h3tG4pefphbvhpgdMtFMegSajPPSEJd1t8lnpc=
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.18/go.mod h1:hTHq8hL4bAxJyng364s9d4IUGXZOs7Y5LSqAhIiIQ2A=
github.com/aws/aws-sdk-go-v2/service/cloudwatch v1.18.3 h1:PK6c4wYv3wbb88eH0X0FjJwRykEoJwAesuslNReY7iE=
github.com/aws/aws-sdk-go-v2/service/cloudwatch v1.18.3/go.mod h1:BrAJyOMrnwzYVQcP5ziqlCpnEuFfkNppZLzqDyW/YTg=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.136.0 h1:nZPVFkGojUUJupKJzaCKE07LaFDO3Tto1U69F8JipsI=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.136.0/go.mod h1:xYJZQIo/YZxEbeBxUYRQJTCJ924EuKtDfrhVx76yzOE=
github.com/aws/aws-sdk-go-v2/service/elasticache v1.32.2 h1:3V4GHH05PtNsDjXlKXVbIw0vSjSNUQoPQEtILns4BMs=
github.com/aws/aws-sdk-go-v2/service/elasticache v1.32.2/go.mod h1:p5/Vq+5NiyqnyQyH5JVK3lrHDygbiQiqdCNaDT018I8=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.10.1 h1:rpkF4n0CyFcrJUG/rNNohoTmhtWlFTRI4BsZOh9PvLs=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.10.1/go.mod h1:l9ymW25HOqymeU2m1gbUQ3rUIsTwKs8gYHXkqDQUhiI=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.5/go.mod h1:ZbkttHXaVn3bBo/wpJbQGiiIWR90eTBUVBrEHUEQlho=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.11 h1:GkYtp4gi4wdWUV+pPetjk5y2aDxbr0t8n5OjVBwZdII=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.11/go.mod h1:OEofCUKF7Hri4ShOCokF6k6hGq9PCB2sywt/9rLSXjY=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.10.3 h1:kJOolE8xBAD13xTCgOakByZkyP4D/owNmvEiioeUNAg=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.10.3/go.mod h1:Owv1I59vaghv1Ax8zz8ELY8DN7/Y0rGS+WWAmjgi950=
github.com/aws/aws-sdk-go-v2/service/rds v1.63.0 h1:Mb6mn6mRfTpI2WEZkLmmJ7jtiPHKzwJ8otzUA5rIq+o=
github.com/aws/aws-sdk-go-v2/service/rds v1.63.0/go.mod h1:C73+OVOdaMZNc/Z8PhnLoArk6mCqxkiqfgmCrvNrDDM=
github.com/aws/aws-sdk-go-v2/service/sso v1.11.7/go.mod h1:TFVe6Rr2joVLsYQ1ABACXgOC6lXip/qpX2x5jWg/A9w=
github.com/aws/aws-sdk-go-v2/service/sso v1.11.15 h1:HaIE5/TtKr66qZTJpvMifDxH4lRt2JZawbkLYOo1F+Y=
github.com/aws/aws-sdk-go-v2/service/sso v1.11.15/go.mod h1:dDVD4ElJRTQXx7dOQ59EkqGyNU9tnwy1RKln+oLIOTU=
github.com/aws/aws-sdk-go-v2/service/sts v1.16.6/go.mod h1:rP1rEOKAGZoXp4iGDxSXFvODAtXpm34Egf0lL0eshaQ=
github.com/aws/aws-sdk-go-v2/service/sts v1.16.12 h1:YU9UHPukkCCnETHEExOptF/BxPvGJKXO/NBx+RMQ/2A=
github.com/aws/aws-sdk-go-v2/service/sts v1.16.12/go.mod h1:b53qpmhHk7mTL2J/tfG6f38neZiyBQSiNXGCuNKq4+4=
github.com/aws/smithy-go v1.11.2/go.mod h1:3xHYmszWVx2c0kIwQeEVf9uSm4fYZt67FBJnwub1bgM=
github.com/aws/smithy-go v1.12.1 h1:yQRC55aXN/y1W10HgwHle01DRuV9Dpf31iGkotjt3Ag=
github.com/aws/smithy-go v1.12.1/go.mod h1:Tg+OJXh4MB2R/uN61Ko2f6hTZwB/ZYGOtib8J3gBHzA=
github.com/aws/smithy-go v1.17.0 h1:wWJD7LX6PBV6etBUwO0zElG0nWN9rUhp0WdYeHSHAaI=
github.com/aws/smithy-go v1.17.0/go.mod h1:NukqUGpCZIILqqiV0NIjeFh24kd/FAa4beRb6nbIUPE=
github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA=
github.com/bufbuild/protocompile v0.4.0/go.mod h1:3v93+mbWn/v3xzN+31nwkJfrEpAUwp+BagBSZWx+TP8=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down
148 changes: 148 additions & 0 deletions internal/rds/aws/aws.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// Copyright 2017-2023 The Cloudprober Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/*
Package aws implements a AWS (Amazon Web Services) resources provider for
ResourceDiscovery server.

See ResourceTypes variable for the list of supported resource types.

AWS provider is configured through a protobuf based config file
(proto/config.proto). Example config:

{
aws_instances {}
}
*/

package aws

import (
"fmt"

configpb "github.com/cloudprober/cloudprober/internal/rds/aws/proto"
pb "github.com/cloudprober/cloudprober/internal/rds/proto"
serverconfigpb "github.com/cloudprober/cloudprober/internal/rds/server/proto"
"github.com/cloudprober/cloudprober/logger"

"google.golang.org/protobuf/proto"
)

// DefaultProviderID is the povider id to use for this provider if a provider
// id is not configured explicitly.
const DefaultProviderID = "aws"

// ResourceTypes declares resource types supported by the AWS provider.
var ResourceTypes = struct {
EC2Instances, ElastiCaches, RDS string
}{
"ec2_instances",
"elasticaches",
"rds",
}

type lister interface {
listResources(req *pb.ListResourcesRequest) ([]*pb.Resource, error)
}

// Provider implements a AWS provider for a ResourceDiscovery server.
type Provider struct {
listers map[string]lister
}

// ListResources returns the list of resources based on the given request.
func (p *Provider) ListResources(req *pb.ListResourcesRequest) (*pb.ListResourcesResponse, error) {
lr := p.listers[req.GetResourcePath()]
if lr == nil {
return nil, fmt.Errorf("unknown resource type: %s", req.GetResourcePath())
}

resources, err := lr.listResources(req)
return &pb.ListResourcesResponse{Resources: resources}, err
}

func initAWSProject(c *configpb.ProviderConfig, l *logger.Logger) (map[string]lister, error) {
resourceLister := make(map[string]lister)

// Enable EC2 instances lister if configured.
if c.GetEc2Instances() != nil {
lr, err := newEC2InstancesLister(c.GetEc2Instances(), c.GetRegion(), l)
if err != nil {
return nil, err
}
resourceLister[ResourceTypes.EC2Instances] = lr
}

// Enable ElastiCaches lister if configured.
if c.GetElasticaches() != nil {
lr, err := newElastiCacheLister(c.GetElasticaches(), c.GetRegion(), l)
if err != nil {
return nil, err
}
resourceLister[ResourceTypes.ElastiCaches] = lr
}

// Enable RDS (AWS) if configured.
if c.GetRds() != nil {
lr, err := newRdsLister(c.GetRds(), c.GetRegion(), l)
if err != nil {
return nil, err
}
resourceLister[ResourceTypes.RDS] = lr
}

return resourceLister, nil
}

// New creates a AWS provider for RDS server, based on the provided config.
func New(c *configpb.ProviderConfig, l *logger.Logger) (*Provider, error) {
resourceLister, err := initAWSProject(c, l)
if err != nil {
return nil, err
}

return &Provider{
listers: resourceLister,
}, nil
}

// DefaultProviderConfig is a convenience function that builds and returns a
// basic AWS provider config based on the given parameters.
func DefaultProviderConfig(resTypes map[string]string, reEvalSec int) *serverconfigpb.Provider {
c := &configpb.ProviderConfig{}

for k := range resTypes {
switch k {
case ResourceTypes.EC2Instances:
c.Ec2Instances = &configpb.EC2Instances{
ReEvalSec: proto.Int32(int32(reEvalSec)),
}

case ResourceTypes.ElastiCaches:
c.Elasticaches = &configpb.ElastiCaches{
ReEvalSec: proto.Int32(int32(reEvalSec)),
}

case ResourceTypes.RDS:
c.Rds = &configpb.RDS{
ReEvalSec: proto.Int32(int32(reEvalSec)),
}
}
}

return &serverconfigpb.Provider{
Id: proto.String(DefaultProviderID),
Config: &serverconfigpb.Provider_AwsConfig{AwsConfig: c},
}
}
100 changes: 100 additions & 0 deletions internal/rds/aws/aws_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright 2019 The Cloudprober Authors.
//
Comment on lines +1 to +2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd put the latest year in the copyright headers of new *.go files.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package aws

import (
"testing"

serverconfigpb "github.com/cloudprober/cloudprober/internal/rds/server/proto"
)

func testAWSConfig(t *testing.T, pc *serverconfigpb.Provider, awsInstances bool, rdsConfig, elasticCachesConfig string, reEvalSec int) {
t.Helper()

if pc.GetId() != DefaultProviderID {
t.Errorf("pc.GetId()=%s, wanted=%s", pc.GetId(), DefaultProviderID)
}
c := pc.GetAwsConfig()

if !awsInstances {
if c.GetEc2Instances() != nil {
t.Errorf("c.GetEc2Instances()=%v, wanted=nil", c.GetEc2Instances())
}
} else {
if c.GetEc2Instances() == nil {
t.Fatal("c.GetGceInstances() is nil, wanted=not-nil")
}
if c.GetEc2Instances().GetReEvalSec() != int32(reEvalSec) {
t.Errorf("AWS instance reEvalSec=%d, wanted=%d", c.GetEc2Instances().GetReEvalSec(), reEvalSec)
}
}

// Verify that RDS config is set correctly.
if rdsConfig == "" {
if c.GetRds() != nil {
t.Errorf("c.GetRds()=%v, wanted=nil", c.GetRds())
}
} else {
if c.GetRds() == nil {
t.Fatalf("c.GetRds()=nil, wanted=not-nil")
}
if c.GetRds().GetReEvalSec() != int32(reEvalSec) {
t.Errorf("RDS config reEvalSec=%d, wanted=%d", c.GetRds().GetReEvalSec(), reEvalSec)
}
}

// Verify that Elasticache is set correctly.
if elasticCachesConfig == "" {
if c.GetElasticaches() != nil {
t.Errorf("c.GetElasticaches()=%v, wanted=nil", c.GetElasticaches())
}
} else {
if c.GetElasticaches() == nil {
t.Fatalf("c.GetElasticaches()=nil, wanted=not-nil")
}
if c.GetElasticaches().GetReEvalSec() != int32(reEvalSec) {
t.Errorf("Elasticaches config reEvalSec=%d, wanted=%d", c.GetElasticaches().GetReEvalSec(), reEvalSec)
}
}
}

func TestDefaultProviderConfig(t *testing.T) {
resTypes := map[string]string{
ResourceTypes.EC2Instances: "",
}

c := DefaultProviderConfig(resTypes, 10)
testAWSConfig(t, c, true, "", "", 10)

// Elasticache and RDS
testElastiCacheConfig := "elasticaches"
testRDSConfig := "rds"

resTypes = map[string]string{
ResourceTypes.ElastiCaches: testElastiCacheConfig,
ResourceTypes.RDS: testRDSConfig,
}
c = DefaultProviderConfig(resTypes, 10)
testAWSConfig(t, c, false, testRDSConfig, testElastiCacheConfig, 10)

// EC2 instances, RTC and pub-sub
resTypes = map[string]string{
ResourceTypes.EC2Instances: "",
ResourceTypes.ElastiCaches: testElastiCacheConfig,
ResourceTypes.RDS: testRDSConfig,
}
c = DefaultProviderConfig(resTypes, 10)
testAWSConfig(t, c, true, testRDSConfig, testElastiCacheConfig, 10)
}