Skip to content

Commit

Permalink
Cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
rallyos authored and Tomjosetj31 committed Feb 10, 2023
1 parent 0c6c0b5 commit f706f71
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 41 deletions.
59 changes: 20 additions & 39 deletions clients/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,31 +128,21 @@ func (r *EC2Resource) getInstanceIds(nameTag string) ([]*string, error) {
return instanceIds, nil
}

// sliceToMap returns map, used to convert slice to map
func sliceToMap(slice []string) map[string]bool { //TODO: give a better name
m := make(map[string]bool)
for _, s := range slice {
m[s] = true
// GetNewResources returns a list of tags which don't have resources on the cluster
func (m *EC2Monitor) GetNewResources(clusterResources map[string]bool) ([]string, error) {
uniqueTags, err := GetUniqueTags()
if err != nil {
return nil, err
}
return m
}

// diff returns a slice of strings, used to compare two maps.
func diff(m1, m2 map[string]bool) []string { //TODO: give a better name
slice := make([]string, 0, len(m1))
for k := range m1 {
if _, ok := m2[k]; !ok {
slice = append(slice, k)
}
}
return slice
}
slice1, slice2 := setDiff(uniqueTags, clusterResources), setDiff(clusterResources, uniqueTags)
nonMatchingTags := append(slice1, slice2...)

// GetUniqueTags returns a slice of unique tags.
// It makes a call through the EC2 client to get all the unique tags in the cluster.
func GetUniqueTags() ([]string, error) {
var uniqueTags []string
return nonMatchingTags, nil
}

// GetUniqueTags makes a call through the EC2 client to collect all instance tags and returns a set of them
func GetUniqueTags() (map[string]bool, error) {
// Prepare filters
tagKey := "tag:" + resourceMonitorTagKey
tagValue := "true"
Expand Down Expand Up @@ -180,25 +170,16 @@ func GetUniqueTags() ([]string, error) {
}
}

for tag := range tagMap {
uniqueTags = append(uniqueTags, tag)
}
return uniqueTags, nil

return tagMap, nil
}

func (m *EC2Monitor) GetNewResources(clusterResources []string) ([]string, error) {
uniqueTags, err := GetUniqueTags()
if err != nil {
return nil, err
// setDiff returns the difference between two sets
func setDiff(m1, m2 map[string]bool) []string {
slice := make([]string, 0, len(m1))
for k := range m1 {
if _, ok := m2[k]; !ok {
slice = append(slice, k)
}
}

uniqueTagsMap := sliceToMap(uniqueTags)
clusterResourcesMap := sliceToMap(clusterResources)

slice1 := diff(uniqueTagsMap, clusterResourcesMap) //TODO: give a better name
slice2 := diff(clusterResourcesMap, uniqueTagsMap) //TODO: give a better name
nonMatchingTags := append(slice1, slice2...)

return nonMatchingTags, nil
return slice
}
4 changes: 2 additions & 2 deletions controllers/resourcemonitor_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type ResourceMonitorReconciler struct {
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (r *ResourceMonitorReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := log.FromContext(ctx)
var clusterResources []string
clusterResources := make(map[string]bool)

log.Info("Reconcile resource monitor")

Expand All @@ -63,7 +63,7 @@ func (r *ResourceMonitorReconciler) Reconcile(ctx context.Context, req ctrl.Requ
}

for _, rs := range resources.Items {
clusterResources = append(clusterResources, rs.Spec.Tag)
clusterResources[rs.Spec.Tag] = true
}

monitor, err := clients.MonitorFactory(resourceMonitor.Spec.Type)
Expand Down

0 comments on commit f706f71

Please sign in to comment.