Skip to content

Commit a420f81

Browse files
authored
refactor: Move matchers to own package and add tests (#125)
1 parent 1563a7b commit a420f81

File tree

5 files changed

+542
-59
lines changed

5 files changed

+542
-59
lines changed

pkg/handlers/httpproxy/matcher.go renamed to pkg/capi/clustertopology/patches/matchers/match.go

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// See: https://github.com/kubernetes-sigs/cluster-api/blob/46412f0a4ea65d8f02478d2ad09ce12925485f56/internal/controllers/topology/cluster/patches/inline/json_patch_generator.go#L125
77
//
88
//nolint:lll // Long URLs in comments above. Adding nolint:lll here because it doesn't work in comment lines. See: https://github.com/golangci/golangci-lint/issues/3983
9-
package httpproxy
9+
package matchers
1010

1111
import (
1212
"strconv"
@@ -19,65 +19,52 @@ import (
1919
"sigs.k8s.io/cluster-api/exp/runtime/topologymutation"
2020
)
2121

22-
func matchSelector(
22+
func MatchesSelector(
2323
selector clusterv1.PatchSelector,
2424
obj runtime.Object,
2525
holderRef *runtimehooksv1.HolderReference,
2626
templateVariables map[string]apiextensionsv1.JSON,
2727
) bool {
28-
if !matchGVK(selector.APIVersion, selector.Kind, obj) {
28+
if !MatchesGVK(selector.APIVersion, selector.Kind, obj) {
2929
return false
3030
}
3131

32-
if selector.MatchResources.InfrastructureCluster {
33-
if !matchInfrastructure(holderRef) {
34-
return false
35-
}
32+
if selector.MatchResources.InfrastructureCluster && MatchesInfrastructure(holderRef) {
33+
return true
3634
}
3735

38-
if selector.MatchResources.ControlPlane {
39-
if !matchControlPlane(holderRef) {
40-
return false
41-
}
36+
if selector.MatchResources.ControlPlane && MatchesControlPlane(holderRef) {
37+
return true
4238
}
4339

44-
if selector.MatchResources.MachineDeploymentClass != nil {
45-
if !matchMachineDeploymentClass(
40+
if selector.MatchResources.MachineDeploymentClass != nil &&
41+
MatchesMachineDeploymentClass(
4642
holderRef,
4743
selector.MatchResources.MachineDeploymentClass.Names,
4844
templateVariables,
4945
) {
50-
return false
51-
}
46+
return true
5247
}
5348

54-
return true
49+
return false
5550
}
5651

57-
// Check if the apiVersion and kind are matching.
58-
func matchGVK(apiVersion, kind string, obj runtime.Object) bool {
59-
objAPIVersion, objKind := obj.GetObjectKind().GroupVersionKind().ToAPIVersionAndKind()
60-
// Check if the apiVersion and kind are matching.
61-
if objAPIVersion != apiVersion {
62-
return false
63-
}
64-
if objKind != kind {
65-
return false
66-
}
67-
return true
52+
// MatchesGVK checks if the apiVersion and kind are matching.
53+
func MatchesGVK(apiVersion, kind string, obj runtime.Object) bool {
54+
gvk := obj.GetObjectKind().GroupVersionKind()
55+
56+
return gvk.GroupVersion().String() == apiVersion && gvk.Kind == kind
6857
}
6958

70-
// Check if the request is for an InfrastructureCluster.
71-
func matchInfrastructure(holderRef *runtimehooksv1.HolderReference) bool {
72-
// Cluster.spec.infrastructureRef holds the InfrastructureCluster.
73-
if holderRef.Kind == "Cluster" && holderRef.FieldPath == "spec.infrastructureRef" {
74-
return true
75-
}
76-
return false
59+
// MatchesInfrastructure checks if the request is for an InfrastructureCluster.
60+
// Cluster.spec.infrastructureRef holds the InfrastructureCluster.
61+
func MatchesInfrastructure(holderRef *runtimehooksv1.HolderReference) bool {
62+
return holderRef.Kind == "Cluster" && holderRef.FieldPath == "spec.infrastructureRef"
7763
}
7864

79-
// Check if the request is for a ControlPlane or the InfrastructureMachineTemplate of a ControlPlane.
80-
func matchControlPlane(holderRef *runtimehooksv1.HolderReference) bool {
65+
// MatchesControlPlane checks if the request is for a ControlPlane or the InfrastructureMachineTemplate of a
66+
// ControlPlane.
67+
func MatchesControlPlane(holderRef *runtimehooksv1.HolderReference) bool {
8168
// Cluster.spec.controlPlaneRef holds the ControlPlane.
8269
if holderRef.Kind == "Cluster" && holderRef.FieldPath == "spec.controlPlaneRef" {
8370
return true
@@ -92,9 +79,9 @@ func matchControlPlane(holderRef *runtimehooksv1.HolderReference) bool {
9279
return false
9380
}
9481

95-
// Check if the request is for a BootstrapConfigTemplate or an InfrastructureMachineTemplate
96-
// of one of the configured MachineDeploymentClasses.
97-
func matchMachineDeploymentClass(
82+
// MatchesMachineDeploymentClass checks if the request is for a BootstrapConfigTemplate or an
83+
// InfrastructureMachineTemplate of one of the configured MachineDeploymentClasses.
84+
func MatchesMachineDeploymentClass(
9885
holderRef *runtimehooksv1.HolderReference,
9986
names []string,
10087
templateVariables map[string]apiextensionsv1.JSON,
@@ -130,5 +117,6 @@ func matchMachineDeploymentClass(
130117
}
131118
}
132119
}
120+
133121
return false
134122
}

0 commit comments

Comments
 (0)