github.com/joelanford/operator-sdk@v0.8.2/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  	m.Delims = AnsibleDelims
    38  
    39  	return m.Input, nil
    40  }
    41  
    42  const moleculeTestLocalPlaybookAnsibleTmpl = `---
    43  
    44  - name: Build Operator in Kubernetes docker container
    45    hosts: k8s
    46    vars:
    47      image_name: [[.Resource.FullGroup]]/[[.ProjectName]]:testing
    48    tasks:
    49    # using command so we don't need to install any dependencies
    50    - name: Get existing image hash
    51      command: docker images -q {{ image_name }}
    52      register: prev_hash
    53      changed_when: false
    54  
    55    - name: Build Operator Image
    56      command: docker build -f /build/build/Dockerfile -t {{ image_name }} /build
    57      register: build_cmd
    58      changed_when: not prev_hash.stdout or (prev_hash.stdout and prev_hash.stdout not in ''.join(build_cmd.stdout_lines[-2:]))
    59  
    60  - name: Converge
    61    hosts: localhost
    62    connection: local
    63    vars:
    64      ansible_python_interpreter: '{{ ansible_playbook_python }}'
    65      deploy_dir: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/deploy"
    66      pull_policy: Never
    67      REPLACE_IMAGE: [[.Resource.FullGroup]]/[[.ProjectName]]:testing
    68      custom_resource: "{{ lookup('file', '/'.join([deploy_dir, 'crds/[[.Resource.Group]]_[[.Resource.Version]]_[[.Resource.LowerKind]]_cr.yaml'])) | from_yaml }}"
    69    tasks:
    70    - block:
    71      - name: Delete the Operator Deployment
    72        k8s:
    73          state: absent
    74          namespace: '{{ namespace }}'
    75          definition: "{{ lookup('template', '/'.join([deploy_dir, 'operator.yaml'])) }}"
    76        register: delete_deployment
    77        when: hostvars[groups.k8s.0].build_cmd.changed
    78  
    79      - name: Wait 30s for Operator Deployment to terminate
    80        k8s_facts:
    81          api_version: '{{ definition.apiVersion }}'
    82          kind: '{{ definition.kind }}'
    83          namespace: '{{ namespace }}'
    84          name: '{{ definition.metadata.name }}'
    85        vars:
    86          definition: "{{ lookup('template', '/'.join([deploy_dir, 'operator.yaml'])) | from_yaml }}"
    87        register: deployment
    88        until: not deployment.resources
    89        delay: 3
    90        retries: 10
    91        when: delete_deployment.changed
    92  
    93      - name: Create the Operator Deployment
    94        k8s:
    95          namespace: '{{ namespace }}'
    96          definition: "{{ lookup('template', '/'.join([deploy_dir, 'operator.yaml'])) }}"
    97  
    98      - name: Create the [[.Resource.FullGroup]]/[[.Resource.Version]].[[.Resource.Kind]]
    99        k8s:
   100          state: present
   101          namespace: '{{ namespace }}'
   102          definition: '{{ custom_resource }}'
   103  
   104      - name: Wait 40s for reconciliation to run
   105        k8s_facts:
   106          api_version: '{{ custom_resource.apiVersion }}'
   107          kind: '{{ custom_resource.kind }}'
   108          namespace: '{{ namespace }}'
   109          name: '{{ custom_resource.metadata.name }}'
   110        register: cr
   111        until:
   112        - "'Successful' in (cr | json_query('resources[].status.conditions[].reason'))"
   113        delay: 4
   114        retries: 10
   115      rescue:
   116      - name: debug cr
   117        ignore_errors: yes
   118        failed_when: false
   119        debug:
   120          var: debug_cr
   121        vars:
   122          debug_cr: '{{ lookup("k8s",
   123            kind=custom_resource.kind,
   124            api_version=custom_resource.apiVersion,
   125            namespace=namespace,
   126            resource_name=custom_resource.metadata.name
   127          )}}'
   128  
   129      - name: debug memcached lookup
   130        ignore_errors: yes
   131        failed_when: false
   132        debug:
   133          var: deploy
   134        vars:
   135          deploy: '{{ lookup("k8s",
   136            kind="Deployment",
   137            api_version="apps/v1",
   138            namespace=namespace,
   139            label_selector="app=memcached"
   140          )}}'
   141  
   142      - name: get operator logs
   143        ignore_errors: yes
   144        failed_when: false
   145        command: kubectl logs deployment/{{ definition.metadata.name }} -n {{ namespace }}
   146        environment:
   147          KUBECONFIG: '{{ lookup("env", "KUBECONFIG") }}'
   148        vars:
   149          definition: "{{ lookup('template', '/'.join([deploy_dir, 'operator.yaml'])) | from_yaml }}"
   150        register: log
   151  
   152      - debug: var=log.stdout_lines
   153  
   154      - fail:
   155          msg: "Failed on action: converge"
   156  
   157  - import_playbook: '{{ playbook_dir }}/../default/asserts.yml'
   158  `