Skip to content

Commit

Permalink
pkg/ansible,pkg/scaffold/ansible; enables leader election for ansible…
Browse files Browse the repository at this point in the history
… operators (operator-framework#904)

* pkg/ansible,pkg/scaffold/ansible; enables leader election for ansible operators

* Fix test-cluster and add some debug statements
  • Loading branch information
mhrivnak authored Jan 18, 2019
1 parent 3b86d1a commit b9401ae
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 3 deletions.
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
8 changes: 8 additions & 0 deletions commands/operator-sdk/cmd/test/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"github.com/operator-framework/operator-sdk/internal/util/fileutil"
k8sInternal "github.com/operator-framework/operator-sdk/internal/util/k8sutil"
"github.com/operator-framework/operator-sdk/internal/util/projutil"
"github.com/operator-framework/operator-sdk/pkg/k8sutil"
"github.com/operator-framework/operator-sdk/pkg/leader"
"github.com/operator-framework/operator-sdk/pkg/scaffold"
"github.com/operator-framework/operator-sdk/pkg/scaffold/ansible"
"github.com/operator-framework/operator-sdk/pkg/test"
Expand Down Expand Up @@ -109,6 +111,12 @@ func testClusterFunc(cmd *cobra.Command, args []string) error {
Env: []v1.EnvVar{{
Name: test.TestNamespaceEnv,
ValueFrom: &v1.EnvVarSource{FieldRef: &v1.ObjectFieldSelector{FieldPath: "metadata.namespace"}},
}, {
Name: k8sutil.OperatorNameEnvVar,
Value: "test-operator",
}, {
Name: leader.PodNameEnv,
ValueFrom: &v1.EnvVarSource{FieldRef: &v1.ObjectFieldSelector{FieldPath: "metadata.name"}},
}},
}},
},
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (b *BuildTestFrameworkAnsibleTestScript) GetInput() (input.Input, error) {

const buildTestFrameworkAnsibleTestScriptAnsibleTmpl = `#!/bin/sh
export WATCH_NAMESPACE=${TEST_NAMESPACE}
(/usr/local/bin/entrypoint > /tmp/operator.log 2>&1)&
(/usr/local/bin/entrypoint)&
trap "kill $!" SIGINT SIGTERM EXIT
cd ${HOME}/project
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}}"
`
2 changes: 1 addition & 1 deletion test/ansible-memcached/asserts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

- name: Wait 2 minutes for memcached deployment
debug:
msg: Waiting for memcached deployment...
var: deploy
until: deploy and deploy.status and deploy.status.replicas == deploy.status.get("availableReplicas", 0)
retries: 12
delay: 10
Expand Down

0 comments on commit b9401ae

Please sign in to comment.