Skip to content

Commit

Permalink
Add OpenAPI.yaml and Modify rrs service
Browse files Browse the repository at this point in the history
* rename rr module to rrs
  • Loading branch information
arka-pramanik-hpe committed Feb 3, 2025
1 parent 283d67b commit 93de4a5
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 74 deletions.
File renamed without changes.
76 changes: 2 additions & 74 deletions cray/modules/rr/cli.py → cray/modules/rrs/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,9 @@
import json
from collections import defaultdict

@click.group(name="rr", help="Rack Resiliency commands")
@click.group(name="rrs", help="Rack Resiliency commands")
def cli():
pass
# cray/modules/rr/cli.py

# def get_zone_data():
# """Fetch and parse Kubernetes node zone information"""
# try:
# # Get node data with zone labels and resources
# cmd = [
# 'kubectl', 'get', 'nodes',
# '-L', 'topology.kubernetes.io/zone',
# '-o', 'json'
# ]
# result = subprocess.check_output(cmd, text=True)
# data = json.loads(result)

# # Organize nodes by zone
# zones = defaultdict(list)
# for node in data['items']:
# zone = node['metadata']['labels'].get(
# 'topology.kubernetes.io/zone',
# 'unassigned'
# )
# zones[zone].append({
# 'name': node['metadata']['name'],
# 'status': node['status'].get('conditions', [{}])[-1].get('type', 'Unknown'),
# 'roles': ','.join(
# k for k, v in node['metadata']['labels'].items()
# if 'node-role.kubernetes.io/' in k
# ),
# 'age': node['metadata']['creationTimestamp'],
# 'version': node['status']['nodeInfo']['kubeletVersion'],
# 'cpu': node['status']['capacity'].get('cpu', 'N/A'),
# 'memory': node['status']['capacity'].get('memory', 'N/A')
# })
# return zones

# except (subprocess.CalledProcessError, json.JSONDecodeError) as e:
# click.echo(f"Error fetching node data: {str(e)}", err=True)
# return {}

@cli.command(name="list", help="List nodes grouped by zones from backend API")
@click.option('--node-ip', '-e', required=True,
Expand Down Expand Up @@ -95,40 +57,7 @@ def list_command(node_ip):
except KeyError as e:
click.echo(f"Invalid data format from backend: missing {str(e)}", err=True)


# @cli.command(name="list", help="List nodes grouped by zones")
# def list_command():
# """Display nodes organized by zone with resource information"""
# zones = get_zone_data()

# # Formatting constants
# header = "{:<15} {:<10} {:<25} {:<10} {:<15} {:<10} {:<15}".format(
# "NAME", "STATUS", "ROLES", "AGE", "VERSION", "CPU", "MEMORY"
# )
# separator = "-" * 100

# for zone, nodes in zones.items():
# click.echo(f"\nZone: {zone}")
# click.echo(separator)
# click.echo(header)

# for node in nodes:
# click.echo(
# "{name:<15} {status:<10} {roles:<25} {age:<10} "
# "{version:<15} {cpu:<10} {memory:<15}".format(
# name=node['name'],
# status=node['status'],
# roles=node['roles'][:23] + '..' if len(node['roles']) > 25 else node['roles'],
# age=node['age'].split('T')[0], # Show date only
# version=node['version'],
# cpu=node['cpu'],
# memory=node['memory']
# )
# )
# click.echo() # Add space between zones

@cli.command(name="hello", help="Fetch greeting from HelloWorld API")
@click.option('--name', '-n', default=None, help="Name for personalized greeting")
@click.option('--node-ip', '-e', required=True, help="Node IP of HelloWorld service (port 8080)")
def hello_command(name, node_ip):
"""Fetch greeting from /hello endpoint"""
Expand All @@ -137,7 +66,6 @@ def hello_command(name, node_ip):
try:
response = requests.get(
endpoint,
params={'name': name} if name else None,
timeout=5 # Add timeout
)
response.raise_for_status()
Expand All @@ -150,4 +78,4 @@ def hello_command(name, node_ip):
except requests.exceptions.HTTPError as e:
click.echo(f"HTTP Error {e.response.status_code}: {e.response.text}", err=True)
except requests.exceptions.RequestException as e:
click.echo(f"Connection Error: {str(e)}", err=True)
click.echo(f"Connection Error: {str(e)}", err=True)
94 changes: 94 additions & 0 deletions cray/modules/rrs/openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
openapi: 3.0.0
info:
title: Kubernetes Zone API
version: 1.0.0
description: API for fetching Kubernetes node zone information
servers:
- url: http://{node_ip}:8080
variables:
node_ip:
default: "localhost"
description: IP address of the node running the service

paths:
/zones:
get:
tags:
- Zones
summary: Get zone data
description: Returns zone data from the Kubernetes API
responses:
'200':
description: Successful response
content:
application/json:
schema:
type: object
additionalProperties:
type: array
items:
$ref: '#/components/schemas/Node'
example:
rack-1:
- name: "ncn-m001"
status: "Ready"
roles: "control-plane"
age: "2024-10-25T12:40:04Z"
version: "v1.24.17"
cpu: "32"
memory: "131032624Ki"
'404':
description: Not Found
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500':
description: Server Error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'

components:
schemas:
Node:
type: object
properties:
name:
type: string
example: "ncn-m001"
status:
type: string
enum: [Ready, NotReady, Unknown]
example: "Ready"
roles:
type: string
example: "control-plane"
age:
type: string
format: date-time
example: "2024-10-25T12:40:04Z"
version:
type: string
example: "v1.24.17"
cpu:
type: string
example: "32"
memory:
type: string
example: "131032624Ki"
required:
- name
- status
- age
- version

Error:
type: object
properties:
error:
type: string
example: "Not Found"
required:
- error

0 comments on commit 93de4a5

Please sign in to comment.