mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-07-17 17:52:12 +00:00
Add schema-path flag to input openAPI for setters
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
package setters2
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
"github.com/go-openapi/spec"
|
||||
@@ -116,6 +117,9 @@ type SetterDefinition struct {
|
||||
// Type is the type of the setter value.
|
||||
Type string `yaml:"type,omitempty"`
|
||||
|
||||
// Schema is the openAPI schema for setter constraints.
|
||||
Schema string `yaml:"schema,omitempty"`
|
||||
|
||||
// EnumValues is a map of possible setter values to actual field values.
|
||||
// If EnumValues is specified, then the value set the by user 1) MUST
|
||||
// be present in the enumValues map as a key, and 2) the map entry value
|
||||
@@ -137,6 +141,26 @@ func (sd SetterDefinition) Filter(object *yaml.RNode) (*yaml.RNode, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
definitions, err := object.Pipe(yaml.Lookup(openapi.SupplementaryOpenAPIFieldName, "definitions"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if sd.Schema != "" {
|
||||
schNode, err := ConvertJSONToYamlNode(sd.Schema)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = definitions.PipeE(yaml.SetField(key, schNode))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// don't write the schema to the extension
|
||||
sd.Schema = ""
|
||||
}
|
||||
|
||||
if sd.Description != "" {
|
||||
err = def.PipeE(yaml.FieldSetter{Name: "description", StringValue: sd.Description})
|
||||
if err != nil {
|
||||
@@ -176,6 +200,24 @@ func (sd SetterDefinition) Filter(object *yaml.RNode) (*yaml.RNode, error) {
|
||||
return object, nil
|
||||
}
|
||||
|
||||
// ConvertJSONToYamlNode parses input json string and returns equivalent yaml node
|
||||
func ConvertJSONToYamlNode(jsonStr string) (*yaml.RNode, error) {
|
||||
var body map[string]interface{}
|
||||
err := json.Unmarshal([]byte(jsonStr), &body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
yml, err := yaml.Marshal(body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ymlStr, err := yaml.Parse(string(yml))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ymlStr, nil
|
||||
}
|
||||
|
||||
// SetterDefinition may be used to update a files OpenAPI definitions with a new substitution.
|
||||
type SubstitutionDefinition struct {
|
||||
// Name is the name of the substitution to create or update
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
package settersutil
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/kustomize/kyaml/openapi"
|
||||
"sigs.k8s.io/kustomize/kyaml/setters2"
|
||||
@@ -21,6 +23,8 @@ type SetterCreator struct {
|
||||
|
||||
Type string
|
||||
|
||||
SchemaPath string
|
||||
|
||||
// FieldName if set will add the OpenAPI reference to fields with this name or path
|
||||
// FieldName may be the full name of the field, full path to the field, or the path suffix.
|
||||
// e.g. all of the following would match spec.template.spec.containers.image --
|
||||
@@ -35,10 +39,14 @@ type SetterCreator struct {
|
||||
}
|
||||
|
||||
func (c SetterCreator) Create(openAPIPath, resourcesPath string) error {
|
||||
schema, err := schemaFromFile(c.SchemaPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Update the OpenAPI definitions to hace the setter
|
||||
sd := setters2.SetterDefinition{
|
||||
Name: c.Name, Value: c.FieldValue, Description: c.Description, SetBy: c.SetBy,
|
||||
Type: c.Type,
|
||||
Type: c.Type, Schema: schema,
|
||||
}
|
||||
if err := sd.AddToFile(openAPIPath); err != nil {
|
||||
return err
|
||||
@@ -62,3 +70,15 @@ func (c SetterCreator) Create(openAPIPath, resourcesPath string) error {
|
||||
Outputs: []kio.Writer{inout},
|
||||
}.Execute()
|
||||
}
|
||||
|
||||
// schemaFromFile reads the contents from schemaPath and returns schema
|
||||
func schemaFromFile(schemaPath string) (string, error) {
|
||||
if schemaPath == "" {
|
||||
return "", nil
|
||||
}
|
||||
sch, err := ioutil.ReadFile(schemaPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(sch), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user