OPen Shift template YAML file usage and example - unix1998/technical_notes GitHub Wiki

In OpenShift, a Template resource YAML can indeed be used to create and manage a wide variety of other resource kinds by defining parameters and substituting values for those parameters within the template. The flexibility of the Template resource YAML is not limited to specific resource types such as Build, BuildConfig, Deployment, or DeploymentConfig; it can encompass virtually any resource kind supported by OpenShift or Kubernetes.

How OpenShift Templates Work

  1. Parameters: Templates can define a list of parameters, each with a name, description, default value, and other attributes. These parameters act as placeholders within the template.

  2. Objects: The template contains a list of objects, which can be any valid OpenShift resource (e.g., Pod, Service, Route, ConfigMap, Secret).

  3. Substitution: When the template is processed, the parameters are substituted with their corresponding values, and the resulting objects are created in the OpenShift project.

Example Template Resource YAML

Here's a simplified example of a Template resource YAML that includes multiple resource kinds:

apiVersion: template.openshift.io/v1
kind: Template
metadata:
  name: my-app-template
parameters:
  - name: APP_NAME
    description: The name of the application
    required: true
  - name: APP_IMAGE
    description: The Docker image for the application
    required: true
objects:
  - apiVersion: v1
    kind: Service
    metadata:
      name: ${APP_NAME}
    spec:
      ports:
        - port: 80
      selector:
        app: ${APP_NAME}
  - apiVersion: v1
    kind: Deployment
    metadata:
      name: ${APP_NAME}
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: ${APP_NAME}
      template:
        metadata:
          labels:
            app: ${APP_NAME}
        spec:
          containers:
            - name: ${APP_NAME}
              image: ${APP_IMAGE}
              ports:
                - containerPort: 80
  - apiVersion: v1
    kind: Route
    metadata:
      name: ${APP_NAME}
    spec:
      to:
        kind: Service
        name: ${APP_NAME}

Creating Resources with a Template

To create resources using a template, you can use the oc process command to process the template and then the oc apply or oc create command to create the resources. Here's an example:

oc process -f my-app-template.yaml -p APP_NAME=my-app -p APP_IMAGE=my-app-image:latest | oc apply -f -

Flexibility of OpenShift Templates

OpenShift Templates are highly flexible and can be used to define any combination of resources needed for an application. They are not limited to Build, BuildConfig, Deployment, or DeploymentConfig resources. This flexibility allows you to create comprehensive deployment configurations that include everything from Pods and Services to Routes, PersistentVolumeClaims, Secrets, and ConfigMaps.

Summary

In summary, OpenShift Template resource YAML can be used to create a wide variety of resource kinds by defining parameters and substituting their values within the template. It is not limited to specific resource types, providing a powerful tool for managing complex deployments.

we can think of the Template section as an "instruction mode content" that guides the processing of the template but is not included in the final processed YAML. This final YAML only contains the actual Kubernetes or OpenShift resources with the specified parameters substituted.

To summarize again:

Template YAML: Contains the template with parameters and resource definitions (like Deployment, Service, Route). Processing the Template: oc process substitutes the parameters and outputs the resources defined in the objects section without including the Template itself. Applying the Resources: oc apply takes the processed YAML and creates the resources in the OpenShift cluster.