Skip to content

Commit

Permalink
refactor: openshift-model-operator 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 authored Sep 30, 2024
1 parent ac682f8 commit 5fcffb4
Show file tree
Hide file tree
Showing 381 changed files with 32,348 additions and 42,194 deletions.
3 changes: 2 additions & 1 deletion doc/MIGRATION-v7.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ It is no longer published, the `io.fabric8:kubernetes-client-api` or `io.fabric8

Some of the types and packages have been moved to more suiting modules and package names.

- `io.openshift.helm` API group is now part of the `openshift-model-miscellaneus` module.
- `io.openshift.helm` API group is now part of the `io.fabric8:openshift-model-miscellaneus` module.
- `io.openshift.network` API group is now part of the `io.fabric8:openshift-model-miscellaneus` module.

### Service Catalog removed (operator.openshift.io) <a href="#service-catalog-removed" id="service-catalog-removed"/>

Expand Down
7 changes: 3 additions & 4 deletions kubernetes-model-generator/openapi/generator/cmd/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@ var openApiRun = func(cobraCmd *cobra.Command, args []string) {
targetDirectory = "."
}
openApiGenerator := openapi.NewGenerator(targetDirectory, "openshift-generated")
openApiGenerator.PutPackageMapping("github.com/openshift/api", "openshift.io")
openShiftModule := parser.NewModule("github.com/openshift/api")
/////////////////////////////////////////////////////////////////////////////////
// Ported from github.com/openshift/api/openapi/cmd/models-schema/main.go
refFunc := func(name string) spec.Ref {
return spec.MustCreateRef(fmt.Sprintf("#/definitions/%s", openApiGenerator.FriendlyName(name)))
return spec.MustCreateRef(fmt.Sprintf("#/definitions/%s", openShiftModule.ApiName(name)))
}
defs := generated_openapi.GetOpenAPIDefinitions(refFunc)
for k, v := range defs {
Expand All @@ -66,11 +65,11 @@ var openApiRun = func(cobraCmd *cobra.Command, args []string) {
// the type.
if schema, ok := v.Schema.Extensions[common.ExtensionV2Schema]; ok {
if v2Schema, isOpenAPISchema := schema.(spec.Schema); isOpenAPISchema {
openApiGenerator.PutDefinition(openApiGenerator.FriendlyName(k), v2Schema)
openApiGenerator.PutDefinition(openShiftModule.ApiName(k), v2Schema)
continue
}
}
openApiGenerator.PutDefinition(openApiGenerator.FriendlyName(k), v.Schema)
openApiGenerator.PutDefinition(openShiftModule.ApiName(k), v.Schema)
}

if err := openApiGenerator.WriteDefinitions(); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/getkin/kin-openapi/openapi3"
openshiftmachinev1 "github.com/openshift/api/machine/v1"
openshiftmachinev1alpha1 "github.com/openshift/api/machine/v1alpha1"
openshiftclusternetworkoperatorv1 "github.com/openshift/cluster-network-operator/pkg/apis/network/v1"
"github.com/spf13/cobra"
admissionV1 "k8s.io/api/admission/v1"
admissionV1Beta1 "k8s.io/api/admission/v1beta1"
Expand Down Expand Up @@ -125,6 +126,11 @@ var reflectionRun = func(cmd *cobra.Command, args []string) {
reflect.TypeOf(openshiftmachinev1.NutanixMachineProviderConfig{}): {false, openshiftmachinev1.GroupVersion.String(), "nutanixmachineproviderconfigs", true},
reflect.TypeOf(openshiftmachinev1.PowerVSMachineProviderConfig{}): {false, openshiftmachinev1.GroupVersion.String(), "powervsmachineproviderconfigs", true},
}, "openshift-machine"),
// OperatorPKI specs are not included in OpenApi
NewPathSchema(map[reflect.Type]ApiVersion{
reflect.TypeOf(openshiftclusternetworkoperatorv1.OperatorPKIList{}): {true, openshiftclusternetworkoperatorv1.GroupVersion.String(), "operatorpkis", true},
reflect.TypeOf(openshiftclusternetworkoperatorv1.OperatorPKI{}): {false, openshiftclusternetworkoperatorv1.GroupVersion.String(), "operatorpkis", true},
}, "openshift-cluster-network-operator"),
}
generate(schemas, targetDirectory)
}
Expand Down
8 changes: 6 additions & 2 deletions kubernetes-model-generator/openapi/generator/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ toolchain go1.23.1

require (
github.com/getkin/kin-openapi v0.125.0
// Match lastest commit in the version branch (e.g. release-4.17)
// Match latest commit in the version branch (e.g. release-4.17)
github.com/openshift/api v0.0.0-20240912201240-0a8800162826
k8s.io/api v0.30.2
k8s.io/apiextensions-apiserver v0.30.2
Expand All @@ -17,7 +17,11 @@ require (
sigs.k8s.io/kustomize/api v0.17.2
)

// TODO: WIP while this gets merged or fixed https://github.com/openshift/api/pull/2050
replace github.com/openshift/api => github.com/marcnuri-forks/api v0.0.0-20240930125604-62d5277244a4

require (
github.com/openshift/cluster-network-operator v0.0.0-20240912134426-a3188633549d
github.com/spf13/cobra v1.8.1
k8s.io/gengo/v2 v2.0.0-20240911193312-2b36238f13e9
k8s.io/kube-openapi v0.0.0-20240903163716-9e1beecbcb38
Expand All @@ -44,7 +48,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/perimeterx/marshmallow v1.1.5 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/pflag v1.0.6-0.20210604193023-d5e0c0615ace // indirect
golang.org/x/mod v0.20.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sync v0.8.0 // indirect
Expand Down
9 changes: 6 additions & 3 deletions kubernetes-model-generator/openapi/generator/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/marcnuri-forks/api v0.0.0-20240930125604-62d5277244a4 h1:OVm/tM0Io+jkCLhrYvDL4B6zJb9ueRBX36zt7q9KFOU=
github.com/marcnuri-forks/api v0.0.0-20240930125604-62d5277244a4/go.mod h1:OOh6Qopf21pSzqNVCB5gomomBXb8o5sGKZxG2KNpaXM=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand All @@ -58,8 +60,8 @@ github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/openshift/api v0.0.0-20240912201240-0a8800162826 h1:A8D9SN/hJUwAbdO0rPCVTqmuBOctdgurr53gK701SYo=
github.com/openshift/api v0.0.0-20240912201240-0a8800162826/go.mod h1:OOh6Qopf21pSzqNVCB5gomomBXb8o5sGKZxG2KNpaXM=
github.com/openshift/cluster-network-operator v0.0.0-20240912134426-a3188633549d h1:9Xf/80gDpRc33FwCEJ6L2/DF1yU/4L6QjoYxKexDzvo=
github.com/openshift/cluster-network-operator v0.0.0-20240912134426-a3188633549d/go.mod h1:qeN8u3CfzClFoykTdlvn0kKngBuVmb3VvWHA51UxiOI=
github.com/perimeterx/marshmallow v1.1.5 h1:a2LALqQ1BlHM8PZblsDdidgv1mWi1DgC2UmX50IvK2s=
github.com/perimeterx/marshmallow v1.1.5/go.mod h1:dsXbUu8CRzfYP5a87xpp0xq9S3u0Vchtcl8we9tYaXw=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand All @@ -70,8 +72,9 @@ github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/pflag v1.0.6-0.20210604193023-d5e0c0615ace h1:9PNP1jnUjRhfmGMlkXHjYPishpcw4jpSt/V/xYY3FMA=
github.com/spf13/pflag v1.0.6-0.20210604193023-d5e0c0615ace/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,20 @@ type Generator struct {
name string
targetDirectory string
definitions map[string]spec.Schema
packageMappings map[string]string
}

func NewGenerator(targetDirectory string, name string) *Generator {
return &Generator{
name: name,
targetDirectory: targetDirectory,
definitions: make(map[string]spec.Schema),
packageMappings: make(map[string]string),
}
}

func (g *Generator) PutDefinition(name string, schema spec.Schema) {
g.definitions[name] = schema
}

func (g *Generator) PutPackageMapping(name, target string) {
g.packageMappings[name] = target
}

func (g *Generator) WriteDefinitions() error {
data, err := json.MarshalIndent(&spec.Swagger{
SwaggerProps: spec.SwaggerProps{
Expand All @@ -74,13 +68,7 @@ func (g *Generator) WriteDefinitions() error {
// FriendlyName returns an OpenAPI friendly name for the given name.
// From vendor/k8s.io/apiserver/pkg/endpoints/openapi/openapi.go
// https://github.com/kubernetes/apiserver/blob/60d1ca672541e1b30b558e32e53cad7c172345a6/pkg/endpoints/openapi/openapi.go#L136-L147
func (g *Generator) FriendlyName(name string) string {
for k, v := range g.packageMappings {
if strings.HasPrefix(name, k) {
name = strings.Replace(name, k, v, 1)
break
}
}
func FriendlyName(name string) string {
nameParts := strings.Split(name, "/")
// Reverse first part. e.g., io.k8s... instead of k8s.io...
if len(nameParts) > 0 && strings.Contains(nameParts[0], ".") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package parser

import (
"fmt"
"github.com/fabric8io/kubernetes-client/kubernetes-model-generator/openapi/generator/pkg/openapi"
"k8s.io/gengo/v2/parser"
"k8s.io/gengo/v2/types"
"strings"
Expand Down Expand Up @@ -75,7 +76,7 @@ func (oam *Module) ExtractInfo(definitionName string) *Fabric8Info {

func (oam *Module) ApiName(definitionName string) string {
if strings.Index(definitionName, oam.Name) != 0 {
return definitionName
return openapi.FriendlyName(definitionName)
}
lastSeparator := strings.LastIndex(definitionName, ".")
typeName := definitionName[lastSeparator+1:]
Expand All @@ -85,7 +86,7 @@ func (oam *Module) ApiName(definitionName string) string {
for i, j := 0, len(groupParts)-1; i < j; i, j = i+1, j-1 {
groupParts[i], groupParts[j] = groupParts[j], groupParts[i]
}
return strings.Join(groupParts, ".") + "." + typeName
return strings.Join(groupParts, ".") + "." + pkg.Name + "." + typeName
}

func (oam *Module) resolvePackage(definitionName string) *types.Package {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"components":{"schemas":{"com.github.openshift.cluster-network-operator.pkg.apis.network.v1.CertSpec":{"properties":{"commonName":{"type":"string"}},"type":"object"},"com.github.openshift.cluster-network-operator.pkg.apis.network.v1.OperatorPKI":{"properties":{"apiVersion":{"type":"string"},"kind":{"type":"string"},"metadata":{"$ref":"#/components/schemas/io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta"},"spec":{"$ref":"#/components/schemas/com.github.openshift.cluster-network-operator.pkg.apis.network.v1.OperatorPKISpec"},"status":{"$ref":"#/components/schemas/com.github.openshift.cluster-network-operator.pkg.apis.network.v1.OperatorPKIStatus"}},"type":"object"},"com.github.openshift.cluster-network-operator.pkg.apis.network.v1.OperatorPKIList":{"properties":{"apiVersion":{"type":"string"},"items":{"items":{"$ref":"#/components/schemas/com.github.openshift.cluster-network-operator.pkg.apis.network.v1.OperatorPKI"},"type":"array"},"kind":{"type":"string"},"metadata":{"$ref":"#/components/schemas/io.k8s.apimachinery.pkg.apis.meta.v1.ListMeta"}},"type":"object"},"com.github.openshift.cluster-network-operator.pkg.apis.network.v1.OperatorPKISpec":{"properties":{"targetCert":{"$ref":"#/components/schemas/com.github.openshift.cluster-network-operator.pkg.apis.network.v1.CertSpec"}},"type":"object"},"com.github.openshift.cluster-network-operator.pkg.apis.network.v1.OperatorPKIStatus":{"type":"object"},"io.k8s.apimachinery.pkg.apis.meta.v1.FieldsV1":{"properties":{"-":{"type":"string"}},"type":"object"},"io.k8s.apimachinery.pkg.apis.meta.v1.ListMeta":{"properties":{"continue":{"type":"string"},"remainingItemCount":{"format":"int64","type":"integer"},"resourceVersion":{"type":"string"},"selfLink":{"type":"string"}},"type":"object"},"io.k8s.apimachinery.pkg.apis.meta.v1.ManagedFieldsEntry":{"properties":{"apiVersion":{"type":"string"},"fieldsType":{"type":"string"},"fieldsV1":{"$ref":"#/components/schemas/io.k8s.apimachinery.pkg.apis.meta.v1.FieldsV1"},"manager":{"type":"string"},"operation":{"type":"string"},"subresource":{"type":"string"},"time":{"$ref":"#/components/schemas/io.k8s.apimachinery.pkg.apis.meta.v1.Time"}},"type":"object"},"io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta":{"properties":{"annotations":{"additionalProperties":{"type":"string"},"type":"object"},"creationTimestamp":{"$ref":"#/components/schemas/io.k8s.apimachinery.pkg.apis.meta.v1.Time"},"deletionGracePeriodSeconds":{"format":"int64","type":"integer"},"deletionTimestamp":{"$ref":"#/components/schemas/io.k8s.apimachinery.pkg.apis.meta.v1.Time"},"finalizers":{"items":{"type":"string"},"type":"array"},"generateName":{"type":"string"},"generation":{"format":"int64","type":"integer"},"labels":{"additionalProperties":{"type":"string"},"type":"object"},"managedFields":{"items":{"$ref":"#/components/schemas/io.k8s.apimachinery.pkg.apis.meta.v1.ManagedFieldsEntry"},"type":"array"},"name":{"type":"string"},"namespace":{"type":"string"},"ownerReferences":{"items":{"$ref":"#/components/schemas/io.k8s.apimachinery.pkg.apis.meta.v1.OwnerReference"},"type":"array"},"resourceVersion":{"type":"string"},"selfLink":{"type":"string"},"uid":{"type":"string"}},"type":"object"},"io.k8s.apimachinery.pkg.apis.meta.v1.OwnerReference":{"properties":{"apiVersion":{"type":"string"},"blockOwnerDeletion":{"type":"boolean"},"controller":{"type":"boolean"},"kind":{"type":"string"},"name":{"type":"string"},"uid":{"type":"string"}},"type":"object"},"io.k8s.apimachinery.pkg.apis.meta.v1.Time":{"properties":{"Time":{"type":"string"}},"type":"object"}}},"info":{"title":"openshift-cluster-network-operator","version":"0.0.0"},"openapi":"3.0.0","paths":{"/apis/network.operator.openshift.io/v1/namespaces/{namespace}/operatorpkis":{"get":{"parameters":[{"in":"path","name":"namespace","required":true}],"responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/com.github.openshift.cluster-network-operator.pkg.apis.network.v1.OperatorPKIList"}}},"description":"OK"},"default":{"description":""}},"x-kubernetes-group-version-kind":{"group":"network.operator.openshift.io","kind":"OperatorPKIList","version":"v1"}}},"/apis/network.operator.openshift.io/v1/namespaces/{namespace}/operatorpkis/{name}":{"get":{"parameters":[{"in":"path","name":"namespace","required":true},{"in":"path","name":"name","required":true}],"responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/com.github.openshift.cluster-network-operator.pkg.apis.network.v1.OperatorPKI"}}},"description":"OK"},"default":{"description":""}},"x-kubernetes-group-version-kind":{"group":"network.operator.openshift.io","kind":"OperatorPKI","version":"v1"}}}}}
Loading

0 comments on commit 5fcffb4

Please sign in to comment.