Skip to content

Commit

Permalink
Merge pull request #28 from SunSince90/support-aws-cloudmap
Browse files Browse the repository at this point in the history
Support aws cloudmap
  • Loading branch information
ljakab authored Feb 26, 2021
2 parents 000238e + bcaea0f commit cd74897
Show file tree
Hide file tree
Showing 27 changed files with 1,841 additions and 232 deletions.
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,34 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.5.0] (2021-02-09)

### Added

- `poll` command
- `cloudmap` command
- `configuration` package: a singleton for containing configuration file
- Capability to re-route to `poll cloudmap` command if no command is provided
but `--conf` is.
- Parse and get the configuration file from `pkg/configuration`.
- Few functions to parse the command flags.
- Documentation for CloudMap
- Goreport badge in readme

### Changed

- `Adaptor` configuration field is now a string: this has be done to be similar
to the `--adaptor-api` flag.
- Move config file definition to `pkg/configuration`
- Merging flag and configuration fields for service directory is now performed
in `validateSDFlags`.
- Configuration file is now parsed in `pkg/configuration`.

### Fixed

- Better adaptor endpoint validation
- Better localhost sanitization

## [0.4.0] (2021-01-26)

### Added
Expand Down
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

Currently, the CN-WAN Reader can discover services/endpoints published to
Google Cloud's [Service directory](https://cloud.google.com/service-directory).
Google Cloud's [Service directory](https://cloud.google.com/service-directory)
and AWS [Cloud Map](https://aws.amazon.com/cloud-map/).

### Google Cloud Service Directory

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

Finally, please make sure your service account has *at least* role
`roles/servicedirectory.viewer`. We suggest you create service account just for
the CNWAN Reader with the aforementioned role.

### AWS Cloud Map

You will need valid
[credentials](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html)
in able to watch changes correctly.

In order to use CN-WAN Reader with Cloud Map, your IAM identity needs to have
*at least* policy `AWSCloudMapReadOnlyAccess` or above.

Please note that, as of now, the reader is only able to read up to `100`
services at a time and `100` instances per service.
While this should more than enough for the vast majority of use-cases, if
demand for supporting a higher number is there, the reader will be able to read
more on next updates.

## Documentation

To learn how to install or use the program, please follow documentation
Expand Down
129 changes: 0 additions & 129 deletions cmd/config.go

This file was deleted.

43 changes: 24 additions & 19 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"os"
"strings"

"github.com/CloudNativeSDWAN/cnwan-reader/pkg/cmd/poll"
"github.com/CloudNativeSDWAN/cnwan-reader/pkg/configuration"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
Expand All @@ -33,45 +35,44 @@ var (
metadataKey string
endpoint string
configFilePath string
config *Config
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "cnwan-reader",
Short: "CN-WAN Reader observes changes in metadata in a service registry.",
TraverseChildren: true,
Use: "cnwan-reader",
Short: "CN-WAN Reader observes changes in metadata in a service registry.",
Long: `CN-WAN Reader connects to a service registry and
observes changes about registered services, delivering found events to a
a separate handler for processing.`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if len(configFilePath) > 0 {
config = parseConfigFile(configFilePath)
configuration.ParseConfigurationFile(cmd)
}
},
Run: func(cmd *cobra.Command, args []string) {
if len(configFilePath) == 0 {
logger.Fatal().Msg("no command nor configuration provided")
conf := configuration.GetConfigFile()
if conf == nil {
logger.Fatal().Msg("no configuration provided")
cmd.Usage()
return
}
if config == nil {
logger.Fatal().Msg("no configuration provided")
cmd.Usage()

if conf.ServiceRegistry != nil && conf.ServiceRegistry.GCPServiceDirectory != nil {
cmd.SetArgs([]string{"servicedirectory"})
cmd.Execute()
return
}

if config.ServiceRegistry == nil || (config.ServiceRegistry != nil && config.ServiceRegistry.GCPServiceDirectory == nil) {
logger.Fatal().Msg("no service registry provided")
cmd.Usage()
if conf.ServiceRegistry != nil && conf.ServiceRegistry.AWSCloudMap != nil {
cmd.SetArgs([]string{"poll", "cloudmap"})
cmd.Execute()
return
}

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

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

rootCmd.PersistentFlags().BoolVarP(&debugMode, "debug", "d", false, "whether to log debug lines")
rootCmd.PersistentFlags().IntVarP(&interval, "interval", "i", 5, "number of seconds between two consecutive polls")
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.")
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.")
rootCmd.PersistentFlags().StringVar(&configFilePath, "conf", "", "path to the configuration file, if any")

// Add the poll command
rootCmd.AddCommand(poll.GetPollCommand())
}

func initConfig() {
Expand All @@ -110,6 +114,7 @@ func initConfig() {
logger = log.Logger
}

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

Expand Down
56 changes: 29 additions & 27 deletions cmd/servicedirectory.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os"
"os/signal"

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

func parseServiceDirectoryConf(conf *ServiceDirectoryConfig) {
if len(gcloudProject) == 0 && len(conf.ProjectID) > 0 {
gcloudProject = conf.ProjectID
func validateSDFlags(cmd *cobra.Command) error {
conf := &configuration.Config{}
sdConf := &configuration.ServiceDirectoryConfig{}
if _conf := configuration.GetConfigFile(); _conf != nil && _conf.ServiceRegistry != nil && _conf.ServiceRegistry.GCPServiceDirectory != nil {
sdConf = _conf.ServiceRegistry.GCPServiceDirectory
conf = _conf
}

if len(gcloudRegion) == 0 && len(conf.Region) > 0 {
gcloudRegion = conf.Region
}

if len(gcloudServAccount) == 0 && len(conf.ServiceAccountPath) > 0 {
gcloudServAccount = conf.ServiceAccountPath
}

if interval == 0 && conf.PollingInterval > 0 {
interval = conf.PollingInterval
}

if interval <= 0 {
logger.Warn().Msg("invalid interval value used, using default...")
interval = 5
}
}

func validateSDFlags() error {
// TODO: this needs to be changed to "metadata-keys" on future versions
if len(metadataKey) == 0 {
return fmt.Errorf("error: no metadata key set")
if len(conf.MetadataKeys) == 0 {
return fmt.Errorf("error: no metadata key set")
}

metadataKey = conf.MetadataKeys[0]
}

if len(gcloudProject) == 0 {
return fmt.Errorf("error: no gcloud project name set")
if len(sdConf.ProjectID) == 0 {
return fmt.Errorf("error: no gcloud project name set")
}

gcloudProject = sdConf.ProjectID
}

if len(gcloudRegion) == 0 {
return fmt.Errorf("error: no gcloud region set")
if len(sdConf.Region) == 0 {
return fmt.Errorf("error: no gcloud region set")
}

gcloudRegion = sdConf.Region
}

if len(gcloudServAccount) == 0 {
return fmt.Errorf("error: no service account path set")
if len(sdConf.ServiceAccountPath) == 0 {
return fmt.Errorf("error: no service account path set")
}

gcloudServAccount = sdConf.ServiceAccountPath
}

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

if err := validateSDFlags(); err != nil {
if err := validateSDFlags(cmd); err != nil {
cmd.Usage()
logger.Fatal().Err(err).Msg("error while starting service directory")
os.Exit(1)
Expand Down
Loading

0 comments on commit cd74897

Please sign in to comment.