Skip to content

Commit

Permalink
Check MachineConfig spec for full equality with Rke2CPSpec
Browse files Browse the repository at this point in the history
Signed-off-by: Danil Grigorev <danil.grigorev@suse.com>
  • Loading branch information
Danil-Grigorev committed May 17, 2024
1 parent 141f4e0 commit 2a7f097
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 1 deletion.
57 changes: 57 additions & 0 deletions bootstrap/internal/controllers/rke2config_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
kubeyaml "sigs.k8s.io/yaml"

Expand Down Expand Up @@ -290,9 +291,65 @@ func (r *RKE2ConfigReconciler) SetupWithManager(mgr ctrl.Manager) error {

return ctrl.NewControllerManagedBy(mgr).
For(&bootstrapv1.RKE2Config{}).
Watches(
&clusterv1.Machine{},
handler.EnqueueRequestsFromMapFunc(r.MachineToBootstrapMapFunc),
).
Watches(
&clusterv1.Cluster{},
handler.EnqueueRequestsFromMapFunc(r.ClusterToRKE2Configs),
).
Complete(r)
}

// MachineToBootstrapMapFunc is a handler.ToRequestsFunc to be used to enqueue
// request for reconciliation of RKE2Config.
func (r *RKE2ConfigReconciler) MachineToBootstrapMapFunc(_ context.Context, o client.Object) []ctrl.Request {
m, ok := o.(*clusterv1.Machine)
if !ok {
panic(fmt.Sprintf("Expected a Machine but got a %T", o))
}

result := []ctrl.Request{}

if m.Spec.Bootstrap.ConfigRef != nil && m.Spec.Bootstrap.ConfigRef.GroupVersionKind() == bootstrapv1.GroupVersion.WithKind("RKE2Config") {
name := client.ObjectKey{Namespace: m.Namespace, Name: m.Spec.Bootstrap.ConfigRef.Name}
result = append(result, ctrl.Request{NamespacedName: name})
}

return result
}

// ClusterToRKE2Configs is a handler.ToRequestsFunc to be used to enqueue
// requests for reconciliation of RKE2Configs.
func (r *RKE2ConfigReconciler) ClusterToRKE2Configs(ctx context.Context, o client.Object) []ctrl.Request {
result := []ctrl.Request{}

c, ok := o.(*clusterv1.Cluster)
if !ok {
panic(fmt.Sprintf("Expected a Cluster but got a %T", o))
}

selectors := []client.ListOption{
client.InNamespace(c.Namespace),
client.MatchingLabels{
clusterv1.ClusterNameLabel: c.Name,
},
}

machineList := &clusterv1.MachineList{}
if err := r.Client.List(ctx, machineList, selectors...); err != nil {
return nil
}

for _, m := range machineList.Items {
m := m
result = append(result, r.MachineToBootstrapMapFunc(ctx, &m)...)
}

return result
}

// handleClusterNotInitialized handles the first control plane node.
func (r *RKE2ConfigReconciler) handleClusterNotInitialized(ctx context.Context, scope *Scope) (res ctrl.Result, reterr error) { //nolint:funlen
if !scope.HasControlPlaneOwner {
Expand Down
6 changes: 5 additions & 1 deletion pkg/rke2/machine_filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ func matchesRKE2BootstrapConfig(machineConfigs map[string]*bootstrapv1.RKE2Confi
}

// Check if RCP AgentConfig and machineBootstrapConfig matches
return reflect.DeepEqual(machineConfig.Spec.AgentConfig, rcp.Spec.AgentConfig)
return reflect.DeepEqual(machineConfig.Spec.AgentConfig, rcp.Spec.AgentConfig) &&
reflect.DeepEqual(machineConfig.Spec.Files, rcp.Spec.Files) &&
reflect.DeepEqual(machineConfig.Spec.PostRKE2Commands, rcp.Spec.PostRKE2Commands) &&
reflect.DeepEqual(machineConfig.Spec.PreRKE2Commands, rcp.Spec.PreRKE2Commands) &&
reflect.DeepEqual(machineConfig.Spec.PrivateRegistriesConfig, rcp.Spec.PrivateRegistriesConfig)
}
}

Expand Down
48 changes: 48 additions & 0 deletions pkg/rke2/machine_filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,54 @@ var _ = Describe("matchAgentConfig", func() {
Expect(matches.Oldest().Name).To(Equal("machine-test"))
},
)

It("shouldn't match Agent Config and different preBootstrapCommands", func() {
machineConfigs := map[string]*bootstrapv1.RKE2Config{
"someMachine": {},
"machine-test": {
ObjectMeta: v1.ObjectMeta{
Name: "rke2-config-example",
Namespace: "example",
},
Spec: bootstrapv1.RKE2ConfigSpec{
AgentConfig: bootstrapv1.RKE2AgentConfig{
NodeLabels: []string{"hello=world"},
},
PreRKE2Commands: []string{"test"},
},
},
}
machineCollection := collections.FromMachines(&machine)
Expect(len(machineCollection)).To(Equal(1))
matches := machineCollection.AnyFilter(matchesRKE2BootstrapConfig(machineConfigs, &rcp))

Expect(len(matches)).To(Equal(0))
},
)

It("shouldn't match Agent Config and different postBootstrapCommands", func() {
machineConfigs := map[string]*bootstrapv1.RKE2Config{
"someMachine": {},
"machine-test": {
ObjectMeta: v1.ObjectMeta{
Name: "rke2-config-example",
Namespace: "example",
},
Spec: bootstrapv1.RKE2ConfigSpec{
AgentConfig: bootstrapv1.RKE2AgentConfig{
NodeLabels: []string{"hello=world"},
},
PostRKE2Commands: []string{"test"},
},
},
}
machineCollection := collections.FromMachines(&machine)
Expect(len(machineCollection)).To(Equal(1))
matches := machineCollection.AnyFilter(matchesRKE2BootstrapConfig(machineConfigs, &rcp))

Expect(len(matches)).To(Equal(0))
},
)
})

var _ = Describe("matching Kubernetes Version", func() {
Expand Down

0 comments on commit 2a7f097

Please sign in to comment.