Skip to content

Commit cba62fd

Browse files
committed
Add pool update job
This patch adds a designate-manage pool update job, which should be run every time the pools.yaml file is modified.
1 parent c44a879 commit cba62fd

File tree

14 files changed

+263
-40
lines changed

14 files changed

+263
-40
lines changed

api/v1beta1/designate_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ const (
3333

3434
// Designate API timeout
3535
APITimeout = 120
36+
37+
// PoolUpdateHash hash
38+
PoolUpdateHash = "pool-update"
3639
)
3740

3841
// DesignateAPISpecCore - this version has no containerImage for use with the OpenStackControlplane

controllers/designate_controller.go

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,8 @@ func (r *DesignateReconciler) reconcileNormal(ctx context.Context, instance *des
816816
return ctrl.Result{}, err
817817
}
818818

819+
Log.Info("Before creating bind configmap")
820+
819821
_, err = controllerutil.CreateOrPatch(ctx, helper.GetClient(), bindConfigMap, func() error {
820822
bindConfigMap.Labels = util.MergeStringMaps(bindConfigMap.Labels, bindLabels)
821823
bindConfigMap.Data = updatedBindMap
@@ -827,25 +829,26 @@ func (r *DesignateReconciler) reconcileNormal(ctx context.Context, instance *des
827829
return ctrl.Result{}, err
828830
}
829831

830-
if err != nil {
831-
return ctrl.Result{}, err
832-
}
833-
if len(nsRecordsConfigMap.Data) > 0 {
832+
Log.Info("Bind configmap was created successfully")
833+
834+
if len(nsRecordsConfigMap.Data) > 0 && instance.Status.DesignateCentralReadyCount > 0 {
835+
Log.Info("len(nsRecordsConfigMap.Data) > 0")
834836
poolsYamlConfigMap := &corev1.ConfigMap{
835837
ObjectMeta: metav1.ObjectMeta{
836-
Name: designate.PoolsYamlsConfigMap,
838+
Name: designate.PoolsYamlConfigMap,
837839
Namespace: instance.GetNamespace(),
838840
Labels: bindLabels,
839841
},
840842
Data: make(map[string]string),
841843
}
844+
Log.Info("before designate.GeneratePoolsYamlData")
842845
poolsYaml, err := designate.GeneratePoolsYamlData(bindConfigMap.Data, mdnsConfigMap.Data, nsRecordsConfigMap.Data)
843846
if err != nil {
844847
return ctrl.Result{}, err
845848
}
846849
Log.Info(fmt.Sprintf("pools.yaml content is\n%v", poolsYaml))
847850
updatedPoolsYaml := make(map[string]string)
848-
updatedPoolsYaml[designate.PoolsYamlsConfigMap] = poolsYaml
851+
updatedPoolsYaml[designate.PoolsYamlContent] = poolsYaml
849852

850853
_, err = controllerutil.CreateOrPatch(ctx, helper.GetClient(), poolsYamlConfigMap, func() error {
851854
poolsYamlConfigMap.Labels = util.MergeStringMaps(poolsYamlConfigMap.Labels, bindLabels)
@@ -864,9 +867,36 @@ func (r *DesignateReconciler) reconcileNormal(ctx context.Context, instance *des
864867
_, changed, err := r.createHashOfInputHashes(ctx, instance, designate.PoolsYamlHash, poolsYamlsEnvVars, configMaps)
865868
if err != nil {
866869
return ctrl.Result{}, err
867-
} else if changed {
868-
// launch the pool update job
869-
Log.Info("Creating a pool update job")
870+
}
871+
if changed {
872+
Log.Info("PoolsYamlHash has changed, creating a pool update job")
873+
874+
var poolUpdateHash string
875+
var ok bool
876+
if poolUpdateHash, ok = instance.Status.Hash[designatev1beta1.PoolUpdateHash]; !ok {
877+
instance.Status.Hash[designatev1beta1.PoolUpdateHash] = ""
878+
poolUpdateHash = ""
879+
}
880+
jobDef := designate.PoolUpdateJob(instance, serviceLabels, serviceAnnotations)
881+
882+
Log.Info("Initializing pool update job")
883+
poolUpdatejob := job.NewJob(
884+
jobDef,
885+
designatev1beta1.PoolUpdateHash,
886+
instance.Spec.PreserveJobs,
887+
time.Duration(15)*time.Second,
888+
poolUpdateHash,
889+
)
890+
_, err = poolUpdatejob.DoJob(ctx, helper)
891+
if err != nil {
892+
return ctrl.Result{}, err
893+
}
894+
instance.Status.Hash[designatev1beta1.PoolUpdateHash] = poolUpdatejob.GetHash()
895+
err = r.Client.Status().Update(ctx, instance)
896+
if err != nil {
897+
return ctrl.Result{}, err
898+
}
899+
Log.Info("Pool update job completed successfully")
870900
}
871901
}
872902

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Append samples you want in your CSV to this file as resources ##
2+
resources:
3+
- ns_records_CR_example.yaml

demo/examples/ns_records_CR_example.yaml renamed to demo/examples/ns_records/ns_records_CR_example.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
name: designate-ns-records-params
66
data:
77
ns_records: |
8-
- host: ns1.example.com.
8+
- hostname: ns1.example.com.
99
priority: 1
10-
- host: ns2.example.com.
10+
- hostname: ns2.example.com.
1111
priority: 2

pkg/designate/const.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@ const (
4747

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

50-
PoolsYamlsConfigMap = "designate-pools-yaml-config-map"
50+
PoolsYamlConfigMap = "designate-pools-yaml-config-map"
5151

52-
PoolsYamlPath = "templates/designatepoolmanager/config/pools.yaml.tmpl"
52+
PoolsYamlPath = "designatepoolmanager/config/pools.yaml.tmpl"
5353

5454
PoolsYamlHash = "pools-yaml-hash"
5555

56+
PoolsYamlContent = "pools-yaml-content"
57+
5658
// BindPredictableIPHash key for status hash
5759
BindPredictableIPHash = "Bind IP Map"
5860

pkg/designate/generate_bind9_pools_yaml.go

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ package designate
1616
import (
1717
"bytes"
1818
"fmt"
19-
"gopkg.in/yaml.v2"
2019
"os"
21-
"os/exec"
22-
"path/filepath"
23-
"strings"
20+
"path"
2421
"text/template"
22+
23+
"gopkg.in/yaml.v2"
24+
25+
"github.com/openstack-k8s-operators/lib-common/modules/common/util"
2526
)
2627

2728
type Pool struct {
@@ -131,16 +132,16 @@ func GeneratePoolsYamlData(BindMap, MdnsMap, NsRecordsMap map[string]string) (st
131132
CatalogZone: nil, // set to catalogZone if this section should be presented
132133
}
133134

134-
poolsYamlPath, err := getPoolsYamlPath()
135+
opTemplates, err := util.GetTemplatesPath()
135136
if err != nil {
136137
return "", err
137138
}
138-
139-
PoolsYaml, err := os.ReadFile(poolsYamlPath)
139+
poolsYamlPath := path.Join(opTemplates, PoolsYamlPath)
140+
poolsYaml, err := os.ReadFile(poolsYamlPath)
140141
if err != nil {
141142
return "", err
142143
}
143-
tmpl, err := template.New("pool").Parse(string(PoolsYaml))
144+
tmpl, err := template.New("pool").Parse(string(poolsYaml))
144145
if err != nil {
145146
return "", err
146147
}
@@ -153,13 +154,3 @@ func GeneratePoolsYamlData(BindMap, MdnsMap, NsRecordsMap map[string]string) (st
153154

154155
return buf.String(), nil
155156
}
156-
157-
// We have this function so different tests could find PoolsYamlPath
158-
func getPoolsYamlPath() (string, error) {
159-
cmdOut, err := exec.Command("git", "rev-parse", "--show-toplevel").Output()
160-
if err != nil {
161-
return "", fmt.Errorf("failed to get repository root directory from: %s because of %w", string(cmdOut), err)
162-
}
163-
repoRoot := strings.TrimSpace(string(cmdOut))
164-
return filepath.Join(repoRoot, PoolsYamlPath), nil
165-
}

pkg/designate/pool_update.go

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package designate
2+
3+
import (
4+
"fmt"
5+
6+
designatev1beta1 "github.com/openstack-k8s-operators/designate-operator/api/v1beta1"
7+
batchv1 "k8s.io/api/batch/v1"
8+
corev1 "k8s.io/api/core/v1"
9+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
)
11+
12+
const (
13+
PoolUpdateCommand = "/usr/bin/designate-manage pool update"
14+
DesignateConfigVolume = "designate-config"
15+
DesignateConfigMount = "/etc/designate"
16+
DesignateConfigKeyPath = "designate.conf"
17+
DesignatePoolsYamlPath = "pools.yaml"
18+
)
19+
20+
func PoolUpdateJob(
21+
instance *designatev1beta1.Designate,
22+
labels map[string]string,
23+
annotations map[string]string,
24+
) *batchv1.Job {
25+
runAsUser := int64(0)
26+
volumeMounts := GetVolumeMounts(instance.Name)
27+
volumes := GetVolumes(instance.Name)
28+
29+
volumes = append(volumes, corev1.Volume{
30+
Name: "pools-yaml-config",
31+
VolumeSource: corev1.VolumeSource{
32+
ConfigMap: &corev1.ConfigMapVolumeSource{
33+
LocalObjectReference: corev1.LocalObjectReference{
34+
Name: PoolsYamlConfigMap,
35+
},
36+
Items: []corev1.KeyToPath{
37+
{
38+
Key: PoolsYamlContent,
39+
Path: DesignatePoolsYamlPath,
40+
},
41+
},
42+
},
43+
},
44+
})
45+
46+
volumes = append(volumes, corev1.Volume{
47+
Name: DesignateConfigVolume,
48+
VolumeSource: corev1.VolumeSource{
49+
Secret: &corev1.SecretVolumeSource{
50+
SecretName: "designate-central-config-data",
51+
Items: []corev1.KeyToPath{
52+
{
53+
Key: "designate.conf",
54+
Path: DesignateConfigKeyPath,
55+
},
56+
},
57+
},
58+
},
59+
})
60+
61+
volumes = append(volumes, corev1.Volume{
62+
Name: "rabbitmq-certs",
63+
VolumeSource: corev1.VolumeSource{
64+
Secret: &corev1.SecretVolumeSource{
65+
SecretName: "cert-rabbitmq-svc",
66+
},
67+
},
68+
})
69+
70+
volumeMounts = append(volumeMounts,
71+
corev1.VolumeMount{
72+
Name: "pools-yaml-config",
73+
MountPath: "/tmp/designate-pools",
74+
ReadOnly: true,
75+
},
76+
corev1.VolumeMount{
77+
Name: DesignateConfigVolume,
78+
MountPath: DesignateConfigMount,
79+
ReadOnly: true,
80+
},
81+
corev1.VolumeMount{
82+
Name: "rabbitmq-certs",
83+
MountPath: "/etc/pki/rabbitmq",
84+
ReadOnly: true,
85+
},
86+
)
87+
88+
job := &batchv1.Job{
89+
ObjectMeta: metav1.ObjectMeta{
90+
Name: ServiceName + "-pool-update",
91+
Namespace: instance.Namespace,
92+
Labels: labels,
93+
},
94+
Spec: batchv1.JobSpec{
95+
Template: corev1.PodTemplateSpec{
96+
ObjectMeta: metav1.ObjectMeta{
97+
Annotations: annotations,
98+
},
99+
Spec: corev1.PodSpec{
100+
RestartPolicy: corev1.RestartPolicyNever,
101+
ServiceAccountName: instance.RbacResourceName(),
102+
Containers: []corev1.Container{
103+
{
104+
Name: ServiceName + "-pool-update",
105+
Image: instance.Spec.DesignateCentral.ContainerImage,
106+
Env: []corev1.EnvVar{
107+
{
108+
Name: "SSL_CERT_FILE",
109+
Value: "/etc/pki/rabbitmq/ca.crt",
110+
},
111+
},
112+
Command: []string{
113+
"/bin/bash",
114+
"-c",
115+
fmt.Sprintf("%s --file /tmp/designate-pools/%s",
116+
PoolUpdateCommand,
117+
DesignatePoolsYamlPath,
118+
),
119+
},
120+
SecurityContext: &corev1.SecurityContext{
121+
RunAsUser: &runAsUser,
122+
},
123+
VolumeMounts: volumeMounts,
124+
},
125+
},
126+
Volumes: volumes,
127+
},
128+
},
129+
},
130+
}
131+
return job
132+
}

pkg/designatecentral/deployment.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ func Deployment(
4343
// designateUser := int64(42411)
4444
// designateGroup := int64(42411)
4545

46-
volumes := designate.GetVolumes("designate-central")
47-
volumeMounts := designate.GetVolumeMounts("designate-central")
46+
serviceName := fmt.Sprintf("%s-central", designate.ServiceName)
47+
volumes := getServicePodVolumes(serviceName)
48+
volumeMounts := getServicePodVolumeMounts(serviceName)
4849

4950
livenessProbe := &corev1.Probe{
5051
// TODO might need tuning
@@ -69,8 +70,6 @@ func Deployment(
6970
envVars["KOLLA_CONFIG_STRATEGY"] = env.SetValue("COPY_ALWAYS")
7071
envVars["CONFIG_HASH"] = env.SetValue(configHash)
7172

72-
serviceName := fmt.Sprintf("%s-central", designate.ServiceName)
73-
7473
// Add the CA bundle
7574
if instance.Spec.TLS.CaBundleSecretName != "" {
7675
volumes = append(volumes, instance.Spec.TLS.CreateVolume())

pkg/designatecentral/volumes.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
package designatecentral
17+
18+
import (
19+
designate "github.com/openstack-k8s-operators/designate-operator/pkg/designate"
20+
corev1 "k8s.io/api/core/v1"
21+
)
22+
23+
func getServicePodVolumes(serviceName string) []corev1.Volume {
24+
// var configMode int32 = 0640
25+
// return append(designate.GetVolumes(serviceName), corev1.Volume{
26+
// Name: "pools-yaml-config",
27+
// VolumeSource: corev1.VolumeSource{
28+
// ConfigMap: &corev1.ConfigMapVolumeSource{
29+
// LocalObjectReference: corev1.LocalObjectReference{
30+
// Name: designate.PoolsYamlConfigMap,
31+
// },
32+
// Items: []corev1.KeyToPath{
33+
// {
34+
// Key: designate.PoolsYamlContent,
35+
// Path: "pools.yaml",
36+
// },
37+
// },
38+
// DefaultMode: &configMode,
39+
// },
40+
// },
41+
// })
42+
return designate.GetVolumes(serviceName)
43+
}
44+
45+
func getServicePodVolumeMounts(serviceName string) []corev1.VolumeMount {
46+
// return append(designate.GetVolumeMounts(serviceName), corev1.VolumeMount{
47+
// Name: "pools-yaml-config",
48+
// MountPath: "/etc/designate/pools.yaml",
49+
// SubPath: "pools.yaml",
50+
// ReadOnly: true,
51+
// })
52+
return designate.GetVolumeMounts(serviceName)
53+
}

0 commit comments

Comments
 (0)