6
6
// See: https://github.com/kubernetes-sigs/cluster-api/blob/46412f0a4ea65d8f02478d2ad09ce12925485f56/internal/controllers/topology/cluster/patches/inline/json_patch_generator.go#L125
7
7
//
8
8
//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
10
10
11
11
import (
12
12
"strconv"
@@ -19,65 +19,52 @@ import (
19
19
"sigs.k8s.io/cluster-api/exp/runtime/topologymutation"
20
20
)
21
21
22
- func matchSelector (
22
+ func MatchesSelector (
23
23
selector clusterv1.PatchSelector ,
24
24
obj runtime.Object ,
25
25
holderRef * runtimehooksv1.HolderReference ,
26
26
templateVariables map [string ]apiextensionsv1.JSON ,
27
27
) bool {
28
- if ! matchGVK (selector .APIVersion , selector .Kind , obj ) {
28
+ if ! MatchesGVK (selector .APIVersion , selector .Kind , obj ) {
29
29
return false
30
30
}
31
31
32
- if selector .MatchResources .InfrastructureCluster {
33
- if ! matchInfrastructure (holderRef ) {
34
- return false
35
- }
32
+ if selector .MatchResources .InfrastructureCluster && MatchesInfrastructure (holderRef ) {
33
+ return true
36
34
}
37
35
38
- if selector .MatchResources .ControlPlane {
39
- if ! matchControlPlane (holderRef ) {
40
- return false
41
- }
36
+ if selector .MatchResources .ControlPlane && MatchesControlPlane (holderRef ) {
37
+ return true
42
38
}
43
39
44
- if selector .MatchResources .MachineDeploymentClass != nil {
45
- if ! matchMachineDeploymentClass (
40
+ if selector .MatchResources .MachineDeploymentClass != nil &&
41
+ MatchesMachineDeploymentClass (
46
42
holderRef ,
47
43
selector .MatchResources .MachineDeploymentClass .Names ,
48
44
templateVariables ,
49
45
) {
50
- return false
51
- }
46
+ return true
52
47
}
53
48
54
- return true
49
+ return false
55
50
}
56
51
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
68
57
}
69
58
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"
77
63
}
78
64
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 {
81
68
// Cluster.spec.controlPlaneRef holds the ControlPlane.
82
69
if holderRef .Kind == "Cluster" && holderRef .FieldPath == "spec.controlPlaneRef" {
83
70
return true
@@ -92,9 +79,9 @@ func matchControlPlane(holderRef *runtimehooksv1.HolderReference) bool {
92
79
return false
93
80
}
94
81
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 (
98
85
holderRef * runtimehooksv1.HolderReference ,
99
86
names []string ,
100
87
templateVariables map [string ]apiextensionsv1.JSON ,
@@ -130,5 +117,6 @@ func matchMachineDeploymentClass(
130
117
}
131
118
}
132
119
}
120
+
133
121
return false
134
122
}
0 commit comments