Skip to content

Environment Template

In this section, you will develop an "environment template" that will comprise the components described below. You will create these as declarative specs in Git and use Rafay's "Git-to-System" synchronization to load them into your Rafay Org. As you can see from the architecture diagram below, the entire experience is powered using an environment template that encapsulates and templatizes the custom application.

Architecture

  • A config context storing Rafay Credentials
  • A resource template which will deploy a Webserver workload onto a cluster
  • A environment template which will deploy the resource template

You will implement the steps described below

  1. You will first create and update the files locally on your machine.
  2. You will push them to a new branch in your Git repository.
  3. Once the files are loaded in the new branch, you will merge the new branch into the main branch. The main branch is actively monitored for changes by Rafay's GitOps pipeline.
  4. This will trigger the automated loading of the resources in your Rafay Org.

Info

When you launch an instance of an environment based on this environment template it will execute the configured Infrastructure as Code (IaC) file. In our example, the IaC will deploy a webserver onto a cluster. The websever will print "Hello < Input_Name >", where Input_Name is a variable defined by the user deploying the template.


Step 1: Download Files

In this step, you will download and extract a ZIP archive which contains the specification files of the EM resources for this part of the guide along with the Infrastructure as Code files that are used by the Environment Manager resources.

Download

  • Download the archive intermediate-part1.zip
  • Extract this archive on your local machine

You will use these files in the subsequent steps.


Step 2: View/Update Config Context

In this step, we will review and update the config context declarative spec file. In our example, this file stores two environment variables with one variable configured to contain sensitive data.

  • In your extracted archive from step 1, navigate to intermediate-part1/projects/em-gs/configcontexts/
  • View the file named rafay-configcontext.yaml in this directory
  • Update any fields to match your environment if different names were used. You do not need to update the value UPDATEVALUE as this will be updated later through the UI.
apiVersion: eaas.envmgmt.io/v1
kind: ConfigContext
metadata:
  name: rafay-configcontext
  project: em-gs
spec:
  envs:
  - key: RCTL_API_KEY
    options:
      mask: true
      override:
        type: notallowed
      sensitive: true
    value: UPDATEVALUE
  - key: RCTL_API_SECRET
    options:
      mask: true
      override:
        type: notallowed
      sensitive: true
    value: UPDATEVALUE
  - key: RCTL_PROJECT
    options:
      override:
        type: notallowed
    value: defaultproject
  - key: RCTL_REST_ENDPOINT
    options:
      override:
        type: notallowed
    value: console.rafay.dev

Step 3: View/Update Resource Template

In this step, we will view and update the resource template. This resource template points to OpenTofu based IaC within the ZIP archive. The IaC files will be pushed to the Git repository, where the GitOps Agent will access and execute them when called.

  • In your extracted archive from step 1, navigate to intermediate-part1/projects/em-gs/resourcetemplates/
  • View the file named em-rt-intermediate.yaml in this directory
  • Update any fields to match your environment if different names were used
apiVersion: eaas.envmgmt.io/v1
kind: ResourceTemplate
metadata:
  name: em-rt-intermediate
  project: em-gs
spec:
  agents:
  - name: em-gs
  contexts:
  - name: rafay-configcontext
  provider: opentofu
  providerOptions:
    openTofu:
      backendType: system
  repositoryOptions:
    branch: main
    directoryPath: /terraform/intermediate/
    name: em-gs
  version: v1
  versionState: draft

Step 4: View/Update Environment Template

In this step, we will view and update the environment template declarative spec file. Note that this references the resource template from the prior step.

  • In your extracted archive from step 1, navigate to em-gs-intermediate-part1/projects/em-gs/environmenttemplates/
  • View the file named em-et-intermediate.yaml in this directory
  • Update any fields to match your environment if different names were used
apiVersion: eaas.envmgmt.io/v1
kind: EnvironmentTemplate
metadata:
  name: em-et-intermediate
  project: em-gs
spec:
  agents:
  - name: em-gs
  resources:
  - kind: resourcetemplate
    name: em-rt-intermediate
    resourceOptions:
      version: v1
    type: dynamic
  variables:
  - name: input_name
    options:
      displayMetadata:
        disabled: false
        section: ""
        sectionDescription: ""
        tooltip: ""
        weight: ""
      override:
        type: allowed
      required: true
    valueType: text
  - name: project
    options:
      displayMetadata:
        disabled: false
        section: ""
        sectionDescription: ""
        tooltip: ""
        weight: ""
      override:
        type: notallowed
    value: $(environment.project.name)$
    valueType: expression
  - name: cluster_name
    options:
      displayMetadata:
        disabled: false
        section: ""
        sectionDescription: ""
        tooltip: ""
        weight: ""
      override:
        type: allowed
      required: true
    valueType: text
  version: v1
  versionState: draft

Step 5: Create and Merge Git Branch

Now, you will create a new branch in your Git repository and copy the contents of the extracted ZIP archive to the branch. You will then merge this new branch into the main branch where Rafay's GitOps will detect the changes.

  • Create a new branch in your Git repository
  • Click Add file -> Upload files
  • Add the folders named projects and terraform that contain the files you viewed/updated
  • Click Commit changes

Merge Branch

Once the changes are pushed into the new branch, we will now merge the changes into the main branch where Rafay GitOps will detect the changes and create the resources in your Rafay Org.

  • Click Compare & pull request

Merge Branch

  • Click Create pull request

Merge Branch

  • Click Merge pull request

Merge Branch

  • Click Confirm merge

Step 6: Update Config Context

Now, you will go back into the Rafay Console and validate that the Config Context resource has been created. You will then update the sensitive variable value from the console. This will then encrypt the sensitive value and automatically write back to the Git repository. The values required to populate the config context can be found in the CLI Config file which can be downloaded from Home -> My Tools -> Download CLI Config.

  • In your project, navigate to Environments -> Config Contexts
  • Edit the newly created config context named rafay-configcontext
  • Navigate to Environment Variables
  • Edit the variable named RCTL_API_KEY
  • Populate the Value field with the API Key
  • Click Save
  • Edit the variable named RCTL_API_SECRET
  • Populate the Value field with the API Key
  • Click Save
  • Click Save again

Config Context


Step 7: Validate Templates

In this step, you will validate that Rafay GitOps created the templates within Environment Manager.

  • In your project, navigate to Environments -> Resource Templates

You will see the newly created resource template.

Resource Template

  • In your project, navigate to Environments -> Environment Templates

You will see the newly created environment template.

Environment Template


Recap

In this part, you used Rafay GitOp's Git to System synchronization capability to create an environment template.