-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from kotaicode/generic-monitors
Generic monitors
- Loading branch information
Showing
3 changed files
with
62 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,31 +35,6 @@ type ResourceMonitorReconciler struct { | |
Scheme *runtime.Scheme | ||
} | ||
|
||
const ( | ||
ResourceType = "ec2" | ||
ResourceNamespace = "default" | ||
) | ||
|
||
// 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 | ||
} | ||
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 | ||
} | ||
|
||
//+kubebuilder:rbac:groups=manager.kotaico.de,resources=resourcemonitors,verbs=get;list;watch;create;update;patch;delete | ||
//+kubebuilder:rbac:groups=manager.kotaico.de,resources=resourcemonitors/status,verbs=get;update;patch | ||
//+kubebuilder:rbac:groups=manager.kotaico.de,resources=resourcemonitors/finalizers,verbs=update | ||
|
@@ -71,7 +46,7 @@ func diff(m1, m2 map[string]bool) []string { //TODO: give a better name | |
// - 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") | ||
|
||
|
@@ -88,34 +63,31 @@ 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 | ||
} | ||
|
||
uniqueTags, err := clients.GetUniqueTags() | ||
monitor, err := clients.MonitorFactory(resourceMonitor.Spec.Type) | ||
if err != nil { | ||
log.Error(err, "Error getting unique tags") | ||
log.Error(err, err.Error()) | ||
return ctrl.Result{}, client.IgnoreNotFound(err) | ||
} | ||
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...) | ||
nonMatchingTags, err := monitor.GetNewResources(clusterResources) | ||
if err != nil { | ||
log.Error(err, err.Error()) | ||
return ctrl.Result{}, client.IgnoreNotFound(err) | ||
} | ||
|
||
for _, tag := range nonMatchingTags { | ||
resource := &managerv1.Resource{ | ||
TypeMeta: metav1.TypeMeta{ | ||
APIVersion: "manager.kotaico.de/v1", | ||
Kind: "Resource", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: tag, | ||
Namespace: ResourceNamespace, | ||
Namespace: resourceMonitor.Namespace, | ||
}, | ||
Spec: managerv1.ResourceSpec{ | ||
Booked: false, | ||
Tag: tag, | ||
Type: ResourceType, | ||
Type: resourceMonitor.Spec.Type, | ||
}, | ||
} | ||
log.Info("creating resources") | ||
|
@@ -125,7 +97,7 @@ func (r *ResourceMonitorReconciler) Reconcile(ctx context.Context, req ctrl.Requ | |
} | ||
} | ||
|
||
return ctrl.Result{RequeueAfter: time.Duration(time.Second * 15)}, nil | ||
return ctrl.Result{RequeueAfter: time.Duration(time.Minute * 2)}, nil | ||
} | ||
|
||
// SetupWithManager sets up the controller with the Manager. | ||
|