...

Source file src/sigs.k8s.io/kustomize/kyaml/fn/framework/parser/schema.go

Documentation: sigs.k8s.io/kustomize/kyaml/fn/framework/parser

     1  // Copyright 2021 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package parser
     5  
     6  import (
     7  	iofs "io/fs"
     8  	"os"
     9  
    10  	"k8s.io/kube-openapi/pkg/validation/spec"
    11  	"sigs.k8s.io/kustomize/kyaml/errors"
    12  	"sigs.k8s.io/kustomize/kyaml/fn/framework"
    13  )
    14  
    15  const (
    16  	// SchemaExtension is the file extension this package requires schema files to have
    17  	SchemaExtension = ".json"
    18  )
    19  
    20  // SchemaStrings returns a SchemaParser that will parse the schemas in the given strings.
    21  //
    22  // This is a helper for use with framework.TemplateProcessor#AdditionalSchemas. Example:
    23  //
    24  //	processor := framework.TemplateProcessor{
    25  //		//...
    26  //		AdditionalSchemas: parser.SchemaStrings(`
    27  //        {
    28  //          "definitions": {
    29  //            "com.example.v1.Foo": {
    30  //               ...
    31  //            }
    32  //          }
    33  //        }
    34  //		`),
    35  //
    36  func SchemaStrings(data ...string) framework.SchemaParser {
    37  	return framework.SchemaParserFunc(func() ([]*spec.Definitions, error) {
    38  		var defs []*spec.Definitions
    39  		for _, content := range data {
    40  			var schema spec.Schema
    41  			if err := schema.UnmarshalJSON([]byte(content)); err != nil {
    42  				return nil, err
    43  			} else if schema.Definitions == nil {
    44  				return nil, errors.Errorf("inline schema did not contain any definitions")
    45  			}
    46  			defs = append(defs, &schema.Definitions)
    47  		}
    48  		return defs, nil
    49  	})
    50  }
    51  
    52  // SchemaFiles returns a SchemaParser that will parse the schemas in the given files.
    53  // This is a helper for use with framework.TemplateProcessor#AdditionalSchemas.
    54  //	processor := framework.TemplateProcessor{
    55  //		//...
    56  //		AdditionalSchemas: parser.SchemaFiles("path/to/crd-schemas", "path/to/special-schema.json),
    57  //	}
    58  func SchemaFiles(paths ...string) SchemaParser {
    59  	return SchemaParser{parser{paths: paths, extensions: []string{SchemaExtension}}}
    60  }
    61  
    62  // SchemaParser is a framework.SchemaParser that can parse files or directories containing openapi schemas.
    63  type SchemaParser struct {
    64  	parser
    65  }
    66  
    67  // Parse implements framework.SchemaParser
    68  func (l SchemaParser) Parse() ([]*spec.Definitions, error) {
    69  	if l.fs == nil {
    70  		l.fs = os.DirFS(".")
    71  	}
    72  
    73  	var defs []*spec.Definitions
    74  	err := l.parse(func(content []byte, name string) error {
    75  		var schema spec.Schema
    76  		if err := schema.UnmarshalJSON(content); err != nil {
    77  			return err
    78  		} else if schema.Definitions == nil {
    79  			return errors.Errorf("schema %s did not contain any definitions", name)
    80  		}
    81  		defs = append(defs, &schema.Definitions)
    82  		return nil
    83  	})
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  	return defs, nil
    88  }
    89  
    90  // FromFS allows you to replace the filesystem in which the parser will look up the given paths.
    91  // For example, you can use an embed.FS.
    92  func (l SchemaParser) FromFS(fs iofs.FS) SchemaParser {
    93  	l.parser.fs = fs
    94  	return l
    95  }
    96  

View as plain text