Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 2 additions & 47 deletions controllers/clusterprofile_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/source"

libsveltosv1alpha1 "github.com/projectsveltos/libsveltos/api/v1alpha1"
"github.com/projectsveltos/libsveltos/lib/clusterproxy"
logs "github.com/projectsveltos/libsveltos/lib/logsettings"
libsveltosset "github.com/projectsveltos/libsveltos/lib/set"
configv1alpha1 "github.com/projectsveltos/sveltos-manager/api/v1alpha1"
Expand Down Expand Up @@ -422,7 +423,7 @@ func (r *ClusterProfileReconciler) cleanClusterReports(ctx context.Context,
func (r *ClusterProfileReconciler) updateClusterSummaries(ctx context.Context, clusterProfileScope *scope.ClusterProfileScope) error {
for i := range clusterProfileScope.ClusterProfile.Status.MatchingClusterRefs {
cluster := clusterProfileScope.ClusterProfile.Status.MatchingClusterRefs[i]
ready, err := r.isClusterReadyToBeConfigured(ctx, clusterProfileScope, &cluster)
ready, err := clusterproxy.IsClusterReadyToBeConfigured(ctx, r.Client, &cluster, clusterProfileScope.Logger)
if err != nil {
return err
}
Expand Down Expand Up @@ -844,52 +845,6 @@ func (r *ClusterProfileReconciler) updateClusterConfigurationClusterProfileResou
return err
}

// isClusterReadyToBeConfigured gets all Machines for a given CAPI Cluster and returns true
// if at least one control plane machine is in running phase
func (r *ClusterProfileReconciler) isClusterReadyToBeConfigured(
ctx context.Context,
clusterProfileScope *scope.ClusterProfileScope,
cluster *corev1.ObjectReference,
) (bool, error) {

machineList, err := r.getMachinesForCluster(ctx, clusterProfileScope, cluster)
if err != nil {
return false, err
}

for i := range machineList.Items {
if util.IsControlPlaneMachine(&machineList.Items[i]) &&
machineList.Items[i].Status.GetTypedPhase() == clusterv1.MachinePhaseRunning {

return true, nil
}
}

return false, nil
}

// getMachinesForCluster find all Machines for a given CAPI Cluster.
func (r *ClusterProfileReconciler) getMachinesForCluster(
ctx context.Context,
clusterProfileScope *scope.ClusterProfileScope,
cluster *corev1.ObjectReference,
) (*clusterv1.MachineList, error) {

listOptions := []client.ListOption{
client.InNamespace(cluster.Namespace),
client.MatchingLabels{clusterv1.ClusterLabelName: cluster.Name},
}
var machineList clusterv1.MachineList
if err := r.Client.List(ctx, &machineList, listOptions...); err != nil {
clusterProfileScope.Error(err, fmt.Sprintf("unable to list Machines for CAPI Cluster %s/%s",
cluster.Namespace, cluster.Name))
return nil, err
}
clusterProfileScope.V(logs.LogDebug).Info(fmt.Sprintf("Found %d machine", len(machineList.Items)))

return &machineList, nil
}

func (r *ClusterProfileReconciler) updatesMaps(clusterProfileScope *scope.ClusterProfileScope) {
currentClusters := &libsveltosset.Set{}
for i := range clusterProfileScope.ClusterProfile.Status.MatchingClusterRefs {
Expand Down
156 changes: 0 additions & 156 deletions controllers/clusterprofile_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,162 +801,6 @@ var _ = Describe("ClusterProfile: Reconciler", func() {
Expect(reflect.DeepEqual(clusterSummaryList.Items[0].Spec.ClusterProfileSpec, clusterProfile.Spec)).To(BeTrue())
})

It("getMachinesForCluster returns list of all machines for a CPI cluster", func() {
cpMachine := &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Namespace: matchingCluster.Namespace,
Name: matchingCluster.Name + randomString(),
Labels: map[string]string{
clusterv1.ClusterLabelName: matchingCluster.Name,
clusterv1.MachineControlPlaneLabelName: "ok",
},
},
}
workerMachine := &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Namespace: matchingCluster.Namespace,
Name: matchingCluster.Name + randomString(),
Labels: map[string]string{
clusterv1.ClusterLabelName: matchingCluster.Name,
},
},
}

initObjects := []client.Object{
workerMachine,
cpMachine,
}

c := fake.NewClientBuilder().WithScheme(scheme).WithObjects(initObjects...).Build()

reconciler := &controllers.ClusterProfileReconciler{
Client: c,
Scheme: scheme,
ClusterMap: make(map[libsveltosv1alpha1.PolicyRef]*libsveltosset.Set),
ClusterProfileMap: make(map[libsveltosv1alpha1.PolicyRef]*libsveltosset.Set),
ClusterProfiles: make(map[libsveltosv1alpha1.PolicyRef]configv1alpha1.Selector),
Mux: sync.Mutex{},
}

clusterProfileScope, err := scope.NewClusterProfileScope(scope.ClusterProfileScopeParams{
Client: c,
Logger: logger,
ClusterProfile: clusterProfile,
ControllerName: "clusterprofile",
})
Expect(err).To(BeNil())

cps, err := controllers.GetMachinesForCluster(reconciler, context.TODO(),
clusterProfileScope, &corev1.ObjectReference{Namespace: matchingCluster.Namespace, Name: matchingCluster.Name})
Expect(err).To(BeNil())
Expect(len(cps.Items)).To(Equal(2))
})

It("IsClusterReadyToBeConfigured returns true for a cluster with one control plane machine in running phase", func() {
cpMachine := &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Namespace: matchingCluster.Namespace,
Name: matchingCluster.Name + randomString(),
Labels: map[string]string{
clusterv1.ClusterLabelName: matchingCluster.Name,
clusterv1.MachineControlPlaneLabelName: "ok",
},
},
}
cpMachine.Status.SetTypedPhase(clusterv1.MachinePhaseRunning)

workerMachine := &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Namespace: matchingCluster.Namespace,
Name: matchingCluster.Name + randomString(),
Labels: map[string]string{
clusterv1.ClusterLabelName: matchingCluster.Name,
},
},
}
initObjects := []client.Object{
workerMachine,
cpMachine,
}

c := fake.NewClientBuilder().WithScheme(scheme).WithObjects(initObjects...).Build()

reconciler := &controllers.ClusterProfileReconciler{
Client: c,
Scheme: scheme,
ClusterMap: make(map[libsveltosv1alpha1.PolicyRef]*libsveltosset.Set),
ClusterProfileMap: make(map[libsveltosv1alpha1.PolicyRef]*libsveltosset.Set),
ClusterProfiles: make(map[libsveltosv1alpha1.PolicyRef]configv1alpha1.Selector),
Mux: sync.Mutex{},
}

clusterProfileScope, err := scope.NewClusterProfileScope(scope.ClusterProfileScopeParams{
Client: c,
Logger: logger,
ClusterProfile: clusterProfile,
ControllerName: "clusterprofile",
})
Expect(err).To(BeNil())

ready, err := controllers.IsClusterReadyToBeConfigured(reconciler, context.TODO(),
clusterProfileScope, &corev1.ObjectReference{Namespace: matchingCluster.Namespace, Name: matchingCluster.Name})
Expect(err).To(BeNil())
Expect(ready).To(Equal(true))
})

It("IsClusterReadyToBeConfigured returns false for a cluster with no control plane machine in running phase", func() {
cpMachine := &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Namespace: matchingCluster.Namespace,
Name: matchingCluster.Name + randomString(),
Labels: map[string]string{
clusterv1.ClusterLabelName: matchingCluster.Name,
clusterv1.MachineControlPlaneLabelName: "ok",
},
},
}
workerMachine := &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Namespace: matchingCluster.Namespace,
Name: matchingCluster.Name + randomString(),
Labels: map[string]string{
clusterv1.ClusterLabelName: matchingCluster.Name,
},
},
Status: clusterv1.MachineStatus{
Phase: "Runnning",
},
}
initObjects := []client.Object{
workerMachine,
cpMachine,
}

c := fake.NewClientBuilder().WithScheme(scheme).WithObjects(initObjects...).Build()

reconciler := &controllers.ClusterProfileReconciler{
Client: c,
Scheme: scheme,
ClusterMap: make(map[libsveltosv1alpha1.PolicyRef]*libsveltosset.Set),
ClusterProfileMap: make(map[libsveltosv1alpha1.PolicyRef]*libsveltosset.Set),
ClusterProfiles: make(map[libsveltosv1alpha1.PolicyRef]configv1alpha1.Selector),
Mux: sync.Mutex{},
}

clusterProfileScope, err := scope.NewClusterProfileScope(scope.ClusterProfileScopeParams{
Client: c,
Logger: logger,
ClusterProfile: clusterProfile,
ControllerName: "clusterprofile",
})
Expect(err).To(BeNil())

ready, err := controllers.IsClusterReadyToBeConfigured(reconciler, context.TODO(),
clusterProfileScope, &corev1.ObjectReference{Namespace: matchingCluster.Namespace, Name: matchingCluster.Name})
Expect(err).To(BeNil())
Expect(ready).To(Equal(false))
})

It("updateClusterReports creates ClusterReport for matching cluster in DryRun mode", func() {
clusterProfile.Spec.SyncMode = configv1alpha1.SyncModeDryRun
clusterProfile.Status.MatchingClusterRefs = []corev1.ObjectReference{
Expand Down
2 changes: 0 additions & 2 deletions controllers/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ var (
CreateClusterSummary = (*ClusterProfileReconciler).createClusterSummary
UpdateClusterSummary = (*ClusterProfileReconciler).updateClusterSummary
DeleteClusterSummary = (*ClusterProfileReconciler).deleteClusterSummary
GetMachinesForCluster = (*ClusterProfileReconciler).getMachinesForCluster
IsClusterReadyToBeConfigured = (*ClusterProfileReconciler).isClusterReadyToBeConfigured
UpdateClusterConfiguration = (*ClusterProfileReconciler).updateClusterConfiguration
CleanClusterConfiguration = (*ClusterProfileReconciler).cleanClusterConfiguration
CleanClusterReports = (*ClusterProfileReconciler).cleanClusterReports
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/onsi/ginkgo/v2 v2.1.6
github.com/onsi/gomega v1.20.1
github.com/pkg/errors v0.9.1
github.com/projectsveltos/libsveltos v0.0.0-20221027171020-c479dfe235bb
github.com/projectsveltos/libsveltos v0.0.0-20221027224732-9836e51633bf
github.com/prometheus/client_golang v1.12.2
github.com/spf13/pflag v1.0.5
github.com/totherme/unstructured v0.0.0-20170821094912-3faf2d56d8b8
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -780,8 +780,8 @@ github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndr
github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s=
github.com/poy/onpar v0.0.0-20190519213022-ee068f8ea4d1 h1:oL4IBbcqwhhNWh31bjOX8C/OCy0zs9906d/VUru+bqg=
github.com/poy/onpar v0.0.0-20190519213022-ee068f8ea4d1/go.mod h1:nSbFQvMj97ZyhFRSJYtut+msi4sOY6zJDGCdSc+/rZU=
github.com/projectsveltos/libsveltos v0.0.0-20221027171020-c479dfe235bb h1:nduHWD9ql99B1iWW0AYZzNB1oQKw8XkTCz/rNWl+2J4=
github.com/projectsveltos/libsveltos v0.0.0-20221027171020-c479dfe235bb/go.mod h1:UiTbOwoj0nATxKGrK7/Ko4JK2zHY6MY61D39RlUFekk=
github.com/projectsveltos/libsveltos v0.0.0-20221027224732-9836e51633bf h1:HLTDi/EZgY41XGJ4Jqjd5pqpwkoklfJ8eRCljmfVDxw=
github.com/projectsveltos/libsveltos v0.0.0-20221027224732-9836e51633bf/go.mod h1:UiTbOwoj0nATxKGrK7/Ko4JK2zHY6MY61D39RlUFekk=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
github.com/prometheus/client_golang v1.1.0/go.mod h1:I1FGZT9+L76gKKOs5djB6ezCbFQP1xR9D75/vuwEF3g=
Expand Down