...

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

Documentation: k8s.io/client-go/openapi

     1  /*
     2  Copyright 2017 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  	"context"
    21  	"net/url"
    22  
    23  	"k8s.io/kube-openapi/pkg/handler3"
    24  )
    25  
    26  const ContentTypeOpenAPIV3PB = "application/com.github.proto-openapi.spec.v3@v1.0+protobuf"
    27  
    28  type GroupVersion interface {
    29  	Schema(contentType string) ([]byte, error)
    30  }
    31  
    32  type groupversion struct {
    33  	client          *client
    34  	item            handler3.OpenAPIV3DiscoveryGroupVersion
    35  	useClientPrefix bool
    36  }
    37  
    38  func newGroupVersion(client *client, item handler3.OpenAPIV3DiscoveryGroupVersion, useClientPrefix bool) *groupversion {
    39  	return &groupversion{client: client, item: item, useClientPrefix: useClientPrefix}
    40  }
    41  
    42  func (g *groupversion) Schema(contentType string) ([]byte, error) {
    43  	if !g.useClientPrefix {
    44  		return g.client.restClient.Get().
    45  			RequestURI(g.item.ServerRelativeURL).
    46  			SetHeader("Accept", contentType).
    47  			Do(context.TODO()).
    48  			Raw()
    49  	}
    50  
    51  	locator, err := url.Parse(g.item.ServerRelativeURL)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	path := g.client.restClient.Get().
    57  		AbsPath(locator.Path).
    58  		SetHeader("Accept", contentType)
    59  
    60  	// Other than root endpoints(openapiv3/apis), resources have hash query parameter to support etags.
    61  	// However, absPath does not support handling query parameters internally,
    62  	// so that hash query parameter is added manually
    63  	for k, value := range locator.Query() {
    64  		for _, v := range value {
    65  			path.Param(k, v)
    66  		}
    67  	}
    68  
    69  	return path.Do(context.TODO()).Raw()
    70  }
    71  

View as plain text