mirror of
https://github.com/kubernetes/client-go.git
synced 2026-05-15 19:53:50 +00:00
Compare commits
14 Commits
release-1.
...
v0.27.4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c04e419b6a | ||
|
|
860d25f20e | ||
|
|
8d8e2bc8c9 | ||
|
|
af4a748070 | ||
|
|
22da8a58be | ||
|
|
81d8c785a4 | ||
|
|
d96d0d8844 | ||
|
|
015caa2eed | ||
|
|
fb5347d849 | ||
|
|
353c489f83 | ||
|
|
6ce3e7e915 | ||
|
|
aab9b0a45a | ||
|
|
a16d525506 | ||
|
|
559da627e8 |
@@ -20,6 +20,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"mime"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sort"
|
||||
@@ -58,8 +59,9 @@ const (
|
||||
defaultBurst = 300
|
||||
|
||||
AcceptV1 = runtime.ContentTypeJSON
|
||||
// Aggregated discovery content-type (currently v2beta1). NOTE: Currently, we are assuming the order
|
||||
// for "g", "v", and "as" from the server. We can only compare this string if we can make that assumption.
|
||||
// Aggregated discovery content-type (v2beta1). NOTE: content-type parameters
|
||||
// MUST be ordered (g, v, as) for server in "Accept" header (BUT we are resilient
|
||||
// to ordering when comparing returned values in "Content-Type" header).
|
||||
AcceptV2Beta1 = runtime.ContentTypeJSON + ";" + "g=apidiscovery.k8s.io;v=v2beta1;as=APIGroupDiscoveryList"
|
||||
// Prioritize aggregated discovery by placing first in the order of discovery accept types.
|
||||
acceptDiscoveryFormats = AcceptV2Beta1 + "," + AcceptV1
|
||||
@@ -259,8 +261,16 @@ func (d *DiscoveryClient) downloadLegacy() (
|
||||
|
||||
var resourcesByGV map[schema.GroupVersion]*metav1.APIResourceList
|
||||
// Switch on content-type server responded with: aggregated or unaggregated.
|
||||
switch responseContentType {
|
||||
case AcceptV1:
|
||||
switch {
|
||||
case isV2Beta1ContentType(responseContentType):
|
||||
var aggregatedDiscovery apidiscovery.APIGroupDiscoveryList
|
||||
err = json.Unmarshal(body, &aggregatedDiscovery)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
apiGroupList, resourcesByGV, failedGVs = SplitGroupsAndResources(aggregatedDiscovery)
|
||||
default:
|
||||
// Default is unaggregated discovery v1.
|
||||
var v metav1.APIVersions
|
||||
err = json.Unmarshal(body, &v)
|
||||
if err != nil {
|
||||
@@ -271,15 +281,6 @@ func (d *DiscoveryClient) downloadLegacy() (
|
||||
apiGroup = apiVersionsToAPIGroup(&v)
|
||||
}
|
||||
apiGroupList.Groups = []metav1.APIGroup{apiGroup}
|
||||
case AcceptV2Beta1:
|
||||
var aggregatedDiscovery apidiscovery.APIGroupDiscoveryList
|
||||
err = json.Unmarshal(body, &aggregatedDiscovery)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
apiGroupList, resourcesByGV, failedGVs = SplitGroupsAndResources(aggregatedDiscovery)
|
||||
default:
|
||||
return nil, nil, nil, fmt.Errorf("Unknown discovery response content-type: %s", responseContentType)
|
||||
}
|
||||
|
||||
return apiGroupList, resourcesByGV, failedGVs, nil
|
||||
@@ -313,13 +314,8 @@ func (d *DiscoveryClient) downloadAPIs() (
|
||||
failedGVs := map[schema.GroupVersion]error{}
|
||||
var resourcesByGV map[schema.GroupVersion]*metav1.APIResourceList
|
||||
// Switch on content-type server responded with: aggregated or unaggregated.
|
||||
switch responseContentType {
|
||||
case AcceptV1:
|
||||
err = json.Unmarshal(body, apiGroupList)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
case AcceptV2Beta1:
|
||||
switch {
|
||||
case isV2Beta1ContentType(responseContentType):
|
||||
var aggregatedDiscovery apidiscovery.APIGroupDiscoveryList
|
||||
err = json.Unmarshal(body, &aggregatedDiscovery)
|
||||
if err != nil {
|
||||
@@ -327,12 +323,38 @@ func (d *DiscoveryClient) downloadAPIs() (
|
||||
}
|
||||
apiGroupList, resourcesByGV, failedGVs = SplitGroupsAndResources(aggregatedDiscovery)
|
||||
default:
|
||||
return nil, nil, nil, fmt.Errorf("Unknown discovery response content-type: %s", responseContentType)
|
||||
// Default is unaggregated discovery v1.
|
||||
err = json.Unmarshal(body, apiGroupList)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return apiGroupList, resourcesByGV, failedGVs, nil
|
||||
}
|
||||
|
||||
// isV2Beta1ContentType checks of the content-type string is both
|
||||
// "application/json" and contains the v2beta1 content-type params.
|
||||
// NOTE: This function is resilient to the ordering of the
|
||||
// content-type parameters, as well as parameters added by
|
||||
// intermediaries such as proxies or gateways. Examples:
|
||||
//
|
||||
// "application/json; g=apidiscovery.k8s.io;v=v2beta1;as=APIGroupDiscoveryList" = true
|
||||
// "application/json; as=APIGroupDiscoveryList;v=v2beta1;g=apidiscovery.k8s.io" = true
|
||||
// "application/json; as=APIGroupDiscoveryList;v=v2beta1;g=apidiscovery.k8s.io;charset=utf-8" = true
|
||||
// "application/json" = false
|
||||
// "application/json; charset=UTF-8" = false
|
||||
func isV2Beta1ContentType(contentType string) bool {
|
||||
base, params, err := mime.ParseMediaType(contentType)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return runtime.ContentTypeJSON == base &&
|
||||
params["g"] == "apidiscovery.k8s.io" &&
|
||||
params["v"] == "v2beta1" &&
|
||||
params["as"] == "APIGroupDiscoveryList"
|
||||
}
|
||||
|
||||
// ServerGroups returns the supported groups, with information like supported versions and the
|
||||
// preferred version.
|
||||
func (d *DiscoveryClient) ServerGroups() (*metav1.APIGroupList, error) {
|
||||
|
||||
@@ -1395,8 +1395,9 @@ func TestAggregatedServerGroups(t *testing.T) {
|
||||
}
|
||||
output, err := json.Marshal(agg)
|
||||
require.NoError(t, err)
|
||||
// Content-type is "aggregated" discovery format.
|
||||
w.Header().Set("Content-Type", AcceptV2Beta1)
|
||||
// Content-Type is "aggregated" discovery format. Add extra parameter
|
||||
// to ensure we are resilient to these extra parameters.
|
||||
w.Header().Set("Content-Type", AcceptV2Beta1+"; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(output)
|
||||
}))
|
||||
@@ -1985,8 +1986,9 @@ func TestAggregatedServerGroupsAndResources(t *testing.T) {
|
||||
}
|
||||
output, err := json.Marshal(agg)
|
||||
require.NoError(t, err)
|
||||
// Content-type is "aggregated" discovery format.
|
||||
w.Header().Set("Content-Type", AcceptV2Beta1)
|
||||
// Content-type is "aggregated" discovery format. Add extra parameter
|
||||
// to ensure we are resilient to these extra parameters.
|
||||
w.Header().Set("Content-Type", AcceptV2Beta1+"; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(output)
|
||||
}))
|
||||
@@ -2125,8 +2127,9 @@ func TestAggregatedServerGroupsAndResourcesWithErrors(t *testing.T) {
|
||||
}
|
||||
output, err := json.Marshal(agg)
|
||||
require.NoError(t, err)
|
||||
// Content-type is "aggregated" discovery format.
|
||||
w.Header().Set("Content-Type", AcceptV2Beta1)
|
||||
// Content-type is "aggregated" discovery format. Add extra parameter
|
||||
// to ensure we are resilient to these extra parameters.
|
||||
w.Header().Set("Content-Type", AcceptV2Beta1+"; charset=utf-8")
|
||||
w.WriteHeader(status)
|
||||
w.Write(output)
|
||||
}))
|
||||
@@ -2733,8 +2736,9 @@ func TestAggregatedServerPreferredResources(t *testing.T) {
|
||||
}
|
||||
output, err := json.Marshal(agg)
|
||||
require.NoError(t, err)
|
||||
// Content-type is "aggregated" discovery format.
|
||||
w.Header().Set("Content-Type", AcceptV2Beta1)
|
||||
// Content-type is "aggregated" discovery format. Add extra parameter
|
||||
// to ensure we are resilient to these extra parameters.
|
||||
w.Header().Set("Content-Type", AcceptV2Beta1+"; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(output)
|
||||
}))
|
||||
@@ -2758,6 +2762,58 @@ func TestAggregatedServerPreferredResources(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiscoveryContentTypeVersion(t *testing.T) {
|
||||
tests := []struct {
|
||||
contentType string
|
||||
isV2Beta1 bool
|
||||
}{
|
||||
{
|
||||
contentType: "application/json; g=apidiscovery.k8s.io;v=v2beta1;as=APIGroupDiscoveryList",
|
||||
isV2Beta1: true,
|
||||
},
|
||||
{
|
||||
// content-type parameters are not in correct order, but comparison ignores order.
|
||||
contentType: "application/json; v=v2beta1;as=APIGroupDiscoveryList;g=apidiscovery.k8s.io",
|
||||
isV2Beta1: true,
|
||||
},
|
||||
{
|
||||
// content-type parameters are not in correct order, but comparison ignores order.
|
||||
contentType: "application/json; as=APIGroupDiscoveryList;g=apidiscovery.k8s.io;v=v2beta1",
|
||||
isV2Beta1: true,
|
||||
},
|
||||
{
|
||||
// Ignores extra parameter "charset=utf-8"
|
||||
contentType: "application/json; g=apidiscovery.k8s.io;v=v2beta1;as=APIGroupDiscoveryList;charset=utf-8",
|
||||
isV2Beta1: true,
|
||||
},
|
||||
{
|
||||
contentType: "application/json",
|
||||
isV2Beta1: false,
|
||||
},
|
||||
{
|
||||
contentType: "application/json; charset=UTF-8",
|
||||
isV2Beta1: false,
|
||||
},
|
||||
{
|
||||
contentType: "text/json",
|
||||
isV2Beta1: false,
|
||||
},
|
||||
{
|
||||
contentType: "text/html",
|
||||
isV2Beta1: false,
|
||||
},
|
||||
{
|
||||
contentType: "",
|
||||
isV2Beta1: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
isV2Beta1 := isV2Beta1ContentType(test.contentType)
|
||||
assert.Equal(t, test.isV2Beta1, isV2Beta1)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUseLegacyDiscovery(t *testing.T) {
|
||||
// Default client sends aggregated discovery accept format (first) as well as legacy format.
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
11
go.mod
11
go.mod
@@ -24,10 +24,10 @@ require (
|
||||
golang.org/x/term v0.6.0
|
||||
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8
|
||||
google.golang.org/protobuf v1.28.1
|
||||
k8s.io/api v0.0.0
|
||||
k8s.io/apimachinery v0.0.0
|
||||
k8s.io/api v0.27.4
|
||||
k8s.io/apimachinery v0.27.4
|
||||
k8s.io/klog/v2 v2.90.1
|
||||
k8s.io/kube-openapi v0.0.0-20230308215209-15aac26d736a
|
||||
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f
|
||||
k8s.io/utils v0.0.0-20230209194617-a36077c30491
|
||||
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd
|
||||
sigs.k8s.io/structured-merge-diff/v4 v4.2.3
|
||||
@@ -59,7 +59,6 @@ require (
|
||||
)
|
||||
|
||||
replace (
|
||||
k8s.io/api => ../api
|
||||
k8s.io/apimachinery => ../apimachinery
|
||||
k8s.io/client-go => ../client-go
|
||||
k8s.io/api => k8s.io/api v0.27.4
|
||||
k8s.io/apimachinery => k8s.io/apimachinery v0.27.4
|
||||
)
|
||||
|
||||
10
go.sum
10
go.sum
@@ -177,7 +177,7 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
|
||||
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
|
||||
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
|
||||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
|
||||
@@ -477,10 +477,14 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
|
||||
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
|
||||
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
|
||||
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
|
||||
k8s.io/api v0.27.4 h1:0pCo/AN9hONazBKlNUdhQymmnfLRbSZjd5H5H3f0bSs=
|
||||
k8s.io/api v0.27.4/go.mod h1:O3smaaX15NfxjzILfiln1D8Z3+gEYpjEpiNA/1EVK1Y=
|
||||
k8s.io/apimachinery v0.27.4 h1:CdxflD4AF61yewuid0fLl6bM4a3q04jWel0IlP+aYjs=
|
||||
k8s.io/apimachinery v0.27.4/go.mod h1:XNfZ6xklnMCOGGFNqXG7bUrQCoR04dh/E7FprV6pb+E=
|
||||
k8s.io/klog/v2 v2.90.1 h1:m4bYOKall2MmOiRaR1J+We67Do7vm9KiQVlT96lnHUw=
|
||||
k8s.io/klog/v2 v2.90.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
|
||||
k8s.io/kube-openapi v0.0.0-20230308215209-15aac26d736a h1:gmovKNur38vgoWfGtP5QOGNOA7ki4n6qNYoFAgMlNvg=
|
||||
k8s.io/kube-openapi v0.0.0-20230308215209-15aac26d736a/go.mod h1:y5VtZWM9sHHc2ZodIH/6SHzXj+TPU5USoA8lcIeKEKY=
|
||||
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f h1:2kWPakN3i/k81b0gvD5C5FJ2kxm1WrQFanWchyKuqGg=
|
||||
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f/go.mod h1:byini6yhqGC14c3ebc/QwanvYwhuMWF6yz2F8uwW8eg=
|
||||
k8s.io/utils v0.0.0-20230209194617-a36077c30491 h1:r0BAOLElQnnFhE/ApUsg3iHdVYYPBjNSSOMowRZxxsY=
|
||||
k8s.io/utils v0.0.0-20230209194617-a36077c30491/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
|
||||
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
|
||||
|
||||
@@ -19,6 +19,7 @@ package openapi
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
"k8s.io/client-go/rest"
|
||||
"k8s.io/kube-openapi/pkg/handler3"
|
||||
@@ -58,7 +59,11 @@ func (c *client) Paths() (map[string]GroupVersion, error) {
|
||||
// Create GroupVersions for each element of the result
|
||||
result := map[string]GroupVersion{}
|
||||
for k, v := range discoMap.Paths {
|
||||
result[k] = newGroupVersion(c, v)
|
||||
// If the server returned a URL rooted at /openapi/v3, preserve any additional client-side prefix.
|
||||
// If the server returned a URL not rooted at /openapi/v3, treat it as an actual server-relative URL.
|
||||
// See https://github.com/kubernetes/kubernetes/issues/117463 for details
|
||||
useClientPrefix := strings.HasPrefix(v.ServerRelativeURL, "/openapi/v3")
|
||||
result[k] = newGroupVersion(c, v, useClientPrefix)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package openapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
|
||||
"k8s.io/kube-openapi/pkg/handler3"
|
||||
)
|
||||
@@ -29,18 +30,41 @@ type GroupVersion interface {
|
||||
}
|
||||
|
||||
type groupversion struct {
|
||||
client *client
|
||||
item handler3.OpenAPIV3DiscoveryGroupVersion
|
||||
client *client
|
||||
item handler3.OpenAPIV3DiscoveryGroupVersion
|
||||
useClientPrefix bool
|
||||
}
|
||||
|
||||
func newGroupVersion(client *client, item handler3.OpenAPIV3DiscoveryGroupVersion) *groupversion {
|
||||
return &groupversion{client: client, item: item}
|
||||
func newGroupVersion(client *client, item handler3.OpenAPIV3DiscoveryGroupVersion, useClientPrefix bool) *groupversion {
|
||||
return &groupversion{client: client, item: item, useClientPrefix: useClientPrefix}
|
||||
}
|
||||
|
||||
func (g *groupversion) Schema(contentType string) ([]byte, error) {
|
||||
return g.client.restClient.Get().
|
||||
RequestURI(g.item.ServerRelativeURL).
|
||||
SetHeader("Accept", contentType).
|
||||
Do(context.TODO()).
|
||||
Raw()
|
||||
if !g.useClientPrefix {
|
||||
return g.client.restClient.Get().
|
||||
RequestURI(g.item.ServerRelativeURL).
|
||||
SetHeader("Accept", contentType).
|
||||
Do(context.TODO()).
|
||||
Raw()
|
||||
}
|
||||
|
||||
locator, err := url.Parse(g.item.ServerRelativeURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
path := g.client.restClient.Get().
|
||||
AbsPath(locator.Path).
|
||||
SetHeader("Accept", contentType)
|
||||
|
||||
// Other than root endpoints(openapiv3/apis), resources have hash query parameter to support etags.
|
||||
// However, absPath does not support handling query parameters internally,
|
||||
// so that hash query parameter is added manually
|
||||
for k, value := range locator.Query() {
|
||||
for _, v := range value {
|
||||
path.Param(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
return path.Do(context.TODO()).Raw()
|
||||
}
|
||||
|
||||
106
openapi/groupversion_test.go
Normal file
106
openapi/groupversion_test.go
Normal file
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
Copyright 2023 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package openapi
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
appsv1 "k8s.io/api/apps/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/client-go/kubernetes/scheme"
|
||||
"k8s.io/client-go/rest"
|
||||
)
|
||||
|
||||
func TestGroupVersion(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
prefix string
|
||||
serverReturnsPrefix bool
|
||||
}{
|
||||
{
|
||||
name: "no prefix",
|
||||
prefix: "",
|
||||
serverReturnsPrefix: false,
|
||||
},
|
||||
{
|
||||
name: "prefix not in discovery",
|
||||
prefix: "/test-endpoint",
|
||||
serverReturnsPrefix: false,
|
||||
},
|
||||
{
|
||||
name: "prefix in discovery",
|
||||
prefix: "/test-endpoint",
|
||||
serverReturnsPrefix: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch {
|
||||
case r.URL.Path == test.prefix+"/openapi/v3/apis/apps/v1" && r.URL.RawQuery == "hash=014fbff9a07c":
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"openapi":"3.0.0","info":{"title":"Kubernetes","version":"unversioned"}}`))
|
||||
case r.URL.Path == test.prefix+"/openapi/v3":
|
||||
// return root content
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
if test.serverReturnsPrefix {
|
||||
w.Write([]byte(fmt.Sprintf(`{"paths":{"apis/apps/v1":{"serverRelativeURL":"%s/openapi/v3/apis/apps/v1?hash=014fbff9a07c"}}}`, test.prefix)))
|
||||
} else {
|
||||
w.Write([]byte(`{"paths":{"apis/apps/v1":{"serverRelativeURL":"/openapi/v3/apis/apps/v1?hash=014fbff9a07c"}}}`))
|
||||
}
|
||||
default:
|
||||
t.Errorf("unexpected request: %s", r.URL.String())
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
c, err := rest.RESTClientFor(&rest.Config{
|
||||
Host: server.URL + test.prefix,
|
||||
ContentConfig: rest.ContentConfig{
|
||||
NegotiatedSerializer: scheme.Codecs,
|
||||
GroupVersion: &appsv1.SchemeGroupVersion,
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error occurred: %v", err)
|
||||
}
|
||||
|
||||
openapiClient := NewClient(c)
|
||||
paths, err := openapiClient.Paths()
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error occurred: %v", err)
|
||||
}
|
||||
schema, err := paths["apis/apps/v1"].Schema(runtime.ContentTypeJSON)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error occurred: %v", err)
|
||||
}
|
||||
expectedResult := `{"openapi":"3.0.0","info":{"title":"Kubernetes","version":"unversioned"}}`
|
||||
if string(schema) != expectedResult {
|
||||
t.Fatalf("unexpected result actual: %s expected: %s", string(schema), expectedResult)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,7 @@ import (
|
||||
"crypto/x509/pkix"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"math"
|
||||
"math/big"
|
||||
"net"
|
||||
"os"
|
||||
@@ -57,8 +58,14 @@ type AltNames struct {
|
||||
// NewSelfSignedCACert creates a CA certificate
|
||||
func NewSelfSignedCACert(cfg Config, key crypto.Signer) (*x509.Certificate, error) {
|
||||
now := time.Now()
|
||||
// returns a uniform random value in [0, max-1), then add 1 to serial to make it a uniform random value in [1, max).
|
||||
serial, err := cryptorand.Int(cryptorand.Reader, new(big.Int).SetInt64(math.MaxInt64-1))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
serial = new(big.Int).Add(serial, big.NewInt(1))
|
||||
tmpl := x509.Certificate{
|
||||
SerialNumber: new(big.Int).SetInt64(0),
|
||||
SerialNumber: serial,
|
||||
Subject: pkix.Name{
|
||||
CommonName: cfg.CommonName,
|
||||
Organization: cfg.Organization,
|
||||
@@ -116,9 +123,14 @@ func GenerateSelfSignedCertKeyWithFixtures(host string, alternateIPs []net.IP, a
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
// returns a uniform random value in [0, max-1), then add 1 to serial to make it a uniform random value in [1, max).
|
||||
serial, err := cryptorand.Int(cryptorand.Reader, new(big.Int).SetInt64(math.MaxInt64-1))
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
serial = new(big.Int).Add(serial, big.NewInt(1))
|
||||
caTemplate := x509.Certificate{
|
||||
SerialNumber: big.NewInt(1),
|
||||
SerialNumber: serial,
|
||||
Subject: pkix.Name{
|
||||
CommonName: fmt.Sprintf("%s-ca@%d", host, time.Now().Unix()),
|
||||
},
|
||||
@@ -144,9 +156,14 @@ func GenerateSelfSignedCertKeyWithFixtures(host string, alternateIPs []net.IP, a
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
// returns a uniform random value in [0, max-1), then add 1 to serial to make it a uniform random value in [1, max).
|
||||
serial, err = cryptorand.Int(cryptorand.Reader, new(big.Int).SetInt64(math.MaxInt64-1))
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
serial = new(big.Int).Add(serial, big.NewInt(1))
|
||||
template := x509.Certificate{
|
||||
SerialNumber: big.NewInt(2),
|
||||
SerialNumber: serial,
|
||||
Subject: pkix.Name{
|
||||
CommonName: fmt.Sprintf("%s@%d", host, time.Now().Unix()),
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user