-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add tests for k0scontrolplane controller #806
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,14 +43,15 @@ import ( | |
|
||
const ( | ||
etcdMemberConditionTypeJoined = "Joined" | ||
generatedMachineRoleLabel = "cluster.x-k8s.io/generateMachine-role" | ||
) | ||
|
||
func (c *K0sController) createMachine(ctx context.Context, name string, cluster *clusterv1.Cluster, kcp *cpv1beta1.K0sControlPlane, infraRef corev1.ObjectReference, failureDomain *string) (*clusterv1.Machine, error) { | ||
machine, err := c.generateMachine(ctx, name, cluster, kcp, infraRef, failureDomain) | ||
if err != nil { | ||
return nil, fmt.Errorf("error generating machine: %w", err) | ||
} | ||
_ = ctrl.SetControllerReference(kcp, machine, c.Scheme) | ||
_ = ctrl.SetControllerReference(kcp, machine, c.Client.Scheme()) | ||
|
||
return machine, c.Client.Patch(ctx, machine, client.Apply, &client.PatchOptions{ | ||
FieldManager: "k0smotron", | ||
|
@@ -81,9 +82,9 @@ func (c *K0sController) generateMachine(_ context.Context, name string, cluster | |
v := kcp.Spec.Version | ||
|
||
labels := map[string]string{ | ||
"cluster.x-k8s.io/cluster-name": kcp.Name, | ||
"cluster.x-k8s.io/control-plane": "true", | ||
"cluster.x-k8s.io/generateMachine-role": "control-plane", | ||
clusterv1.ClusterNameLabel: kcp.Name, | ||
clusterv1.MachineControlPlaneLabel: "true", | ||
generatedMachineRoleLabel: "control-plane", | ||
} | ||
|
||
for _, arg := range kcp.Spec.K0sConfigSpec.Args { | ||
|
@@ -210,7 +211,11 @@ func (c *K0sController) generateMachineFromTemplate(ctx context.Context, name st | |
return nil, err | ||
} | ||
|
||
_ = ctrl.SetControllerReference(kcp, unstructuredMachineTemplate, c.Scheme) | ||
_ = ctrl.SetControllerReference(cluster, unstructuredMachineTemplate, c.Client.Scheme()) | ||
err = c.Client.Patch(ctx, unstructuredMachineTemplate, client.Merge, &client.PatchOptions{FieldManager: "k0smotron"}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you patch it here? We apply patches in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. currently we patch InfraMachine but not InfraMachineTemplate. The current SetControllerReference in (Infra)MachineTemplate is not patched. If I dont patch this ownership, test will fail when checking ownership:
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
template, found, err := unstructured.NestedMap(unstructuredMachineTemplate.UnstructuredContent(), "spec", "template") | ||
if !found { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of curiosity, why do we change it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
writing tests for the controller I saw that we access the schema at this point and I thought about refactoring it using the client schema directly and remove
K0scontroller.Scheme
field. Both are obtained frommanager.Manager
so I thought it was safe to make this change