Skip to content

Commit

Permalink
Merge branch 'main' into docs/misc-feedback-updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ADubhlaoich authored Jan 31, 2024
2 parents 80b5960 + 81a2233 commit 14d1607
Show file tree
Hide file tree
Showing 5 changed files with 304 additions and 186 deletions.
9 changes: 3 additions & 6 deletions cmd/nginx-ingress/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func main() {
appProtectVersion = getAppProtectVersionInfo()
}

go updateSelfWithVersionInfo(kubeClient, version, nginxVersion.String(), appProtectVersion, 10, time.Second*5)
go updateSelfWithVersionInfo(kubeClient, version, appProtectVersion, nginxVersion, 10, time.Second*5)

templateExecutor, templateExecutorV2 := createTemplateExecutors()

Expand Down Expand Up @@ -789,10 +789,7 @@ func processConfigMaps(kubeClient *kubernetes.Clientset, cfgParams *configs.Conf
return cfgParams
}

func updateSelfWithVersionInfo(kubeClient *kubernetes.Clientset, version, nginxVersion, appProtectVersion string, maxRetries int, waitTime time.Duration) {
nginxVer := strings.TrimSuffix(strings.Split(nginxVersion, "/")[1], "\n")
replacer := strings.NewReplacer(" ", "-", "(", "", ")", "")
nginxVer = replacer.Replace(nginxVer)
func updateSelfWithVersionInfo(kubeClient *kubernetes.Clientset, version, appProtectVersion string, nginxVersion nginx.Version, maxRetries int, waitTime time.Duration) {
podUpdated := false

for i := 0; (i < maxRetries || maxRetries == 0) && !podUpdated; i++ {
Expand All @@ -812,7 +809,7 @@ func updateSelfWithVersionInfo(kubeClient *kubernetes.Clientset, version, nginxV
labels = make(map[string]string)
}

labels[nginxVersionLabel] = nginxVer
labels[nginxVersionLabel] = nginxVersion.Format()
labels[versionLabel] = strings.TrimPrefix(version, "v")
if appProtectVersion != "" {
labels[appProtectVersionLabel] = appProtectVersion
Expand Down
87 changes: 1 addition & 86 deletions internal/nginx/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,10 @@ const (
)

var (
re = regexp.MustCompile(`(?P<name>\S+)/(?P<version>\S+)`)
ossre = regexp.MustCompile(`(?P<name>\S+)/(?P<version>\S+)`)
plusre = regexp.MustCompile(`(?P<name>\S+)/(?P<version>\S+).\((?P<plus>\S+plus\S+)\)`)
)

// Version holds the parsed output from `nginx -v`
type Version struct {
raw string
OSS string
IsPlus bool
Plus string
}

// ServerConfig holds the config data for an upstream server in NGINX Plus.
type ServerConfig struct {
MaxFails int
Expand Down Expand Up @@ -446,83 +438,6 @@ func (lm *LocalManager) CreateOpenTracingTracerConfig(content string) error {
return nil
}

// Return the raw Nginx version string from `nginx -v`
func (v *Version) String() string {
return v.raw
}

// PlusGreaterThanOrEqualTo compares the supplied nginx-plus version string with the Version{} struct
func (v Version) PlusGreaterThanOrEqualTo(target string) (bool, error) {
r, p, err := extractPlusVersionValues(v.String())
if err != nil {
return false, err
}
tr, tp, err := extractPlusVersionValues(target)
if err != nil {
return false, err
}

return (r > tr || (r == tr && p >= tp)), nil
}

// NewVersion will take the output from `nginx -v` and explodes it into the `nginx.Version` struct
func NewVersion(line string) Version {
matches := re.FindStringSubmatch(line)
plusmatches := plusre.FindStringSubmatch(line)
nv := Version{
raw: line,
}

if len(plusmatches) > 0 {
subNames := plusre.SubexpNames()
nv.IsPlus = true
for i, v := range plusmatches {
switch subNames[i] {
case "plus":
nv.Plus = v
case "version":
nv.OSS = v
}
}
}

if len(matches) > 0 {
for i, key := range re.SubexpNames() {
val := matches[i]
if key == "version" {
nv.OSS = val
}
}
}

return nv
}

// extractPlusVersionValues
func extractPlusVersionValues(input string) (int, int, error) {
var rValue, pValue int
re := regexp.MustCompile(`-r(\d+)(?:-p(\d+))?`)
matches := re.FindStringSubmatch(input)

if len(matches) < 2 {
return 0, 0, fmt.Errorf("no matches found in the input string")
}

rValue, err := strconv.Atoi(matches[1])
if err != nil {
return 0, 0, fmt.Errorf("failed to convert rValue to integer: %w", err)
}

if len(matches) > 2 && len(matches[2]) > 0 {
pValue, err = strconv.Atoi(matches[2])
if err != nil {
return 0, 0, fmt.Errorf("failed to convert pValue to integer: %w", err)
}
}

return rValue, pValue, nil
}

// verifyConfigVersion is used to check if the worker process that the API client is connected
// to is using the latest version of nginx config. This way we avoid making changes on
// a worker processes that is being shut down.
Expand Down
94 changes: 0 additions & 94 deletions internal/nginx/manager_test.go

This file was deleted.

101 changes: 101 additions & 0 deletions internal/nginx/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package nginx

import (
"fmt"
"regexp"
"strconv"
)

// Version holds the parsed output from `nginx -v`.
type Version struct {
Raw string
OSS string
IsPlus bool
Plus string
}

// NewVersion takes the output from `nginx -v` and explodes it into the `nginx.Version` struct.
func NewVersion(line string) Version {
matches := ossre.FindStringSubmatch(line)
plusmatches := plusre.FindStringSubmatch(line)
nv := Version{
Raw: line,
}

if len(plusmatches) > 0 {
subNames := plusre.SubexpNames()
nv.IsPlus = true
for i, v := range plusmatches {
switch subNames[i] {
case "plus":
nv.Plus = v
case "version":
nv.OSS = v
}
}
}

if len(matches) > 0 {
for i, key := range ossre.SubexpNames() {
val := matches[i]
if key == "version" {
nv.OSS = val
}
}
}

return nv
}

// String returns the raw Nginx version string from `nginx -v`.
func (v Version) String() string {
return v.Raw
}

// Format returns a string representing Nginx version.
func (v Version) Format() string {
if v.IsPlus {
return fmt.Sprintf("%s-%s", v.OSS, v.Plus)
}
return v.OSS
}

// PlusGreaterThanOrEqualTo compares the supplied nginx-plus version string with the Version{} struct.
func (v Version) PlusGreaterThanOrEqualTo(target string) (bool, error) {
r, p, err := extractPlusVersionValues(v.String())
if err != nil {
return false, err
}
tr, tp, err := extractPlusVersionValues(target)
if err != nil {
return false, err
}

return (r > tr || (r == tr && p >= tp)), nil
}

var rePlus = regexp.MustCompile(`-r(\d+)(?:-p(\d+))?`)

// extractPlusVersionValues
func extractPlusVersionValues(input string) (int, int, error) {
var rValue, pValue int
matches := rePlus.FindStringSubmatch(input)

if len(matches) < 2 {
return 0, 0, fmt.Errorf("no matches found in the input string")
}

rValue, err := strconv.Atoi(matches[1])
if err != nil {
return 0, 0, fmt.Errorf("failed to convert rValue to integer: %w", err)
}

if len(matches) > 2 && len(matches[2]) > 0 {
pValue, err = strconv.Atoi(matches[2])
if err != nil {
return 0, 0, fmt.Errorf("failed to convert pValue to integer: %w", err)
}
}

return rValue, pValue, nil
}
Loading

0 comments on commit 14d1607

Please sign in to comment.