Red Hat OpenShift Container Platform is an Enterprise Kubernetes offering by Red Hat, that allows users to deploy cloud native applications as well as manage lifecycle of microservices deployed in containers. OpenShift Online is a SaaS offering for OpenShift provided by Red Hat. OpenShift Online takes away the effort required to set up your OpenShift clusters on-prem and allows organizations to quickly leverage all that OpenShift offers, including Developer console, without worry about managing the underlying infrastructure.
OpenShift Online provides REST based APIs for all functions that can be carried out via the console, and the oc command line. Therefore, teams can build automated functionality that leverages Kubernetes cluster using OpenShift management plane. Today, we will look at one such function – to create a Project. Any user that wants to create a project using APIs is required to have appropriate role bindings in the specific namespace that they want to create or manage projects in. By default, OpenShift Online provides you the ability to create Projects via the console, using the ProjectRequest API call.
Assuming you have the oc command line setup, the command to create a project is:
$ oc new-project <project_name>
--description="<description>" --display-name="<display_name>"
We will take a look at how to create a Project in OpenShift Online using the REST API. We will be using Postman to trigger our API call. This sample was run against OpenShift v3.11, Postman v7.30.1.
1) The first thing we will do is log into our OpenShift Online console, and on the top right section in the drop-down that shows up under your name, select ‘Copy Login Command’. Paste the copied contents into Notepad and capture the ‘token’ value.
2) Download and import the Postman collection for this sample API call here
3) Paste the copied token value under ‘Authorization’ section of the request
4) Update the sections in bold for an appropriate name you want your Project to have
{
"kind": "ProjectRequest",
"apiVersion": "v1",
"displayName": "82520759",
"description": "test project from postman",
"metadata": {
"labels": {
"name": "82520759",
"namespace": "82520759"
},
"name": "82520759"
}
}
5) Execute the Postman call. You should now see a new project created under your OpenShift Online instance.
You can adjust the Body of the sample call to pass in more values associated with the ProjectRequest object. For reference, the object schema includes the below
https://docs.openshift.com/container-platform/3.11/rest_api/oapi/v1.ProjectRequest.html
apiVersion:
description:
displayName:
kind:
metadata:
annotations:
clusterName:
creationTimestamp:
deletionGracePeriodSeconds:
deletionTimestamp:
finalizers:
generateName:
generation:
initializers:
labels:
name:
namespace:
ownerReferences:
resourceVersion:
selfLink:
uid:
Once you’ve unit tested the REST call with Postman for your OpenShift Online environment, you can very easily port this over to using one of the existing modules in Ansible, and making it a step within your playbook.
If you have any questions or comments on the tutorial content above, or run in to specific errors not covered here, please feel free to reach out to [email protected]
Anuj Tuli is the chief technology officer at Keyva. In this role, he specializes in developing and delivering vendor-agnostic solutions that avoid the “rip-and-replace” of existing IT investments. Tuli helps customers chart a prescriptive strategy for Application Containerization, CI/CD Pipeline Implementations, API abstraction, Application Modernization, and Cloud Automation integrations. He leads the development and management of Cloud Automation IP and related professional services. With an application developer background, he provides a hands-on perspective towards various technologies.
Like what you read? Follow Anuj on LinkedIn.
Join the Keyva Community! Follow Keyva on LinkedIn at: