Skip to content

Commit

Permalink
assign missing namespace w/signoff
Browse files Browse the repository at this point in the history
Signed-off-by: Jougan-0 <prasantmishra2018@gmail.com>
  • Loading branch information
Jougan-0 committed Oct 8, 2024
1 parent 0777946 commit d799564
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions utils/component/openapi_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"strings"

"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
Expand Down Expand Up @@ -100,6 +101,26 @@ func GenerateFromOpenAPI(resource string, pkg models.Package) ([]component.Compo
continue
}

// Determine if the resource is namespaced
var isNamespaced bool

scopeCue, err := utils.Lookup(fieldVal, `"x-kubernetes-resource".scope`)
if err == nil {
scope, err := scopeCue.String()
if err == nil {
if scope == "Namespaced" {
isNamespaced = true
} else if scope == "Cluster" {
isNamespaced = false
}
}
} else {
isNamespaced, err = getResourceScope(resource, kind)
if err != nil {
isNamespaced = false
}
}

c := component.ComponentDefinition{
SchemaVersion: v1beta1.ComponentSchemaVersion,
Format: component.JSON,
Expand All @@ -109,6 +130,9 @@ func GenerateFromOpenAPI(resource string, pkg models.Package) ([]component.Compo
Schema: string(crd),
},
DisplayName: manifests.FormatToReadableString(kind),
Metadata: component.ComponentDefinition_Metadata{
IsNamespaced: isNamespaced,
},
Model: model.ModelDefinition{
SchemaVersion: v1beta1.ModelSchemaVersion,
Model: model.Model{
Expand All @@ -127,7 +151,27 @@ func GenerateFromOpenAPI(resource string, pkg models.Package) ([]component.Compo
components = append(components, c)
}
return components, nil
}
func getResourceScope(manifest string, kind string) (bool, error) {
var m map[string]interface{}

err := yaml.Unmarshal([]byte(manifest), &m)
if err != nil {
return false, utils.ErrDecodeYaml(err)
}

paths, ok := m["paths"].(map[string]interface{})
if !ok {
return false, fmt.Errorf("paths not found in manifest")
}

for path := range paths {
if strings.Contains(path, "/namespaces/{namespace}/") && strings.Contains(path, strings.ToLower(kind)) {
return true, nil // Resource is namespaced
}
}

return false, nil // Resource is cluster-scoped
}

func getResolvedManifest(manifest string) (string, error) {
Expand Down

0 comments on commit d799564

Please sign in to comment.