Skip to content

Commit

Permalink
init observe func (#57)
Browse files Browse the repository at this point in the history
Signed-off-by: Fahed DORGAA <[email protected]>
  • Loading branch information
fahedouch authored May 20, 2022
1 parent 750b662 commit 20ba32d
Show file tree
Hide file tree
Showing 15 changed files with 588 additions and 327 deletions.
28 changes: 8 additions & 20 deletions apis/v1alpha1/ansibleRun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,18 @@ type Var struct {

// AnsibleRunParameters are the configurable fields of a AnsibleRun.
type AnsibleRunParameters struct {
// The configuration of this AnsibleRun; i.e. the configuration containing its playbook(s)/Role(s)
// files. When the AnsibleRun's Provider source is 'Remote' (the default) this can be
// any address supported by Ansible.Builtin.git,
// TODO support other remotes https://docs.ansible.com/ansible/latest/collections/ansible/builtin/index.html
// When the AnsibleRun's source is 'Inline' the
// content of a simple playbook.yml file may be written inline.
Module string `json:"module"`

// Source of configuration of this AnsibleRun.
Source ConfigurationSource `json:"source"`

// This is the playbook name. This playbook is expected to be simply a way to call roles.
// The inline configuration of this AnsibleRun; the content of a simple playbook.yml file may be written inline.
// This field is mutually exclusive with the “role” field.
// For remote source, the playbook is expected to be in the remote project directory.
// this filed has non effect on inline mode
// +optional
Playbook string `json:"playbook,omitempty"`
PlaybookInline *string `json:"playbookInline"`

// Specifies a role to be executed. This field is mutually exclusive with the “playbook” field. For remote source This field can be:
// - a relative path within the project working directory
// - a relative path within one of the directories specified by ANSIBLE_ROLES_PATH environment variable or ansible-roles-path flag.
// this filed has non effect on inline mode
// The remote configuration of this AnsibleRun; the content can be retrieved from Ansible Galaxy as community contents
// +optional
Role string `json:"role,omitempty"`
Roles []string `json:"roles"`

// The remote configuration of this AnsibleRun; the content can be retrieved from Ansible Galaxy as community contents
// +optional
Playbooks []string `json:"playbooks"`

// Configuration variables.
// +optional
Expand Down
15 changes: 15 additions & 0 deletions apis/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cmd/provider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func main() {
app = kingpin.New(filepath.Base(os.Args[0]), "Template support for Crossplane.").DefaultEnvars()
debug = app.Flag("debug", "Run with debug logging.").Short('d').Bool()
ansibleCollectionsPath = app.Flag("ansible-collections-path", "Path where ansible collections are installed.").String()
ansibleRolesPath = app.Flag("ansible-roles-path", "Path where role(s) exists.").String()
ansibleRolesPath = app.Flag("ansible-roles-path", "Path where role(s) exists.").Envar("DEFAULT_ROLES_PATH").Envar("ANSIBLE_ROLES_PATH").String()
syncPeriod = app.Flag("sync", "Controller manager sync period such as 300ms, 1.5h, or 2h45m").Short('s').Default("1h").Duration()
leaderElection = app.Flag("leader-election", "Use leader election for the controller manager.").Short('l').Default("false").OverrideDefaultFromEnvar("LEADER_ELECTION").Bool()
)
Expand Down
4 changes: 3 additions & 1 deletion docs/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,9 @@ In this case, we define the desired state as source of truth for our OpenShift c

The annotation used to specify the policy information is not part of the desired state or source of truth. It is really just a small chunk of metadata that instructs the Ansible provider how to trigger the Ansible role.

When user creates the `AnsibleRun` resource, it means they claim to request the cluster. This will trigger the Ansible role in `Observe()`. When user deletes the `AnsibleRun` resource, it means they claim to drop the cluster. This will trigger the same Ansible role in `Delete()` to clean the cluster.
When user creates the `AnsibleRun` resource, it means they claim to request the cluster. This will trigger the Ansible role in `Observe()`.
When user edits the `AnsibleRun` resource, it means they claim to update the cluster. This will trigger the Ansible role in `Observe()`.
When user deletes the `AnsibleRun` resource, it means they claim to drop the cluster. This will trigger the same Ansible role in `Delete()` to clean the cluster.

In order to differentiate the presence or absence of `AnsibleRun`, a special variable will be sent to the Ansible role when it starts to run:

Expand Down
26 changes: 26 additions & 0 deletions examples/ansible/ansibleRun-inline-advanced.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: ansible.crossplane.io/v1alpha1
kind: AnsibleRun
metadata:
annotations:
ansible.crossplane.io/runPolicy: ObserveAndDelete
name: gcpbucket
spec:
forProvider:
# AnsibleRun default to using a remote source - like ansibleRun-remote.yaml.
# For simple cases you can use an inline source to specify the content of
# playbook.yaml as opaque, inline yaml.
# playbook source : https://docs.ansible.com/ansible/latest/collections/google/cloud/gcp_storage_bucket_module.html#examples
playbookInline: |
---
- hosts: localhost
tasks:
- name: create a gcp bucket
google.cloud.gcp_storage_bucket:
name: ansible-storage-module
project: test-project
auth_kind: serviceaccount
service_account_file: gcp-credentials.json
state: present
when: ansible_provider_meta.gcpbucket.state == 'present'
providerConfigRef:
name: default
7 changes: 4 additions & 3 deletions examples/ansible/ansibleRun-inline.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
apiVersion: ansible.crossplane.io/v1alpha1
kind: AnsibleRun
metadata:
annotations:
ansible.crossplane.io/runPolicy: ObserveAndDelete
name: example
spec:
forProvider:
# PlaybookSet default to using a remote source - like playbookSet-remote.yaml.
# AnsibleRun default to using a remote source - like ansibleRun-remote.yaml.
# For simple cases you can use an inline source to specify the content of
# playbook.yaml as opaque, inline yaml.
source: Inline
module: |
playbookInline: |
---
- hosts: localhost
tasks:
Expand Down
2 changes: 1 addition & 1 deletion examples/provider/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
name: gcp-credentials
type: Opaque
data:
# credentials: BASE64ENCODED_PROVIDER_CREDS
credentials: BASE64ENCODED_PROVIDER_CREDS
---
apiVersion: ansible.crossplane.io/v1alpha1
kind: ProviderConfig
Expand Down
43 changes: 12 additions & 31 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,53 @@ require (
github.com/crossplane/crossplane-runtime v0.15.1
github.com/crossplane/crossplane-tools v0.0.0-20210320162312-1baca298c527
github.com/google/go-cmp v0.5.8
github.com/hashicorp/go-getter v1.5.11
github.com/pkg/errors v0.9.1
github.com/spf13/afero v1.8.2
gopkg.in/alecthomas/kingpin.v2 v2.2.6
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gotest.tools/v3 v3.2.0
k8s.io/api v0.24.0
k8s.io/apimachinery v0.24.0
k8s.io/client-go v0.24.0
sigs.k8s.io/controller-runtime v0.12.1
sigs.k8s.io/controller-tools v0.8.0
)

require gopkg.in/yaml.v2 v2.4.0

require (
cloud.google.com/go v0.81.0 // indirect
cloud.google.com/go/storage v1.14.0 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect
github.com/aws/aws-sdk-go v1.15.78 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/dave/jennifer v1.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful v2.9.5+incompatible // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/fatih/color v1.12.0 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/go-logr/logr v1.2.0 // indirect
github.com/go-logr/logr v1.2.2 // indirect
github.com/go-logr/zapr v1.2.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.5 // indirect
github.com/go-openapi/swag v0.19.14 // indirect
github.com/gobuffalo/flect v0.2.3 // indirect
github.com/go-openapi/swag v0.19.15 // indirect
github.com/gobuffalo/flect v0.2.4 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/googleapis/gax-go/v2 v2.0.5 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/go-version v1.1.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/jstemmer/go-junit-report v0.9.1 // indirect
github.com/klauspost/compress v1.11.2 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.0.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
Expand All @@ -73,31 +62,23 @@ require (
github.com/prometheus/procfs v0.7.3 // indirect
github.com/spf13/cobra v1.4.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/ulikunitz/xz v0.5.8 // indirect
go.opencensus.io v0.23.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.19.1 // indirect
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
golang.org/x/tools v0.1.10-0.20220218145154-897bd77cd717 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
google.golang.org/api v0.44.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220107163113-42d7afdf6368 // indirect
google.golang.org/grpc v1.40.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
k8s.io/api v0.24.0 // indirect
k8s.io/apiextensions-apiserver v0.24.0 // indirect
k8s.io/component-base v0.24.0 // indirect
k8s.io/klog/v2 v2.60.1 // indirect
Expand Down
Loading

0 comments on commit 20ba32d

Please sign in to comment.