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
5 changes: 3 additions & 2 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 0 additions & 13 deletions controllers/clusterprofile_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,19 +261,6 @@ func (r *ClusterProfileReconciler) WatchForCAPI(mgr ctrl.Manager, c controller.C
return err
}

sourceMachine := source.Kind[*clusterv1.Machine](
mgr.GetCache(),
&clusterv1.Machine{},
handler.TypedEnqueueRequestsFromMapFunc(r.requeueClusterProfileForMachine),
predicates.MachinePredicate{Logger: mgr.GetLogger().WithValues("predicate", "machinepredicate")},
)

// When cluster-api machine changes, according to ClusterPredicates,
// one or more ClusterProfiles need to be reconciled.
if err := c.Watch(sourceMachine); err != nil {
return err
}

return nil
}

Expand Down
75 changes: 0 additions & 75 deletions controllers/clusterprofile_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,79 +404,4 @@ var _ = Describe("ClusterProfileReconciler: requeue methods", func() {
return false
}, timeout, pollingInterval).Should(BeTrue())
})

It("RequeueClusterProfileForMachine returns correct ClusterProfiles for a CAPI machine", func() {
ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
},
}

clusterConfiguration := &configv1beta1.ClusterConfiguration{
ObjectMeta: metav1.ObjectMeta{
Namespace: cluster.Namespace,
Name: controllers.GetClusterConfigurationName(cluster.Name, libsveltosv1beta1.ClusterTypeCapi),
},
}

cpMachine := &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Namespace: cluster.Namespace,
Name: cluster.Name + randomString(),
Labels: map[string]string{
clusterv1.ClusterNameLabel: cluster.Name,
clusterv1.MachineControlPlaneLabel: "ok",
},
},
}

Expect(testEnv.Create(context.TODO(), ns)).To(Succeed())
Expect(testEnv.Create(context.TODO(), cluster)).To(Succeed())
Expect(waitForObject(context.TODO(), testEnv.Client, cluster)).To(Succeed())

// Set Cluster ControlPlaneReady to true
currentCluster := &clusterv1.Cluster{}
Expect(testEnv.Get(context.TODO(),
types.NamespacedName{Namespace: cluster.Namespace, Name: cluster.Name}, currentCluster)).To(Succeed())
initialized := true
currentCluster.Status.Initialization.ControlPlaneInitialized = &initialized
Expect(testEnv.Status().Update(context.TODO(), currentCluster)).To(Succeed())

Expect(testEnv.Create(context.TODO(), cpMachine)).To(Succeed())
cpMachine.Status.SetTypedPhase(clusterv1.MachinePhaseRunning)
Expect(testEnv.Status().Update(context.TODO(), cpMachine)).To(Succeed())
Expect(addTypeInformationToObject(scheme, cpMachine)).To(Succeed())

Expect(testEnv.Create(context.TODO(), matchingClusterProfile)).To(Succeed())
Expect(testEnv.Create(context.TODO(), nonMatchingClusterProfile)).To(Succeed())

Expect(testEnv.Create(context.TODO(), clusterConfiguration)).To(Succeed())
Expect(addTypeInformationToObject(scheme, cluster)).To(Succeed())
Expect(addTypeInformationToObject(scheme, cpMachine)).To(Succeed())

Expect(waitForObject(context.TODO(), testEnv.Client, nonMatchingClusterProfile)).To(Succeed())

clusterProfileName := client.ObjectKey{
Name: matchingClusterProfile.Name,
}

clusterProfileReconciler := getClusterProfileReconciler(testEnv.Client)
_, err := clusterProfileReconciler.Reconcile(context.TODO(), ctrl.Request{
NamespacedName: clusterProfileName,
})
Expect(err).ToNot(HaveOccurred())

// Eventual loop so testEnv Cache is synced
Eventually(func() bool {
clusterProfileList := controllers.RequeueClusterProfileForMachine(clusterProfileReconciler,
context.TODO(), cpMachine)
result := reconcile.Request{NamespacedName: types.NamespacedName{Name: matchingClusterProfile.Name}}
for i := range clusterProfileList {
if clusterProfileList[i] == result {
return true
}
}
return false
}, timeout, pollingInterval).Should(BeTrue())
})
})
12 changes: 0 additions & 12 deletions controllers/clusterprofile_transformations.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,3 @@ func (r *ClusterProfileReconciler) requeueClusterProfileForCluster(

return requeueForCluster(cluster, r.ClusterProfiles, r.ClusterLabels, r.ClusterMap, configv1beta1.ClusterProfileKind, r.Logger)
}

func (r *ClusterProfileReconciler) requeueClusterProfileForMachine(
ctx context.Context, machine *clusterv1.Machine,
) []reconcile.Request {

addTypeInformationToObject(r.Scheme, machine)

r.Mux.Lock()
defer r.Mux.Unlock()

return requeueForMachine(machine, r.ClusterProfiles, r.ClusterLabels, r.ClusterMap, configv1beta1.ClusterProfileKind, r.Logger)
}
67 changes: 0 additions & 67 deletions controllers/clusterprofile_transformations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,71 +155,4 @@ var _ = Describe("ClusterProfileReconciler map functions", func() {
requests = controllers.RequeueClusterProfileForCluster(reconciler, context.TODO(), cluster)
Expect(requests).To(HaveLen(0))
})

It("RequeueClusterProfileForMachine returns correct ClusterProfiles for a CAPI machine", func() {
cluster := &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: upstreamClusterNamePrefix + randomString(),
Namespace: namespace,
Labels: map[string]string{
"env": "production",
},
},
}

cpMachine := &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Namespace: cluster.Namespace,
Name: cluster.Name + randomString(),
Labels: map[string]string{
clusterv1.ClusterNameLabel: cluster.Name,
clusterv1.MachineControlPlaneLabel: "ok",
},
},
}

clusterProfile := &configv1beta1.ClusterProfile{
ObjectMeta: metav1.ObjectMeta{
Name: clusterProfileNamePrefix + randomString(),
},
Spec: configv1beta1.Spec{
ClusterSelector: libsveltosv1beta1.Selector{
LabelSelector: metav1.LabelSelector{
MatchLabels: map[string]string{
"env": "production",
},
},
},
},
}

Expect(addTypeInformationToObject(scheme, cluster)).To(Succeed())
Expect(addTypeInformationToObject(scheme, cpMachine)).To(Succeed())
Expect(addTypeInformationToObject(scheme, clusterProfile)).To(Succeed())

// In this scenario:
// - ClusterProfile added first
// - Cluster matching ClusterProfile added later
// - First controlplane Machine in Cluster is ready
// The only information Sveltos has are:
// - Cluster's labels (stored in ClusterLabels map)
// - ClusterProfile's selector (stored in ClusterProfiles maps)
// RequeueClusterProfileForMachine gets cluster from machine and using ClusterLabels
// and ClusterProfiles maps finds the ClusterProfiles that need to be reconciled

apiVersion, kind := cluster.GetObjectKind().GroupVersionKind().ToAPIVersionAndKind()
clusterProfileReconciler := getClusterProfileReconciler(testEnv.Client)

clusterInfo := corev1.ObjectReference{APIVersion: apiVersion, Kind: kind,
Namespace: cluster.GetNamespace(), Name: cluster.GetName()}
clusterProfileReconciler.ClusterLabels[clusterInfo] = cluster.Labels

apiVersion, kind = clusterProfile.GetObjectKind().GroupVersionKind().ToAPIVersionAndKind()
clusterProfileInfo := corev1.ObjectReference{APIVersion: apiVersion, Kind: kind, Name: clusterProfile.GetName()}
clusterProfileReconciler.ClusterProfiles[clusterProfileInfo] = clusterProfile.Spec.ClusterSelector

clusterProfileList := controllers.RequeueClusterProfileForMachine(clusterProfileReconciler,
context.TODO(), cpMachine)
Expect(len(clusterProfileList)).To(Equal(1))
})
})
11 changes: 0 additions & 11 deletions controllers/controllers_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,3 @@ func getClusterProfileReconciler(c client.Client) *controllers.ClusterProfileRec
Mux: sync.Mutex{},
}
}

func getProfileReconciler(c client.Client) *controllers.ProfileReconciler {
return &controllers.ProfileReconciler{
Client: c,
Scheme: scheme,
ClusterMap: make(map[corev1.ObjectReference]*libsveltosset.Set),
Profiles: make(map[corev1.ObjectReference]libsveltosv1beta1.Selector),
ClusterLabels: make(map[corev1.ObjectReference]map[string]string),
Mux: sync.Mutex{},
}
}
2 changes: 0 additions & 2 deletions controllers/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@ var (

var (
RequeueClusterProfileForCluster = (*ClusterProfileReconciler).requeueClusterProfileForCluster
RequeueClusterProfileForMachine = (*ClusterProfileReconciler).requeueClusterProfileForMachine
GetClustersFromClusterSets = (*ClusterProfileReconciler).getClustersFromClusterSets
)

var (
RequeueProfileForCluster = (*ProfileReconciler).requeueProfileForCluster
RequeueProfileForMachine = (*ProfileReconciler).requeueProfileForMachine
LimitReferencesToNamespace = (*ProfileReconciler).limitReferencesToNamespace
GetClustersFromSets = (*ProfileReconciler).getClustersFromSets
)
Expand Down
13 changes: 0 additions & 13 deletions controllers/profile_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,19 +291,6 @@ func (r *ProfileReconciler) WatchForCAPI(mgr ctrl.Manager, c controller.Controll
return err
}

sourceMachine := source.Kind[*clusterv1.Machine](
mgr.GetCache(),
&clusterv1.Machine{},
handler.TypedEnqueueRequestsFromMapFunc(r.requeueProfileForMachine),
predicates.MachinePredicate{Logger: mgr.GetLogger().WithValues("predicate", "machinepredicate")},
)

// When cluster-api machine changes, according to ClusterPredicates,
// one or more ClusterProfiles need to be reconciled.
if err := c.Watch(sourceMachine); err != nil {
return err
}

return nil
}

Expand Down
70 changes: 0 additions & 70 deletions controllers/profile_transformation_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
Expand Down Expand Up @@ -91,72 +90,3 @@ func requeueForCluster(cluster client.Object,

return requests
}

func requeueForMachine(machine client.Object,
profileSelectors map[corev1.ObjectReference]libsveltosv1beta1.Selector,
clusterLabels map[corev1.ObjectReference]map[string]string,
clusterMap map[corev1.ObjectReference]*libsveltosset.Set,
kind string, logger logr.Logger) []reconcile.Request {

logger = logger.WithValues("machine", fmt.Sprintf("%s/%s", machine.GetNamespace(), machine.GetName()))

logger.V(logs.LogVerbose).Info("reacting to CAPI Machine change")

machineLabels := machine.GetLabels()
if machineLabels == nil {
return nil
}

clusterNameLabel, ok := machineLabels[clusterv1.ClusterNameLabel]
if !ok {
logger.V(logs.LogVerbose).Info("Machine has not ClusterNameLabel")
return nil
}

clusterInfo := corev1.ObjectReference{
APIVersion: clusterv1.GroupVersion.String(),
Kind: clusterKind,
Namespace: machine.GetNamespace(),
Name: clusterNameLabel}

// Get all ClusterProfile previously matching this cluster and reconcile those
requests := make([]ctrl.Request, getConsumersForEntry(clusterMap, &clusterInfo).Len())
consumers := getConsumersForEntry(clusterMap, &clusterInfo).Items()

for i := range consumers {
requests[i] = ctrl.Request{
NamespacedName: client.ObjectKey{
Namespace: consumers[i].Namespace,
Name: consumers[i].Name,
},
}
}

// Get Cluster labels
if clusterLabels, ok := clusterLabels[clusterInfo]; ok {
// Iterate over all current ClusterProfile and reconcile the ClusterProfile now
// matching the Cluster
for k := range profileSelectors {
profileSelector := profileSelectors[k]

clusterSelector, err := metav1.LabelSelectorAsSelector(&profileSelector.LabelSelector)
if err != nil {
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to convert selector %v", err))
continue
}

if clusterSelector.Matches(labels.Set(clusterLabels)) {
l := logger.WithValues(kind, k.Name)
l.V(logs.LogDebug).Info(fmt.Sprintf("queuing %s", kind))
requests = append(requests, ctrl.Request{
NamespacedName: client.ObjectKey{
Name: k.Name,
Namespace: k.Namespace,
},
})
}
}
}

return requests
}
12 changes: 0 additions & 12 deletions controllers/profile_transformations.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,6 @@ func (r *ProfileReconciler) requeueProfileForCluster(
return requeueForCluster(cluster, r.Profiles, r.ClusterLabels, r.ClusterMap, configv1beta1.ProfileKind, r.Logger)
}

func (r *ProfileReconciler) requeueProfileForMachine(
ctx context.Context, machine *clusterv1.Machine,
) []reconcile.Request {

addTypeInformationToObject(r.Scheme, machine)

r.Mux.Lock()
defer r.Mux.Unlock()

return requeueForMachine(machine, r.Profiles, r.ClusterLabels, r.ClusterMap, configv1beta1.ProfileKind, r.Logger)
}

func (r *ProfileReconciler) requeueProfileForSet(
ctx context.Context, o client.Object,
) []reconcile.Request {
Expand Down
Loading