...

Source file src/k8s.io/apimachinery/pkg/util/strategicpatch/testing/openapi3.go

Documentation: k8s.io/apimachinery/pkg/util/strategicpatch/testing

     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 testing
    18  
    19  import (
    20  	"os"
    21  	"sync"
    22  
    23  	"k8s.io/kube-openapi/pkg/spec3"
    24  )
    25  
    26  type OpenAPIV3Getter struct {
    27  	Path      string
    28  	once      sync.Once
    29  	bytes     []byte
    30  	openapiv3 spec3.OpenAPI
    31  }
    32  
    33  func (f *OpenAPIV3Getter) SchemaBytesOrDie() []byte {
    34  	f.once.Do(func() {
    35  		_, err := os.Stat(f.Path)
    36  		if err != nil {
    37  			panic(err)
    38  		}
    39  		spec, err := os.ReadFile(f.Path)
    40  		if err != nil {
    41  			panic(err)
    42  		}
    43  		f.bytes = spec
    44  	})
    45  	return f.bytes
    46  }
    47  
    48  func (f *OpenAPIV3Getter) SchemaOrDie() *spec3.OpenAPI {
    49  	f.once.Do(func() {
    50  		_, err := os.Stat(f.Path)
    51  		if err != nil {
    52  			panic(err)
    53  		}
    54  		spec, err := os.ReadFile(f.Path)
    55  		if err != nil {
    56  			panic(err)
    57  		}
    58  
    59  		err = f.openapiv3.UnmarshalJSON(spec)
    60  		if err != nil {
    61  			panic(err)
    62  		}
    63  	})
    64  	return &f.openapiv3
    65  }
    66  

View as plain text