Skip to content

Commit

Permalink
MEDIUM: write configuration and increase version only when there is a…
Browse files Browse the repository at this point in the history
… change

Also uses a client native option to add a _md5hash comment in the configuration
  • Loading branch information
hdurand0710 committed Feb 7, 2025
1 parent 7695a38 commit fee73d8
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 45 deletions.
1 change: 0 additions & 1 deletion examples/test-informers/replay-delete.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/bin/bash

curl -X DELETE -H "Content-Type: application/yaml" --data-binary @configmap.yaml http://localhost:8081
curl -X DELETE -H "Content-Type: application/yaml" --data-binary @echo-app.yaml http://localhost:8081
curl -X DELETE -H "Content-Type: application/yaml" --data-binary @echo.endpointslices.yaml http://localhost:8081
50 changes: 41 additions & 9 deletions pkg/haproxy/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package api

import (
"context"
//nolint:gosec
"crypto/md5" // G501: Blocklisted import crypto/md5: weak cryptographic primitive
"encoding/hex"
"encoding/json"

clientnative "github.com/haproxytech/client-native/v5"
Expand Down Expand Up @@ -133,11 +136,11 @@ type Backend struct { // use same names as in client native v6
}

type clientNative struct {
nativeAPI clientnative.HAProxyClient
activeTransaction string
activeTransactionHasChanges bool
backends map[string]Backend
previousBackends []byte
nativeAPI clientnative.HAProxyClient
activeTransaction string
backends map[string]Backend
previousBackends []byte
configurationHashAtTransactionStart string
}

func New(transactionDir, configFile, programPath, runtimeSocket string) (client HAProxyClient, err error) { //nolint:ireturn
Expand All @@ -155,6 +158,7 @@ func New(transactionDir, configFile, programPath, runtimeSocket string) (client
cfgoptions.ConfigurationFile(configFile),
cfgoptions.HAProxyBin(programPath),
cfgoptions.UseModelsValidation,
cfgoptions.UseMd5Hash,
cfgoptions.TransactionsDir(transactionDir),
)
if err != nil {
Expand Down Expand Up @@ -193,16 +197,40 @@ func (c *clientNative) APIStartTransaction() error {
}
logger.WithField(utils.LogFieldTransactionID, transaction.ID)
c.activeTransaction = transaction.ID
c.activeTransactionHasChanges = false

hash, err := c.computeConfigurationHash(configuration)
if err != nil {
return err
}
c.configurationHashAtTransactionStart = hash

return nil
}

func (c *clientNative) computeConfigurationHash(configuration configuration.Configuration) (string, error) {
p, err := configuration.GetParser(c.activeTransaction)
if err != nil {
return "", err
}
// Note that p.String() does not include the hash!!!
content := p.String()
//nolint: gosec
hash := md5.Sum([]byte(content))
return hex.EncodeToString(hash[:]), err
}

func (c *clientNative) APICommitTransaction() error {
configuration, err := c.nativeAPI.Configuration()
if err != nil {
return err
}
if !c.activeTransactionHasChanges {

hash, err := c.computeConfigurationHash(configuration)
if err != nil {
return err
}

if c.configurationHashAtTransactionStart == hash {
if errDel := configuration.DeleteTransaction(c.activeTransaction); errDel != nil {
return errDel
}
Expand Down Expand Up @@ -235,7 +263,12 @@ func (c *clientNative) APIFinalCommitTransaction() error {
c.backends[backendName] = backend
}

if !c.activeTransactionHasChanges {
hash, err := c.computeConfigurationHash(configuration)
if err != nil {
return err
}

if c.configurationHashAtTransactionStart == hash {
if errDel := configuration.DeleteTransaction(c.activeTransaction); errDel != nil {
errs.Add(errDel)
}
Expand All @@ -249,7 +282,6 @@ func (c *clientNative) APIFinalCommitTransaction() error {
func (c *clientNative) APIDisposeTransaction() {
logger.ResetFields()
c.activeTransaction = ""
c.activeTransactionHasChanges = false
}

func (c *clientNative) SetAuxCfgFile(auxCfgFile string) {
Expand Down
5 changes: 0 additions & 5 deletions pkg/haproxy/api/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ func (c *clientNative) BackendCreateOrUpdate(backend models.Backend) (diff map[s

diff = oldBackend.BackendBase.Diff(backend)

c.activeTransactionHasChanges = len(diff) > 0

oldBackend.BackendBase = backend
oldBackend.Used = true
c.backends[backend.Name] = oldBackend
Expand All @@ -83,7 +81,6 @@ func (c *clientNative) BackendCfgSnippetSet(backendName string, value []string)
return fmt.Errorf("backend %s : %w", backendName, ErrNotFound)
}

c.activeTransactionHasChanges = slices.Compare(backend.ConfigSnippets, value) != 0
backend.ConfigSnippets = value
c.backends[backendName] = backend
return nil
Expand Down Expand Up @@ -117,7 +114,6 @@ func (c *clientNative) BackendRuleDeleteAll(backend string) {
logger.Error(err)
return
}
c.activeTransactionHasChanges = true

// Currently we are only using HTTPRequest rules on backend
err = configuration.DeleteHTTPRequestRule(0, "backend", backend, c.activeTransaction, 0)
Expand Down Expand Up @@ -253,7 +249,6 @@ func (c *clientNative) BackendDeleteAllUnnecessary() ([]string, error) {
return nil, err
}

c.activeTransactionHasChanges = true
var errs utils.Errors
var backendDeleted []string //nolint:prealloc
for _, backend := range c.backends {
Expand Down
2 changes: 0 additions & 2 deletions pkg/haproxy/api/backend_switching_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ func (c *clientNative) BackendSwitchingRuleCreate(frontend string, rule models.B
if err != nil {
return err
}
c.activeTransactionHasChanges = true
return configuration.CreateBackendSwitchingRule(frontend, &rule, c.activeTransaction, 0)
}

Expand All @@ -16,7 +15,6 @@ func (c *clientNative) BackendSwitchingRuleDeleteAll(frontend string) (err error
if err != nil {
return
}
c.activeTransactionHasChanges = true
_, switchingRules, err := configuration.GetBackendSwitchingRules(frontend, c.activeTransaction)
if err != nil {
return
Expand Down
2 changes: 0 additions & 2 deletions pkg/haproxy/api/capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ func (c *clientNative) CaptureCreate(frontend string, rule models.Capture) error
if err != nil {
return err
}
c.activeTransactionHasChanges = true
return configuration.CreateDeclareCapture(frontend, &rule, c.activeTransaction, 0)
}

Expand All @@ -16,7 +15,6 @@ func (c *clientNative) CaptureDeleteAll(frontend string) (err error) {
if err != nil {
return
}
c.activeTransactionHasChanges = true
_, rules, err := configuration.GetDeclareCaptures(frontend, c.activeTransaction)
if err != nil {
return
Expand Down
2 changes: 0 additions & 2 deletions pkg/haproxy/api/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ func (c *clientNative) FilterCreate(parentType, parentName string, rule models.F
if err != nil {
return err
}
c.activeTransactionHasChanges = true
return configuration.CreateFilter(parentType, parentName, &rule, c.activeTransaction, 0)
}

Expand All @@ -16,7 +15,6 @@ func (c *clientNative) FilterDeleteAll(parentType, parentName string) (err error
if err != nil {
return
}
c.activeTransactionHasChanges = true
_, rules, err := configuration.GetFilters(parentType, parentName, c.activeTransaction)
if err != nil {
return
Expand Down
15 changes: 0 additions & 15 deletions pkg/haproxy/api/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ func (c *clientNative) FrontendCfgSnippetSet(frontendName string, value []string
} else {
err = config.Set("frontend", frontendName, "config-snippet", types.StringSliceC{Value: value})
}
if err != nil {
c.activeTransactionHasChanges = true
}
return err
}

Expand All @@ -33,7 +30,6 @@ func (c *clientNative) FrontendCreate(frontend models.Frontend) error {
if err != nil {
return err
}
c.activeTransactionHasChanges = true
return configuration.CreateFrontend(&frontend, c.activeTransaction, 0)
}

Expand All @@ -42,7 +38,6 @@ func (c *clientNative) FrontendDelete(frontendName string) error {
if err != nil {
return err
}
c.activeTransactionHasChanges = true
return configuration.DeleteFrontend(frontendName, c.activeTransaction, 0)
}

Expand Down Expand Up @@ -72,7 +67,6 @@ func (c *clientNative) FrontendEdit(frontend models.Frontend) error {
if err != nil {
return err
}
c.activeTransactionHasChanges = true
return configuration.EditFrontend(frontend.Name, &frontend, c.activeTransaction, 0)
}

Expand Down Expand Up @@ -143,7 +137,6 @@ func (c *clientNative) FrontendBindCreate(frontend string, bind models.Bind) err
if err != nil {
return err
}
c.activeTransactionHasChanges = true
return configuration.CreateBind("frontend", frontend, &bind, c.activeTransaction, 0)
}

Expand All @@ -152,7 +145,6 @@ func (c *clientNative) FrontendBindEdit(frontend string, bind models.Bind) error
if err != nil {
return err
}
c.activeTransactionHasChanges = true
return configuration.EditBind(bind.Name, "frontend", frontend, &bind, c.activeTransaction, 0)
}

Expand All @@ -161,7 +153,6 @@ func (c *clientNative) FrontendBindDelete(frontend string, bind string) error {
if err != nil {
return err
}
c.activeTransactionHasChanges = true
return configuration.DeleteBind(bind, "frontend", frontend, c.activeTransaction, 0)
}

Expand All @@ -170,7 +161,6 @@ func (c *clientNative) FrontendHTTPRequestRuleCreate(frontend string, rule model
if err != nil {
return err
}
c.activeTransactionHasChanges = true
if ingressACL != "" {
rule.Cond = "if"
rule.CondTest = fmt.Sprintf("%s %s", ingressACL, rule.CondTest)
Expand All @@ -183,7 +173,6 @@ func (c *clientNative) FrontendHTTPResponseRuleCreate(frontend string, rule mode
if err != nil {
return err
}
c.activeTransactionHasChanges = true
if ingressACL != "" {
rule.Cond = "if"
rule.CondTest = fmt.Sprintf("%s %s", ingressACL, rule.CondTest)
Expand All @@ -196,7 +185,6 @@ func (c *clientNative) FrontendTCPRequestRuleCreate(frontend string, rule models
if err != nil {
return err
}
c.activeTransactionHasChanges = true
if ingressACL != "" {
rule.Cond = "if"
rule.CondTest = fmt.Sprintf("%s %s", ingressACL, rule.CondTest)
Expand All @@ -211,7 +199,6 @@ func (c *clientNative) FrontendRuleDeleteAll(frontend string) {
logger.Error(err)
return
}
c.activeTransactionHasChanges = true

for {
err := configuration.DeleteHTTPRequestRule(0, "frontend", frontend, c.activeTransaction, 0)
Expand Down Expand Up @@ -239,7 +226,6 @@ func (c *clientNative) PeerEntryEdit(peerSection string, peerEntry models.PeerEn
if err != nil {
return err
}
c.activeTransactionHasChanges = true
return configuration.EditPeerEntry(peerEntry.Name, peerSection, &peerEntry, c.activeTransaction, 0)
}

Expand All @@ -248,7 +234,6 @@ func (c *clientNative) PeerEntryCreateOrEdit(peerSection string, peerEntry model
if err != nil {
return err
}
c.activeTransactionHasChanges = true
err = configuration.EditPeerEntry(peerEntry.Name, peerSection, &peerEntry, c.activeTransaction, 0)
if err != nil {
err = configuration.CreatePeerEntry(peerSection, &peerEntry, c.activeTransaction, 0)
Expand Down
2 changes: 0 additions & 2 deletions pkg/haproxy/api/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ func (c *clientNative) GlobalGetLogTargets() (lg models.LogTargets, err error) {
if err != nil {
return nil, err
}
c.activeTransactionHasChanges = true
_, lg, err = configuration.GetLogTargets("global", parser.GlobalSectionName, c.activeTransaction)
if err != nil {
return lg, fmt.Errorf("unable to get HAProxy's global log targets: %w", err)
Expand All @@ -91,7 +90,6 @@ func (c *clientNative) GlobalPushLogTargets(logTargets models.LogTargets) error
if err != nil {
return err
}
c.activeTransactionHasChanges = true
for {
err = configuration.DeleteLogTarget(0, "global", parser.GlobalSectionName, c.activeTransaction, 0)
if err != nil {
Expand Down
2 changes: 0 additions & 2 deletions pkg/haproxy/api/log_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ func (c *clientNative) LogTargetCreate(parentType, parentName string, rule model
if err != nil {
return err
}
c.activeTransactionHasChanges = true
return configuration.CreateLogTarget(parentType, parentName, &rule, c.activeTransaction, 0)
}

Expand All @@ -16,7 +15,6 @@ func (c *clientNative) LogTargetDeleteAll(parentType, parentName string) (err er
if err != nil {
return
}
c.activeTransactionHasChanges = true
_, rules, err := configuration.GetLogTargets(parentType, parentName, c.activeTransaction)
if err != nil {
return
Expand Down
2 changes: 0 additions & 2 deletions pkg/haproxy/api/tcp_request_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ func (c *clientNative) TCPRequestRuleCreate(parentType, parentName string, rule
if err != nil {
return err
}
c.activeTransactionHasChanges = true
return configuration.CreateTCPRequestRule(parentType, parentName, &rule, c.activeTransaction, 0)
}

Expand All @@ -16,7 +15,6 @@ func (c *clientNative) TCPRequestRuleDeleteAll(parentType, parentName string) (e
if err != nil {
return
}
c.activeTransactionHasChanges = true
_, rules, err := configuration.GetTCPRequestRules(parentType, parentName, c.activeTransaction)
if err != nil {
return
Expand Down
3 changes: 0 additions & 3 deletions pkg/haproxy/api/userlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ func (c *clientNative) UserListExistsByGroup(group string) (exist bool, err erro
if err != nil {
return false, err
}
c.activeTransactionHasChanges = true

var p parser.Parser
var sections []string
Expand All @@ -32,7 +31,6 @@ func (c *clientNative) UserListDeleteAll() (err error) {
if err != nil {
return err
}
c.activeTransactionHasChanges = true

var p parser.Parser
if p, err = configuration.GetParser(c.activeTransaction); err != nil {
Expand All @@ -58,7 +56,6 @@ func (c *clientNative) UserListCreateByGroup(group string, userPasswordMap map[s
if err != nil {
return err
}
c.activeTransactionHasChanges = true

var p parser.Parser
if p, err = configuration.GetParser(c.activeTransaction); err != nil {
Expand Down

0 comments on commit fee73d8

Please sign in to comment.