Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
postgres-operator
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
GitHub Mirror
zalando
postgres-operator
Commits
8f5a8e21
Commit
8f5a8e21
authored
May 29, 2017
by
Murat Kabilov
Browse files
Options
Downloads
Patches
Plain Diff
split processEvent function into several functions
parent
d39cfaab
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
pkg/controller/postgresql.go
+96
-67
96 additions, 67 deletions
pkg/controller/postgresql.go
with
96 additions
and
67 deletions
pkg/controller/postgresql.go
+
96
−
67
View file @
8f5a8e21
...
...
@@ -74,36 +74,13 @@ func (c *Controller) clusterWatchFunc(options api.ListOptions) (watch.Interface,
return
req
.
Watch
()
}
func
(
c
*
Controller
)
processEvent
(
obj
interface
{})
error
{
var
clusterName
spec
.
NamespacedName
event
,
ok
:=
obj
.
(
spec
.
ClusterEvent
)
if
!
ok
{
return
fmt
.
Errorf
(
"could not cast to ClusterEvent"
)
}
logger
:=
c
.
logger
.
WithField
(
"worker"
,
event
.
WorkerID
)
if
event
.
EventType
==
spec
.
EventAdd
||
event
.
EventType
==
spec
.
EventSync
{
clusterName
=
util
.
NameFromMeta
(
event
.
NewSpec
.
Metadata
)
}
else
{
clusterName
=
util
.
NameFromMeta
(
event
.
OldSpec
.
Metadata
)
}
c
.
clustersMu
.
RLock
()
cl
,
clusterFound
:=
c
.
clusters
[
clusterName
]
c
.
clustersMu
.
RUnlock
()
switch
event
.
EventType
{
case
spec
.
EventAdd
:
if
clusterFound
{
logger
.
Debugf
(
"Cluster '%s' already exists"
,
clusterName
)
return
nil
}
func
(
c
*
Controller
)
processAddEvent
(
workerID
uint32
,
clusterName
spec
.
NamespacedName
,
spec
*
spec
.
Postgresql
)
{
log
:=
c
.
logger
.
WithField
(
"worker"
,
workerID
)
log
ger
.
Infof
(
"Creation of the '%s' cluster started"
,
clusterName
)
log
.
Infof
(
"Creation of the '%s' cluster started"
,
clusterName
)
stopCh
:=
make
(
chan
struct
{})
cl
=
cluster
.
New
(
c
.
makeClusterConfig
(),
*
event
.
NewS
pec
,
log
ger
)
cl
:
=
cluster
.
New
(
c
.
makeClusterConfig
(),
*
s
pec
,
log
)
cl
.
Run
(
stopCh
)
c
.
clustersMu
.
Lock
()
...
...
@@ -113,37 +90,39 @@ func (c *Controller) processEvent(obj interface{}) error {
if
err
:=
cl
.
Create
();
err
!=
nil
{
cl
.
Error
=
fmt
.
Errorf
(
"could not create cluster: %v"
,
err
)
log
ger
.
Errorf
(
"%v"
,
cl
.
Error
)
log
.
Errorf
(
"%v"
,
cl
.
Error
)
return
nil
return
}
logger
.
Infof
(
"Cluster '%s' has been created"
,
clusterName
)
case
spec
.
EventUpdate
:
logger
.
Infof
(
"Update of the '%s' cluster started"
,
clusterName
)
if
!
clusterFound
{
logger
.
Warnf
(
"Cluster '%s' does not exist"
,
clusterName
)
return
nil
log
.
Infof
(
"Cluster '%s' has been created"
,
clusterName
)
}
if
err
:=
cl
.
Update
(
event
.
NewSpec
);
err
!=
nil
{
func
(
c
*
Controller
)
processUpdateEvent
(
workerID
uint32
,
clusterName
spec
.
NamespacedName
,
cl
*
cluster
.
Cluster
,
newSpec
*
spec
.
Postgresql
)
{
log
:=
c
.
logger
.
WithField
(
"worker"
,
workerID
)
log
.
Infof
(
"Update of the '%s' cluster started"
,
clusterName
)
if
err
:=
cl
.
Update
(
newSpec
);
err
!=
nil
{
cl
.
Error
=
fmt
.
Errorf
(
"could not update cluster: %s"
,
err
)
log
ger
.
Errorf
(
"%v"
,
cl
.
Error
)
log
.
Errorf
(
"%v"
,
cl
.
Error
)
return
nil
return
}
cl
.
Error
=
nil
logger
.
Infof
(
"Cluster '%s' has been updated"
,
clusterName
)
case
spec
.
EventDelete
:
logger
.
Infof
(
"Deletion of the '%s' cluster started"
,
clusterName
)
if
!
clusterFound
{
logger
.
Errorf
(
"Unknown cluster: %s"
,
clusterName
)
return
nil
log
.
Infof
(
"Cluster '%s' has been updated"
,
clusterName
)
}
func
(
c
*
Controller
)
processDeleteEvent
(
workerID
uint32
,
clusterName
spec
.
NamespacedName
,
cl
*
cluster
.
Cluster
)
{
log
:=
c
.
logger
.
WithField
(
"worker"
,
workerID
)
log
.
Infof
(
"Deletion of the '%s' cluster started"
,
clusterName
)
if
err
:=
cl
.
Delete
();
err
!=
nil
{
logger
.
Errorf
(
"could not delete cluster '%s': %s"
,
clusterName
,
err
)
return
nil
log
.
Errorf
(
"could not delete cluster '%s': %s"
,
clusterName
,
err
)
return
}
close
(
c
.
stopChs
[
clusterName
])
...
...
@@ -152,14 +131,18 @@ func (c *Controller) processEvent(obj interface{}) error {
delete
(
c
.
stopChs
,
clusterName
)
c
.
clustersMu
.
Unlock
()
logger
.
Infof
(
"Cluster '%s' has been deleted"
,
clusterName
)
case
spec
.
EventSync
:
logger
.
Infof
(
"Syncing of the '%s' cluster started"
,
clusterName
)
log
.
Infof
(
"Cluster '%s' has been deleted"
,
clusterName
)
}
func
(
c
*
Controller
)
processSyncEvent
(
workerID
uint32
,
cl
*
cluster
.
Cluster
,
clusterFound
bool
,
clusterName
spec
.
NamespacedName
,
newSpec
*
spec
.
Postgresql
)
{
log
:=
c
.
logger
.
WithField
(
"worker"
,
workerID
)
log
.
Infof
(
"Syncing of the '%s' cluster started"
,
clusterName
)
// no race condition because a cluster is always processed by single worker
if
!
clusterFound
{
stopCh
:=
make
(
chan
struct
{})
cl
=
cluster
.
New
(
c
.
makeClusterConfig
(),
*
event
.
N
ewSpec
,
log
ger
)
cl
:
=
cluster
.
New
(
c
.
makeClusterConfig
(),
*
n
ewSpec
,
log
)
cl
.
Run
(
stopCh
)
c
.
clustersMu
.
Lock
()
...
...
@@ -170,12 +153,58 @@ func (c *Controller) processEvent(obj interface{}) error {
if
err
:=
cl
.
Sync
();
err
!=
nil
{
cl
.
Error
=
fmt
.
Errorf
(
"could not sync cluster '%s': %s"
,
clusterName
,
err
)
logger
.
Errorf
(
"%v"
,
cl
)
return
nil
log
.
Errorf
(
"%v"
,
cl
)
return
}
cl
.
Error
=
nil
logger
.
Infof
(
"Cluster '%s' has been synced"
,
clusterName
)
log
.
Infof
(
"Cluster '%s' has been synced"
,
clusterName
)
}
func
(
c
*
Controller
)
processEvent
(
obj
interface
{})
error
{
var
clusterName
spec
.
NamespacedName
event
,
ok
:=
obj
.
(
spec
.
ClusterEvent
)
if
!
ok
{
return
fmt
.
Errorf
(
"could not cast to ClusterEvent"
)
}
log
:=
c
.
logger
.
WithField
(
"worker"
,
event
.
WorkerID
)
if
event
.
EventType
==
spec
.
EventAdd
||
event
.
EventType
==
spec
.
EventSync
{
clusterName
=
util
.
NameFromMeta
(
event
.
NewSpec
.
Metadata
)
}
else
{
clusterName
=
util
.
NameFromMeta
(
event
.
OldSpec
.
Metadata
)
}
c
.
clustersMu
.
RLock
()
cl
,
clusterFound
:=
c
.
clusters
[
clusterName
]
c
.
clustersMu
.
RUnlock
()
switch
event
.
EventType
{
case
spec
.
EventAdd
:
if
clusterFound
{
log
.
Debugf
(
"Cluster '%s' already exists"
,
clusterName
)
return
nil
}
c
.
processAddEvent
(
event
.
WorkerID
,
clusterName
,
event
.
NewSpec
)
case
spec
.
EventUpdate
:
if
!
clusterFound
{
log
.
Warnf
(
"Cluster '%s' does not exist"
,
clusterName
)
return
nil
}
c
.
processUpdateEvent
(
event
.
WorkerID
,
clusterName
,
cl
,
event
.
NewSpec
)
case
spec
.
EventDelete
:
if
!
clusterFound
{
log
.
Errorf
(
"Unknown cluster: %s"
,
clusterName
)
return
nil
}
c
.
processDeleteEvent
(
event
.
WorkerID
,
clusterName
,
cl
)
case
spec
.
EventSync
:
c
.
processSyncEvent
(
event
.
WorkerID
,
cl
,
clusterFound
,
clusterName
,
event
.
NewSpec
)
}
return
nil
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment