mirror of
https://github.com/zalando/postgres-operator.git
synced 2026-10-01 07:15:18 +02:00
Linter-induced code refactoring, run round 2. (#360)
Run more linters in the gometalinter, i.e. deadcode, megacheck, nakedret, dup. More consistent code formatting, remove two dead functions, eliminate naked a bunch of naked returns, refactor a few functions to avoid code duplication.
This commit is contained in:
@@ -3,11 +3,11 @@ package config
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
"github.com/zalando-incubator/postgres-operator/pkg/spec"
|
||||
"github.com/mohae/deepcopy"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
|
||||
"github.com/mohae/deepcopy"
|
||||
)
|
||||
|
||||
type OperatorConfiguration struct {
|
||||
@@ -159,11 +159,12 @@ func (opc *OperatorConfiguration) DeepCopyInto(out *OperatorConfiguration) {
|
||||
if opc != nil {
|
||||
*out = deepcopy.Copy(*opc).(OperatorConfiguration)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (opc *OperatorConfiguration) DeepCopy() *OperatorConfiguration {
|
||||
if opc == nil { return nil }
|
||||
if opc == nil {
|
||||
return nil
|
||||
}
|
||||
out := new(OperatorConfiguration)
|
||||
opc.DeepCopyInto(out)
|
||||
return out
|
||||
@@ -189,11 +190,12 @@ func (opcl *OperatorConfigurationList) DeepCopyInto(out *OperatorConfigurationLi
|
||||
if opcl != nil {
|
||||
*out = deepcopy.Copy(*opcl).(OperatorConfigurationList)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (opcl *OperatorConfigurationList) DeepCopy() *OperatorConfigurationList {
|
||||
if opcl == nil { return nil }
|
||||
if opcl == nil {
|
||||
return nil
|
||||
}
|
||||
out := new(OperatorConfigurationList)
|
||||
opcl.DeepCopyInto(out)
|
||||
return out
|
||||
@@ -205,4 +207,3 @@ func (opcl *OperatorConfigurationList) DeepCopyObject() runtime.Object {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ func apiURL(masterPod *v1.Pod) string {
|
||||
return fmt.Sprintf("http://%s:%d", masterPod.Status.PodIP, apiPort)
|
||||
}
|
||||
|
||||
func (p *Patroni) httpPostOrPatch(method string, url string, body *bytes.Buffer) error {
|
||||
func (p *Patroni) httpPostOrPatch(method string, url string, body *bytes.Buffer) (err error) {
|
||||
request, err := http.NewRequest(method, url, body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not create request: %v", err)
|
||||
@@ -59,7 +59,16 @@ func (p *Patroni) httpPostOrPatch(method string, url string, body *bytes.Buffer)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not make request: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
defer func() {
|
||||
if err2 := resp.Body.Close(); err2 != nil {
|
||||
if err != nil {
|
||||
err = fmt.Errorf("could not close request: %v, prior error: %v", err2, err)
|
||||
} else {
|
||||
err = fmt.Errorf("could not close request: %v", err2)
|
||||
}
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
bodyBytes, err := ioutil.ReadAll(resp.Body)
|
||||
@@ -83,6 +92,7 @@ func (p *Patroni) Switchover(master *v1.Pod, candidate string) error {
|
||||
}
|
||||
|
||||
//TODO: add an option call /patroni to check if it is necessary to restart the server
|
||||
|
||||
//SetPostgresParameters sets Postgres options via Patroni patch API call.
|
||||
func (p *Patroni) SetPostgresParameters(server *v1.Pod, parameters map[string]string) error {
|
||||
buf := &bytes.Buffer{}
|
||||
|
||||
+10
-19
@@ -76,17 +76,15 @@ func (t *API) TeamInfo(teamID, token string) (tm *Team, err error) {
|
||||
t.logger.Debugf("request url: %s", url)
|
||||
req, err = http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Add("Authorization", "Bearer "+token)
|
||||
resp, err = t.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return
|
||||
if resp, err = t.httpClient.Do(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() {
|
||||
closeErr := resp.Body.Close()
|
||||
if closeErr != nil {
|
||||
if closeErr := resp.Body.Close(); closeErr != nil {
|
||||
err = fmt.Errorf("error when closing response: %v", closeErr)
|
||||
}
|
||||
}()
|
||||
@@ -95,27 +93,20 @@ func (t *API) TeamInfo(teamID, token string) (tm *Team, err error) {
|
||||
d := json.NewDecoder(resp.Body)
|
||||
err = d.Decode(&raw)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("team API query failed with status code %d and malformed response: %v", resp.StatusCode, err)
|
||||
return
|
||||
return nil, fmt.Errorf("team API query failed with status code %d and malformed response: %v", resp.StatusCode, err)
|
||||
}
|
||||
|
||||
if errMessage, ok := raw["error"]; ok {
|
||||
err = fmt.Errorf("team API query failed with status code %d and message: '%v'", resp.StatusCode, string(errMessage))
|
||||
return
|
||||
return nil, fmt.Errorf("team API query failed with status code %d and message: '%v'", resp.StatusCode, string(errMessage))
|
||||
}
|
||||
err = fmt.Errorf("team API query failed with status code %d", resp.StatusCode)
|
||||
|
||||
return
|
||||
return nil, fmt.Errorf("team API query failed with status code %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
tm = &Team{}
|
||||
d := json.NewDecoder(resp.Body)
|
||||
err = d.Decode(tm)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("could not parse team API response: %v", err)
|
||||
tm = nil
|
||||
return
|
||||
if err = d.Decode(tm); err != nil {
|
||||
return nil, fmt.Errorf("could not parse team API response: %v", err)
|
||||
}
|
||||
|
||||
return
|
||||
return tm, nil
|
||||
}
|
||||
|
||||
+17
-21
@@ -30,8 +30,9 @@ type DefaultUserSyncStrategy struct {
|
||||
|
||||
// ProduceSyncRequests figures out the types of changes that need to happen with the given users.
|
||||
func (strategy DefaultUserSyncStrategy) ProduceSyncRequests(dbUsers spec.PgUserMap,
|
||||
newUsers spec.PgUserMap) (reqs []spec.PgSyncUserRequest) {
|
||||
newUsers spec.PgUserMap) []spec.PgSyncUserRequest {
|
||||
|
||||
var reqs []spec.PgSyncUserRequest
|
||||
// No existing roles are deleted or stripped of role memebership/flags
|
||||
for name, newUser := range newUsers {
|
||||
dbUser, exists := dbUsers[name]
|
||||
@@ -66,7 +67,7 @@ func (strategy DefaultUserSyncStrategy) ProduceSyncRequests(dbUsers spec.PgUserM
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
return reqs
|
||||
}
|
||||
|
||||
// ExecuteSyncRequests makes actual database changes from the requests passed in its arguments.
|
||||
@@ -102,7 +103,7 @@ func (strategy DefaultUserSyncStrategy) alterPgUserSet(user spec.PgUser, db *sql
|
||||
return
|
||||
}
|
||||
|
||||
func (strategy DefaultUserSyncStrategy) createPgUser(user spec.PgUser, db *sql.DB) (err error) {
|
||||
func (strategy DefaultUserSyncStrategy) createPgUser(user spec.PgUser, db *sql.DB) error {
|
||||
var userFlags []string
|
||||
var userPassword string
|
||||
|
||||
@@ -120,16 +121,14 @@ func (strategy DefaultUserSyncStrategy) createPgUser(user spec.PgUser, db *sql.D
|
||||
}
|
||||
query := fmt.Sprintf(createUserSQL, user.Name, strings.Join(userFlags, " "), userPassword)
|
||||
|
||||
_, err = db.Exec(query) // TODO: Try several times
|
||||
if err != nil {
|
||||
err = fmt.Errorf("dB error: %v, query: %s", err, query)
|
||||
return
|
||||
if _, err := db.Exec(query); err != nil { // TODO: Try several times
|
||||
return fmt.Errorf("dB error: %v, query: %s", err, query)
|
||||
}
|
||||
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
func (strategy DefaultUserSyncStrategy) alterPgUser(user spec.PgUser, db *sql.DB) (err error) {
|
||||
func (strategy DefaultUserSyncStrategy) alterPgUser(user spec.PgUser, db *sql.DB) error {
|
||||
var resultStmt []string
|
||||
|
||||
if user.Password != "" || len(user.Flags) > 0 {
|
||||
@@ -140,19 +139,16 @@ func (strategy DefaultUserSyncStrategy) alterPgUser(user spec.PgUser, db *sql.DB
|
||||
grantStmt := produceGrantStmt(user)
|
||||
resultStmt = append(resultStmt, grantStmt)
|
||||
}
|
||||
if len(resultStmt) == 0 {
|
||||
return nil
|
||||
|
||||
if len(resultStmt) > 0 {
|
||||
query := fmt.Sprintf(doBlockStmt, strings.Join(resultStmt, ";"))
|
||||
|
||||
if _, err := db.Exec(query); err != nil { // TODO: Try several times
|
||||
return fmt.Errorf("dB error: %v query %s", err, query)
|
||||
}
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(doBlockStmt, strings.Join(resultStmt, ";"))
|
||||
|
||||
_, err = db.Exec(query) // TODO: Try several times
|
||||
if err != nil {
|
||||
err = fmt.Errorf("dB error: %v query %s", err, query)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
func produceAlterStmt(user spec.PgUser) string {
|
||||
@@ -205,7 +201,7 @@ func quoteParameterValue(name, val string) string {
|
||||
// containing spaces (but something more complex, like double quotes inside double quotes or spaces
|
||||
// in the schema name would break the parsing code in the operator.)
|
||||
if start == '\'' && end == '\'' {
|
||||
return fmt.Sprintf("%s", val[1:len(val)-1])
|
||||
return val[1 : len(val)-1]
|
||||
}
|
||||
|
||||
return val
|
||||
|
||||
Reference in New Issue
Block a user