...

Source file src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/cel/model/schemas.go

Documentation: k8s.io/apiextensions-apiserver/pkg/apiserver/schema/cel/model

     1  /*
     2  Copyright 2022 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 model
    18  
    19  import (
    20  	apiservercel "k8s.io/apiserver/pkg/cel"
    21  	"k8s.io/apiserver/pkg/cel/common"
    22  
    23  	"k8s.io/apiextensions-apiserver/pkg/apiserver/schema"
    24  )
    25  
    26  // SchemaDeclType converts the structural schema to a CEL declaration, or returns nil if the
    27  // structural schema should not be exposed in CEL expressions.
    28  // Set isResourceRoot to true for the root of a custom resource or embedded resource.
    29  //
    30  // Schemas with XPreserveUnknownFields not exposed unless they are objects. Array and "maps" schemas
    31  // are not exposed if their items or additionalProperties schemas are not exposed. Object Properties are not exposed
    32  // if their schema is not exposed.
    33  //
    34  // The CEL declaration for objects with XPreserveUnknownFields does not expose unknown fields.
    35  func SchemaDeclType(s *schema.Structural, isResourceRoot bool) *apiservercel.DeclType {
    36  	return common.SchemaDeclType(&Structural{Structural: s}, isResourceRoot)
    37  }
    38  
    39  // WithTypeAndObjectMeta ensures the kind, apiVersion and
    40  // metadata.name and metadata.generateName properties are specified, making a shallow copy of the provided schema if needed.
    41  func WithTypeAndObjectMeta(s *schema.Structural) *schema.Structural {
    42  	if s.Properties != nil &&
    43  		s.Properties["kind"].Type == "string" &&
    44  		s.Properties["apiVersion"].Type == "string" &&
    45  		s.Properties["metadata"].Type == "object" &&
    46  		s.Properties["metadata"].Properties != nil &&
    47  		s.Properties["metadata"].Properties["name"].Type == "string" &&
    48  		s.Properties["metadata"].Properties["generateName"].Type == "string" {
    49  		return s
    50  	}
    51  	result := &schema.Structural{
    52  		Generic:         s.Generic,
    53  		Extensions:      s.Extensions,
    54  		ValueValidation: s.ValueValidation,
    55  	}
    56  	props := make(map[string]schema.Structural, len(s.Properties))
    57  	for k, prop := range s.Properties {
    58  		props[k] = prop
    59  	}
    60  	stringType := schema.Structural{Generic: schema.Generic{Type: "string"}}
    61  	props["kind"] = stringType
    62  	props["apiVersion"] = stringType
    63  	props["metadata"] = schema.Structural{
    64  		Generic: schema.Generic{Type: "object"},
    65  		Properties: map[string]schema.Structural{
    66  			"name":         stringType,
    67  			"generateName": stringType,
    68  		},
    69  	}
    70  	result.Properties = props
    71  
    72  	return result
    73  }
    74  

View as plain text