mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 02:41:25 +00:00
fix dropped subresources
some subresources were not properly included in the array due to pointers becoming stale over a resize
This commit is contained in:
parent
c9b3488400
commit
d74b6b2cfa
@ -71,7 +71,7 @@ type action struct {
|
|||||||
|
|
||||||
func ConvertGroupVersionIntoToDiscovery(list []metav1.APIResource) ([]apidiscoveryv2beta1.APIResourceDiscovery, error) {
|
func ConvertGroupVersionIntoToDiscovery(list []metav1.APIResource) ([]apidiscoveryv2beta1.APIResourceDiscovery, error) {
|
||||||
var apiResourceList []apidiscoveryv2beta1.APIResourceDiscovery
|
var apiResourceList []apidiscoveryv2beta1.APIResourceDiscovery
|
||||||
parentResources := map[string]*apidiscoveryv2beta1.APIResourceDiscovery{}
|
parentResources := make(map[string]int)
|
||||||
|
|
||||||
// Loop through all top-level resources
|
// Loop through all top-level resources
|
||||||
for _, r := range list {
|
for _, r := range list {
|
||||||
@ -87,7 +87,7 @@ func ConvertGroupVersionIntoToDiscovery(list []metav1.APIResource) ([]apidiscove
|
|||||||
scope = apidiscoveryv2beta1.ScopeCluster
|
scope = apidiscoveryv2beta1.ScopeCluster
|
||||||
}
|
}
|
||||||
|
|
||||||
apiResourceList = append(apiResourceList, apidiscoveryv2beta1.APIResourceDiscovery{
|
resource := apidiscoveryv2beta1.APIResourceDiscovery{
|
||||||
Resource: r.Name,
|
Resource: r.Name,
|
||||||
Scope: scope,
|
Scope: scope,
|
||||||
ResponseKind: &metav1.GroupVersionKind{
|
ResponseKind: &metav1.GroupVersionKind{
|
||||||
@ -99,8 +99,9 @@ func ConvertGroupVersionIntoToDiscovery(list []metav1.APIResource) ([]apidiscove
|
|||||||
ShortNames: r.ShortNames,
|
ShortNames: r.ShortNames,
|
||||||
Categories: r.Categories,
|
Categories: r.Categories,
|
||||||
SingularResource: r.SingularName,
|
SingularResource: r.SingularName,
|
||||||
})
|
}
|
||||||
parentResources[r.Name] = &apiResourceList[len(apiResourceList)-1]
|
apiResourceList = append(apiResourceList, resource)
|
||||||
|
parentResources[r.Name] = len(apiResourceList) - 1
|
||||||
}
|
}
|
||||||
|
|
||||||
// Loop through all subresources
|
// Loop through all subresources
|
||||||
@ -120,23 +121,19 @@ func ConvertGroupVersionIntoToDiscovery(list []metav1.APIResource) ([]apidiscove
|
|||||||
scope = apidiscoveryv2beta1.ScopeCluster
|
scope = apidiscoveryv2beta1.ScopeCluster
|
||||||
}
|
}
|
||||||
|
|
||||||
var parent *apidiscoveryv2beta1.APIResourceDiscovery
|
parentidx, exists := parentResources[split[0]]
|
||||||
var exists bool
|
|
||||||
|
|
||||||
parent, exists = parentResources[split[0]]
|
|
||||||
if !exists {
|
if !exists {
|
||||||
// If a subresource exists without a parent, create a parent
|
// If a subresource exists without a parent, create a parent
|
||||||
apiResourceList = append(apiResourceList, apidiscoveryv2beta1.APIResourceDiscovery{
|
apiResourceList = append(apiResourceList, apidiscoveryv2beta1.APIResourceDiscovery{
|
||||||
Resource: split[0],
|
Resource: split[0],
|
||||||
Scope: scope,
|
Scope: scope,
|
||||||
})
|
})
|
||||||
parentResources[split[0]] = &apiResourceList[len(apiResourceList)-1]
|
parentidx = len(apiResourceList) - 1
|
||||||
parent = &apiResourceList[len(apiResourceList)-1]
|
parentResources[split[0]] = parentidx
|
||||||
parentResources[split[0]] = parent
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if parent.Scope != scope {
|
if apiResourceList[parentidx].Scope != scope {
|
||||||
return nil, fmt.Errorf("Error: Parent %s (scope: %s) and subresource %s (scope: %s) scope do not match", split[0], parent.Scope, split[1], scope)
|
return nil, fmt.Errorf("Error: Parent %s (scope: %s) and subresource %s (scope: %s) scope do not match", split[0], apiResourceList[parentidx].Scope, split[1], scope)
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -151,9 +148,9 @@ func ConvertGroupVersionIntoToDiscovery(list []metav1.APIResource) ([]apidiscove
|
|||||||
Kind: r.Kind,
|
Kind: r.Kind,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
parent.Subresources = append(parent.Subresources, subresource)
|
apiResourceList[parentidx].Subresources = append(apiResourceList[parentidx].Subresources, subresource)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return apiResourceList, nil
|
return apiResourceList, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -206,6 +206,87 @@ func TestConvertAPIResourceToDiscovery(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
name: "Test multiple resources and subresources",
|
||||||
|
resources: []metav1.APIResource{
|
||||||
|
{
|
||||||
|
Name: "cronjobs",
|
||||||
|
Namespaced: true,
|
||||||
|
Kind: "CronJob",
|
||||||
|
Group: "batch",
|
||||||
|
Version: "v1",
|
||||||
|
ShortNames: []string{"cj"},
|
||||||
|
Verbs: []string{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "cronjobs/status",
|
||||||
|
Namespaced: true,
|
||||||
|
Kind: "CronJob",
|
||||||
|
Group: "batch",
|
||||||
|
Version: "v1",
|
||||||
|
ShortNames: []string{"cj"},
|
||||||
|
Verbs: []string{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "deployments",
|
||||||
|
Namespaced: true,
|
||||||
|
Kind: "Deployment",
|
||||||
|
Group: "apps",
|
||||||
|
Version: "v1",
|
||||||
|
ShortNames: []string{"deploy"},
|
||||||
|
Verbs: []string{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "deployments/status",
|
||||||
|
Namespaced: true,
|
||||||
|
Kind: "Deployment",
|
||||||
|
Group: "apps",
|
||||||
|
Version: "v1",
|
||||||
|
ShortNames: []string{"deploy"},
|
||||||
|
Verbs: []string{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
wantAPIResourceDiscovery: []apidiscoveryv2beta1.APIResourceDiscovery{
|
||||||
|
{
|
||||||
|
Resource: "cronjobs",
|
||||||
|
Scope: apidiscoveryv2beta1.ScopeNamespace,
|
||||||
|
ResponseKind: &metav1.GroupVersionKind{
|
||||||
|
Group: "batch",
|
||||||
|
Version: "v1",
|
||||||
|
Kind: "CronJob",
|
||||||
|
},
|
||||||
|
ShortNames: []string{"cj"},
|
||||||
|
Verbs: []string{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
|
||||||
|
Subresources: []apidiscoveryv2beta1.APISubresourceDiscovery{{
|
||||||
|
Subresource: "status",
|
||||||
|
ResponseKind: &metav1.GroupVersionKind{
|
||||||
|
Group: "batch",
|
||||||
|
Version: "v1",
|
||||||
|
Kind: "CronJob",
|
||||||
|
},
|
||||||
|
Verbs: []string{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
|
||||||
|
}},
|
||||||
|
}, {
|
||||||
|
Resource: "deployments",
|
||||||
|
Scope: apidiscoveryv2beta1.ScopeNamespace,
|
||||||
|
ResponseKind: &metav1.GroupVersionKind{
|
||||||
|
Group: "apps",
|
||||||
|
Version: "v1",
|
||||||
|
Kind: "Deployment",
|
||||||
|
},
|
||||||
|
ShortNames: []string{"deploy"},
|
||||||
|
Verbs: []string{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
|
||||||
|
Subresources: []apidiscoveryv2beta1.APISubresourceDiscovery{{
|
||||||
|
Subresource: "status",
|
||||||
|
ResponseKind: &metav1.GroupVersionKind{
|
||||||
|
Group: "apps",
|
||||||
|
Version: "v1",
|
||||||
|
Kind: "Deployment",
|
||||||
|
},
|
||||||
|
Verbs: []string{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}, {
|
||||||
name: "Test with subresource with no parent",
|
name: "Test with subresource with no parent",
|
||||||
resources: []metav1.APIResource{
|
resources: []metav1.APIResource{
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user