Skip to content

Commit

Permalink
unify regexp usage across codebase (gardener#8379)
Browse files Browse the repository at this point in the history
* unify regexp usage across codebase

* address PR feedback
  • Loading branch information
dimityrmirchev authored Aug 22, 2023
1 parent ad64150 commit 086280a
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ func (healthChecker *ManagedResourceHealthChecker) DeepCopy() healthcheck.Health
return &shallowCopy
}

// configurationProblemRegex is used to check if a not healthy managed resource has a configuration problem.
var configurationProblemRegex = regexp.MustCompile(`(?i)(error during apply of object .* is invalid:)`)

// Check executes the health check
func (healthChecker *ManagedResourceHealthChecker) Check(ctx context.Context, request types.NamespacedName) (*healthcheck.SingleCheckResult, error) {
mcmDeployment := &resourcesv1alpha1.ManagedResource{}
Expand All @@ -81,12 +84,8 @@ func (healthChecker *ManagedResourceHealthChecker) Check(ctx context.Context, re
if isHealthy, err := managedResourceIsHealthy(mcmDeployment); !isHealthy {
healthChecker.logger.Error(err, "Health check failed")

var (
errorCodes []gardencorev1beta1.ErrorCode
configurationProblemRegexp = regexp.MustCompile(`(?i)(error during apply of object .* is invalid:)`)
)

if configurationProblemRegexp.MatchString(err.Error()) {
var errorCodes []gardencorev1beta1.ErrorCode
if configurationProblemRegex.MatchString(err.Error()) {
errorCodes = append(errorCodes, gardencorev1beta1.ErrorConfigurationProblem)
}

Expand Down
11 changes: 2 additions & 9 deletions extensions/pkg/controller/worker/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
machinev1alpha1 "github.com/gardener/machine-controller-manager/pkg/apis/machine/v1alpha1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"

extensionscontroller "github.com/gardener/gardener/extensions/pkg/controller"
"github.com/gardener/gardener/extensions/pkg/util"
Expand All @@ -33,13 +32,7 @@ import (
"github.com/gardener/gardener/pkg/utils"
)

var diskSizeRegexp *regexp.Regexp

func init() {
regexp, err := regexp.Compile(`^(\d+)`)
utilruntime.Must(err)
diskSizeRegexp = regexp
}
var diskSizeRegex = regexp.MustCompile(`^(\d+)`)

// MachineDeployment holds information about the name, class, replicas of a MachineDeployment
// managed by the machine-controller-manager.
Expand Down Expand Up @@ -237,7 +230,7 @@ func DistributePositiveIntOrPercent(zoneIndex int32, intOrPercent intstr.IntOrSt
// DiskSize extracts the numerical component of DiskSize strings, i.e. strings like "10Gi" and
// returns it as string, i.e. "10" will be returned.
func DiskSize(size string) (int, error) {
i, err := strconv.Atoi(diskSizeRegexp.FindString(size))
i, err := strconv.Atoi(diskSizeRegex.FindString(size))
if err != nil {
return -1, err
}
Expand Down
10 changes: 6 additions & 4 deletions extensions/pkg/terraformer/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ import (
"strings"
)

var (
regexTerraformError = regexp.MustCompile(`(?:Error): *([\s\S]*)`)
regexUUID = regexp.MustCompile(`(?i)[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}`)
regexMultiNewline = regexp.MustCompile(`\n{2,}`)
)

// findTerraformErrors gets the <output> of a Terraform run and parses it to find the occurred
// errors (which will be returned). If no errors occurred, an empty string will be returned.
func findTerraformErrors(output string) string {
var (
regexTerraformError = regexp.MustCompile(`(?:Error): *([\s\S]*)`)
regexUUID = regexp.MustCompile(`(?i)[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}`)
regexMultiNewline = regexp.MustCompile(`\n{2,}`)

errorMessage = output
valid []string
)
Expand Down
5 changes: 4 additions & 1 deletion extensions/pkg/webhook/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ func AppendUniqueUnit(units *[]extensionsv1alpha1.Unit, unit extensionsv1alpha1.
*units = append(*units, unit)
}

// splitCommandLineRegex is used to split command line arguments by white space or "\".
var splitCommandLineRegex = regexp.MustCompile(`[\\\s]+`)

// DeserializeCommandLine de-serializes the given string to a slice of command line elements by splitting it
// on white space and the "\" character.
func DeserializeCommandLine(s string) []string {
return regexp.MustCompile(`[\\\s]+`).Split(s, -1)
return splitCommandLineRegex.Split(s, -1)
}

// SerializeCommandLine serializes the given command line elements slice to a string by joining the first
Expand Down
8 changes: 5 additions & 3 deletions pkg/apis/core/validation/cloudprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ func ValidateCloudProfileSpec(spec *core.CloudProfileSpec, fldPath *field.Path)
return allErrs
}

// k8sVersionCPRegex is used to validate kubernetes versions in a cloud profile.
var k8sVersionCPRegex = regexp.MustCompile(`^([0-9]+\.){2}[0-9]+$`)

func validateKubernetesSettings(kubernetes core.KubernetesSettings, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if len(kubernetes.Versions) == 0 {
Expand All @@ -100,11 +103,10 @@ func validateKubernetesSettings(kubernetes core.KubernetesSettings, fldPath *fie
}

versionsFound := sets.New[string]()
r, _ := regexp.Compile(`^([0-9]+\.){2}[0-9]+$`)
for i, version := range kubernetes.Versions {
idxPath := fldPath.Child("versions").Index(i)
if !r.MatchString(version.Version) {
allErrs = append(allErrs, field.Invalid(idxPath, version, fmt.Sprintf("all Kubernetes versions must match the regex %s", r)))
if !k8sVersionCPRegex.MatchString(version.Version) {
allErrs = append(allErrs, field.Invalid(idxPath, version, fmt.Sprintf("all Kubernetes versions must match the regex %s", k8sVersionCPRegex)))
} else if versionsFound.Has(version.Version) {
allErrs = append(allErrs, field.Duplicate(idxPath.Child("version"), version.Version))
} else {
Expand Down
5 changes: 3 additions & 2 deletions pkg/apis/core/validation/shoot.go
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,9 @@ const (
maxVolumeNameLength = 15
)

// volumeSizeRegex is used for volume size validation.
var volumeSizeRegex = regexp.MustCompile(`^(\d)+Gi$`)

// ValidateWorker validates the worker object.
func ValidateWorker(worker core.Worker, kubernetes core.Kubernetes, fldPath *field.Path, inTemplate bool) field.ErrorList {
kubernetesVersion := kubernetes.Version
Expand Down Expand Up @@ -1476,8 +1479,6 @@ func ValidateWorker(worker core.Worker, kubernetes core.Kubernetes, fldPath *fie
}
}

volumeSizeRegex, _ := regexp.Compile(`^(\d)+Gi$`)

if worker.Volume != nil {
if !volumeSizeRegex.MatchString(worker.Volume.VolumeSize) {
allErrs = append(allErrs, field.Invalid(fldPath.Child("volume", "size"), worker.Volume.VolumeSize, fmt.Sprintf("volume size must match the regex %s", volumeSizeRegex)))
Expand Down
6 changes: 4 additions & 2 deletions pkg/operation/botanist/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ func (b *Botanist) generateSSHKeypair(ctx context.Context) error {
return nil
}

// quotaExceededRegex is used to check if an error occurred due to infrastructure quota limits.
var quotaExceededRegex = regexp.MustCompile(`(?i)((?:^|[^t]|(?:[^s]|^)t|(?:[^e]|^)st|(?:[^u]|^)est|(?:[^q]|^)uest|(?:[^e]|^)quest|(?:[^r]|^)equest)LimitExceeded|Quotas|Quota.*exceeded|exceeded quota|Quota has been met|QUOTA_EXCEEDED)`)

func (b *Botanist) syncShootCredentialToGarden(
ctx context.Context,
nameSuffix string,
Expand All @@ -303,8 +306,7 @@ func (b *Botanist) syncShootCredentialToGarden(
return nil
})

quotaExceededRegexp := regexp.MustCompile(`(?i)((?:^|[^t]|(?:[^s]|^)t|(?:[^e]|^)st|(?:[^u]|^)est|(?:[^q]|^)uest|(?:[^e]|^)quest|(?:[^r]|^)equest)LimitExceeded|Quotas|Quota.*exceeded|exceeded quota|Quota has been met|QUOTA_EXCEEDED)`)
if err != nil && quotaExceededRegexp.MatchString(err.Error()) {
if err != nil && quotaExceededRegex.MatchString(err.Error()) {
return v1beta1helper.NewErrorWithCodes(err, gardencorev1beta1.ErrorInfraQuotaExceeded)
}
return err
Expand Down
10 changes: 6 additions & 4 deletions pkg/operation/care/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,18 @@ func (b *HealthChecker) checkStatefulSets(condition gardencorev1beta1.Condition,
return nil
}

// kubeletConfigProblemRegex is used to check if an error occurred due to a kubelet configuration problem.
var kubeletConfigProblemRegex = regexp.MustCompile(`(?i)(KubeletHasInsufficientMemory|KubeletHasDiskPressure|KubeletHasInsufficientPID)`)

func (b *HealthChecker) checkNodes(condition gardencorev1beta1.Condition, nodes []corev1.Node, workerGroupName string, workerGroupKubernetesVersion *semver.Version) *gardencorev1beta1.Condition {
for _, object := range nodes {
if err := health.CheckNode(&object); err != nil {
var (
errorCodes []gardencorev1beta1.ErrorCode
message = fmt.Sprintf("Node %q in worker group %q is unhealthy: %v", object.Name, workerGroupName, err)
configurationProblemRegexp = regexp.MustCompile(`(?i)(KubeletHasInsufficientMemory|KubeletHasDiskPressure|KubeletHasInsufficientPID)`)
errorCodes []gardencorev1beta1.ErrorCode
message = fmt.Sprintf("Node %q in worker group %q is unhealthy: %v", object.Name, workerGroupName, err)
)

if configurationProblemRegexp.MatchString(err.Error()) {
if kubeletConfigProblemRegex.MatchString(err.Error()) {
errorCodes = append(errorCodes, gardencorev1beta1.ErrorConfigurationProblem)
}

Expand Down
6 changes: 1 addition & 5 deletions pkg/resourcemanager/controller/networkpolicy/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ import (
kubernetesutils "github.com/gardener/gardener/pkg/utils/kubernetes"
)

var fromPolicyRegexp *regexp.Regexp

func init() {
fromPolicyRegexp = regexp.MustCompile(resourcesv1alpha1.NetworkPolicyFromPolicyAnnotationPrefix + "(.*)" + resourcesv1alpha1.NetworkPolicyFromPolicyAnnotationSuffix)
}
var fromPolicyRegexp = regexp.MustCompile(resourcesv1alpha1.NetworkPolicyFromPolicyAnnotationPrefix + "(.*)" + resourcesv1alpha1.NetworkPolicyFromPolicyAnnotationSuffix)

// Reconciler reconciles Service objects and creates NetworkPolicy objects.
type Reconciler struct {
Expand Down
6 changes: 4 additions & 2 deletions pkg/utils/kubernetes/bootstraptoken/bootstraptoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ import (
"github.com/gardener/gardener/pkg/utils/kubernetes"
)

// validBootstrapTokenRegex is used to check if an existing token can be interpreted as a bootstrap token.
var validBootstrapTokenRegex = regexp.MustCompile(`[a-z0-9]{16}`)

// ComputeBootstrapToken computes and creates a new bootstrap token, and returns it.
func ComputeBootstrapToken(ctx context.Context, c client.Client, tokenID, description string, validity time.Duration) (secret *corev1.Secret, err error) {
var (
Expand All @@ -47,8 +50,7 @@ func ComputeBootstrapToken(ctx context.Context, c client.Client, tokenID, descri
return nil, err
}

validBootstrapTokenSecret, _ := regexp.Compile(`[a-z0-9]{16}`)
if existingSecretToken, ok := secret.Data[bootstraptokenapi.BootstrapTokenSecretKey]; ok && validBootstrapTokenSecret.Match(existingSecretToken) {
if existingSecretToken, ok := secret.Data[bootstraptokenapi.BootstrapTokenSecretKey]; ok && validBootstrapTokenRegex.Match(existingSecretToken) {
bootstrapTokenSecretKey = string(existingSecretToken)
} else {
bootstrapTokenSecretKey, err = utils.GenerateRandomStringFromCharset(16, "0123456789abcdefghijklmnopqrstuvwxyz")
Expand Down
11 changes: 5 additions & 6 deletions pkg/utils/managedresources/managedresources.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,13 +352,12 @@ func RenderChartAndCreate(ctx context.Context, namespace string, name string, se
return Create(ctx, client, namespace, name, nil, secretNameWithPrefix, "", map[string][]byte{chartName: data}, pointer.Bool(false), injectedLabels, &forceOverwriteAnnotations)
}

func checkConfigurationError(err error) []gardencorev1beta1.ErrorCode {
var (
errorCodes []gardencorev1beta1.ErrorCode
configurationProblemRegexp = regexp.MustCompile(`(?i)(error during apply of object .* is invalid:)`)
)
// configurationProblemRegex is used to check if an error is caused by a bad managed resource configuration.
var configurationProblemRegex = regexp.MustCompile(`(?i)(error during apply of object .* is invalid:)`)

if configurationProblemRegexp.MatchString(err.Error()) {
func checkConfigurationError(err error) []gardencorev1beta1.ErrorCode {
var errorCodes []gardencorev1beta1.ErrorCode
if configurationProblemRegex.MatchString(err.Error()) {
errorCodes = append(errorCodes, gardencorev1beta1.ErrorConfigurationProblem)
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/utils/miscellaneous.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,12 @@ func FindFreePort() (int, error) {
return l.Addr().(*net.TCPAddr).Port, nil
}

// emailVefiryRegex is used to verify the validity of an email.
var emailVefiryRegex = regexp.MustCompile(`^[^@]+@(?:[a-zA-Z-0-9]+\.)+[a-zA-Z]{2,}$`)

// TestEmail validates the provided <email> against a regular expression and returns whether it matches.
func TestEmail(email string) bool {
match, _ := regexp.MatchString(`^[^@]+@(?:[a-zA-Z-0-9]+\.)+[a-zA-Z]{2,}$`, email)
return match
return emailVefiryRegex.MatchString(email)
}

// IDForKeyWithOptionalValue returns an identifier for the given key + optional value.
Expand Down
2 changes: 1 addition & 1 deletion test/framework/reporter/esreporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ type GardenerESReporter struct {
index []byte
}

var matchLabel, _ = regexp.Compile(`\\[(.*?)\\]`)
var matchLabel = regexp.MustCompile(`\\[(.*?)\\]`)

// newGardenerESReporter creates a new Gardener elasticsearch reporter.
// Any report will be encoded to json and stored to the passed filename in the given es index.
Expand Down

0 comments on commit 086280a

Please sign in to comment.