Skip to content
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

refactor: Move matchers to own package and add tests #125

Merged
merged 2 commits into from
Aug 30, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// See: https://github.com/kubernetes-sigs/cluster-api/blob/46412f0a4ea65d8f02478d2ad09ce12925485f56/internal/controllers/topology/cluster/patches/inline/json_patch_generator.go#L125
//
//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
package httpproxy
package matchers

import (
"strconv"
Expand All @@ -19,65 +19,52 @@ import (
"sigs.k8s.io/cluster-api/exp/runtime/topologymutation"
)

func matchSelector(
func MatchesSelector(
selector clusterv1.PatchSelector,
obj runtime.Object,
holderRef *runtimehooksv1.HolderReference,
templateVariables map[string]apiextensionsv1.JSON,
) bool {
if !matchGVK(selector.APIVersion, selector.Kind, obj) {
if !MatchesGVK(selector.APIVersion, selector.Kind, obj) {
return false
}

if selector.MatchResources.InfrastructureCluster {
if !matchInfrastructure(holderRef) {
return false
}
if selector.MatchResources.InfrastructureCluster && MatchesInfrastructure(holderRef) {
return true
}

if selector.MatchResources.ControlPlane {
if !matchControlPlane(holderRef) {
return false
}
if selector.MatchResources.ControlPlane && MatchesControlPlane(holderRef) {
return true
}

if selector.MatchResources.MachineDeploymentClass != nil {
if !matchMachineDeploymentClass(
if selector.MatchResources.MachineDeploymentClass != nil &&
MatchesMachineDeploymentClass(
holderRef,
selector.MatchResources.MachineDeploymentClass.Names,
templateVariables,
) {
return false
}
return true
}

return true
return false
}

// Check if the apiVersion and kind are matching.
func matchGVK(apiVersion, kind string, obj runtime.Object) bool {
objAPIVersion, objKind := obj.GetObjectKind().GroupVersionKind().ToAPIVersionAndKind()
// Check if the apiVersion and kind are matching.
if objAPIVersion != apiVersion {
return false
}
if objKind != kind {
return false
}
return true
// MatchesGVK checks if the apiVersion and kind are matching.
func MatchesGVK(apiVersion, kind string, obj runtime.Object) bool {
gvk := obj.GetObjectKind().GroupVersionKind()

return gvk.GroupVersion().String() == apiVersion && gvk.Kind == kind
}

// Check if the request is for an InfrastructureCluster.
func matchInfrastructure(holderRef *runtimehooksv1.HolderReference) bool {
// Cluster.spec.infrastructureRef holds the InfrastructureCluster.
if holderRef.Kind == "Cluster" && holderRef.FieldPath == "spec.infrastructureRef" {
return true
}
return false
// MatchesInfrastructure checks if the request is for an InfrastructureCluster.
// Cluster.spec.infrastructureRef holds the InfrastructureCluster.
func MatchesInfrastructure(holderRef *runtimehooksv1.HolderReference) bool {
return holderRef.Kind == "Cluster" && holderRef.FieldPath == "spec.infrastructureRef"
}

// Check if the request is for a ControlPlane or the InfrastructureMachineTemplate of a ControlPlane.
func matchControlPlane(holderRef *runtimehooksv1.HolderReference) bool {
// MatchesControlPlane checks if the request is for a ControlPlane or the InfrastructureMachineTemplate of a
// ControlPlane.
func MatchesControlPlane(holderRef *runtimehooksv1.HolderReference) bool {
// Cluster.spec.controlPlaneRef holds the ControlPlane.
if holderRef.Kind == "Cluster" && holderRef.FieldPath == "spec.controlPlaneRef" {
return true
Expand All @@ -92,9 +79,9 @@ func matchControlPlane(holderRef *runtimehooksv1.HolderReference) bool {
return false
}

// Check if the request is for a BootstrapConfigTemplate or an InfrastructureMachineTemplate
// of one of the configured MachineDeploymentClasses.
func matchMachineDeploymentClass(
// MatchesMachineDeploymentClass checks if the request is for a BootstrapConfigTemplate or an
// InfrastructureMachineTemplate of one of the configured MachineDeploymentClasses.
func MatchesMachineDeploymentClass(
holderRef *runtimehooksv1.HolderReference,
names []string,
templateVariables map[string]apiextensionsv1.JSON,
Expand Down Expand Up @@ -130,5 +117,6 @@ func matchMachineDeploymentClass(
}
}
}

return false
}
Loading
Loading