Skip to content

Commit

Permalink
refactor: openshift-model-autoscaling generated from OpenAPI schemas
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa committed Oct 1, 2024
1 parent 234cb60 commit 1608c61
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 8 deletions.
3 changes: 0 additions & 3 deletions kubernetes-model-generator/openapi/maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@
<packaging>maven-plugin</packaging>

<properties>
<!-- <maven.compiler.release>11</maven.compiler.release>-->
<!-- <maven.compiler.target>11</maven.compiler.target>-->
<!-- <maven.compiler.source>11</maven.compiler.source>-->
<maven.deploy.skip>true</maven.deploy.skip><!-- Keep module private -->
</properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ public static OpenAPI crdToOpenApi(URI crdUri) throws IOException {
}
}
final File tempOpenApi = Files.createTempFile("openapi", ".json").toFile();
assert tempOpenApi.setReadable(true, true);
assert tempOpenApi.setWritable(true, true);
assert tempOpenApi.setExecutable(true, true);
tempOpenApi.deleteOnExit();
// Writing to a file and parsing it fixes any issue with the computed OpenAPI values, this step is necessary
Json31.pretty().writeValue(tempOpenApi, openAPI);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.kubernetes.schema.generator.schema;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.Schema;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import java.net.URI;
import java.util.Objects;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

class CrdParserTest {

@Nested
class SingleDocument {

private OpenAPI singleDocument;

@BeforeEach
void parse() throws Exception {
final URI uri = Objects.requireNonNull(CrdParserTest.class.getResource("/crd-parser/single-document.yaml"))
.toURI();
singleDocument = CrdParser.crdToOpenApi(uri);
}

@Test
void parsesSingleDocument() {
assertNotNull(singleDocument);
// 1 list and 1 resource
assertEquals(2, singleDocument.getComponents().getSchemas().size());
}

@Test
void createsResourceNamespacedPath() {
assertNotNull(singleDocument.getPaths().get("/apis/example.com/v1/namespaces/{namespace}/resources/{name}"));
}

@Test
void createsResourceNamespacedListPath() {
assertNotNull(singleDocument.getPaths().get("/apis/example.com/v1/namespaces/{namespace}/resources"));
}

@Test
void createsResourceComponent() {
assertNotNull(singleDocument.getComponents().getSchemas().get("com.example.resources.v1.Resource"));
}

@Test
void createsListComponent() {
assertNotNull(singleDocument.getComponents().getSchemas().get("com.example.resources.v1.ResourceList"));
}

@Test
void setsReferenceToObjectMeta() {
assertEquals("#/components/schemas/io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta",
((Schema<?>) singleDocument.getComponents().getSchemas().get("com.example.resources.v1.Resource")
.getProperties().get("metadata")).get$ref());
}

}

@Nested
class MultipleDocuments {

private OpenAPI multipleDocuments;

@BeforeEach
void parse() throws Exception {
final URI uri = Objects.requireNonNull(CrdParserTest.class.getResource("/crd-parser/multiple-documents.yaml"))
.toURI();
multipleDocuments = CrdParser.crdToOpenApi(uri);
}

@Test
void parsesMultipleDocuments() {
assertNotNull(multipleDocuments);
// 1 list and 1 resource
assertEquals(4, multipleDocuments.getComponents().getSchemas().size());
}

@Test
void createsResourceNamespacedPaths() {
assertNotNull(multipleDocuments.getPaths().get("/apis/example.com/v1/namespaces/{namespace}/resources/{name}"));
assertNotNull(multipleDocuments.getPaths().get("/apis/example.com/v1/additional-resources/{name}"));
}

@Test
void createsResourceNamespacedListPaths() {
assertNotNull(multipleDocuments.getPaths().get("/apis/example.com/v1/namespaces/{namespace}/resources"));
assertNotNull(multipleDocuments.getPaths().get("/apis/example.com/v1/additional-resources"));
}

@Test
void createsResourceComponents() {
assertNotNull(multipleDocuments.getComponents().getSchemas().get("com.example.resources.v1.Resource"));
assertNotNull(
multipleDocuments.getComponents().getSchemas().get("com.example.additional-resources.v1.AdditionalResource"));
}

@Test
void createsListComponents() {
assertNotNull(multipleDocuments.getComponents().getSchemas().get("com.example.resources.v1.ResourceList"));
assertNotNull(
multipleDocuments.getComponents().getSchemas().get("com.example.additional-resources.v1.AdditionalResourceList"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#
# Copyright (C) 2015 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: resources.example.com
spec:
group: example.com
names:
kind: Resource
plural: resources
singular: resource
scope: Namespaced
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
properties:
metadata:
type: object
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: additional-resources.example.com
spec:
group: example.com
names:
kind: AdditionalResource
plural: additional-resources
singular: additional-resource
scope: Cluster
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
properties:
metadata:
type: object
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#
# Copyright (C) 2015 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: resources.example.com
spec:
group: example.com
names:
kind: Resource
plural: resources
singular: resource
scope: Namespaced
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
properties:
metadata:
type: object
11 changes: 6 additions & 5 deletions kubernetes-model-generator/openshift-model-autoscaling/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,13 @@
<artifactId>openapi-model-generator-maven-plugin</artifactId>
<configuration >
<settings combine.self="append">
<schemas>
<schema>${openapi.schema.openshift-latest}</schema>
</schemas>
<urls>
<url>https://raw.githubusercontent.com/openshift/cluster-autoscaler-operator/refs/heads/release-${openapi.openshift-version}/install/01_clusterautoscaler.crd.yaml</url>
<url>https://raw.githubusercontent.com/openshift/cluster-autoscaler-operator/refs/heads/release-${openapi.openshift-version}/install/02_machineautoscaler.crd.yaml</url>
</urls>
<packageMappings combine.self="append">
<io.k8s.api>io.fabric8.kubernetes.api.model</io.k8s.api>
<io.openshift.autoscaling>io.fabric8.openshift.api.model.autoscaling</io.openshift.autoscaling>
<io.openshift.autoscaling.clusterautoscalers>io.fabric8.openshift.api.model.autoscaling</io.openshift.autoscaling.clusterautoscalers>
<io.openshift.autoscaling.machineautoscalers>io.fabric8.openshift.api.model.autoscaling</io.openshift.autoscaling.machineautoscalers>
</packageMappings>
<includeGenerationRegexes>
<includeGenerationRegex>^io\.openshift\.autoscaling\..*$</includeGenerationRegex>
Expand Down
1 change: 1 addition & 0 deletions kubernetes-model-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<!-- Contains Paths. Retrieve from a real cluster -->
<openapi.schema.openshift-latest>${project.parent.basedir}/openapi/schemas/openshift-4.17.0.json</openapi.schema.openshift-latest>
<openapi.schema.openshift-generated>${project.parent.basedir}/openapi/schemas/openshift-generated.json</openapi.schema.openshift-generated>
<openapi.openshift-version>4.17</openapi.openshift-version>
<osgi.require-capability>
osgi.extender;
filter:="(osgi.extender=osgi.serviceloader.registrar)"
Expand Down

0 comments on commit 1608c61

Please sign in to comment.