...

Source file src/k8s.io/client-go/openapi/typeconverter.go

Documentation: k8s.io/client-go/openapi

     1  /*
     2  Copyright 2023 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 openapi
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  
    23  	"k8s.io/apimachinery/pkg/util/managedfields"
    24  	"k8s.io/kube-openapi/pkg/spec3"
    25  	"k8s.io/kube-openapi/pkg/validation/spec"
    26  )
    27  
    28  func NewTypeConverter(client Client, preserveUnknownFields bool) (managedfields.TypeConverter, error) {
    29  	spec := map[string]*spec.Schema{}
    30  	paths, err := client.Paths()
    31  	if err != nil {
    32  		return nil, fmt.Errorf("failed to list paths: %w", err)
    33  	}
    34  	for _, gv := range paths {
    35  		s, err := gv.Schema("application/json")
    36  		if err != nil {
    37  			return nil, fmt.Errorf("failed to download schema: %w", err)
    38  		}
    39  		var openapi spec3.OpenAPI
    40  		if err := json.Unmarshal(s, &openapi); err != nil {
    41  			return nil, fmt.Errorf("failed to parse schema: %w", err)
    42  		}
    43  		for k, v := range openapi.Components.Schemas {
    44  			spec[k] = v
    45  		}
    46  	}
    47  	return managedfields.NewTypeConverter(spec, preserveUnknownFields)
    48  }
    49  

View as plain text