Skip to content

Commit

Permalink
Merge pull request #235 from omersch381/add_pools_yaml_generation
Browse files Browse the repository at this point in the history
Add pools.yaml generator
  • Loading branch information
openshift-merge-bot[bot] authored Oct 30, 2024
2 parents 2183db2 + 184d58c commit 74f95be
Show file tree
Hide file tree
Showing 10 changed files with 310 additions and 26 deletions.
85 changes: 78 additions & 7 deletions controllers/designate_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ func (r *DesignateReconciler) reconcileInit(
// create hash over all the different input resources to identify if any those changed
// and a restart/recreate is required.
//
_, hashChanged, err := r.createHashOfInputHashes(ctx, instance, configMapVars)
_, hashChanged, err := r.createHashOfInputHashes(ctx, instance, common.InputHashName, configMapVars, nil)
if err != nil {
return ctrl.Result{}, err
} else if hashChanged {
Expand Down Expand Up @@ -742,6 +742,12 @@ func (r *DesignateReconciler) reconcileNormal(ctx context.Context, instance *des
return ctrl.Result{}, err
}

nsRecordsLabels := labels.GetLabels(instance, labels.GetGroupLabel(instance.ObjectMeta.Name), map[string]string{})
nsRecordsConfigMap, err := r.handleConfigMap(ctx, helper, instance, designate.NsRecordsConfigMap, nsRecordsLabels)
if err != nil {
return ctrl.Result{}, err
}

allocatedIPs := make(map[string]bool)
for _, predIP := range bindConfigMap.Data {
allocatedIPs[predIP] = true
Expand Down Expand Up @@ -807,6 +813,49 @@ func (r *DesignateReconciler) reconcileNormal(ctx context.Context, instance *des
return ctrl.Result{}, err
}

if err != nil {
return ctrl.Result{}, err
}
if len(nsRecordsConfigMap.Data) > 0 {
poolsYamlConfigMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: designate.PoolsYamlsConfigMap,
Namespace: instance.GetNamespace(),
Labels: bindLabels,
},
Data: make(map[string]string),
}
poolsYaml, err := designate.GeneratePoolsYamlData(bindConfigMap.Data, mdnsConfigMap.Data, nsRecordsConfigMap.Data)
if err != nil {
return ctrl.Result{}, err
}
Log.Info(fmt.Sprintf("pools.yaml content is\n%v", poolsYaml))
updatedPoolsYaml := make(map[string]string)
updatedPoolsYaml[designate.PoolsYamlsConfigMap] = poolsYaml

_, err = controllerutil.CreateOrPatch(ctx, helper.GetClient(), poolsYamlConfigMap, func() error {
poolsYamlConfigMap.Labels = util.MergeStringMaps(poolsYamlConfigMap.Labels, bindLabels)
poolsYamlConfigMap.Data = updatedPoolsYaml
return controllerutil.SetControllerReference(instance, poolsYamlConfigMap, helper.GetScheme())
})
if err != nil {
Log.Info("Unable to create config map for pools.yaml file")
return ctrl.Result{}, err
}
configMaps := []interface{}{
poolsYamlConfigMap.Data,
}

poolsYamlsEnvVars := make(map[string]env.Setter)
_, changed, err := r.createHashOfInputHashes(ctx, instance, designate.PoolsYamlHash, poolsYamlsEnvVars, configMaps)
if err != nil {
return ctrl.Result{}, err
} else if changed {
// launch the pool update job
Log.Info("Creating a pool update job")
}
}

// deploy designate-central
designateCentral, op, err := r.centralDeploymentCreateOrUpdate(ctx, instance)
if err != nil {
Expand Down Expand Up @@ -1087,7 +1136,7 @@ func (r *DesignateReconciler) handleConfigMap(ctx context.Context, helper *helpe
err := helper.GetClient().Get(ctx, types.NamespacedName{Name: configMapName, Namespace: instance.GetNamespace()}, foundMap)
if err != nil {
if k8s_errors.IsNotFound(err) {
Log.Info(fmt.Sprintf("Ip map %s doesn't exist, creating.", configMapName))
Log.Info(fmt.Sprintf("configmap %s doesn't exist, creating.", configMapName))
} else {
return nil, err
}
Expand Down Expand Up @@ -1346,23 +1395,45 @@ func (r *DesignateReconciler) generateServiceConfigMaps(
func (r *DesignateReconciler) createHashOfInputHashes(
ctx context.Context,
instance *designatev1beta1.Designate,
hashType string,
envVars map[string]env.Setter,
additionalConfigmaps []interface{},
) (string, bool, error) {
Log := r.GetLogger(ctx)

var hashMap map[string]string
changed := false
mergedMapVars := env.MergeEnvs([]corev1.EnvVar{}, envVars)
hash, err := util.ObjectHash(mergedMapVars)
combinedHashes := []string{}

envHash, err := util.ObjectHash(mergedMapVars)
if err != nil {
Log.Info("XXX - Error creating hash")
return hash, changed, err
return "", changed, err
}
combinedHashes = append(combinedHashes, envHash)

for _, configMap := range additionalConfigmaps {
configMapHash, err := util.ObjectHash(configMap)
if err != nil {
Log.Info(fmt.Sprintf("Error creating hash for %v", configMap))
return "", changed, err
}
combinedHashes = append(combinedHashes, configMapHash)
}

finalHash, err := util.ObjectHash(combinedHashes)
if err != nil {
Log.Info("Error creating final hash")
return "", changed, err
}
if hashMap, changed = util.SetHash(instance.Status.Hash, common.InputHashName, hash); changed {

if hashMap, changed = util.SetHash(instance.Status.Hash, hashType, finalHash); changed {
instance.Status.Hash = hashMap
Log.Info(fmt.Sprintf("Input maps hash %s - %s", common.InputHashName, hash))
Log.Info(fmt.Sprintf("Input maps hash %s - %s", hashType, finalHash))
}
return hash, changed, nil

return finalHash, changed, nil
}

func (r *DesignateReconciler) transportURLCreateOrUpdate(
Expand Down
19 changes: 1 addition & 18 deletions controllers/designatemdns_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import (
designatemdns "github.com/openstack-k8s-operators/designate-operator/pkg/designatemdns"
"github.com/openstack-k8s-operators/lib-common/modules/common"
"github.com/openstack-k8s-operators/lib-common/modules/common/condition"
"github.com/openstack-k8s-operators/lib-common/modules/common/configmap"
"github.com/openstack-k8s-operators/lib-common/modules/common/daemonset"
"github.com/openstack-k8s-operators/lib-common/modules/common/env"
"github.com/openstack-k8s-operators/lib-common/modules/common/helper"
Expand Down Expand Up @@ -496,7 +495,7 @@ func (r *DesignateMdnsReconciler) reconcileNormal(ctx context.Context, instance
// create hash over all the different input resources to identify if any those changed
// and a restart/recreate is required.
//
inputHash, hashChanged, err := r.createHashOfInputHashes(ctx, helper, instance, configMapVars)
inputHash, hashChanged, err := r.createHashOfInputHashes(ctx, instance, configMapVars)
if err != nil {
instance.Status.Conditions.Set(condition.FalseCondition(
condition.ServiceConfigReadyCondition,
Expand Down Expand Up @@ -810,7 +809,6 @@ func (r *DesignateMdnsReconciler) generateServiceConfigMaps(
// returns the hash, whether the hash changed (as a bool) and any error
func (r *DesignateMdnsReconciler) createHashOfInputHashes(
ctx context.Context,
h *helper.Helper,
instance *designatev1beta1.DesignateMdns,
envVars map[string]env.Setter,
) (string, bool, error) {
Expand All @@ -819,21 +817,6 @@ func (r *DesignateMdnsReconciler) createHashOfInputHashes(
var hashMap map[string]string
changed := false

// If MdnsPredIPConfigMap exists, add its hash to status hash
mdnsPredIPCM := &corev1.ConfigMap{}
err := h.GetClient().Get(ctx, types.NamespacedName{
Name: designate.MdnsPredIPConfigMap,
Namespace: instance.Namespace,
}, mdnsPredIPCM)
if err != nil {
Log.Error(err, "Unable to retrieve Mdns predictable IPs ConfigMap")
return "", false, err
}
mdnsPredIPCMHash, err := configmap.Hash(mdnsPredIPCM)
if err != nil {
return mdnsPredIPCMHash, changed, err
}

mergedMapVars := env.MergeEnvs([]corev1.EnvVar{}, envVars)
hash, err := util.ObjectHash(mergedMapVars)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions controllers/designateworker_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,7 @@ func (r *DesignateWorkerReconciler) createHashOfInputHashes(
if err != nil {
return hash, changed, err
}

if hashMap, changed = util.SetHash(instance.Status.Hash, common.InputHashName, hash); changed {
instance.Status.Hash = hashMap
Log.Info(fmt.Sprintf("Input maps hash %s - %s", common.InputHashName, hash))
Expand Down
11 changes: 11 additions & 0 deletions demo/examples/ns_records_CR_example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: ConfigMap
metadata:
# Note, if you change this name, please change the name on pkg/designate/const.go file as well
name: designate-ns-records-params
data:
ns_records: |
- host: host1
priority: 1
- host: host2
priority: 2
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/openstack-k8s-operators/keystone-operator/api v0.4.1-0.20241013092400-3f9337945472
github.com/openstack-k8s-operators/lib-common/modules/common v0.5.1-0.20241029151503-4878b3fa3333
github.com/openstack-k8s-operators/mariadb-operator/api v0.4.1-0.20241015090956-b0954ab72dcd
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.29.10
k8s.io/apimachinery v0.29.10
k8s.io/client-go v0.29.10
Expand Down Expand Up @@ -70,7 +71,6 @@ require (
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.29.10 // indirect
k8s.io/component-base v0.29.10 // indirect
Expand Down
10 changes: 10 additions & 0 deletions pkg/designate/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,15 @@ const (

MdnsPredIPConfigMap = "designate-mdns-ip-map"

NsRecordsConfigMap = "designate-ns-records-params"

BindPredIPConfigMap = "designate-bind-ip-map"

RndcConfDir = "/etc/designate/rndc-keys"

PoolsYamlsConfigMap = "designate-pools-yaml-config-map"

PoolsYamlPath = "templates/designatepoolmanager/config/pools.yaml.tmpl"

PoolsYamlHash = "pools-yaml-hash"
)
147 changes: 147 additions & 0 deletions pkg/designate/generate_bind9_pools_yaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package designate

import (
"bytes"
"fmt"
"gopkg.in/yaml.v2"
"os"
"text/template"
)

type Pool struct {
Name string
Description string
Attributes map[string]string
NSRecords []NSRecord
Nameservers []Nameserver
Targets []Target
CatalogZone *CatalogZone // it is a pointer because it is optional
}

type NSRecord struct {
Hostname string
Priority int
}

type Nameserver struct {
Host string
Port int
}

type Target struct {
Type string
Description string
Masters []Master
Options Options
}

type Master struct {
Host string
Port int
}

type Options struct {
Host string
Port int
RNDCHost string
RNDCPort int
RNDCConfigFile string
}

type CatalogZone struct {
FQDN string
Refresh string
}

func GeneratePoolsYamlData(BindMap, MdnsMap, NsRecordsMap map[string]string) (string, error) {
// Create ns_records
nsRecords := []NSRecord{}
for _, data := range NsRecordsMap {
err := yaml.Unmarshal([]byte(data), &nsRecords)
if err != nil {
return "", fmt.Errorf("error unmarshalling yaml: %w", err)
}
}

// Create targets and nameservers
nameservers := []Nameserver{}
targets := []Target{}
rndcKeyNum := 1

for _, bindIP := range BindMap {
nameservers = append(nameservers, Nameserver{
Host: bindIP,
Port: 53,
})

masters := []Master{}
for _, masterHost := range MdnsMap {
masters = append(masters, Master{
Host: masterHost,
Port: 5354,
})
}

target := Target{
Type: "bind9",
Description: fmt.Sprintf("BIND9 Server %d (%s)", rndcKeyNum, bindIP),
Masters: masters,
Options: Options{
Host: bindIP,
Port: 53,
RNDCHost: bindIP,
RNDCPort: 953,
RNDCConfigFile: fmt.Sprintf("%s/%s-%d.conf", RndcConfDir, DesignateRndcKey, rndcKeyNum),
},
}
targets = append(targets, target)
rndcKeyNum++
}

// Catalog zone is an optional section
// catalogZone := &CatalogZone{
// FQDN: "example.org.",
// Refresh: "60",
// }
defaultAttributes := make(map[string]string)
// Create the Pool struct with the dynamic values
pool := Pool{
Name: "default",
Description: "Default BIND Pool",
Attributes: defaultAttributes,
NSRecords: nsRecords,
Nameservers: nameservers,
Targets: targets,
CatalogZone: nil, // set to catalogZone if this section should be presented
}

PoolsYaml, err := os.ReadFile(PoolsYamlPath)
if err != nil {
return "", err
}
tmpl, err := template.New("pool").Parse(string(PoolsYaml))
if err != nil {
return "", err
}

var buf bytes.Buffer
err = tmpl.Execute(&buf, pool)
if err != nil {
return "", err
}

return buf.String(), nil
}
Loading

0 comments on commit 74f95be

Please sign in to comment.