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 committed Sep 30, 2024
1 parent d1fe13c commit 0218482
Show file tree
Hide file tree
Showing 367 changed files with 31,965 additions and 42,718 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
5 changes: 4 additions & 1 deletion 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,6 +17,9 @@ 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/spf13/cobra v1.8.1
k8s.io/gengo/v2 v2.0.0-20240911193312-2b36238f13e9
Expand Down
4 changes: 2 additions & 2 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,6 @@ 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/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 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
Loading

0 comments on commit 0218482

Please sign in to comment.