Skip to content

Commit

Permalink
Various fixes
Browse files Browse the repository at this point in the history
- Sort resources by lastAppliedTime first. In case of tie, use
GroupVersionKind to sort
- Sort helm charts by lastAppliedTime first. In case of tie, use
release namespace and release name.

Grant permissions to watch/get/list clusterSummary and ClusterSummary.Status
  • Loading branch information
mgianluc committed May 22, 2024
1 parent 393757e commit a79b597
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 2 deletions.
16 changes: 16 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ rules:
- get
- list
- watch
- apiGroups:
- config.projectsveltos.io
resources:
- clustersummaries
verbs:
- get
- list
- watch
- apiGroups:
- config.projectsveltos.io
resources:
- clustersummaries/status
verbs:
- get
- list
- watch
- apiGroups:
- lib.projectsveltos.io
resources:
Expand Down
3 changes: 3 additions & 0 deletions internal/controller/clustersummary_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ type ClusterSummaryReconciler struct {
ConcurrentReconciles int
}

//+kubebuilder:rbac:groups=config.projectsveltos.io,resources=clustersummaries,verbs=get;list;watch
//+kubebuilder:rbac:groups=config.projectsveltos.io,resources=clustersummaries/status,verbs=get;list;watch

func (r *ClusterSummaryReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := ctrl.LoggerFrom(ctx)
logger.V(logs.LogInfo).Info("Reconciling")
Expand Down
39 changes: 39 additions & 0 deletions internal/server/addons.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"

configv1alpha1 "github.com/projectsveltos/addon-controller/api/v1alpha1"
Expand Down Expand Up @@ -301,3 +302,41 @@ func getHelmReleaseInRange(helmReleases []HelmRelease, limit, skip int) ([]HelmR
func getResourcesInRange(resources []Resource, limit, skip int) ([]Resource, error) {
return getSliceInRange(resources, limit, skip)
}

// sortResources sorts resources by last applied time. In case time is same,
// resources are sorted by GVK
func sortResources(resources []Resource, i, j int) bool {
if resources[i].LastAppliedTime.Equal(resources[j].LastAppliedTime) {
// If deployment time is same, sort by GVK
gvk1 := schema.GroupVersionKind{
Group: resources[i].Group,
Kind: resources[i].Kind,
Version: resources[i].Version,
}

gvk2 := schema.GroupVersionKind{
Group: resources[j].Group,
Kind: resources[j].Kind,
Version: resources[j].Version,
}

return gvk1.String() < gvk2.String()
}

return resources[i].LastAppliedTime.Before(resources[j].LastAppliedTime)
}

// sortHelmCharts sorts helm charts by last applied time. In case time is same,
// resources are sorted by namespace and finally release name
func sortHelmCharts(helmCharts []HelmRelease, i, j int) bool {
if helmCharts[i].LastAppliedTime.Equal(helmCharts[j].LastAppliedTime) {
// If deployment time is same, sort by release namespace and then release name
if helmCharts[i].Namespace == helmCharts[j].Namespace {
return helmCharts[i].ReleaseName < helmCharts[j].ReleaseName
}

return helmCharts[i].Namespace < helmCharts[j].Namespace
}

return helmCharts[i].LastAppliedTime.Before(helmCharts[j].LastAppliedTime)
}
106 changes: 106 additions & 0 deletions internal/server/addons_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ package server_test

import (
"reflect"
"sort"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"

"github.com/projectsveltos/ui-backend/internal/server"
)
Expand Down Expand Up @@ -115,4 +119,106 @@ var _ = Describe("Deployed Addons and Applications", func() {
_, err = server.GetResourcesInRange(resources, limit, skip)
Expect(err).ToNot(BeNil())
})

It("sortResources sorts resources by applied time first and GVK then", func() {
time1 := metav1.Time{Time: time.Now()}
resources := make([]server.Resource, 0)
for i := 0; i < 10; i++ {
resource := server.Resource{
Namespace: randomString(),
Name: randomString(),
Group: randomString(),
Version: randomString(),
Kind: randomString(),
ProfileNames: []string{randomString()},
LastAppliedTime: &time1,
}

resources = append(resources, resource)
}

time2 := metav1.Time{Time: time.Now().Add(-time.Minute)}
resource := server.Resource{
Namespace: randomString(),
Name: randomString(),
Group: randomString(),
Version: randomString(),
Kind: randomString(),
ProfileNames: []string{randomString()},
LastAppliedTime: &time2,
}

resources = append(resources, resource)

sort.Slice(resources, func(i, j int) bool {
return server.SortResources(resources, i, j)
})

Expect(resources[0]).To(Equal(resource))

// Remove first element
resources = resources[1:]
for i := 0; i < len(resources)-2; i++ {
gvk1 := schema.GroupVersionKind{
Group: resources[i].Group,
Kind: resources[i].Kind,
Version: resources[i].Version,
}

gvk2 := schema.GroupVersionKind{
Group: resources[i+1].Group,
Kind: resources[i+1].Kind,
Version: resources[i+1].Version,
}

Expect(gvk1.String() < gvk2.String()).To(BeTrue())
}
})

It("sortHelmCharts sorts Helm Charts by applied time first and namespace/release name then", func() {
time1 := metav1.Time{Time: time.Now()}
helmReleases := make([]server.HelmRelease, 0)
for i := 0; i < 5; i++ {
helmRelease := server.HelmRelease{
Namespace: randomString(),
ReleaseName: randomString(),
RepoURL: randomString(),
Icon: randomString(),
ChartVersion: randomString(),
ProfileName: randomString(),
LastAppliedTime: &time1,
}

helmReleases = append(helmReleases, helmRelease)
}

time2 := metav1.Time{Time: time.Now().Add(-time.Minute)}
helmRelease := server.HelmRelease{
Namespace: randomString(),
ReleaseName: randomString(),
RepoURL: randomString(),
Icon: randomString(),
ChartVersion: randomString(),
ProfileName: randomString(),
LastAppliedTime: &time2,
}

helmReleases = append(helmReleases, helmRelease)

sort.Slice(helmReleases, func(i, j int) bool {
return server.SortHelmCharts(helmReleases, i, j)
})

Expect(helmReleases[0]).To(Equal(helmRelease))

// Remove first element
helmReleases = helmReleases[1:]
for i := 0; i < len(helmReleases)-2; i++ {
if helmReleases[i].Namespace == helmReleases[i+1].Namespace {
Expect(helmReleases[i].ReleaseName < helmReleases[i+1].ReleaseName)
}

Expect(helmReleases[i].Namespace < helmReleases[i+1].Namespace).To(BeTrue())
}
})
})
3 changes: 3 additions & 0 deletions internal/server/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ var (
GetClustersInRange = getClustersInRange
GetHelmReleaseInRange = getHelmReleaseInRange
GetResourcesInRange = getResourcesInRange

SortResources = sortResources
SortHelmCharts = sortHelmCharts
)

var (
Expand Down
4 changes: 2 additions & 2 deletions internal/server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ var (
_ = c.AbortWithError(http.StatusBadRequest, err)
}
sort.Slice(helmCharts, func(i, j int) bool {
return helmCharts[i].LastAppliedTime.Before(helmCharts[j].LastAppliedTime)
return sortHelmCharts(helmCharts, i, j)
})

result, err := getHelmReleaseInRange(helmCharts, limit, skip)
Expand Down Expand Up @@ -156,7 +156,7 @@ var (
_ = c.AbortWithError(http.StatusBadRequest, err)
}
sort.Slice(resources, func(i, j int) bool {
return resources[i].LastAppliedTime.Before(resources[j].LastAppliedTime)
return sortResources(resources, i, j)
})

result, err := getResourcesInRange(resources, limit, skip)
Expand Down
16 changes: 16 additions & 0 deletions manifest/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ rules:
- get
- list
- watch
- apiGroups:
- config.projectsveltos.io
resources:
- clustersummaries
verbs:
- get
- list
- watch
- apiGroups:
- config.projectsveltos.io
resources:
- clustersummaries/status
verbs:
- get
- list
- watch
- apiGroups:
- lib.projectsveltos.io
resources:
Expand Down

0 comments on commit a79b597

Please sign in to comment.