Skip to content

Commit

Permalink
added test comment (#124)
Browse files Browse the repository at this point in the history
* added test comment

* resolved warnings

* updated go version

* formated ...go fmt ./...
  • Loading branch information
sundaram2021 authored Nov 22, 2024
1 parent d060a72 commit fa717f5
Show file tree
Hide file tree
Showing 14 changed files with 525 additions and 524 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:

name: Build
env:
GO_VERSION: 1.19
GO_VERSION: 1.21

jobs:
build:
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/rs/zerolog/log"
)

// main function
func main() {
if len(os.Args) < 2 {
fmt.Println("Please provide (aws, gcp or azure) as the first argument.")
Expand Down Expand Up @@ -50,7 +51,7 @@ func main() {
log.Err(err).Msg("Program failed for AWS")
os.Exit(1)
}

case "azure":
ctx, err = optionsAZURE.Parse(os.Args[2:])
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions program/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (program *Options) getUniqueSessions() <-chan *sessionInfo {

for _, region := range program.Regions {
wg.Add(1)
go func(profile, region, account string) {
go func(profile, region, account string, info *sessionInfo) {
defer wg.Done()
if region != info.region {
log := log.With().Str("profile", info.profile).Str("region", region).Logger()
Expand All @@ -143,7 +143,7 @@ func (program *Options) getUniqueSessions() <-chan *sessionInfo {
log.Error().Err(err).Msg("Failed to create session")
}
}
}(info.profile, region, info.account)
}(info.profile, region, info.account, info)
}
}
}
Expand Down Expand Up @@ -202,4 +202,4 @@ func NewSession(profile, region string, log zerolog.Logger) (*sessionInfo, error
} else {
return nil, err
}
}
}
4 changes: 2 additions & 2 deletions program/aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (program *Options) WriteConfig(config *api.Config) error {
}

if err := os.Rename(program.KubeConfig, bakFile); err == nil {
if e2 := os.Rename(newFile, program.KubeConfig); err == nil {
if e2 := os.Rename(newFile, program.KubeConfig); e2 == nil {
return nil
} else {
// There was an error renaming the new file, so restore the bak file
Expand All @@ -109,4 +109,4 @@ func (program *Options) WriteConfig(config *api.Config) error {
} else {
return err
}
}
}
2 changes: 1 addition & 1 deletion program/aws/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,4 @@ func isTerminal(file *os.File) bool {
} else {
return (fileInfo.Mode() & os.ModeCharDevice) != 0
}
}
}
2 changes: 1 addition & 1 deletion program/aws/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ func (s *Stats) Log() {
Msg("Statistics")
}

var stats Stats
var stats Stats
2 changes: 1 addition & 1 deletion program/aws/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ func (v *VersionCmd) Run(program *Options) error {
_ = program
_, _ = fmt.Println(Version)
return nil
}
}
210 changes: 105 additions & 105 deletions program/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,116 +60,116 @@ func (program *Options) getSubscriptions() <-chan string {
}

func (program *Options) getClustersFrom(s *azureSessionInfo, clusters chan<- AzureClusterInfo) {
var wg sync.WaitGroup
defer wg.Wait()

client := s.client
ctx := context.Background()
uniqueClusters := make(map[string]struct{})

pager := client.NewListPager(nil)

for pager.More() {
page, err := pager.NextPage(ctx)
if err != nil {
s.log.Error().Err(err).Msg("Error listing AKS clusters")
return
}

for _, c := range page.Value {
clusterKey := fmt.Sprintf("%s-%s", *c.Name, *c.Location)
if _, exists := uniqueClusters[clusterKey]; !exists {
uniqueClusters[clusterKey] = struct{}{}
stats.Clusters.Add(1)

wg.Add(1)
go func(c *armcontainerservice.ManagedCluster) {
defer wg.Done()

s.log.Debug().
Str("cluster_name", *c.Name).
Str("location", *c.Location).
Msg("Found unique AKS cluster")

clusters <- AzureClusterInfo{
ManagedCluster: c,
log: s.log.With().Str("cluster_name", *c.Name).Str("location", *c.Location).Logger(),
session: s,
}
}(c)
}
}
}
var wg sync.WaitGroup
defer wg.Wait()

client := s.client
ctx := context.Background()
uniqueClusters := make(map[string]struct{})

pager := client.NewListPager(nil)

for pager.More() {
page, err := pager.NextPage(ctx)
if err != nil {
s.log.Error().Err(err).Msg("Error listing AKS clusters")
return
}

for _, c := range page.Value {
clusterKey := fmt.Sprintf("%s-%s", *c.Name, *c.Location)
if _, exists := uniqueClusters[clusterKey]; !exists {
uniqueClusters[clusterKey] = struct{}{}
stats.Clusters.Add(1)

wg.Add(1)
go func(c *armcontainerservice.ManagedCluster) {
defer wg.Done()

s.log.Debug().
Str("cluster_name", *c.Name).
Str("location", *c.Location).
Msg("Found unique AKS cluster")

clusters <- AzureClusterInfo{
ManagedCluster: c,
log: s.log.With().Str("cluster_name", *c.Name).Str("location", *c.Location).Logger(),
session: s,
}
}(c)
}
}
}
}

func (program *Options) getUniqueAzureSessions() <-chan *azureSessionInfo {
sessions := make(chan *azureSessionInfo)

go func() {
wg := sync.WaitGroup{}
defer close(sessions)
defer wg.Wait()

subscriptions := make(map[string]bool)
for info := range program.getSubscriptionSessions() {
if _, found := subscriptions[info.subscription]; found {
info.log.Debug().Msg("Subscription is duplicate")
continue
}

stats.UniqueSubscriptions.Add(1)
info.log.Debug().Msg("Subscription is good for use")
subscriptions[info.subscription] = true
sessions <- info

for _, location := range program.Locations {
if location != info.location {
stats.Locations.Add(1)
wg.Add(1)
go func(subscription, location string) {
defer wg.Done()
log := log.With().Str("subscription", subscription).Str("location", location).Logger()
log.Debug().Msg("Creating regional session")

if s, err := program.newAzureSession(subscription, location); err == nil {
sessions <- s
} else {
log.Error().Err(err).Msg("Failed to create Azure session")
}
}(info.subscription, location)
}
}
}
}()

return sessions
sessions := make(chan *azureSessionInfo)

go func() {
wg := sync.WaitGroup{}
defer close(sessions)
defer wg.Wait()

subscriptions := make(map[string]bool)
for info := range program.getSubscriptionSessions() {
if _, found := subscriptions[info.subscription]; found {
info.log.Debug().Msg("Subscription is duplicate")
continue
}

stats.UniqueSubscriptions.Add(1)
info.log.Debug().Msg("Subscription is good for use")
subscriptions[info.subscription] = true
sessions <- info

for _, location := range program.Locations {
if location != info.location {
stats.Locations.Add(1)
wg.Add(1)
go func(subscription, location string) {
defer wg.Done()
log := log.With().Str("subscription", subscription).Str("location", location).Logger()
log.Debug().Msg("Creating regional session")

if s, err := program.newAzureSession(subscription, location); err == nil {
sessions <- s
} else {
log.Error().Err(err).Msg("Failed to create Azure session")
}
}(info.subscription, location)
}
}
}
}()

return sessions
}

func (program *Options) getSubscriptionSessions() <-chan *azureSessionInfo {
sessions := make(chan *azureSessionInfo)
wg := sync.WaitGroup{}

go func() {
defer close(sessions)
defer wg.Wait()

subscriptions := program.getSubscriptions()

for s := range subscriptions {
stats.Subscriptions.Add(1)
log := log.With().Str("subscription", s).Str("location", program.Locations[0]).Logger()
wg.Add(1)
go func(s string) {
defer wg.Done()
if session, err := NewAzureSession(s, program.Locations[0], log); err == nil {
stats.UsableSubscriptions.Add(1)
sessions <- session
}
}(s)
}
}()

return sessions
sessions := make(chan *azureSessionInfo)
wg := sync.WaitGroup{}

go func() {
defer close(sessions)
defer wg.Wait()

subscriptions := program.getSubscriptions()

for s := range subscriptions {
stats.Subscriptions.Add(1)
log := log.With().Str("subscription", s).Str("location", program.Locations[0]).Logger()
wg.Add(1)
go func(s string) {
defer wg.Done()
if session, err := NewAzureSession(s, program.Locations[0], log); err == nil {
stats.UsableSubscriptions.Add(1)
sessions <- session
}
}(s)
}
}()

return sessions
}

func (program *Options) newAzureSession(subscription, location string) (*azureSessionInfo, error) {
Expand Down Expand Up @@ -219,4 +219,4 @@ func NewAzureSession(subscription, location string, log zerolog.Logger) (*azureS
client: client,
log: log,
}, nil
}
}
2 changes: 1 addition & 1 deletion program/azure/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func captureConfig(c AzureClusterInfo, resourceGroup string, i *api.Config) error {
certificateData := []byte(*c.ManagedCluster.Properties.NetworkProfile.ServiceCidr)
certificateData := []byte(*c.ManagedCluster.Properties.NetworkProfile.ServiceCidr)

cluster := api.Cluster{
Server: "https://" + *c.ManagedCluster.Properties.Fqdn,
Expand Down
Loading

0 comments on commit fa717f5

Please sign in to comment.