-
Notifications
You must be signed in to change notification settings - Fork 3
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 #12 from marcomarinodev/cluster-status-api
cluster status api + tests
- Loading branch information
Showing
11 changed files
with
543 additions
and
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,6 @@ manager_pull_policy.yaml-e | |
manager_auth_proxy_patch.yaml-e | ||
|
||
version.txt | ||
|
||
# vs code | ||
.vscode |
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 |
---|---|---|
|
@@ -97,7 +97,6 @@ linters: | |
- containedctx | ||
- bodyclose | ||
- dogsled | ||
- dupl | ||
- durationcheck | ||
- errcheck | ||
- errname | ||
|
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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/pkg/errors" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/controller" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
|
||
logs "github.com/projectsveltos/libsveltos/lib/logsettings" | ||
"github.com/projectsveltos/ui-backend/internal/server" | ||
|
||
configv1alpha1 "github.com/projectsveltos/addon-controller/api/v1alpha1" | ||
) | ||
|
||
type ClusterSummaryReconciler struct { | ||
client.Client | ||
Scheme *runtime.Scheme | ||
ConcurrentReconciles int | ||
} | ||
|
||
func (r *ClusterSummaryReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
logger := ctrl.LoggerFrom(ctx) | ||
logger.V(logs.LogInfo).Info("Reconciling") | ||
|
||
clusterSummary := &configv1alpha1.ClusterSummary{} | ||
if err := r.Get(ctx, req.NamespacedName, clusterSummary); err != nil { | ||
if apierrors.IsNotFound(err) { | ||
r.removeClusterSummary(req.Namespace, req.Name, logger) | ||
return reconcile.Result{}, nil | ||
} | ||
logger.Error(err, "Failed to fetch ClusterSummary") | ||
return reconcile.Result{}, errors.Wrapf( | ||
err, | ||
"Failed to fetch ClusterSummary %s", | ||
req.NamespacedName, | ||
) | ||
} | ||
|
||
// Handle deleted ClusterSummary | ||
if !clusterSummary.DeletionTimestamp.IsZero() { | ||
r.removeClusterSummary(clusterSummary.Namespace, clusterSummary.Name, logger) | ||
} else { | ||
// Handle non-deleted ClusterSummary | ||
r.reconcileNormal(clusterSummary, logger) | ||
} | ||
|
||
return reconcile.Result{}, nil | ||
} | ||
|
||
func (r *ClusterSummaryReconciler) removeClusterSummary(clusterSummaryNamespace, clusterSummaryName string, logger logr.Logger) { | ||
logger.V(logs.LogInfo).Info("Reconciling Cluster delete") | ||
|
||
manager := server.GetManagerInstance() | ||
|
||
manager.RemoveClusterProfileStatus(clusterSummaryNamespace, clusterSummaryName) | ||
|
||
logger.V(logs.LogInfo).Info("Reconcile delete success") | ||
} | ||
|
||
func (r *ClusterSummaryReconciler) reconcileNormal(clusterSummary *configv1alpha1.ClusterSummary, logger logr.Logger) { | ||
logger.V(logs.LogInfo).Info("Reconciling new ClusterSummary") | ||
|
||
manager := server.GetManagerInstance() | ||
|
||
manager.AddClusterProfileStatus(clusterSummary) | ||
|
||
logger.V(logs.LogInfo).Info("Reconciling new ClusterSummary success") | ||
} | ||
|
||
// SetupWithManager sets up the controller with the Manager. | ||
func (r *ClusterSummaryReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
_, err := ctrl.NewControllerManagedBy(mgr). | ||
For(&configv1alpha1.ClusterSummary{}). | ||
WithOptions(controller.Options{ | ||
MaxConcurrentReconciles: r.ConcurrentReconciles, | ||
}). | ||
Build(r) | ||
if err != nil { | ||
return errors.Wrap(err, "error creating controller") | ||
} | ||
|
||
return nil | ||
} |
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package server | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"sort" | ||
) | ||
|
||
// getMapInRange extracts a subset of key-value pairs from the given map, skipping the first 'skip' pairs and then taking up to 'limit' pairs. | ||
func getMapInRange[K comparable, V any](m map[K]V, limit, skip int) (map[K]V, error) { | ||
if skip < 0 { | ||
return nil, errors.New("skip cannot be negative") | ||
} | ||
if limit < 0 { | ||
return nil, errors.New("limit cannot be negative") | ||
} | ||
if skip >= len(m) { | ||
return nil, errors.New("skip cannot be greater than or equal to the length of the map") | ||
} | ||
|
||
// Extract keys and sort them | ||
keys := make([]K, 0, len(m)) | ||
for k := range m { | ||
keys = append(keys, k) | ||
} | ||
sort.Slice(keys, func(i, j int) bool { | ||
return fmt.Sprintf("%v", keys[i]) < fmt.Sprintf("%v", keys[j]) | ||
}) | ||
|
||
// Create a new map for the result | ||
result := make(map[K]V) | ||
|
||
// Iterate over the sorted keys and collect the desired key-value pairs | ||
for i := skip; i < skip+limit && i < len(keys); i++ { | ||
k := keys[i] | ||
result[k] = m[k] | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func getProfileStatusesInRange(profileStatuses map[string][]ClusterFeatureSummary, limit, skip int) (map[string][]ClusterFeatureSummary, error) { | ||
return getMapInRange(profileStatuses, limit, skip) | ||
} |
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
Oops, something went wrong.