Skip to content

pkg/ansible,pkg/scaffold/ansible; enables leader election for ansible operators #904

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

Merged
merged 5 commits into from
Jan 18, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
- New commands [`operator-sdk run ansible`](https://github.com/operator-framework/operator-sdk/blob/master/doc/sdk-cli-reference.md#ansible) and [`operator-sdk run helm`](https://github.com/operator-framework/operator-sdk/blob/master/doc/sdk-cli-reference.md#helm) which run the SDK as ansible and helm operator processes, respectively. These are intended to be used when running in a Pod inside a cluster. Developers wanting to run their operator locally should continue to use `up local`. ([#887](https://github.com/operator-framework/operator-sdk/pull/887) and [#897](https://github.com/operator-framework/operator-sdk/pull/897))
- Ansible operator proxy added the cache handler which allows the get requests to use the operators cache. [#760](https://github.com/operator-framework/operator-sdk/pull/760)
- Ansible operator proxy added ability to dynamically watch dependent resource that were created by ansible operator. [#857](https://github.com/operator-framework/operator-sdk/pull/857)
- Ansible-based operators have leader election turned on by default. When upgrading, add environment variable `POD_NAME` to your operator's Deployment using the Kubernetes downward API. To see an example, run `operator-sdk new --type=ansible ...` and see file `deploy/operator.yaml`.

### Changed

- The official images for the Ansible and Helm operators have moved! Travis now builds, tags, and pushes operator base images during CI ([#832](https://github.com/operator-framework/operator-sdk/pull/832)).
Expand Down
16 changes: 15 additions & 1 deletion pkg/ansible/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
package ansible

import (
"context"
"os"
"runtime"

aoflags "github.com/operator-framework/operator-sdk/pkg/ansible/flags"
"github.com/operator-framework/operator-sdk/pkg/ansible/operator"
proxy "github.com/operator-framework/operator-sdk/pkg/ansible/proxy"
"github.com/operator-framework/operator-sdk/pkg/k8sutil"
"github.com/operator-framework/operator-sdk/pkg/leader"
sdkVersion "github.com/operator-framework/operator-sdk/version"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

Expand All @@ -42,6 +44,8 @@ func printVersion() {
func Run(flags *aoflags.AnsibleOperatorFlags) {
logf.SetLogger(logf.ZapLogger(false))

printVersion()

namespace, found := os.LookupEnv(k8sutil.WatchNamespaceEnvVar)
if found {
log.Infof("Watching %v namespace.", namespace)
Expand All @@ -58,7 +62,17 @@ func Run(flags *aoflags.AnsibleOperatorFlags) {
log.Fatal(err)
}

printVersion()
name, found := os.LookupEnv(k8sutil.OperatorNameEnvVar)
if !found {
log.Fatal("OPERATOR_NAME environment variable not set")
}
// Become the leader before proceeding
err = leader.Become(context.TODO(), name+"-lock")
if err != nil {
log.Error(err, "")
os.Exit(1)
}

done := make(chan error)
cMap := proxy.NewControllerMap()

Expand Down
4 changes: 4 additions & 0 deletions pkg/scaffold/ansible/deploy_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ spec:
fieldRef:
fieldPath: metadata.namespace
{{- end}}
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: OPERATOR_NAME
value: "{{.ProjectName}}"
`