Skip to content

Commit 19014a3

Browse files
author
jmccormick2001
committed
refactor the container resources template processing to fix panic on operator side
1 parent 2d34e6b commit 19014a3

File tree

12 files changed

+106
-30
lines changed

12 files changed

+106
-30
lines changed

apiserver/loadservice/loadimpl.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"github.com/crunchydata/postgres-operator/apiserver"
2525
"github.com/crunchydata/postgres-operator/apiserver/policyservice"
2626
msgs "github.com/crunchydata/postgres-operator/apiservermsgs"
27-
"github.com/crunchydata/postgres-operator/config"
27+
//"github.com/crunchydata/postgres-operator/config"
2828
"github.com/crunchydata/postgres-operator/kubeapi"
2929
operutil "github.com/crunchydata/postgres-operator/util"
3030
v1batch "k8s.io/api/batch/v1"
@@ -97,7 +97,7 @@ func Load(request *msgs.LoadRequest) msgs.LoadResponse {
9797
resp.Status.Msg = err.Error()
9898
return resp
9999
}
100-
LoadConfigTemplate.ContainerResources = config.GetContainerResourcesJSON(&tmp)
100+
LoadConfigTemplate.ContainerResources = apiserver.GetContainerResourcesJSON(&tmp)
101101
}
102102

103103
args := request.Args

apiserver/pvcservice/pvcimpl.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"encoding/json"
2121
log "github.com/Sirupsen/logrus"
2222
"github.com/crunchydata/postgres-operator/apiserver"
23-
"github.com/crunchydata/postgres-operator/config"
23+
//"github.com/crunchydata/postgres-operator/config"
2424
"github.com/crunchydata/postgres-operator/kubeapi"
2525
"github.com/crunchydata/postgres-operator/util"
2626
"io"
@@ -122,7 +122,7 @@ func printPVCListing(clusterName, pvcName, PVCRoot string) ([]string, error) {
122122
log.Error(err.Error())
123123
return newlines, err
124124
}
125-
cr = config.GetContainerResourcesJSON(&tmp)
125+
cr = apiserver.GetContainerResourcesJSON(&tmp)
126126

127127
}
128128

apiserver/root.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"fmt"
2323
log "github.com/Sirupsen/logrus"
2424
//crdclient "github.com/crunchydata/postgres-operator/client"
25+
"bytes"
26+
crv1 "github.com/crunchydata/postgres-operator/apis/cr/v1"
2527
"github.com/crunchydata/postgres-operator/config"
2628
"github.com/crunchydata/postgres-operator/kubeapi"
2729
"github.com/crunchydata/postgres-operator/util"
@@ -89,6 +91,11 @@ var JobTemplate *template.Template
8991

9092
var Pgo config.PgoConfig
9193

94+
type containerResourcesTemplateFields struct {
95+
RequestsMemory, RequestsCPU string
96+
LimitsMemory, LimitsCPU string
97+
}
98+
9299
func Initialize() {
93100

94101
Pgo.GetConf()
@@ -507,3 +514,31 @@ func validateWithKube() {
507514
}
508515
}
509516
}
517+
518+
// GetContainerResources ...
519+
func GetContainerResourcesJSON(resources *crv1.PgContainerResources) string {
520+
521+
//test for the case where no container resources are specified
522+
if resources.RequestsMemory == "" || resources.RequestsCPU == "" ||
523+
resources.LimitsMemory == "" || resources.LimitsCPU == "" {
524+
return ""
525+
}
526+
fields := containerResourcesTemplateFields{}
527+
fields.RequestsMemory = resources.RequestsMemory
528+
fields.RequestsCPU = resources.RequestsCPU
529+
fields.LimitsMemory = resources.LimitsMemory
530+
fields.LimitsCPU = resources.LimitsCPU
531+
532+
doc := bytes.Buffer{}
533+
err := ContainerResourcesTemplate.Execute(&doc, fields)
534+
if err != nil {
535+
log.Error(err.Error())
536+
return ""
537+
}
538+
539+
if log.GetLevel() == log.DebugLevel {
540+
ContainerResourcesTemplate.Execute(os.Stdout, fields)
541+
}
542+
543+
return doc.String()
544+
}

conf/postgres-operator/pgo.yaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Cluster:
44
CCPImagePrefix: crunchydata
55
Metrics: false
66
Badger: false
7-
CCPImageTag: centos7-10.6-2.2.0
7+
CCPImageTag: rhel7-10.6-2.2.0-rc9
88
Port: 5432
99
User: testuser
1010
Database: userdb
@@ -19,9 +19,9 @@ Cluster:
1919
Autofail: false
2020
LogStatement: none
2121
LogMinDurationStatement: 60000
22-
PrimaryStorage: hostpathstorage
23-
BackupStorage: hostpathstorage
24-
ReplicaStorage: hostpathstorage
22+
PrimaryStorage: nfsstorage
23+
BackupStorage: nfsstorage
24+
ReplicaStorage: nfsstorage
2525
Storage:
2626
hostpathstorage:
2727
AccessMode: ReadWriteMany
@@ -43,8 +43,8 @@ DefaultLoadResources:
4343
DefaultLspvcResources:
4444
DefaultRmdataResources:
4545
DefaultBackupResources:
46-
DefaultPgbouncerResources:
47-
DefaultPgpoolResources:
46+
DefaultPgbouncerResources: small
47+
DefaultPgpoolResources: small
4848
ContainerResources:
4949
small:
5050
RequestsMemory: 512Mi
@@ -62,4 +62,4 @@ Pgo:
6262
LSPVCTemplate: /pgo-config/pgo.lspvc-template.json
6363
LoadTemplate: /pgo-config/pgo.load-template.json
6464
COImagePrefix: crunchydata
65-
COImageTag: centos7-3.4.0
65+
COImageTag: rhel7-3.4.0-rc5

config/pgoconfig.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ limitations under the License.
1616
*/
1717

1818
import (
19-
"bytes"
19+
//"bytes"
2020
"errors"
2121
log "github.com/Sirupsen/logrus"
2222
crv1 "github.com/crunchydata/postgres-operator/apis/cr/v1"
2323
"gopkg.in/yaml.v2"
2424
"io/ioutil"
25-
"os"
25+
//"os"
2626
"strconv"
2727
"strings"
28-
"text/template"
28+
//"text/template"
2929
)
3030

3131
type ClusterStruct struct {
@@ -103,14 +103,16 @@ const LOAD_BALANCER_SERVICE_TYPE = "LoadBalancer"
103103
const NODEPORT_SERVICE_TYPE = "NodePort"
104104
const CONFIG_PATH = "/pgo-config/pgo.yaml"
105105

106-
const ContainerResourcesTemplate1Path = "/pgo-config/container-resources.json"
106+
//const ContainerResourcesTemplate1Path = "/pgo-config/container-resources.json"
107107

108-
var ContainerResourcesTemplate1 *template.Template
108+
//var ContainerResourcesTemplate1 *template.Template
109109

110+
/**
110111
type containerResourcesTemplateFields struct {
111112
RequestsMemory, RequestsCPU string
112113
LimitsMemory, LimitsCPU string
113114
}
115+
*/
114116

115117
var log_statement_values = []string{"ddl", "none", "mod", "all"}
116118

@@ -349,6 +351,7 @@ func (c *PgoConfig) GetContainerResource(name string) (crv1.PgContainerResources
349351

350352
}
351353

354+
/**
352355
// GetContainerResources ...
353356
func GetContainerResourcesJSON(resources *crv1.PgContainerResources) string {
354357
@@ -363,7 +366,7 @@ func GetContainerResourcesJSON(resources *crv1.PgContainerResources) string {
363366
fields.LimitsMemory = resources.LimitsMemory
364367
fields.LimitsCPU = resources.LimitsCPU
365368
366-
var doc bytes.Buffer
369+
doc := bytes.Buffer{}
367370
err := ContainerResourcesTemplate1.Execute(&doc, fields)
368371
if err != nil {
369372
log.Error(err.Error())
@@ -376,3 +379,4 @@ func GetContainerResourcesJSON(resources *crv1.PgContainerResources) string {
376379
377380
return doc.String()
378381
}
382+
*/

operator/backup/backup.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323

2424
log "github.com/Sirupsen/logrus"
2525
crv1 "github.com/crunchydata/postgres-operator/apis/cr/v1"
26-
"github.com/crunchydata/postgres-operator/config"
26+
//"github.com/crunchydata/postgres-operator/config"
2727
"github.com/crunchydata/postgres-operator/kubeapi"
2828
"github.com/crunchydata/postgres-operator/operator"
2929
"github.com/crunchydata/postgres-operator/operator/pvc"
@@ -86,7 +86,7 @@ func AddBackupBase(clientset *kubernetes.Clientset, client *rest.RESTClient, job
8686
log.Error(err)
8787
return
8888
}
89-
cr = config.GetContainerResourcesJSON(&tmp)
89+
cr = operator.GetContainerResourcesJSON(&tmp)
9090

9191
}
9292

operator/cluster/cluster_strategy_1.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"encoding/json"
2424
log "github.com/Sirupsen/logrus"
2525
crv1 "github.com/crunchydata/postgres-operator/apis/cr/v1"
26-
"github.com/crunchydata/postgres-operator/config"
26+
//"github.com/crunchydata/postgres-operator/config"
2727
"github.com/crunchydata/postgres-operator/kubeapi"
2828
"github.com/crunchydata/postgres-operator/operator"
2929
"github.com/crunchydata/postgres-operator/util"
@@ -150,7 +150,7 @@ func (r Strategy1) AddCluster(clientset *kubernetes.Clientset, client *rest.REST
150150
PrimarySecretName: cl.Spec.PrimarySecretName,
151151
UserSecretName: cl.Spec.UserSecretName,
152152
NodeSelector: GetAffinity(cl.Spec.UserLabels["NodeLabelKey"], cl.Spec.UserLabels["NodeLabelValue"], "In"),
153-
ContainerResources: config.GetContainerResourcesJSON(&cl.Spec.ContainerResources),
153+
ContainerResources: operator.GetContainerResourcesJSON(&cl.Spec.ContainerResources),
154154
ConfVolume: GetConfVolume(clientset, cl, namespace),
155155
CollectAddon: GetCollectAddon(clientset, namespace, &cl.Spec),
156156
BadgerAddon: GetBadgerAddon(clientset, namespace, &cl.Spec),
@@ -346,7 +346,7 @@ func (r Strategy1) CreateReplica(serviceName string, clientset *kubernetes.Clien
346346
SecurityContext: util.CreateSecContext(cl.Spec.ReplicaStorage.Fsgroup, cl.Spec.ReplicaStorage.SupplementalGroups),
347347
RootSecretName: cl.Spec.RootSecretName,
348348
PrimarySecretName: cl.Spec.PrimarySecretName,
349-
ContainerResources: config.GetContainerResourcesJSON(&cl.Spec.ContainerResources),
349+
ContainerResources: operator.GetContainerResourcesJSON(&cl.Spec.ContainerResources),
350350
UserSecretName: cl.Spec.UserSecretName,
351351
NodeSelector: GetAffinity(cl.Spec.UserLabels["NodeLabelKey"], cl.Spec.UserLabels["NodeLabelValue"], "NotIn"),
352352
PgbackrestEnvVars: GetPgbackrestEnvVars(cl.Spec.UserLabels[util.LABEL_BACKREST], "db", "/pgdata/"+depName, "/backrestrepo/"+depName+"-backups"),
@@ -599,7 +599,7 @@ func (r Strategy1) Scale(clientset *kubernetes.Clientset, client *rest.RESTClien
599599
RootSecretName: cluster.Spec.RootSecretName,
600600
PrimarySecretName: cluster.Spec.PrimarySecretName,
601601
UserSecretName: cluster.Spec.UserSecretName,
602-
ContainerResources: config.GetContainerResourcesJSON(&cs),
602+
ContainerResources: operator.GetContainerResourcesJSON(&cs),
603603
NodeSelector: GetReplicaAffinity(cluster.Spec.UserLabels, replica.Spec.UserLabels),
604604
CollectAddon: GetCollectAddon(clientset, namespace, &cluster.Spec),
605605
BadgerAddon: GetBadgerAddon(clientset, namespace, &cluster.Spec),
@@ -665,7 +665,7 @@ func GetBadgerAddon(clientset *kubernetes.Clientset, namespace string, spec *crv
665665
log.Error(err)
666666
return ""
667667
}
668-
badgerTemplateFields.ContainerResources = config.GetContainerResourcesJSON(&tmp)
668+
badgerTemplateFields.ContainerResources = operator.GetContainerResourcesJSON(&tmp)
669669

670670
}
671671

operator/cluster/pgbouncer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"encoding/json"
2121
log "github.com/Sirupsen/logrus"
2222
crv1 "github.com/crunchydata/postgres-operator/apis/cr/v1"
23-
"github.com/crunchydata/postgres-operator/config"
23+
//"github.com/crunchydata/postgres-operator/config"
2424
"github.com/crunchydata/postgres-operator/kubeapi"
2525
"github.com/crunchydata/postgres-operator/operator"
2626
"github.com/crunchydata/postgres-operator/util"
@@ -230,7 +230,7 @@ func AddPgbouncer(clientset *kubernetes.Clientset, cl *crv1.Pgcluster, namespace
230230
log.Error(err)
231231
return
232232
}
233-
fields.ContainerResources = config.GetContainerResourcesJSON(&tmp)
233+
fields.ContainerResources = operator.GetContainerResourcesJSON(&tmp)
234234

235235
}
236236

operator/cluster/pgpool.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"encoding/json"
2121
log "github.com/Sirupsen/logrus"
2222
crv1 "github.com/crunchydata/postgres-operator/apis/cr/v1"
23-
"github.com/crunchydata/postgres-operator/config"
23+
//"github.com/crunchydata/postgres-operator/config"
2424
"github.com/crunchydata/postgres-operator/kubeapi"
2525
"github.com/crunchydata/postgres-operator/operator"
2626
"github.com/crunchydata/postgres-operator/util"
@@ -231,7 +231,7 @@ func AddPgpool(clientset *kubernetes.Clientset, cl *crv1.Pgcluster, namespace st
231231
log.Error(err)
232232
return
233233
}
234-
fields.ContainerResources = config.GetContainerResourcesJSON(&tmp)
234+
fields.ContainerResources = operator.GetContainerResourcesJSON(&tmp)
235235

236236
}
237237

operator/common.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ package operator
1616
*/
1717

1818
import (
19+
"bytes"
1920
log "github.com/Sirupsen/logrus"
21+
crv1 "github.com/crunchydata/postgres-operator/apis/cr/v1"
2022
"github.com/crunchydata/postgres-operator/config"
2123
"github.com/crunchydata/postgres-operator/util"
2224
"os"
@@ -88,6 +90,11 @@ var ReplicadeploymentTemplate1Shared *template.Template
8890

8991
var Pgo config.PgoConfig
9092

93+
type containerResourcesTemplateFields struct {
94+
RequestsMemory, RequestsCPU string
95+
LimitsMemory, LimitsCPU string
96+
}
97+
9198
func Initialize() {
9299

93100
tmp := os.Getenv("CRUNCHY_DEBUG")
@@ -163,3 +170,31 @@ func Initialize() {
163170
panic("pgo.yaml COImageTag env var not set")
164171
}
165172
}
173+
174+
// GetContainerResources ...
175+
func GetContainerResourcesJSON(resources *crv1.PgContainerResources) string {
176+
177+
//test for the case where no container resources are specified
178+
if resources.RequestsMemory == "" || resources.RequestsCPU == "" ||
179+
resources.LimitsMemory == "" || resources.LimitsCPU == "" {
180+
return ""
181+
}
182+
fields := containerResourcesTemplateFields{}
183+
fields.RequestsMemory = resources.RequestsMemory
184+
fields.RequestsCPU = resources.RequestsCPU
185+
fields.LimitsMemory = resources.LimitsMemory
186+
fields.LimitsCPU = resources.LimitsCPU
187+
188+
doc := bytes.Buffer{}
189+
err := ContainerResourcesTemplate1.Execute(&doc, fields)
190+
if err != nil {
191+
log.Error(err.Error())
192+
return ""
193+
}
194+
195+
if log.GetLevel() == log.DebugLevel {
196+
ContainerResourcesTemplate1.Execute(os.Stdout, fields)
197+
}
198+
199+
return doc.String()
200+
}

0 commit comments

Comments
 (0)