1
0
mirror of https://github.com/tektoncd/catalog.git synced 2024-11-21 05:55:35 +00:00
catalog/task/openshift-client-python/0.1
Quan Zhang e294e1246b [TEP-0110] Update Tekton Catalog installation instructions
Prior to this change, the installation instructions directly use the resource urls in the Catalog repo, which results in tight coupling between the organization and how users fetch resources (as described in TEP-0110). This commit updates the installation guide to install Tekton Catalog resources via Tekton Hub Api: https://github.com/tektoncd/hub/pull/539

This change decouples the Tekton Catalog organization from resouces resolution, which enables Tekton Catalog reorganization.
2022-08-16 16:25:52 +01:00
..
Dockerfile Update all images using openshift to latest stable 4.6 2021-01-05 10:15:11 +00:00
openshift-client-python.yaml Add linux/amd64 platform annotation to the rest of the tasks 2021-10-29 17:08:38 +01:00
README.md [TEP-0110] Update Tekton Catalog installation instructions 2022-08-16 16:25:52 +01:00

Python OpenShift Runner

OpenShift is a Kubernetes distribution from Red Hat which provides oc, the OpenShift CLI that complements kubectl for simplifying deployment and configuration applications on OpenShift. The following task can be used to interact with the openshift cluster via oc commands in a python script.

The set of possible scripts can be found in the docs here.

Pre-requisites

  1. Need to have an OpenShift cluster up and running.
  2. config file of the cluster needs to be mounted via the Secrets to the default location ~/.kube/ in order to obtain the cluster details.

Install the Task

kubectl apply -f https://api.hub.tekton.dev/v1/resource/tekton/task/openshift-client-python/0.1/raw

Parameters

  • SCRIPT: The python script which you want to run in the task. Default script would be :-
#!/usr/bin/python

import openshift as oc
print('OpenShift client version: {}'.format(oc.get_client_version()))
print('OpenShift server version: {}'.format(oc.get_server_version()))

Workspaces

  • kubeconfig-mount: The following workspace is used to mount the config file of the kubeconfig from the Secrets.

The Secrets can be created by

oc create secret generic kubeconfig --from-file=config

TaskRun

apiVersion: tekton.dev/v1beta1
kind: TaskRun
metadata:
  name: openshift-client-python-run
spec:
  taskRef:
    name: openshift-client-python
  params:
    - name: SCRIPT
      value: |
        #!/usr/bin/python

        import openshift as oc

        print('OpenShift client version: {}'.format(oc.get_client_version()))
        print('OpenShift server version: {}'.format(oc.get_server_version()))
        
        # Set a project context for all inner `oc` invocations and limit execution to 10 minutes
        with oc.project('openshift-infra'), oc.timeout(10*60):
          # Print the list of qualified pod names (e.g. ['pod/xyz', 'pod/abc', ...]  in the current project
          print('Found the following pods in {}: {}'.format(oc.get_project_name(), oc.selector('pods').qnames()))

          # Read in the current state of the pod resources and represent them as python objects
          for pod_obj in oc.selector('pods').objects():
            
            # The APIObject class exposes several convenience methods for interacting with objects
            print('Analyzing pod: {}'.format(pod_obj.name()))
            pod_obj.print_logs(timestamps=True, tail=15)
            
            # If you need access to the underlying resource definition, get a Model instance for the resource
            pod_model = pod_obj.model
            
            # Model objects enable dot notation and allow you to navigate through resources
            # to an arbitrary depth without checking if any ancestor elements exist.
            # In the following example, there is no need for boilerplate like:
            #    `if .... 'ownerReferences' in pod_model['metadata'] ....`
            # Fields that do not resolve will always return oc.Missing which 
            # is a singleton and can also be treated as an empty dict.
            for owner in pod_model.metadata.ownerReferences:  # ownerReferences == oc.Missing if not present in resource
              # elements of a Model are also instances of Model or ListModel
              if owner.kind is not oc.Missing:  # Compare as singleton
                print('  pod owned by a {}'.format(owner.kind))  # e.g. pod was created by a StatefulSet
    
  workspaces:
    - name: kubeconfig-mount
      secret:
        secretName: kubeconfig

Platforms

The Task can be run on linux/amd64 platform.