Skip to content

Commit cd74897

Browse files
authored
Merge pull request #28 from SunSince90/support-aws-cloudmap
Support aws cloudmap
2 parents 000238e + bcaea0f commit cd74897

27 files changed

+1841
-232
lines changed

CHANGELOG.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,34 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.5.0] (2021-02-09)
9+
10+
### Added
11+
12+
- `poll` command
13+
- `cloudmap` command
14+
- `configuration` package: a singleton for containing configuration file
15+
- Capability to re-route to `poll cloudmap` command if no command is provided
16+
but `--conf` is.
17+
- Parse and get the configuration file from `pkg/configuration`.
18+
- Few functions to parse the command flags.
19+
- Documentation for CloudMap
20+
- Goreport badge in readme
21+
22+
### Changed
23+
24+
- `Adaptor` configuration field is now a string: this has be done to be similar
25+
to the `--adaptor-api` flag.
26+
- Move config file definition to `pkg/configuration`
27+
- Merging flag and configuration fields for service directory is now performed
28+
in `validateSDFlags`.
29+
- Configuration file is now parsed in `pkg/configuration`.
30+
31+
### Fixed
32+
33+
- Better adaptor endpoint validation
34+
- Better localhost sanitization
35+
836
## [0.4.0] (2021-01-26)
937

1038
### Added

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
![GitHub](https://img.shields.io/github/license/CloudNativeSDWAN/cnwan-reader)
44
![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/CloudNativeSDWAN/cnwan-reader)
5+
[![Go Report Card](https://goreportcard.com/badge/github.com/CloudNativeSDWAN/cnwan-reader)](https://goreportcard.com/report/github.com/CloudNativeSDWAN/cnwan-reader)
56
![OpenAPI version](https://img.shields.io/badge/OpenAPI-3.0.1-green)
67
![GitHub Workflow Status](https://img.shields.io/github/workflow/status/CloudNativeSDWAN/cnwan-reader/Build)
78
![GitHub release (latest semver)](https://img.shields.io/github/v/release/CloudNativeSDWAN/cnwan-reader)
@@ -30,7 +31,10 @@ Please follow this readme to know more about *OpenAPI*, *Adaptors* and
3031
## Supported Service Registries
3132

3233
Currently, the CN-WAN Reader can discover services/endpoints published to
33-
Google Cloud's [Service directory](https://cloud.google.com/service-directory).
34+
Google Cloud's [Service directory](https://cloud.google.com/service-directory)
35+
and AWS [Cloud Map](https://aws.amazon.com/cloud-map/).
36+
37+
### Google Cloud Service Directory
3438

3539
In order to connect correctly, a
3640
[service account](https://cloud.google.com/iam/docs/service-accounts) is
@@ -40,6 +44,25 @@ To learn more about Google Cloud Service Accounts, you can also consult
4044
Finally, you can read Service Directory's [documentation](https://cloud.google.com/service-directory/docs)
4145
to know more about how it works.
4246

47+
Finally, please make sure your service account has *at least* role
48+
`roles/servicedirectory.viewer`. We suggest you create service account just for
49+
the CNWAN Reader with the aforementioned role.
50+
51+
### AWS Cloud Map
52+
53+
You will need valid
54+
[credentials](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html)
55+
in able to watch changes correctly.
56+
57+
In order to use CN-WAN Reader with Cloud Map, your IAM identity needs to have
58+
*at least* policy `AWSCloudMapReadOnlyAccess` or above.
59+
60+
Please note that, as of now, the reader is only able to read up to `100`
61+
services at a time and `100` instances per service.
62+
While this should more than enough for the vast majority of use-cases, if
63+
demand for supporting a higher number is there, the reader will be able to read
64+
more on next updates.
65+
4366
## Documentation
4467

4568
To learn how to install or use the program, please follow documentation

cmd/config.go

Lines changed: 0 additions & 129 deletions
This file was deleted.

cmd/root.go

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"os"
2222
"strings"
2323

24+
"github.com/CloudNativeSDWAN/cnwan-reader/pkg/cmd/poll"
25+
"github.com/CloudNativeSDWAN/cnwan-reader/pkg/configuration"
2426
"github.com/rs/zerolog"
2527
"github.com/rs/zerolog/log"
2628
"github.com/spf13/cobra"
@@ -33,45 +35,44 @@ var (
3335
metadataKey string
3436
endpoint string
3537
configFilePath string
36-
config *Config
3738
)
3839

3940
// rootCmd represents the base command when called without any subcommands
4041
var rootCmd = &cobra.Command{
41-
Use: "cnwan-reader",
42-
Short: "CN-WAN Reader observes changes in metadata in a service registry.",
42+
TraverseChildren: true,
43+
Use: "cnwan-reader",
44+
Short: "CN-WAN Reader observes changes in metadata in a service registry.",
4345
Long: `CN-WAN Reader connects to a service registry and
4446
observes changes about registered services, delivering found events to a
4547
a separate handler for processing.`,
4648
PersistentPreRun: func(cmd *cobra.Command, args []string) {
4749
if len(configFilePath) > 0 {
48-
config = parseConfigFile(configFilePath)
50+
configuration.ParseConfigurationFile(cmd)
4951
}
5052
},
5153
Run: func(cmd *cobra.Command, args []string) {
52-
if len(configFilePath) == 0 {
53-
logger.Fatal().Msg("no command nor configuration provided")
54+
conf := configuration.GetConfigFile()
55+
if conf == nil {
56+
logger.Fatal().Msg("no configuration provided")
5457
cmd.Usage()
5558
return
5659
}
57-
if config == nil {
58-
logger.Fatal().Msg("no configuration provided")
59-
cmd.Usage()
60+
61+
if conf.ServiceRegistry != nil && conf.ServiceRegistry.GCPServiceDirectory != nil {
62+
cmd.SetArgs([]string{"servicedirectory"})
63+
cmd.Execute()
6064
return
6165
}
6266

63-
if config.ServiceRegistry == nil || (config.ServiceRegistry != nil && config.ServiceRegistry.GCPServiceDirectory == nil) {
64-
logger.Fatal().Msg("no service registry provided")
65-
cmd.Usage()
67+
if conf.ServiceRegistry != nil && conf.ServiceRegistry.AWSCloudMap != nil {
68+
cmd.SetArgs([]string{"poll", "cloudmap"})
69+
cmd.Execute()
6670
return
6771
}
6872

69-
// Note that this generally is not the correct way of doing this
70-
// because id does not honor (p)preruns and/or (p)postruns, but we
71-
// remove any prerun from servicedirectory command and so, this is
72-
// fine.
73-
// Nonetheless, I will think of a new technique for next versions.
74-
servicedirectoryCmd.Run(servicedirectoryCmd, args)
73+
logger.Fatal().Msg("no service registry provided")
74+
cmd.Usage()
75+
return
7576
},
7677
}
7778

@@ -89,8 +90,11 @@ func init() {
8990

9091
rootCmd.PersistentFlags().BoolVarP(&debugMode, "debug", "d", false, "whether to log debug lines")
9192
rootCmd.PersistentFlags().IntVarP(&interval, "interval", "i", 5, "number of seconds between two consecutive polls")
92-
rootCmd.PersistentFlags().StringVar(&endpoint, "adaptor-api", "localhost/cnwan", "the api, in forrm of host:port/path, where the events will be sent to. Look at the documentation to learn more about this.")
93+
rootCmd.PersistentFlags().StringVar(&endpoint, "adaptor-api", "localhost:80/cnwan", "the api, in forrm of host:port/path, where the events will be sent to. Look at the documentation to learn more about this.")
9394
rootCmd.PersistentFlags().StringVar(&configFilePath, "conf", "", "path to the configuration file, if any")
95+
96+
// Add the poll command
97+
rootCmd.AddCommand(poll.GetPollCommand())
9498
}
9599

96100
func initConfig() {
@@ -110,6 +114,7 @@ func initConfig() {
110114
logger = log.Logger
111115
}
112116

117+
// TODO: remove this and use utils.SanitizeLocalhost.
113118
func sanitizeAdaptorEndpoint(endp string) string {
114119
endp = strings.Trim(endp, "/")
115120

cmd/servicedirectory.go

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"os"
2323
"os/signal"
2424

25+
"github.com/CloudNativeSDWAN/cnwan-reader/pkg/configuration"
2526
"github.com/CloudNativeSDWAN/cnwan-reader/pkg/poller"
2627
"github.com/CloudNativeSDWAN/cnwan-reader/pkg/queue"
2728
"github.com/CloudNativeSDWAN/cnwan-reader/pkg/sdhandler"
@@ -61,44 +62,45 @@ func init() {
6162
servicedirectoryCmd.Flags().StringVar(&metadataKey, "metadata-key", "", "name of the metadata key to look for")
6263
}
6364

64-
func parseServiceDirectoryConf(conf *ServiceDirectoryConfig) {
65-
if len(gcloudProject) == 0 && len(conf.ProjectID) > 0 {
66-
gcloudProject = conf.ProjectID
65+
func validateSDFlags(cmd *cobra.Command) error {
66+
conf := &configuration.Config{}
67+
sdConf := &configuration.ServiceDirectoryConfig{}
68+
if _conf := configuration.GetConfigFile(); _conf != nil && _conf.ServiceRegistry != nil && _conf.ServiceRegistry.GCPServiceDirectory != nil {
69+
sdConf = _conf.ServiceRegistry.GCPServiceDirectory
70+
conf = _conf
6771
}
6872

69-
if len(gcloudRegion) == 0 && len(conf.Region) > 0 {
70-
gcloudRegion = conf.Region
71-
}
72-
73-
if len(gcloudServAccount) == 0 && len(conf.ServiceAccountPath) > 0 {
74-
gcloudServAccount = conf.ServiceAccountPath
75-
}
76-
77-
if interval == 0 && conf.PollingInterval > 0 {
78-
interval = conf.PollingInterval
79-
}
80-
81-
if interval <= 0 {
82-
logger.Warn().Msg("invalid interval value used, using default...")
83-
interval = 5
84-
}
85-
}
86-
87-
func validateSDFlags() error {
73+
// TODO: this needs to be changed to "metadata-keys" on future versions
8874
if len(metadataKey) == 0 {
89-
return fmt.Errorf("error: no metadata key set")
75+
if len(conf.MetadataKeys) == 0 {
76+
return fmt.Errorf("error: no metadata key set")
77+
}
78+
79+
metadataKey = conf.MetadataKeys[0]
9080
}
9181

9282
if len(gcloudProject) == 0 {
93-
return fmt.Errorf("error: no gcloud project name set")
83+
if len(sdConf.ProjectID) == 0 {
84+
return fmt.Errorf("error: no gcloud project name set")
85+
}
86+
87+
gcloudProject = sdConf.ProjectID
9488
}
9589

9690
if len(gcloudRegion) == 0 {
97-
return fmt.Errorf("error: no gcloud region set")
91+
if len(sdConf.Region) == 0 {
92+
return fmt.Errorf("error: no gcloud region set")
93+
}
94+
95+
gcloudRegion = sdConf.Region
9896
}
9997

10098
if len(gcloudServAccount) == 0 {
101-
return fmt.Errorf("error: no service account path set")
99+
if len(sdConf.ServiceAccountPath) == 0 {
100+
return fmt.Errorf("error: no service account path set")
101+
}
102+
103+
gcloudServAccount = sdConf.ServiceAccountPath
102104
}
103105

104106
return nil
@@ -108,7 +110,7 @@ func runServiceDirectory(cmd *cobra.Command, args []string) {
108110
var err error
109111
l := log.With().Str("func", "cmd.runServiceDirectory").Logger()
110112

111-
if err := validateSDFlags(); err != nil {
113+
if err := validateSDFlags(cmd); err != nil {
112114
cmd.Usage()
113115
logger.Fatal().Err(err).Msg("error while starting service directory")
114116
os.Exit(1)

0 commit comments

Comments
 (0)