Fix golint warnings

This commit is contained in:
Murat Kabilov
2017-08-01 16:08:56 +02:00
committed by GitHub
parent c02a740e10
commit cf663cb841
26 changed files with 122 additions and 89 deletions
+25 -17
View File
@@ -41,7 +41,7 @@ type Config struct {
}
type kubeResources struct {
Service map[PostgresRole]*v1.Service
Service map[postgresRole]*v1.Service
Endpoint *v1.Endpoints
Secrets map[types.UID]*v1.Secret
Statefulset *v1beta1.StatefulSet
@@ -49,6 +49,7 @@ type kubeResources struct {
//PVCs are treated separately
}
// Cluster describes postgresql cluster
type Cluster struct {
kubeResources
spec.Postgresql
@@ -79,7 +80,7 @@ type compareStatefulsetResult struct {
// New creates a new cluster. This function should be called from a controller.
func New(cfg Config, kubeClient k8sutil.KubernetesClient, pgSpec spec.Postgresql, logger *logrus.Entry) *Cluster {
lg := logger.WithField("pkg", "cluster").WithField("cluster-name", pgSpec.Name)
kubeResources := kubeResources{Secrets: make(map[types.UID]*v1.Secret), Service: make(map[PostgresRole]*v1.Service)}
k8sResources := kubeResources{Secrets: make(map[types.UID]*v1.Secret), Service: make(map[postgresRole]*v1.Service)}
orphanDependents := true
podEventsQueue := cache.NewFIFO(func(obj interface{}) (string, error) {
@@ -98,7 +99,7 @@ func New(cfg Config, kubeClient k8sutil.KubernetesClient, pgSpec spec.Postgresql
pgUsers: make(map[string]spec.PgUser),
systemUsers: make(map[string]spec.PgUser),
podSubscribers: make(map[spec.NamespacedName]chan spec.PodEvent),
kubeResources: kubeResources,
kubeResources: k8sResources,
masterLess: false,
userSyncStrategy: users.DefaultUserSyncStrategy{},
deleteOptions: &metav1.DeleteOptions{OrphanDependents: &orphanDependents},
@@ -164,7 +165,13 @@ func (c *Cluster) initUsers() error {
func (c *Cluster) Create() error {
c.mu.Lock()
defer c.mu.Unlock()
var err error
var (
err error
service *v1.Service
ep *v1.Endpoints
ss *v1beta1.StatefulSet
)
defer func() {
if err == nil {
@@ -177,17 +184,17 @@ func (c *Cluster) Create() error {
c.setStatus(spec.ClusterStatusCreating)
//TODO: service will create endpoint implicitly
ep, err := c.createEndpoint()
ep, err = c.createEndpoint()
if err != nil {
return fmt.Errorf("could not create endpoint: %v", err)
}
c.logger.Infof("endpoint %q has been successfully created", util.NameFromMeta(ep.ObjectMeta))
for _, role := range []PostgresRole{Master, Replica} {
if role == Replica && !c.Spec.ReplicaLoadBalancer {
for _, role := range []postgresRole{master, replica} {
if role == replica && !c.Spec.ReplicaLoadBalancer {
continue
}
service, err := c.createService(role)
service, err = c.createService(role)
if err != nil {
return fmt.Errorf("could not create %s service: %v", role, err)
}
@@ -204,7 +211,7 @@ func (c *Cluster) Create() error {
}
c.logger.Infof("secrets have been successfully created")
ss, err := c.createStatefulSet()
ss, err = c.createStatefulSet()
if err != nil {
return fmt.Errorf("could not create statefulset: %v", err)
}
@@ -219,10 +226,12 @@ func (c *Cluster) Create() error {
c.logger.Infof("pods are ready")
if !(c.masterLess || c.databaseAccessDisabled()) {
if err := c.initDbConn(); err != nil {
err = c.initDbConn()
if err != nil {
return fmt.Errorf("could not init db connection: %v", err)
}
if err = c.createUsers(); err != nil {
err = c.createUsers()
if err != nil {
return fmt.Errorf("could not create users: %v", err)
}
c.logger.Infof("Users have been successfully created")
@@ -240,9 +249,8 @@ func (c *Cluster) Create() error {
return nil
}
func (c *Cluster) sameServiceWith(role PostgresRole, service *v1.Service) (match bool, reason string) {
func (c *Cluster) sameServiceWith(role postgresRole, service *v1.Service) (match bool, reason string) {
//TODO: improve comparison
match = true
if c.Service[role].Spec.Type != service.Spec.Type {
return false, fmt.Sprintf("new %s service's type %q doesn't match the current one %q",
role, service.Spec.Type, c.Service[role].Spec.Type)
@@ -414,8 +422,8 @@ func (c *Cluster) Update(newSpec *spec.Postgresql) error {
c.Postgresql = *newSpec
}()
for _, role := range []PostgresRole{Master, Replica} {
if role == Replica {
for _, role := range []postgresRole{master, replica} {
if role == replica {
if !newSpec.Spec.ReplicaLoadBalancer {
// old spec had a load balancer, but the new one doesn't
if c.Spec.ReplicaLoadBalancer {
@@ -513,8 +521,8 @@ func (c *Cluster) Delete() error {
return fmt.Errorf("could not delete endpoint: %v", err)
}
for _, role := range []PostgresRole{Master, Replica} {
if role == Replica && !c.Spec.ReplicaLoadBalancer {
for _, role := range []postgresRole{master, replica} {
if role == replica && !c.Spec.ReplicaLoadBalancer {
continue
}
if err := c.deleteService(role); err != nil {
+1
View File
@@ -13,6 +13,7 @@ import (
"github.com/zalando-incubator/postgres-operator/pkg/spec"
)
//ExecCommand executes arbitrary command inside the pod
func (c *Cluster) ExecCommand(podName *spec.NamespacedName, command ...string) (string, error) {
var (
execOut bytes.Buffer
+7 -7
View File
@@ -427,12 +427,12 @@ func (c *Cluster) generateSingleUserSecret(namespace string, pgUser spec.PgUser)
return &secret
}
func (c *Cluster) generateService(role PostgresRole, newSpec *spec.PostgresSpec) *v1.Service {
func (c *Cluster) generateService(role postgresRole, newSpec *spec.PostgresSpec) *v1.Service {
dnsNameFunction := c.masterDnsName
dnsNameFunction := c.masterDNSName
name := c.Name
if role == Replica {
dnsNameFunction = c.replicaDnsName
if role == replica {
dnsNameFunction = c.replicaDNSName
name = name + "-repl"
}
@@ -441,8 +441,8 @@ func (c *Cluster) generateService(role PostgresRole, newSpec *spec.PostgresSpec)
Type: v1.ServiceTypeClusterIP,
}
if role == Replica {
serviceSpec.Selector = map[string]string{c.OpConfig.PodRoleLabel: string(Replica)}
if role == replica {
serviceSpec.Selector = map[string]string{c.OpConfig.PodRoleLabel: string(replica)}
}
var annotations map[string]string
@@ -486,7 +486,7 @@ func (c *Cluster) generateMasterEndpoints(subsets []v1.EndpointSubset) *v1.Endpo
ObjectMeta: metav1.ObjectMeta{
Name: c.Name,
Namespace: c.Namespace,
Labels: c.roleLabelsSet(Master),
Labels: c.roleLabelsSet(master),
},
}
if len(subsets) > 0 {
+1 -1
View File
@@ -49,7 +49,7 @@ func (c *Cluster) initDbConn() (err error) {
err = conn.Ping()
if err != nil {
if err2 := conn.Close(); err2 != nil {
c.logger.Error("error when closing PostgreSQL connection after another error: %v", err2)
c.logger.Errorf("error when closing PostgreSQL connection after another error: %v", err2)
}
return err
}
+10 -10
View File
@@ -29,11 +29,11 @@ func (c *Cluster) loadResources() error {
return fmt.Errorf("too many(%d) services for a cluster", len(services.Items))
}
for i, svc := range services.Items {
switch PostgresRole(svc.Labels[c.OpConfig.PodRoleLabel]) {
case Replica:
c.Service[Replica] = &services.Items[i]
switch postgresRole(svc.Labels[c.OpConfig.PodRoleLabel]) {
case replica:
c.Service[replica] = &services.Items[i]
default:
c.Service[Master] = &services.Items[i]
c.Service[master] = &services.Items[i]
}
}
@@ -46,7 +46,7 @@ func (c *Cluster) loadResources() error {
}
for i, ep := range endpoints.Items {
if ep.Labels[c.OpConfig.PodRoleLabel] != string(Replica) {
if ep.Labels[c.OpConfig.PodRoleLabel] != string(replica) {
c.Endpoint = &endpoints.Items[i]
break
}
@@ -230,7 +230,7 @@ func (c *Cluster) deleteStatefulSet() error {
return nil
}
func (c *Cluster) createService(role PostgresRole) (*v1.Service, error) {
func (c *Cluster) createService(role postgresRole) (*v1.Service, error) {
if c.Service[role] != nil {
return nil, fmt.Errorf("service already exists in the cluster")
}
@@ -245,7 +245,7 @@ func (c *Cluster) createService(role PostgresRole) (*v1.Service, error) {
return service, nil
}
func (c *Cluster) updateService(role PostgresRole, newService *v1.Service) error {
func (c *Cluster) updateService(role postgresRole, newService *v1.Service) error {
if c.Service[role] == nil {
return fmt.Errorf("there is no service in the cluster")
}
@@ -260,7 +260,7 @@ func (c *Cluster) updateService(role PostgresRole, newService *v1.Service) error
err error
)
if role == Master {
if role == master {
// for the master service we need to re-create the endpoint as well. Get the up-to-date version of
// the addresses stored in it before the service is deleted (deletion of the service removes the endpooint)
currentEndpoint, err = c.KubeClient.Endpoints(c.Service[role].Namespace).Get(c.Service[role].Name, metav1.GetOptions{})
@@ -278,7 +278,7 @@ func (c *Cluster) updateService(role PostgresRole, newService *v1.Service) error
return fmt.Errorf("could not create service %q: %v", serviceName, err)
}
c.Service[role] = svc
if role == Master {
if role == master {
// create the new endpoint using the addresses obtained from the previous one
endpointSpec := c.generateMasterEndpoints(currentEndpoint.Subsets)
ep, err := c.KubeClient.Endpoints(c.Service[role].Namespace).Create(endpointSpec)
@@ -320,7 +320,7 @@ func (c *Cluster) updateService(role PostgresRole, newService *v1.Service) error
return nil
}
func (c *Cluster) deleteService(role PostgresRole) error {
func (c *Cluster) deleteService(role postgresRole) error {
c.logger.Debugf("Deleting service %s", role)
if c.Service[role] == nil {
return fmt.Errorf("There is no %s service in the cluster", role)
+3 -3
View File
@@ -34,8 +34,8 @@ func (c *Cluster) Sync() error {
}
c.logger.Debugf("Syncing services")
for _, role := range []PostgresRole{Master, Replica} {
if role == Replica && !c.Spec.ReplicaLoadBalancer {
for _, role := range []postgresRole{master, replica} {
if role == replica && !c.Spec.ReplicaLoadBalancer {
if c.Service[role] != nil {
// delete the left over replica service
if err := c.deleteService(role); err != nil {
@@ -87,7 +87,7 @@ func (c *Cluster) syncSecrets() error {
return err
}
func (c *Cluster) syncService(role PostgresRole) error {
func (c *Cluster) syncService(role postgresRole) error {
cSpec := c.Spec
if c.Service[role] == nil {
c.logger.Infof("could not find the cluster's %s service", role)
+3 -3
View File
@@ -1,8 +1,8 @@
package cluster
type PostgresRole string
type postgresRole string
const (
Master PostgresRole = "master"
Replica PostgresRole = "replica"
master postgresRole = "master"
replica postgresRole = "replica"
)
+4 -4
View File
@@ -94,7 +94,7 @@ func (c *Cluster) logStatefulSetChanges(old, new *v1beta1.StatefulSet, isUpdate
}
}
func (c *Cluster) logServiceChanges(role PostgresRole, old, new *v1.Service, isUpdate bool, reason string) {
func (c *Cluster) logServiceChanges(role postgresRole, old, new *v1.Service, isUpdate bool, reason string) {
if isUpdate {
c.logger.Infof("%s service %q has been changed",
role, util.NameFromMeta(old.ObjectMeta),
@@ -283,20 +283,20 @@ func (c *Cluster) labelsSet() labels.Set {
return labels.Set(lbls)
}
func (c *Cluster) roleLabelsSet(role PostgresRole) labels.Set {
func (c *Cluster) roleLabelsSet(role postgresRole) labels.Set {
lbls := c.labelsSet()
lbls[c.OpConfig.PodRoleLabel] = string(role)
return lbls
}
func (c *Cluster) masterDnsName() string {
func (c *Cluster) masterDNSName() string {
return strings.ToLower(c.OpConfig.MasterDNSNameFormat.Format(
"cluster", c.Spec.ClusterName,
"team", c.teamName(),
"hostedzone", c.OpConfig.DbHostedZone))
}
func (c *Cluster) replicaDnsName() string {
func (c *Cluster) replicaDNSName() string {
return strings.ToLower(c.OpConfig.ReplicaDNSNameFormat.Format(
"cluster", c.Spec.ClusterName,
"team", c.teamName(),
+5 -5
View File
@@ -103,7 +103,7 @@ func (c *Cluster) resizeVolumes(newVolume spec.Volume, resizers []volumes.Volume
if !resizer.VolumeBelongsToProvider(pv) {
continue
}
totalCompatible += 1
totalCompatible++
if !resizer.IsConnectedToProvider() {
err := resizer.ConnectToProvider()
if err != nil {
@@ -115,13 +115,13 @@ func (c *Cluster) resizeVolumes(newVolume spec.Volume, resizers []volumes.Volume
}
}()
}
awsVolumeId, err := resizer.GetProviderVolumeID(pv)
awsVolumeID, err := resizer.GetProviderVolumeID(pv)
if err != nil {
return err
}
c.logger.Debugf("updating persistent volume %q to %d", pv.Name, newSize)
if err := resizer.ResizeVolume(awsVolumeId, newSize); err != nil {
return fmt.Errorf("could not resize EBS volume %q: %v", awsVolumeId, err)
if err := resizer.ResizeVolume(awsVolumeID, newSize); err != nil {
return fmt.Errorf("could not resize EBS volume %q: %v", awsVolumeID, err)
}
c.logger.Debugf("resizing the filesystem on the volume %q", pv.Name)
podName := getPodNameFromPersistentVolume(pv)
@@ -174,7 +174,7 @@ func (c *Cluster) listVolumesWithManifestSize(newVolume spec.Volume) ([]*v1.Pers
func getPodNameFromPersistentVolume(pv *v1.PersistentVolume) *spec.NamespacedName {
namespace := pv.Spec.ClaimRef.Namespace
name := pv.Spec.ClaimRef.Name[len(constants.DataVolumeName)+1:]
return &spec.NamespacedName{namespace, name}
return &spec.NamespacedName{Namespace: namespace, Name: name}
}
func quantityToGigabyte(q resource.Quantity) int64 {