github.com/alex123012/deckhouse-controller-tools@v0.0.0-20230510090815-d594daf1af8c/pkg/crd/schema_visitor.go (about)

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package crd
    18  
    19  import (
    20  	apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    21  )
    22  
    23  // SchemaVisitor walks the nodes of a schema.
    24  type SchemaVisitor interface {
    25  	// Visit is called for each schema node.  If it returns a visitor,
    26  	// the visitor will be called on each direct child node, and then
    27  	// this visitor will be called again with `nil` to indicate that
    28  	// all children have been visited.  If a nil visitor is returned,
    29  	// children are not visited.
    30  	//
    31  	// It is *NOT* safe to save references to the given schema.
    32  	// Make deepcopies if you need to keep things around beyond
    33  	// the lifetime of the call.
    34  	Visit(schema *apiext.JSONSchemaProps) SchemaVisitor
    35  }
    36  
    37  // EditSchema walks the given schema using the given visitor.  Actual
    38  // pointers to each schema node are passed to the visitor, so any changes
    39  // made by the visitor will be reflected to the passed-in schema.
    40  func EditSchema(schema *apiext.JSONSchemaProps, visitor SchemaVisitor) {
    41  	walker := schemaWalker{visitor: visitor}
    42  	walker.walkSchema(schema)
    43  }
    44  
    45  // schemaWalker knows how to walk the schema, saving modifications
    46  // made by the given visitor.
    47  type schemaWalker struct {
    48  	visitor SchemaVisitor
    49  }
    50  
    51  // walkSchema walks the given schema, saving modifications made by the visitor
    52  // (this is as simple as passing a pointer in most cases, but special care
    53  // needs to be taken to persist with maps).  It also visits referenced
    54  // schemata, dealing with circular references appropriately.  The returned
    55  // visitor will be used to visit all "children" of the current schema, followed
    56  // by a nil schema with the returned visitor to mark completion.  If a nil visitor
    57  // is returned, traversal will no continue into the children of the current schema.
    58  func (w schemaWalker) walkSchema(schema *apiext.JSONSchemaProps) {
    59  	// Walk a potential chain of schema references, keeping track of seen
    60  	// references to avoid circular references
    61  	subVisitor := w.visitor
    62  	seenRefs := map[string]bool{}
    63  	if schema.Ref != nil {
    64  		seenRefs[*schema.Ref] = true
    65  	}
    66  	for {
    67  		subVisitor = subVisitor.Visit(schema)
    68  		if subVisitor == nil {
    69  			return
    70  		}
    71  		// mark completion of the visitor
    72  		defer subVisitor.Visit(nil)
    73  
    74  		// Break if schema is not a reference or a cycle is detected
    75  		if schema.Ref == nil || len(*schema.Ref) == 0 || seenRefs[*schema.Ref] {
    76  			break
    77  		}
    78  		seenRefs[*schema.Ref] = true
    79  	}
    80  
    81  	// walk sub-schemata
    82  	subWalker := schemaWalker{visitor: subVisitor}
    83  	if schema.Items != nil {
    84  		subWalker.walkPtr(schema.Items.Schema)
    85  		subWalker.walkSlice(schema.Items.JSONSchemas)
    86  	}
    87  	subWalker.walkSlice(schema.AllOf)
    88  	subWalker.walkSlice(schema.OneOf)
    89  	subWalker.walkSlice(schema.AnyOf)
    90  	subWalker.walkPtr(schema.Not)
    91  	subWalker.walkMap(schema.Properties)
    92  	if schema.AdditionalProperties != nil {
    93  		subWalker.walkPtr(schema.AdditionalProperties.Schema)
    94  	}
    95  	subWalker.walkMap(schema.PatternProperties)
    96  	for name, dep := range schema.Dependencies {
    97  		subWalker.walkPtr(dep.Schema)
    98  		schema.Dependencies[name] = dep
    99  	}
   100  	if schema.AdditionalItems != nil {
   101  		subWalker.walkPtr(schema.AdditionalItems.Schema)
   102  	}
   103  	subWalker.walkMap(schema.Definitions)
   104  }
   105  
   106  // walkMap walks over values of the given map, saving changes to them.
   107  func (w schemaWalker) walkMap(defs map[string]apiext.JSONSchemaProps) {
   108  	for name, def := range defs {
   109  		// this is iter var reference is because we immediately preseve it below
   110  		//nolint:gosec
   111  		w.walkSchema(&def)
   112  		// make sure the edits actually go through since we can't
   113  		// take a reference to the value in the map
   114  		defs[name] = def
   115  	}
   116  }
   117  
   118  // walkSlice walks over items of the given slice.
   119  func (w schemaWalker) walkSlice(defs []apiext.JSONSchemaProps) {
   120  	for i := range defs {
   121  		w.walkSchema(&defs[i])
   122  	}
   123  }
   124  
   125  // walkPtr walks over the contents of the given pointer, if it's not nil.
   126  func (w schemaWalker) walkPtr(def *apiext.JSONSchemaProps) {
   127  	if def == nil {
   128  		return
   129  	}
   130  	w.walkSchema(def)
   131  }