Skip to content

Commit 21f43f6

Browse files
Merge pull request #1846 from logonoff/CONSOLE-4775
CONSOLE-4775: Proposal for automated types generation from OpenAPI specs
2 parents 7aa5a35 + a11e037 commit 21f43f6

File tree

1 file changed

+223
-0
lines changed

1 file changed

+223
-0
lines changed
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
---
2+
title: generated-resource-types
3+
authors:
4+
- "@logonoff"
5+
reviewers:
6+
- "@jhadvig"
7+
- "@spadgett"
8+
approvers:
9+
- "@spadgett"
10+
api-approvers:
11+
- "None"
12+
creation-date: 2025-09-16
13+
last-updated: 2025-09-25
14+
tracking-link:
15+
- https://issues.redhat.com/browse/CONSOLE-4775
16+
---
17+
18+
# Migration to generated resource types from their OpenAPI specs
19+
20+
## Summary
21+
22+
The OpenShift web console interacts with various Kubernetes (k8s) resources to
23+
provide its functionality. The schemas of these resources which are used by the
24+
web console to interact with the k8s API are currently manually defined as TypeScript
25+
types in the `frontend/public/module/k8s/types.ts` file.
26+
27+
These types are often not reviewed for accuracy or completeness, and may not
28+
always align with the actual OpenAPI specifications of the resources. This leads
29+
to bugs in the web console due to incorrect assumptions about the structure of
30+
the resources.
31+
32+
This enhancement proposes the addition of automated generation of TypeScript types
33+
for Kubernetes resources used by the OpenShift web console. The goal is to ensure
34+
better alignment with the source-of-truth schemas, reduce manual effort, and minimize
35+
he risk of bugs due to schema misalignment.
36+
37+
## Motivation
38+
39+
The effort to manually maintain TypeScript resource types in the web console is
40+
cumbersome and error-prone, and is occasionally the cause of bugs due to misalignment
41+
between the actual resource schema and the manually defined TypeScript types.
42+
43+
To address this, we propose adding automated generation of these TypeScript types
44+
based on the OpenAPI specifications of the k8s resources. This will ensure better
45+
alignment with the source-of-truth schemas, reduce manual effort, and minimize the
46+
risk of bugs due to schema misalignment.
47+
48+
49+
### User Stories
50+
51+
* As an OpenShift engineer, I want accurate TypeScript types for k8s resources used
52+
by the web console, so that I can make correct assumptions about the structure of
53+
these resources when developing features.
54+
* As an OpenShift engineer, I want to reduce the manual effort required to maintain
55+
TypeScript types for k8s resources, so that I can focus on developing features
56+
rather than maintaining types.
57+
58+
### Goals
59+
60+
The current manual maintenance of TypeScript types was originally done in during
61+
the early development of the web console by CoreOS, when the web console was meant
62+
to be a simple management interface for k8s clusters.
63+
64+
The goals of this enhancement are to:
65+
66+
* Periodically publish a package on npm which contains TypeScript types for k8s
67+
resources used by the web console, based on their OpenAPI specifications.
68+
* Consume this package in the web console and in associated console dynamic plugins.
69+
70+
### Non-Goals
71+
72+
This enhancement will not seek to implement:
73+
74+
* Validation of objects against the generated types during runtime.
75+
* Generation of types for all CustomResourceDefinitions (CRDs) used by the web console.
76+
The initial focus will be on resources defined by the `openshift/kubernetes`
77+
swagger definitions and the CRDs defined by the `openshift/api` repository.
78+
79+
## Proposal
80+
81+
### Workflow Description
82+
83+
**library authors** are responsible for maintaining the type generation library.
84+
1. They will run the generation scripts at some point during the OpenShift release
85+
2. The generated types will be published to a package on npm (e.g. `@openshift/k8s-types`)
86+
87+
**web console developers** will consume the generated types in the web console.
88+
1. They will add or update the dependency to the package in the web console's `package.json`.
89+
2. They will import the types from the package in the web console's codebase.
90+
3. They will fix resulting type errors in the web console's codebase due to
91+
changes in the generated types.
92+
93+
94+
### API Extensions
95+
NA
96+
97+
### Risks and Mitigations
98+
99+
The proposal aims to mitigate risks which already exist and have manifested through
100+
various bugs.
101+
102+
### Drawbacks
103+
104+
* Web console developers would loose unlimited control over types which are used
105+
in the codebase.
106+
* The current implementation of the generated types replaces the `K8sResourceCommon`
107+
`type` with an `interface`, which aligns with TypeScript's recommendations. However,
108+
this may cause some type errors in the web console and dependent dynamic plugins
109+
which will need to be fixed. The type errors often occur when the `omit` utility
110+
function in Lodash is used on objects of type `K8sResourceCommon`. However,
111+
this is mitigated by the fact that TypeScript is compiled away during build time,
112+
so the runtime behavior of the web console will not be affected.
113+
114+
## Test Plan
115+
116+
As this enhancement proposal focuses on the generation of TypeScript types,
117+
existing unit and integration tests that utilize these types will serve as the
118+
primary means of validation. To ensure the correctness of the generated types,
119+
we will implement the following test strategies:
120+
121+
* **Unit tests in the generation library**: We will add unit tests to the type
122+
generation library to verify that it correctly transforms OpenAPI specifications
123+
into TypeScript types.
124+
* **Integration tests in the web console**: We will leverage existing unit and
125+
integration tests in the web console that utilize the generated types. These
126+
tests will help ensure that the generated types are accurate and function as
127+
expected within the context of the web console.
128+
* **Manual verification**: We will perform manual verification of the generated
129+
types by inspecting them and comparing them to the original OpenAPI definitions.
130+
131+
## Graduation Criteria
132+
N/A
133+
134+
### Dev Preview -> Tech Preview
135+
N/A
136+
137+
### Tech Preview -> GA
138+
N/A
139+
140+
### Removing a deprecated feature
141+
N/A
142+
143+
## Upgrade / Downgrade Strategy
144+
N/A
145+
146+
## Version Skew Strategy
147+
148+
Updating the types package should only affect the contents of the types when
149+
they are updated by the source resource specifications. As such, version skew
150+
is mitigated by OpenShift's policy of not breaking APIs.
151+
152+
However, when breaking changes do occur, older versions of resource types can
153+
still be exported by the type library to ensure backwards-compatibility with
154+
existing web console code and resources.
155+
156+
## Operational Aspects of API Extensions
157+
N/A
158+
159+
### Deprecated Feature
160+
N/A
161+
162+
### Topology Considerations
163+
N/A
164+
165+
#### Hypershift / Hosted Control Planes
166+
N/A
167+
168+
#### Standalone Clusters
169+
N/A
170+
171+
#### Single-node Deployments or MicroShift
172+
N/A
173+
174+
## Support Procedures
175+
N/A
176+
177+
### Implementation Details/Notes/Constraints
178+
179+
A proof of concept for generating these types was written in [logonoff/openshift-types].
180+
While these types align with the web console naming conventions, there are changes
181+
which cause type errors when directly replaced with the web console equivalents.
182+
183+
[kubernetes-model-ts]: https://github.com/tommy351/kubernetes-models-ts
184+
[logonoff/openshift-types]: https://github.com/logonoff/openshift-types
185+
186+
#### Steps to Implement This Feature
187+
188+
1. **Develop the type generation library**: Create a library that can fetch OpenAPI
189+
specifications and generate TypeScript types from them. This library should be
190+
capable of handling both core k8s resources and CRDs defined by the `openshift/api`
191+
repository. This library will be located in a separate repository from both `openshift/api`
192+
and `openshift/console`.
193+
194+
2. **Integrate the generation process into the OpenShift release cycle**: Determine
195+
the appropriate point in the OpenShift release cycle to run the type generation
196+
scripts and publish the generated types to a package on npm.
197+
3. **Update the web console to consume the generated types**: Add or update the
198+
dependency to the generated types package in the web console's `package.json`,
199+
and update the web console's codebase to import and use the generated types.
200+
4. **Fix type errors in the web console**: Address any type errors that arise in
201+
the web console's codebase due to changes in the generated types.
202+
203+
## Alternatives (Not Implemented)
204+
205+
Prior art exists in the form of npm packages containing TypeScript types for k8s
206+
resources, such as [kubernetes-model-ts]. This package is used by already used
207+
and has contributors from Red Hat Developer Hub (based on Backstage); however,
208+
these packages do not cover OpenShift CustomResourceDefinitions.
209+
210+
Moreover, the type names and overall implementation of the types do not align with
211+
the status quo we have in the web console. This would require further work in the
212+
web console to both adapt to the new type and to implement the OpenShift-specific
213+
CustomResourceDefinitions in the upstream library.
214+
215+
## Open Questions
216+
217+
1. OpenShift web console currently uses manually defined TypeScript types for k8s
218+
resources, which assume that all resources share the same version. How should
219+
we handle resources that may exist in different versions (e.g., `v1`,
220+
`v1beta1`, etc.)?
221+
- One approach could be to ensure that the code consuming these types can safely
222+
assume a specific version (e.g., the latest available version), and to request
223+
the API server to return resources in that version.

0 commit comments

Comments
 (0)