Skip to content

Instantly share code, notes, and snippets.

@pwright
Last active December 5, 2024 09:27
Show Gist options
  • Save pwright/80515245a2ade45903bde3c24f4f443d to your computer and use it in GitHub Desktop.
Save pwright/80515245a2ade45903bde3c24f4f443d to your computer and use it in GitHub Desktop.
api-controller-ocp

Template for CR and route YAMLs

./create.py api-controller.template api-controller.yaml values.yaml
# Replace mycluster.example.com with your cluster hostname
# Create an API Controller custom resource
apiVersion: registry.apicur.io/v1
kind: ApicurioRegistry3
metadata:
name: ${resource}
namespace: ${namespace}
spec:
studioUi:
enabled: true
env:
- name: APICURIO_REGISTRY_API_URL
value: 'https://api-controller-app.apps.${clusterHost}/apis/registry/v3'
- name: APICURIO_REGISTRY_UI_URL
value: 'https://api-controller-ui.apps.${clusterHost}'
ui:
env:
- name: REGISTRY_API_URL
value: 'https://api-controller-app.apps.${clusterHost}/apis/registry/v3'
app:
sql:
dataSource:
username: apicurio
password: registry
url: 'jdbc:postgresql://postgresql-service:5432/registry'
---
# Create a route for the Apicurio Registry API
apiVersion: route.openshift.io/v1
kind: Route
metadata:
name: api-controller-registry-api
namespace: ${namespace}
spec:
host: api-controller-app.apps.${clusterHost}
path: /
to:
kind: Service
name: ${resource}-app-service
port:
targetPort: http
tls:
termination: edge
insecureEdgeTerminationPolicy: Redirect
wildcardPolicy: None
---
# Create a route for the Apicurio Registry UI
apiVersion: route.openshift.io/v1
kind: Route
metadata:
name: api-controller-registry-ui
namespace: ${namespace}
spec:
host: api-controller-ui.apps.${clusterHost}
path: /
to:
kind: Service
name: ${resource}-ui-service
port:
targetPort: http
tls:
termination: edge
insecureEdgeTerminationPolicy: Redirect
wildcardPolicy: None
---
# Create a route for the Apicurio Studio UI
apiVersion: route.openshift.io/v1
kind: Route
metadata:
name: api-controller-studio-ui
namespace: ${namespace}
spec:
host: api-controller-studio-ui.apps.${clusterHost}
path: /
to:
kind: Service
name: ${resource}-studio-ui-service
port:
targetPort: http
tls:
termination: edge
insecureEdgeTerminationPolicy: Redirect
wildcardPolicy: None
# Replace mycluster.example.com with your cluster hostname
# Create an API Controller custom resource
apiVersion: registry.apicur.io/v1
kind: ApicurioRegistry3
metadata:
name: apicurio-registry-deployment
namespace: api-controller
spec:
studioUi:
enabled: true
env:
- name: APICURIO_REGISTRY_API_URL
value: 'https://api-controller-app.apps.mycluster.example.com/apis/registry/v3'
- name: APICURIO_REGISTRY_UI_URL
value: 'https://api-controller-ui.apps.mycluster.example.com'
ui:
env:
- name: REGISTRY_API_URL
value: 'https://api-controller-app.apps.mycluster.example.com/apis/registry/v3'
app:
sql:
dataSource:
username: apicurio
password: registry
url: 'jdbc:postgresql://postgresql-service:5432/registry'
---
# Create a route for the Apicurio Registry API
apiVersion: route.openshift.io/v1
kind: Route
metadata:
name: api-controller-registry-api
namespace: api-controller
spec:
host: api-controller-app.apps.mycluster.example.com
path: /
to:
kind: Service
name: apicurio-registry-deployment-app-service
port:
targetPort: http
tls:
termination: edge
insecureEdgeTerminationPolicy: Redirect
wildcardPolicy: None
---
# Create a route for the Apicurio Registry UI
apiVersion: route.openshift.io/v1
kind: Route
metadata:
name: api-controller-registry-ui
namespace: api-controller
spec:
host: api-controller-ui.apps.mycluster.example.com
path: /
to:
kind: Service
name: apicurio-registry-deployment-ui-service
port:
targetPort: http
tls:
termination: edge
insecureEdgeTerminationPolicy: Redirect
wildcardPolicy: None
---
# Create a route for the Apicurio Studio UI
apiVersion: route.openshift.io/v1
kind: Route
metadata:
name: api-controller-studio-ui
namespace: api-controller
spec:
host: api-controller-studio-ui.apps.mycluster.example.com
path: /
to:
kind: Service
name: apicurio-registry-deployment-studio-ui-service
port:
targetPort: http
tls:
termination: edge
insecureEdgeTerminationPolicy: Redirect
wildcardPolicy: None
#!/usr/bin/env python3
import argparse
import yaml
from string import Template
import sys
def load_yaml(file_path):
"""Load key-value pairs from a YAML file."""
try:
with open(file_path, 'r') as yaml_file:
return yaml.safe_load(yaml_file)
except Exception as e:
print(f"Error reading YAML file '{file_path}': {e}", file=sys.stderr)
sys.exit(1)
def load_template(file_path):
"""Load the template content."""
try:
with open(file_path, 'r') as template_file:
return template_file.read()
except Exception as e:
print(f"Error reading template file '{file_path}': {e}", file=sys.stderr)
sys.exit(1)
def write_output(file_path, content):
"""Write processed content to output file."""
try:
with open(file_path, 'w') as output_file:
output_file.write(content)
except Exception as e:
print(f"Error writing to output file '{file_path}': {e}", file=sys.stderr)
sys.exit(1)
def main():
parser = argparse.ArgumentParser(
description="A simple templating CLI tool using string.Template."
)
parser.add_argument("template", help="Path to the input template file.")
parser.add_argument("output", help="Path to the output file.")
parser.add_argument("values", help="Path to the YAML file containing key-value pairs.")
args = parser.parse_args()
# Load files
template_content = load_template(args.template)
values = load_yaml(args.values)
# Create and process template
try:
template = Template(template_content)
output_content = template.substitute(values)
except KeyError as e:
print(f"Error: Missing value for placeholder {e}", file=sys.stderr)
sys.exit(1)
# Write output
write_output(args.output, output_content)
if __name__ == "__main__":
main()
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: registry-pvc
namespace: ${namespace}
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi # Adjust the storage size as needed
---
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: "${namespace}"
labels:
app: postgresql
name: postgresql
spec:
replicas: 1
selector:
matchLabels:
app: postgresql
template:
metadata:
labels:
app: postgresql
spec:
initContainers:
- name: init-data
image: busybox
command: ['sh', '-c', 'rm -rf /var/lib/postgresql/data/* && mkdir -p /var/lib/postgresql/data/pgdata']
volumeMounts:
- mountPath: "/var/lib/postgresql/data"
name: "registry-pgdata"
containers:
- name: postgresql
image: quay.io/debezium/postgres:13-alpine
ports:
- containerPort: 5432
env:
- name: POSTGRES_DB
value: registry
- name: POSTGRES_USER
value: apicurio
- name: POSTGRES_PASSWORD
value: registry
- name: PGDATA
value: "/var/lib/postgresql/data/pgdata"
volumeMounts:
- mountPath: "/var/lib/postgresql/data"
name: "registry-pgdata"
volumes:
- name: registry-pgdata
persistentVolumeClaim:
claimName: registry-pvc
---
apiVersion: v1
kind: Service
metadata:
namespace: "${namespace}"
labels:
app: postgresql
name: postgresql-service
spec:
ports:
- name: http
port: 5432
protocol: TCP
targetPort: 5432
selector:
app: postgresql
type: ClusterIP
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: registry-pvc
namespace: api-controller
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi # Adjust the storage size as needed
---
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: "api-controller"
labels:
app: postgresql
name: postgresql
spec:
replicas: 1
selector:
matchLabels:
app: postgresql
template:
metadata:
labels:
app: postgresql
spec:
initContainers:
- name: init-data
image: busybox
command: ['sh', '-c', 'rm -rf /var/lib/postgresql/data/* && mkdir -p /var/lib/postgresql/data/pgdata']
volumeMounts:
- mountPath: "/var/lib/postgresql/data"
name: "registry-pgdata"
containers:
- name: postgresql
image: quay.io/debezium/postgres:13-alpine
ports:
- containerPort: 5432
env:
- name: POSTGRES_DB
value: registry
- name: POSTGRES_USER
value: apicurio
- name: POSTGRES_PASSWORD
value: registry
- name: PGDATA
value: "/var/lib/postgresql/data/pgdata"
volumeMounts:
- mountPath: "/var/lib/postgresql/data"
name: "registry-pgdata"
volumes:
- name: registry-pgdata
persistentVolumeClaim:
claimName: registry-pvc
---
apiVersion: v1
kind: Service
metadata:
namespace: "api-controller"
labels:
app: postgresql
name: postgresql-service
spec:
ports:
- name: http
port: 5432
protocol: TCP
targetPort: 5432
selector:
app: postgresql
type: ClusterIP
namespace: api-controller
resource: apicurio-registry-deployment
clusterHost: mycluster.example.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment