github.com/mkimuram/operator-sdk@v0.7.1-0.20190410172100-52ad33a4bda0/internal/pkg/scaffold/ansible/molecule_test_local_playbook.go (about) 1 // Copyright 2018 The Operator-SDK Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package ansible 16 17 import ( 18 "path/filepath" 19 20 "github.com/operator-framework/operator-sdk/internal/pkg/scaffold" 21 "github.com/operator-framework/operator-sdk/internal/pkg/scaffold/input" 22 ) 23 24 const MoleculeTestLocalPlaybookFile = "playbook.yml" 25 26 type MoleculeTestLocalPlaybook struct { 27 input.Input 28 Resource scaffold.Resource 29 } 30 31 // GetInput - gets the input 32 func (m *MoleculeTestLocalPlaybook) GetInput() (input.Input, error) { 33 if m.Path == "" { 34 m.Path = filepath.Join(MoleculeTestLocalDir, MoleculeTestLocalPlaybookFile) 35 } 36 m.TemplateBody = moleculeTestLocalPlaybookAnsibleTmpl 37 38 return m.Input, nil 39 } 40 41 const moleculeTestLocalPlaybookAnsibleTmpl = `--- 42 43 - name: Build Operator in Kubernetes docker container 44 hosts: k8s 45 vars: 46 image_name: {{.Resource.FullGroup}}/{{.ProjectName}}:testing 47 tasks: 48 # using command so we don't need to install any dependencies 49 - name: Get existing image hash 50 command: docker images -q {{"{{image_name}}"}} 51 register: prev_hash 52 changed_when: false 53 54 - name: Build Operator Image 55 command: docker build -f /build/build/Dockerfile -t {{"{{ image_name }}"}} /build 56 register: build_cmd 57 changed_when: not prev_hash.stdout or (prev_hash.stdout and prev_hash.stdout not in ''.join(build_cmd.stdout_lines[-2:])) 58 59 - name: Converge 60 hosts: localhost 61 connection: local 62 vars: 63 ansible_python_interpreter: '{{ "{{ ansible_playbook_python }}" }}' 64 deploy_dir: "{{"{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/deploy"}}" 65 pull_policy: Never 66 REPLACE_IMAGE: {{.Resource.FullGroup}}/{{.ProjectName}}:testing 67 custom_resource: "{{"{{"}} lookup('file', '/'.join([deploy_dir, 'crds/{{.Resource.Group}}_{{.Resource.Version}}_{{.Resource.LowerKind}}_cr.yaml'])) | from_yaml {{"}}"}}" 68 tasks: 69 - block: 70 - name: Delete the Operator Deployment 71 k8s: 72 state: absent 73 namespace: '{{ "{{ namespace }}" }}' 74 definition: "{{"{{"}} lookup('template', '/'.join([deploy_dir, 'operator.yaml'])) {{"}}"}}" 75 register: delete_deployment 76 when: hostvars[groups.k8s.0].build_cmd.changed 77 78 - name: Wait 30s for Operator Deployment to terminate 79 k8s_facts: 80 api_version: '{{"{{"}} definition.apiVersion {{"}}"}}' 81 kind: '{{"{{"}} definition.kind {{"}}"}}' 82 namespace: '{{"{{"}} namespace {{"}}"}}' 83 name: '{{"{{"}} definition.metadata.name {{"}}"}}' 84 vars: 85 definition: "{{"{{"}} lookup('template', '/'.join([deploy_dir, 'operator.yaml'])) | from_yaml {{"}}"}}" 86 register: deployment 87 until: not deployment.resources 88 delay: 3 89 retries: 10 90 when: delete_deployment.changed 91 92 - name: Create the Operator Deployment 93 k8s: 94 namespace: '{{ "{{ namespace }}" }}' 95 definition: "{{"{{"}} lookup('template', '/'.join([deploy_dir, 'operator.yaml'])) {{"}}"}}" 96 97 - name: Create the {{.Resource.FullGroup}}/{{.Resource.Version}}.{{.Resource.Kind}} 98 k8s: 99 state: present 100 namespace: '{{ "{{ namespace }}" }}' 101 definition: "{{ "{{ custom_resource }}" }}" 102 103 - name: Wait 40s for reconciliation to run 104 k8s_facts: 105 api_version: '{{"{{"}} custom_resource.apiVersion {{"}}"}}' 106 kind: '{{"{{"}} custom_resource.kind {{"}}"}}' 107 namespace: '{{"{{"}} namespace {{"}}"}}' 108 name: '{{"{{"}} custom_resource.metadata.name {{"}}"}}' 109 register: cr 110 until: 111 - "'Successful' in (cr | json_query('resources[].status.conditions[].reason'))" 112 delay: 4 113 retries: 10 114 rescue: 115 - name: debug cr 116 ignore_errors: yes 117 failed_when: false 118 debug: 119 var: debug_cr 120 vars: 121 debug_cr: '{{"{{"}} lookup("k8s", 122 kind=custom_resource.kind, 123 api_version=custom_resource.apiVersion, 124 namespace=namespace, 125 resource_name=custom_resource.metadata.name 126 ){{"}}"}}' 127 128 - name: debug memcached lookup 129 ignore_errors: yes 130 failed_when: false 131 debug: 132 var: deploy 133 vars: 134 deploy: '{{"{{"}} lookup("k8s", 135 kind="Deployment", 136 api_version="apps/v1", 137 namespace=namespace, 138 label_selector="app=memcached" 139 ){{"}}"}}' 140 141 - name: get operator logs 142 ignore_errors: yes 143 failed_when: false 144 command: kubectl logs deployment/{{"{{"}} definition.metadata.name {{"}}"}} -n {{"{{"}} namespace {{"}}"}} 145 environment: 146 KUBECONFIG: '{{"{{"}} lookup("env", "KUBECONFIG") {{"}}"}}' 147 vars: 148 definition: "{{"{{"}} lookup('template', '/'.join([deploy_dir, 'operator.yaml'])) | from_yaml {{"}}"}}" 149 register: log 150 151 - debug: var=log.stdout_lines 152 153 - fail: 154 msg: "Failed on action: converge" 155 156 - import_playbook: '{{"{{ playbook_dir }}/../default/asserts.yml"}}' 157 `