6
6
"os"
7
7
"regexp"
8
8
9
+ "github.com/telekom/das-schiff-network-operator/pkg/config"
9
10
"github.com/telekom/das-schiff-network-operator/pkg/healthcheck"
10
11
"github.com/telekom/das-schiff-network-operator/pkg/nl"
11
12
)
@@ -33,7 +34,7 @@ type templateConfig struct {
33
34
HostRouterID string
34
35
}
35
36
36
- func (m * Manager ) Configure (in Configuration , nm * nl.Manager ) (bool , error ) {
37
+ func (m * Manager ) Configure (in Configuration , nm * nl.Manager , nwopCfg * config. Config ) (bool , error ) {
37
38
// Remove permit from VRF and only allow deny rules for mgmt VRFs
38
39
for i := range in .VRFs {
39
40
if in .VRFs [i ].Name != m .mgmtVrf {
@@ -50,7 +51,7 @@ func (m *Manager) Configure(in Configuration, nm *nl.Manager) (bool, error) {
50
51
}
51
52
}
52
53
53
- config , err := m .renderSubtemplates (in , nm )
54
+ frrConfig , err := m .renderSubtemplates (in , nm )
54
55
if err != nil {
55
56
return false , err
56
57
}
@@ -60,12 +61,13 @@ func (m *Manager) Configure(in Configuration, nm *nl.Manager) (bool, error) {
60
61
return false , fmt .Errorf ("error reading configuration file: %w" , err )
61
62
}
62
63
63
- targetConfig , err := render (m .configTemplate , config )
64
+ targetConfig , err := render (m .configTemplate , frrConfig )
64
65
if err != nil {
65
66
return false , err
66
67
}
67
68
68
69
targetConfig = fixRouteTargetReload (targetConfig )
70
+ targetConfig = applyCfgReplacements (targetConfig , nwopCfg .Replacements )
69
71
70
72
if ! bytes .Equal (currentConfig , targetConfig ) {
71
73
err = os .WriteFile (m .ConfigPath , targetConfig , frrPermissions )
@@ -154,8 +156,8 @@ func (m *Manager) renderSubtemplates(in Configuration, nlManager *nl.Manager) (*
154
156
155
157
// fixRouteTargetReload is a workaround for FRR's inability to reload route-targets if they are configured in a single line.
156
158
// This function splits such lines into multiple lines, each containing a single route-target.
157
- func fixRouteTargetReload (config []byte ) []byte {
158
- return rtLinesRe .ReplaceAllFunc (config , func (s []byte ) []byte {
159
+ func fixRouteTargetReload (frrConfig []byte ) []byte {
160
+ return rtLinesRe .ReplaceAllFunc (frrConfig , func (s []byte ) []byte {
159
161
parts := rtPartsRe .FindSubmatch (s )
160
162
if parts == nil {
161
163
return s
@@ -172,3 +174,16 @@ func fixRouteTargetReload(config []byte) []byte {
172
174
return []byte (lines [:len (lines )- 1 ])
173
175
})
174
176
}
177
+
178
+ // applyCfgReplacements replaces placeholders in the configuration with the actual values.
179
+ func applyCfgReplacements (frrConfig []byte , replacements []config.Replacement ) []byte {
180
+ for _ , replacement := range replacements {
181
+ if ! replacement .Regex {
182
+ frrConfig = bytes .ReplaceAll (frrConfig , []byte (replacement .Old ), []byte (replacement .New ))
183
+ } else {
184
+ re := regexp .MustCompile (replacement .Old )
185
+ frrConfig = re .ReplaceAll (frrConfig , []byte (replacement .New ))
186
+ }
187
+ }
188
+ return frrConfig
189
+ }
0 commit comments