Skip to content

Commit 74f95be

Browse files
Merge pull request #235 from omersch381/add_pools_yaml_generation
Add pools.yaml generator
2 parents 2183db2 + 184d58c commit 74f95be

File tree

10 files changed

+310
-26
lines changed

10 files changed

+310
-26
lines changed

controllers/designate_controller.go

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ func (r *DesignateReconciler) reconcileInit(
378378
// create hash over all the different input resources to identify if any those changed
379379
// and a restart/recreate is required.
380380
//
381-
_, hashChanged, err := r.createHashOfInputHashes(ctx, instance, configMapVars)
381+
_, hashChanged, err := r.createHashOfInputHashes(ctx, instance, common.InputHashName, configMapVars, nil)
382382
if err != nil {
383383
return ctrl.Result{}, err
384384
} else if hashChanged {
@@ -742,6 +742,12 @@ func (r *DesignateReconciler) reconcileNormal(ctx context.Context, instance *des
742742
return ctrl.Result{}, err
743743
}
744744

745+
nsRecordsLabels := labels.GetLabels(instance, labels.GetGroupLabel(instance.ObjectMeta.Name), map[string]string{})
746+
nsRecordsConfigMap, err := r.handleConfigMap(ctx, helper, instance, designate.NsRecordsConfigMap, nsRecordsLabels)
747+
if err != nil {
748+
return ctrl.Result{}, err
749+
}
750+
745751
allocatedIPs := make(map[string]bool)
746752
for _, predIP := range bindConfigMap.Data {
747753
allocatedIPs[predIP] = true
@@ -807,6 +813,49 @@ func (r *DesignateReconciler) reconcileNormal(ctx context.Context, instance *des
807813
return ctrl.Result{}, err
808814
}
809815

816+
if err != nil {
817+
return ctrl.Result{}, err
818+
}
819+
if len(nsRecordsConfigMap.Data) > 0 {
820+
poolsYamlConfigMap := &corev1.ConfigMap{
821+
ObjectMeta: metav1.ObjectMeta{
822+
Name: designate.PoolsYamlsConfigMap,
823+
Namespace: instance.GetNamespace(),
824+
Labels: bindLabels,
825+
},
826+
Data: make(map[string]string),
827+
}
828+
poolsYaml, err := designate.GeneratePoolsYamlData(bindConfigMap.Data, mdnsConfigMap.Data, nsRecordsConfigMap.Data)
829+
if err != nil {
830+
return ctrl.Result{}, err
831+
}
832+
Log.Info(fmt.Sprintf("pools.yaml content is\n%v", poolsYaml))
833+
updatedPoolsYaml := make(map[string]string)
834+
updatedPoolsYaml[designate.PoolsYamlsConfigMap] = poolsYaml
835+
836+
_, err = controllerutil.CreateOrPatch(ctx, helper.GetClient(), poolsYamlConfigMap, func() error {
837+
poolsYamlConfigMap.Labels = util.MergeStringMaps(poolsYamlConfigMap.Labels, bindLabels)
838+
poolsYamlConfigMap.Data = updatedPoolsYaml
839+
return controllerutil.SetControllerReference(instance, poolsYamlConfigMap, helper.GetScheme())
840+
})
841+
if err != nil {
842+
Log.Info("Unable to create config map for pools.yaml file")
843+
return ctrl.Result{}, err
844+
}
845+
configMaps := []interface{}{
846+
poolsYamlConfigMap.Data,
847+
}
848+
849+
poolsYamlsEnvVars := make(map[string]env.Setter)
850+
_, changed, err := r.createHashOfInputHashes(ctx, instance, designate.PoolsYamlHash, poolsYamlsEnvVars, configMaps)
851+
if err != nil {
852+
return ctrl.Result{}, err
853+
} else if changed {
854+
// launch the pool update job
855+
Log.Info("Creating a pool update job")
856+
}
857+
}
858+
810859
// deploy designate-central
811860
designateCentral, op, err := r.centralDeploymentCreateOrUpdate(ctx, instance)
812861
if err != nil {
@@ -1087,7 +1136,7 @@ func (r *DesignateReconciler) handleConfigMap(ctx context.Context, helper *helpe
10871136
err := helper.GetClient().Get(ctx, types.NamespacedName{Name: configMapName, Namespace: instance.GetNamespace()}, foundMap)
10881137
if err != nil {
10891138
if k8s_errors.IsNotFound(err) {
1090-
Log.Info(fmt.Sprintf("Ip map %s doesn't exist, creating.", configMapName))
1139+
Log.Info(fmt.Sprintf("configmap %s doesn't exist, creating.", configMapName))
10911140
} else {
10921141
return nil, err
10931142
}
@@ -1346,23 +1395,45 @@ func (r *DesignateReconciler) generateServiceConfigMaps(
13461395
func (r *DesignateReconciler) createHashOfInputHashes(
13471396
ctx context.Context,
13481397
instance *designatev1beta1.Designate,
1398+
hashType string,
13491399
envVars map[string]env.Setter,
1400+
additionalConfigmaps []interface{},
13501401
) (string, bool, error) {
13511402
Log := r.GetLogger(ctx)
13521403

13531404
var hashMap map[string]string
13541405
changed := false
13551406
mergedMapVars := env.MergeEnvs([]corev1.EnvVar{}, envVars)
1356-
hash, err := util.ObjectHash(mergedMapVars)
1407+
combinedHashes := []string{}
1408+
1409+
envHash, err := util.ObjectHash(mergedMapVars)
13571410
if err != nil {
13581411
Log.Info("XXX - Error creating hash")
1359-
return hash, changed, err
1412+
return "", changed, err
1413+
}
1414+
combinedHashes = append(combinedHashes, envHash)
1415+
1416+
for _, configMap := range additionalConfigmaps {
1417+
configMapHash, err := util.ObjectHash(configMap)
1418+
if err != nil {
1419+
Log.Info(fmt.Sprintf("Error creating hash for %v", configMap))
1420+
return "", changed, err
1421+
}
1422+
combinedHashes = append(combinedHashes, configMapHash)
1423+
}
1424+
1425+
finalHash, err := util.ObjectHash(combinedHashes)
1426+
if err != nil {
1427+
Log.Info("Error creating final hash")
1428+
return "", changed, err
13601429
}
1361-
if hashMap, changed = util.SetHash(instance.Status.Hash, common.InputHashName, hash); changed {
1430+
1431+
if hashMap, changed = util.SetHash(instance.Status.Hash, hashType, finalHash); changed {
13621432
instance.Status.Hash = hashMap
1363-
Log.Info(fmt.Sprintf("Input maps hash %s - %s", common.InputHashName, hash))
1433+
Log.Info(fmt.Sprintf("Input maps hash %s - %s", hashType, finalHash))
13641434
}
1365-
return hash, changed, nil
1435+
1436+
return finalHash, changed, nil
13661437
}
13671438

13681439
func (r *DesignateReconciler) transportURLCreateOrUpdate(

controllers/designatemdns_controller.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import (
4343
designatemdns "github.com/openstack-k8s-operators/designate-operator/pkg/designatemdns"
4444
"github.com/openstack-k8s-operators/lib-common/modules/common"
4545
"github.com/openstack-k8s-operators/lib-common/modules/common/condition"
46-
"github.com/openstack-k8s-operators/lib-common/modules/common/configmap"
4746
"github.com/openstack-k8s-operators/lib-common/modules/common/daemonset"
4847
"github.com/openstack-k8s-operators/lib-common/modules/common/env"
4948
"github.com/openstack-k8s-operators/lib-common/modules/common/helper"
@@ -496,7 +495,7 @@ func (r *DesignateMdnsReconciler) reconcileNormal(ctx context.Context, instance
496495
// create hash over all the different input resources to identify if any those changed
497496
// and a restart/recreate is required.
498497
//
499-
inputHash, hashChanged, err := r.createHashOfInputHashes(ctx, helper, instance, configMapVars)
498+
inputHash, hashChanged, err := r.createHashOfInputHashes(ctx, instance, configMapVars)
500499
if err != nil {
501500
instance.Status.Conditions.Set(condition.FalseCondition(
502501
condition.ServiceConfigReadyCondition,
@@ -810,7 +809,6 @@ func (r *DesignateMdnsReconciler) generateServiceConfigMaps(
810809
// returns the hash, whether the hash changed (as a bool) and any error
811810
func (r *DesignateMdnsReconciler) createHashOfInputHashes(
812811
ctx context.Context,
813-
h *helper.Helper,
814812
instance *designatev1beta1.DesignateMdns,
815813
envVars map[string]env.Setter,
816814
) (string, bool, error) {
@@ -819,21 +817,6 @@ func (r *DesignateMdnsReconciler) createHashOfInputHashes(
819817
var hashMap map[string]string
820818
changed := false
821819

822-
// If MdnsPredIPConfigMap exists, add its hash to status hash
823-
mdnsPredIPCM := &corev1.ConfigMap{}
824-
err := h.GetClient().Get(ctx, types.NamespacedName{
825-
Name: designate.MdnsPredIPConfigMap,
826-
Namespace: instance.Namespace,
827-
}, mdnsPredIPCM)
828-
if err != nil {
829-
Log.Error(err, "Unable to retrieve Mdns predictable IPs ConfigMap")
830-
return "", false, err
831-
}
832-
mdnsPredIPCMHash, err := configmap.Hash(mdnsPredIPCM)
833-
if err != nil {
834-
return mdnsPredIPCMHash, changed, err
835-
}
836-
837820
mergedMapVars := env.MergeEnvs([]corev1.EnvVar{}, envVars)
838821
hash, err := util.ObjectHash(mergedMapVars)
839822
if err != nil {

controllers/designateworker_controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,7 @@ func (r *DesignateWorkerReconciler) createHashOfInputHashes(
833833
if err != nil {
834834
return hash, changed, err
835835
}
836+
836837
if hashMap, changed = util.SetHash(instance.Status.Hash, common.InputHashName, hash); changed {
837838
instance.Status.Hash = hashMap
838839
Log.Info(fmt.Sprintf("Input maps hash %s - %s", common.InputHashName, hash))
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
# Note, if you change this name, please change the name on pkg/designate/const.go file as well
5+
name: designate-ns-records-params
6+
data:
7+
ns_records: |
8+
- host: host1
9+
priority: 1
10+
- host: host2
11+
priority: 2

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
github.com/openstack-k8s-operators/keystone-operator/api v0.4.1-0.20241013092400-3f9337945472
1313
github.com/openstack-k8s-operators/lib-common/modules/common v0.5.1-0.20241029151503-4878b3fa3333
1414
github.com/openstack-k8s-operators/mariadb-operator/api v0.4.1-0.20241015090956-b0954ab72dcd
15+
gopkg.in/yaml.v2 v2.4.0
1516
k8s.io/api v0.29.10
1617
k8s.io/apimachinery v0.29.10
1718
k8s.io/client-go v0.29.10
@@ -70,7 +71,6 @@ require (
7071
google.golang.org/appengine v1.6.8 // indirect
7172
google.golang.org/protobuf v1.34.1 // indirect
7273
gopkg.in/inf.v0 v0.9.1 // indirect
73-
gopkg.in/yaml.v2 v2.4.0 // indirect
7474
gopkg.in/yaml.v3 v3.0.1 // indirect
7575
k8s.io/apiextensions-apiserver v0.29.10 // indirect
7676
k8s.io/component-base v0.29.10 // indirect

pkg/designate/const.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,15 @@ const (
4141

4242
MdnsPredIPConfigMap = "designate-mdns-ip-map"
4343

44+
NsRecordsConfigMap = "designate-ns-records-params"
45+
4446
BindPredIPConfigMap = "designate-bind-ip-map"
47+
48+
RndcConfDir = "/etc/designate/rndc-keys"
49+
50+
PoolsYamlsConfigMap = "designate-pools-yaml-config-map"
51+
52+
PoolsYamlPath = "templates/designatepoolmanager/config/pools.yaml.tmpl"
53+
54+
PoolsYamlHash = "pools-yaml-hash"
4555
)
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
Licensed under the Apache License, Version 2.0 (the "License");
3+
you may not use this file except in compliance with the License.
4+
You may obtain a copy of the License at
5+
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
*/
14+
package designate
15+
16+
import (
17+
"bytes"
18+
"fmt"
19+
"gopkg.in/yaml.v2"
20+
"os"
21+
"text/template"
22+
)
23+
24+
type Pool struct {
25+
Name string
26+
Description string
27+
Attributes map[string]string
28+
NSRecords []NSRecord
29+
Nameservers []Nameserver
30+
Targets []Target
31+
CatalogZone *CatalogZone // it is a pointer because it is optional
32+
}
33+
34+
type NSRecord struct {
35+
Hostname string
36+
Priority int
37+
}
38+
39+
type Nameserver struct {
40+
Host string
41+
Port int
42+
}
43+
44+
type Target struct {
45+
Type string
46+
Description string
47+
Masters []Master
48+
Options Options
49+
}
50+
51+
type Master struct {
52+
Host string
53+
Port int
54+
}
55+
56+
type Options struct {
57+
Host string
58+
Port int
59+
RNDCHost string
60+
RNDCPort int
61+
RNDCConfigFile string
62+
}
63+
64+
type CatalogZone struct {
65+
FQDN string
66+
Refresh string
67+
}
68+
69+
func GeneratePoolsYamlData(BindMap, MdnsMap, NsRecordsMap map[string]string) (string, error) {
70+
// Create ns_records
71+
nsRecords := []NSRecord{}
72+
for _, data := range NsRecordsMap {
73+
err := yaml.Unmarshal([]byte(data), &nsRecords)
74+
if err != nil {
75+
return "", fmt.Errorf("error unmarshalling yaml: %w", err)
76+
}
77+
}
78+
79+
// Create targets and nameservers
80+
nameservers := []Nameserver{}
81+
targets := []Target{}
82+
rndcKeyNum := 1
83+
84+
for _, bindIP := range BindMap {
85+
nameservers = append(nameservers, Nameserver{
86+
Host: bindIP,
87+
Port: 53,
88+
})
89+
90+
masters := []Master{}
91+
for _, masterHost := range MdnsMap {
92+
masters = append(masters, Master{
93+
Host: masterHost,
94+
Port: 5354,
95+
})
96+
}
97+
98+
target := Target{
99+
Type: "bind9",
100+
Description: fmt.Sprintf("BIND9 Server %d (%s)", rndcKeyNum, bindIP),
101+
Masters: masters,
102+
Options: Options{
103+
Host: bindIP,
104+
Port: 53,
105+
RNDCHost: bindIP,
106+
RNDCPort: 953,
107+
RNDCConfigFile: fmt.Sprintf("%s/%s-%d.conf", RndcConfDir, DesignateRndcKey, rndcKeyNum),
108+
},
109+
}
110+
targets = append(targets, target)
111+
rndcKeyNum++
112+
}
113+
114+
// Catalog zone is an optional section
115+
// catalogZone := &CatalogZone{
116+
// FQDN: "example.org.",
117+
// Refresh: "60",
118+
// }
119+
defaultAttributes := make(map[string]string)
120+
// Create the Pool struct with the dynamic values
121+
pool := Pool{
122+
Name: "default",
123+
Description: "Default BIND Pool",
124+
Attributes: defaultAttributes,
125+
NSRecords: nsRecords,
126+
Nameservers: nameservers,
127+
Targets: targets,
128+
CatalogZone: nil, // set to catalogZone if this section should be presented
129+
}
130+
131+
PoolsYaml, err := os.ReadFile(PoolsYamlPath)
132+
if err != nil {
133+
return "", err
134+
}
135+
tmpl, err := template.New("pool").Parse(string(PoolsYaml))
136+
if err != nil {
137+
return "", err
138+
}
139+
140+
var buf bytes.Buffer
141+
err = tmpl.Execute(&buf, pool)
142+
if err != nil {
143+
return "", err
144+
}
145+
146+
return buf.String(), nil
147+
}

0 commit comments

Comments
 (0)