workflow:
  id: openshift-pod-restart-remediation
  name: OpenShift Pod Restart Remediation
  description: Automatically restart failing pods and scale deployments based on alerts or manual triggers
  triggers:
    - type: manual
    - type: alert
      filters:
        - key: source
          value: openshift
        - key: pod_status
          value: CrashLoopBackOff
  steps:
    # Get pod details for a specific namespace
    - name: get-namespace-pods
      if: "{{ alert.namespace }}"
      provider:
        type: openshift
        config: "{{ providers.openshift }}"
        with:
          command_type: get_pods
          namespace: "{{ alert.namespace }}"

    # Get pod logs if specific pod is mentioned
    - name: get-failing-pod-logs
      if: "{{ alert.pod_name and alert.namespace }}"
      provider:
        type: openshift
        config: "{{ providers.openshift }}"
        with:
          command_type: get_logs
          namespace: "{{ alert.namespace }}"
          pod_name: "{{ alert.pod_name }}"
          tail_lines: 100

    # Get events for the namespace to understand issues
    - name: get-namespace-events
      if: "{{ alert.namespace }}"
      provider:
        type: openshift
        config: "{{ providers.openshift }}"
        with:
          command_type: get_events
          namespace: "{{ alert.namespace }}"

  actions:
    # Restart specific pod if mentioned in alert
    - name: restart-specific-pod
      if: "{{ alert.pod_name and alert.namespace }}"
      provider:
        type: openshift
        config: "{{ providers.openshift }}"
        with:
          action: restart_pod
          namespace: "{{ alert.namespace }}"
          pod_name: "{{ alert.pod_name }}"
          message: "Restarting pod due to {{ alert.pod_status | default('failure') }}"

    # Scale deployment if replica count is specified
    - name: scale-deployment
      if: "{{ alert.deployment_name and alert.namespace and alert.replicas }}"
      provider:
        type: openshift
        config: "{{ providers.openshift }}"
        with:
          action: scale_deployment
          namespace: "{{ alert.namespace }}"
          deployment_name: "{{ alert.deployment_name }}"
          replicas: "{{ alert.replicas }}"

    # Scale deployment config if specified
    - name: scale-deploymentconfig
      if: "{{ alert.deploymentconfig_name and alert.namespace and alert.replicas }}"
      provider:
        type: openshift
        config: "{{ providers.openshift }}"
        with:
          action: scale_deploymentconfig
          namespace: "{{ alert.namespace }}"
          deploymentconfig_name: "{{ alert.deploymentconfig_name }}"
          replicas: "{{ alert.replicas }}"

    # Rollout restart deployment
    - name: rollout-restart-deployment
      if: "{{ alert.deployment_name and alert.namespace and alert.restart_deployment }}"
      provider:
        type: openshift
        config: "{{ providers.openshift }}"
        with:
          action: rollout_restart
          kind: "deployment"
          name: "{{ alert.deployment_name }}"
          namespace: "{{ alert.namespace }}"

    # Rollout restart deployment config
    - name: rollout-restart-deploymentconfig
      if: "{{ alert.deploymentconfig_name and alert.namespace and alert.restart_deployment }}"
      provider:
        type: openshift
        config: "{{ providers.openshift }}"
        with:
          action: rollout_restart
          kind: "deploymentconfig"
          name: "{{ alert.deploymentconfig_name }}"
          namespace: "{{ alert.namespace }}"

    # Report remediation actions taken
    - name: report-actions
      provider:
        type: console
        with:
          message: |
            🔧 OpenShift Remediation Actions Completed:
            {% if alert.pod_name %}
            - Restarted pod: {{ alert.pod_name }} in {{ alert.namespace }}
            {% endif %}
            {% if alert.deployment_name and alert.replicas %}
            - Scaled deployment {{ alert.deployment_name }} to {{ alert.replicas }} replicas
            {% endif %}
            {% if alert.deploymentconfig_name and alert.replicas %}
            - Scaled DeploymentConfig {{ alert.deploymentconfig_name }} to {{ alert.replicas }} replicas
            {% endif %}
            {% if alert.restart_deployment %}
            - Performed rollout restart on {{ alert.deployment_name or alert.deploymentconfig_name }}
            {% endif %}

# Example alert payloads:

# Restart specific pod:
# {
#   "source": "openshift",
#   "namespace": "production",
#   "pod_name": "web-app-789-xyz",
#   "pod_status": "CrashLoopBackOff"
# }

# Scale deployment:
# {
#   "source": "openshift", 
#   "namespace": "production",
#   "deployment_name": "web-app",
#   "replicas": 5
# }

# Scale deployment config:
# {
#   "source": "openshift",
#   "namespace": "staging",
#   "deploymentconfig_name": "api-server", 
#   "replicas": 3
# }

# Rollout restart deployment:
# {
#   "source": "openshift",
#   "namespace": "production",
#   "deployment_name": "web-app",
#   "restart_deployment": true
# }