Skip to content

Commit

Permalink
Merge pull request #292 from gianlucam76/list-clusters
Browse files Browse the repository at this point in the history
ClusterAPI cluster list
  • Loading branch information
gianlucam76 authored Aug 7, 2023
2 parents 7a390da + a943d05 commit b738f2c
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 95 deletions.
4 changes: 4 additions & 0 deletions controllers/clusterprofile_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (

configv1alpha1 "github.com/projectsveltos/addon-controller/api/v1alpha1"
"github.com/projectsveltos/addon-controller/controllers"
"github.com/projectsveltos/addon-controller/internal/test/helpers/external"
"github.com/projectsveltos/addon-controller/pkg/scope"
libsveltosv1alpha1 "github.com/projectsveltos/libsveltos/api/v1alpha1"
libsveltosset "github.com/projectsveltos/libsveltos/lib/set"
Expand Down Expand Up @@ -93,7 +94,10 @@ var _ = Describe("ClusterProfile: Reconciler", func() {
})

It("getMatchingCluster considers both ClusterSelector and ClusterRefs", func() {
clusterCRD := external.TestClusterCRD.DeepCopy()

initObjects := []client.Object{
clusterCRD,
matchingCluster,
nonMatchingCluster,
clusterProfile,
Expand Down
2 changes: 1 addition & 1 deletion controllers/resourcesummary_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func collectAndProcessResourceSummaries(ctx context.Context, c client.Client, lo

for {
logger.V(logs.LogDebug).Info("collecting ResourceSummaries")
clusterList, err := getListOfClusters(ctx, c, logger)
clusterList, err := clusterproxy.GetListOfClusters(ctx, c, logger)
if err != nil {
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to get clusters: %v", err))
}
Expand Down
89 changes: 0 additions & 89 deletions controllers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

sourcev1 "github.com/fluxcd/source-controller/api/v1"
sourcev1b2 "github.com/fluxcd/source-controller/api/v1beta2"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -185,94 +184,6 @@ func addTypeInformationToObject(scheme *runtime.Scheme, obj client.Object) {
}
}

// getListOfCAPIClusters returns all CAPI Clusters where Classifier needs to be deployed.
// Currently a Classifier instance needs to be deployed in every existing CAPI cluster.
func getListOfCAPICluster(ctx context.Context, c client.Client, logger logr.Logger,
) ([]corev1.ObjectReference, error) {

clusterList := &clusterv1.ClusterList{}
if err := c.List(ctx, clusterList); err != nil {
logger.Error(err, "failed to list all Cluster")
return nil, err
}

clusters := make([]corev1.ObjectReference, 0)

for i := range clusterList.Items {
cluster := &clusterList.Items[i]

if !cluster.DeletionTimestamp.IsZero() {
// Only existing cluster can match
continue
}

addTypeInformationToObject(c.Scheme(), cluster)

clusters = append(clusters, corev1.ObjectReference{
Namespace: cluster.Namespace,
Name: cluster.Name,
APIVersion: cluster.APIVersion,
Kind: cluster.Kind,
})
}

return clusters, nil
}

// getListOfSveltosClusters returns all Sveltos Clusters where Classifier needs to be deployed.
// Currently a Classifier instance needs to be deployed in every existing sveltosCluster.
func getListOfSveltosCluster(ctx context.Context, c client.Client, logger logr.Logger,
) ([]corev1.ObjectReference, error) {

clusterList := &libsveltosv1alpha1.SveltosClusterList{}
if err := c.List(ctx, clusterList); err != nil {
logger.Error(err, "failed to list all Cluster")
return nil, err
}

clusters := make([]corev1.ObjectReference, 0)

for i := range clusterList.Items {
cluster := &clusterList.Items[i]

if !cluster.DeletionTimestamp.IsZero() {
// Only existing cluster can match
continue
}

addTypeInformationToObject(c.Scheme(), cluster)

clusters = append(clusters, corev1.ObjectReference{
Namespace: cluster.Namespace,
Name: cluster.Name,
APIVersion: cluster.APIVersion,
Kind: cluster.Kind,
})
}

return clusters, nil
}

// getListOfClusters returns all Sveltos/CAPI Clusters where Classifier needs to be deployed.
// Currently a Classifier instance needs to be deployed in every existing clusters.
func getListOfClusters(ctx context.Context, c client.Client, logger logr.Logger,
) ([]corev1.ObjectReference, error) {

clusters, err := getListOfCAPICluster(ctx, c, logger)
if err != nil {
return nil, err
}

var tmpClusters []corev1.ObjectReference
tmpClusters, err = getListOfSveltosCluster(ctx, c, logger)
if err != nil {
return nil, err
}

clusters = append(clusters, tmpClusters...)
return clusters, nil
}

// collectMgmtResources collects clusterSummary.Spec.ClusterProfileSpec.MgmtClusterResources
// from management cluster
func collectMgmtResources(ctx context.Context, clusterSummary *configv1alpha1.ClusterSummary,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/onsi/ginkgo/v2 v2.11.0
github.com/onsi/gomega v1.27.8
github.com/pkg/errors v0.9.1
github.com/projectsveltos/libsveltos v0.14.1-0.20230801071844-911755ff3e1d
github.com/projectsveltos/libsveltos v0.14.1-0.20230807153329-6951d8505e2b
github.com/prometheus/client_golang v1.16.0
github.com/spf13/pflag v1.0.5
github.com/yuin/gopher-lua v1.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,8 @@ github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSg
github.com/poy/onpar v0.0.0-20200406201722-06f95a1c68e8/go.mod h1:nSbFQvMj97ZyhFRSJYtut+msi4sOY6zJDGCdSc+/rZU=
github.com/poy/onpar v1.1.2 h1:QaNrNiZx0+Nar5dLgTVp5mXkyoVFIbepjyEoGSnhbAY=
github.com/poy/onpar v1.1.2/go.mod h1:6X8FLNoxyr9kkmnlqpK6LSoiOtrO6MICtWwEuWkLjzg=
github.com/projectsveltos/libsveltos v0.14.1-0.20230801071844-911755ff3e1d h1:caZaXoIcHWkc9NPVxL2xPQlLtb3bZNEMy664+GWooTI=
github.com/projectsveltos/libsveltos v0.14.1-0.20230801071844-911755ff3e1d/go.mod h1:7PaqLwqxsnsG+lV07m4RO4rOg2gKhlmpJVanWcB84JQ=
github.com/projectsveltos/libsveltos v0.14.1-0.20230807153329-6951d8505e2b h1:qmKZb2vqj3oLuetFO2GEkzleRbXe972WLsRat501KWM=
github.com/projectsveltos/libsveltos v0.14.1-0.20230807153329-6951d8505e2b/go.mod h1:7PaqLwqxsnsG+lV07m4RO4rOg2gKhlmpJVanWcB84JQ=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso=
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
Expand Down
6 changes: 6 additions & 0 deletions pkg/compliances/constraints_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ import (
. "github.com/onsi/gomega"

"github.com/pkg/errors"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/projectsveltos/addon-controller/internal/test/helpers"
Expand Down Expand Up @@ -70,6 +73,8 @@ var _ = BeforeSuite(func() {

ctx, cancel = context.WithCancel(context.TODO())

ctrl.SetLogger(klog.Background())

var err error
scheme = setupScheme()

Expand Down Expand Up @@ -117,6 +122,7 @@ func setupScheme() *runtime.Scheme {
s := runtime.NewScheme()
Expect(libsveltosv1alpha1.AddToScheme(s)).To(Succeed())
Expect(clusterv1.AddToScheme(s)).To(Succeed())
Expect(apiextensionsv1.AddToScheme(s)).To(Succeed())
return s
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/compliances/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ func InitializeManagerWithSkip(ctx context.Context, l logr.Logger, config *rest.
managerInstance.muMap = &sync.RWMutex{}
managerInstance.openAPIValidations = make(map[string]map[string][]byte)
managerInstance.clusters = make(map[string]bool)

managerInstance.capiPresent, _ = isCAPIInstalled(ctx, c)
}
}
}
Expand Down
35 changes: 33 additions & 2 deletions pkg/compliances/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import (
"time"

"github.com/go-logr/logr"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/rest"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -47,8 +50,9 @@ type manager struct {
client.Client
config *rest.Config

reEvaluate atomic.Value
ready atomic.Value
reEvaluate atomic.Value
ready atomic.Value
capiPresent bool

muMap *sync.RWMutex
// openAPIValidations contains all openapi validations for a given cluster
Expand Down Expand Up @@ -175,6 +179,14 @@ func (m *manager) setReEvaluate() {
// per cluster
func (m *manager) evaluate(ctx context.Context) {
for {
var err error
m.capiPresent, err = isCAPIInstalled(ctx, m.Client)
if err != nil {
// Sleep before next evaluation
time.Sleep(m.interval)
continue
}

select {
case <-ctx.Done():
m.log.V(logs.LogDebug).Info("Context canceled. Exiting goroutine.")
Expand Down Expand Up @@ -239,6 +251,10 @@ func (m *manager) reEvaluateClusters(ctx context.Context) {
}

func (m *manager) updateCurrentClusters(ctx context.Context, currentClusterMap map[string]bool) map[string]bool {
if !m.capiPresent {
return currentClusterMap
}

clusters := &clusterv1.ClusterList{}
if err := m.Client.List(ctx, clusters); err != nil {
m.log.V(logs.LogInfo).Info(fmt.Sprintf("failed to get clusters: %v", err))
Expand Down Expand Up @@ -363,3 +379,18 @@ func (m *manager) getClusterKey(clusterNamespace, clusterName string,

return fmt.Sprintf("%s:%s/%s", string(*clusterType), clusterNamespace, clusterName)
}

// isCAPIInstalled returns true if CAPI is installed, false otherwise
func isCAPIInstalled(ctx context.Context, c client.Client) (bool, error) {
clusterCRD := &apiextensionsv1.CustomResourceDefinition{}

err := c.Get(ctx, types.NamespacedName{Name: "clusters.cluster.x-k8s.io"}, clusterCRD)
if err != nil {
if apierrors.IsNotFound(err) {
return false, nil
}
return false, err
}

return true, nil
}
4 changes: 4 additions & 0 deletions pkg/compliances/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

"github.com/projectsveltos/addon-controller/internal/test/helpers/external"
"github.com/projectsveltos/addon-controller/pkg/compliances"
libsveltosv1alpha1 "github.com/projectsveltos/libsveltos/api/v1alpha1"
"github.com/projectsveltos/libsveltos/lib/clusterproxy"
Expand Down Expand Up @@ -222,7 +223,10 @@ var _ = Describe("Constraints", func() {
},
}

clusterCRD := external.TestClusterCRD.DeepCopy()

initObjects := []client.Object{
clusterCRD,
cluster1,
cluster2,
sveltosCluster1,
Expand Down

0 comments on commit b738f2c

Please sign in to comment.