Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor operator in preparation for leader-election #6

Merged
merged 8 commits into from
Sep 7, 2021
Merged

Conversation

juliaogris
Copy link
Contributor

@juliaogris juliaogris commented Sep 6, 2021

Refactor, reformat, rename and reorganise markdown, yaml and go code
files in preparation for leader election. This PR was created as all
non-functional changes for the leader-election implementation so that
leader-election is purely functional and can be more easily reviewed
with care (see #7 ).

Issue: #8

Reformat markdown files with prettier 2.3.2. Format all the things
because automating what can be automated is grand. Sadly all go
alternatives investigated for markdown formatting fall short of
prettier.
@juliaogris juliaogris changed the title Implement leader election Refactor operator in preparation for leader-election Sep 6, 2021
Copy link

@camh- camh- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one teeny tiny thing.

deployment/transflect.yaml Outdated Show resolved Hide resolved
Julia Ogris added 6 commits September 6, 2021 20:41
Reformat transflect deployment yaml file for the ClusterRole to read
more compact. In a follow-up commit we will add a further CluterRole
rule for `leases.coordination.k8s.io`. It is easier to take these in a
more compact format me thinks.
Refactor operator main and move probes server into separate file to keep
the main file lean. This is in preparation for adding leader election
code which will require a bit more rework.
Update inline code comments comment for readability.
Rework operator initialisation so that on `start` call non-constant
operator fields and structures, such as informers, stopper channel get
re-initialised. This is a preparatory step for adding leader-election,
where these structures need to be re-initialised when leadership /
execution is re-gained.
Change function order for readability. This a pure code reorganisation
within the operator.go file on a function level without any further
additions.

This is another step towards adding leader election. Readability has
been optimised for the final code listing with leader-election, however
to keep the leader-election change small, pure code movements have been
moved out to this commit.

Signed-off-by: Julia Ogris <juliao@squareup.com>
Reorganise Replicaset/Filter processing in opertor.go, for new workloads
retrieved from the workqueue. This is again purely a refactor without
any functional additions.

log.Debug().Msg("Starting: informer event handlers registered")
// Run workers and informers
o.runWorkers(42)
o.rsInformer.Informer().Run(o.stopper)
o.deployInformer.Informer().Run(o.stopper)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two calls to Run() are not done asynchronously. From the Run docs:

Run starts and runs the shared informer, returning after it stops.

So the deployInformer does not start until the rsInformer stops.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yesss!! thanks for the eagle eye yet again.

Fix informer call to start in separate go routine so that the deployment
informer is actually executed concurrently with the replicaset
informer. oups.
Copy link

@camh- camh- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🍏

@preetim-cash preetim-cash linked an issue Sep 7, 2021 that may be closed by this pull request
@juliaogris juliaogris merged commit c0b346f into master Sep 7, 2021
@juliaogris juliaogris deleted the leader branch September 7, 2021 00:24
juliaogris pushed a commit that referenced this pull request Sep 9, 2021
Add leader-election to operator so that several operators may be ready
in a cluster, but only one at a time executes.

Add cleanup on cancelled context to probes server and signal handler, to
ensure quick leaser release and handover.

This change leans heavily on k8s client-go leader-election example.
https://github.com/kubernetes/client-go/blob/a31b18a6ac98bbff0dd1055707dfde1b0a98ff68/examples/leader-election/main.go

There are no tests for the leader election, but I have manually tested it.

This PR is a re-creation of #7 which got deleted as it was a stacked PR with #6 and not rebased before merge/delete of base branch.

issue: #8

This merges the following commits:
* Add leader-election to operator

     Makefile                            |  2 +-
     README.md                           |  1 +
     cmd/transflect-operator/main.go     | 33 +++++-----
     cmd/transflect-operator/operator.go | 97 ++++++++++++++++++++++++-----
     cmd/transflect-operator/probes.go   |  6 +-
     deployment/transflect.yaml          |  7 +++
     go.mod                              |  1 +
     go.sum                              |  2 +
     8 files changed, 117 insertions(+), 32 deletions(-)

Pull-Request: #9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add leader election to transflect
3 participants