Skip to content

Commit

Permalink
Merge pull request #113171 from Jefftree/aggregated-discovery-generic
Browse files Browse the repository at this point in the history
Aggregated discovery server changes

Kubernetes-commit: 8058e8eff8ba8541ddd48cd54bbb19a3fce62c09
  • Loading branch information
k8s-publishing-bot committed Nov 9, 2022
2 parents 470c040 + 915a888 commit d2c9e18
Show file tree
Hide file tree
Showing 5 changed files with 503 additions and 16 deletions.
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ require (
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.0.0-20221108053748-98c1aa6b3d0a
k8s.io/apimachinery v0.0.0-20221108055230-fd8a60496be5
k8s.io/apiserver v0.0.0-20221108213719-5643daa2db6a
k8s.io/apiserver v0.0.0-20221109094018-807ba65cc7a8
k8s.io/client-go v0.0.0-20221108173010-769443557e04
k8s.io/code-generator v0.0.0-20221108000200-7429fbb99432
k8s.io/component-base v0.0.0-20221108213136-021afb59bb71
k8s.io/component-base v0.0.0-20221109013135-4e8a9589a311
k8s.io/klog/v2 v2.80.1
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280
k8s.io/utils v0.0.0-20221107191617-1a15be271d1d
Expand Down Expand Up @@ -127,9 +127,9 @@ require (
replace (
k8s.io/api => k8s.io/api v0.0.0-20221108053748-98c1aa6b3d0a
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20221108055230-fd8a60496be5
k8s.io/apiserver => k8s.io/apiserver v0.0.0-20221108213719-5643daa2db6a
k8s.io/apiserver => k8s.io/apiserver v0.0.0-20221109094018-807ba65cc7a8
k8s.io/client-go => k8s.io/client-go v0.0.0-20221108173010-769443557e04
k8s.io/code-generator => k8s.io/code-generator v0.0.0-20221108000200-7429fbb99432
k8s.io/component-base => k8s.io/component-base v0.0.0-20221108213136-021afb59bb71
k8s.io/component-base => k8s.io/component-base v0.0.0-20221109013135-4e8a9589a311
k8s.io/kms => k8s.io/kms v0.0.0-20221028080743-a9ba1c11c0c6
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -873,14 +873,14 @@ k8s.io/api v0.0.0-20221108053748-98c1aa6b3d0a h1:GaCla9HtNyi63kysI/cyeA4bv6wRkIy
k8s.io/api v0.0.0-20221108053748-98c1aa6b3d0a/go.mod h1:PSXY9/fSNyKgKHUU+O9scnZiW8m+V1znqk49oI6hAEY=
k8s.io/apimachinery v0.0.0-20221108055230-fd8a60496be5 h1:iFAMJ1evvrO6X7dS7EKujS6An+bp3u/dD6opu8rn0QA=
k8s.io/apimachinery v0.0.0-20221108055230-fd8a60496be5/go.mod h1:VXMmlsE7YRJ5vyAyWpkKIfFkEbDNpVs0ObpkuQf1WfM=
k8s.io/apiserver v0.0.0-20221108213719-5643daa2db6a h1:Pgwr1+mXasRCcSKqXNgXPj497HPJjZ5KT8u4GVvNoY0=
k8s.io/apiserver v0.0.0-20221108213719-5643daa2db6a/go.mod h1:HEuVcGugsLz1DrS8BkNLT7wjhrBVrZh2wFZfTAuy2NE=
k8s.io/apiserver v0.0.0-20221109094018-807ba65cc7a8 h1:nb4/V/X+Isa/QCEEoMibzBiAsUunnY9TJl9XdKCSH44=
k8s.io/apiserver v0.0.0-20221109094018-807ba65cc7a8/go.mod h1:D07/28EX92tshRpWdcPpMb4TwlTZNGMh9DKrw5WQ+1E=
k8s.io/client-go v0.0.0-20221108173010-769443557e04 h1:ad7JkOkiLiyMKWHRkmbJgjCzySdkXxRxWeNosATW0mo=
k8s.io/client-go v0.0.0-20221108173010-769443557e04/go.mod h1:O6sEWJ2BPd8Dag831LA1lzC3WnE29nuwUJZZ4H2vlyo=
k8s.io/code-generator v0.0.0-20221108000200-7429fbb99432 h1:lWxXcC+YG3ZY5sCsk337h9RgzUK0sWoNWf8fs6KZm0I=
k8s.io/code-generator v0.0.0-20221108000200-7429fbb99432/go.mod h1:EzIGoP4u7NcVT4orMRQyvQcWqFCUVApa+V+28R7t/+M=
k8s.io/component-base v0.0.0-20221108213136-021afb59bb71 h1:Qr7dcMdpWjUZUEkZcbvGGQbtTlVR9b9VqQci/G0jzfY=
k8s.io/component-base v0.0.0-20221108213136-021afb59bb71/go.mod h1:5bp64lK0p+wJD2BFMin7Akfxiwvt58T4iDz2Q+6woBE=
k8s.io/component-base v0.0.0-20221109013135-4e8a9589a311 h1:rUbuNcL4yd5fSmGBMgRYsaodvXqHiu9TwjcmqH2toRY=
k8s.io/component-base v0.0.0-20221109013135-4e8a9589a311/go.mod h1:5bp64lK0p+wJD2BFMin7Akfxiwvt58T4iDz2Q+6woBE=
k8s.io/gengo v0.0.0-20220902162205-c0856e24416d h1:U9tB195lKdzwqicbJvyJeOXV7Klv+wNAWENRnXEGi08=
k8s.io/gengo v0.0.0-20220902162205-c0856e24416d/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E=
k8s.io/klog/v2 v2.2.0/go.mod h1:Od+F08eJP+W3HUb4pSrPpgp9DGU4GzlpG/TmITuYh/Y=
Expand Down
2 changes: 1 addition & 1 deletion pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (c completedConfig) New(delegationTarget genericapiserver.DelegationTarget)
s.GenericAPIServer.Handler.NonGoRestfulMux.HandlePrefix("/apis/", crdHandler)
s.GenericAPIServer.RegisterDestroyFunc(crdHandler.destroy)

discoveryController := NewDiscoveryController(s.Informers.Apiextensions().V1().CustomResourceDefinitions(), versionDiscoveryHandler, groupDiscoveryHandler)
discoveryController := NewDiscoveryController(s.Informers.Apiextensions().V1().CustomResourceDefinitions(), versionDiscoveryHandler, groupDiscoveryHandler, genericServer.AggregatedDiscoveryGroupManager)
namingController := status.NewNamingConditionController(s.Informers.Apiextensions().V1().CustomResourceDefinitions(), crdClient.ApiextensionsV1())
nonStructuralSchemaController := nonstructuralschema.NewConditionController(s.Informers.Apiextensions().V1().CustomResourceDefinitions(), crdClient.ApiextensionsV1())
apiApprovalController := apiapproval.NewKubernetesAPIApprovalPolicyConformantConditionController(s.Informers.Apiextensions().V1().CustomResourceDefinitions(), crdClient.ApiextensionsV1())
Expand Down
93 changes: 86 additions & 7 deletions pkg/apiserver/customresource_discovery_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"k8s.io/klog/v2"

apidiscoveryv2beta1 "k8s.io/api/apidiscovery/v2beta1"
autoscaling "k8s.io/api/autoscaling/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
Expand All @@ -31,6 +32,7 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/version"
"k8s.io/apiserver/pkg/endpoints/discovery"
discoveryendpoint "k8s.io/apiserver/pkg/endpoints/discovery/aggregated"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"

Expand All @@ -41,8 +43,9 @@ import (
)

type DiscoveryController struct {
versionHandler *versionDiscoveryHandler
groupHandler *groupDiscoveryHandler
versionHandler *versionDiscoveryHandler
groupHandler *groupDiscoveryHandler
resourceManager discoveryendpoint.ResourceManager

crdLister listers.CustomResourceDefinitionLister
crdsSynced cache.InformerSynced
Expand All @@ -53,12 +56,18 @@ type DiscoveryController struct {
queue workqueue.RateLimitingInterface
}

func NewDiscoveryController(crdInformer informers.CustomResourceDefinitionInformer, versionHandler *versionDiscoveryHandler, groupHandler *groupDiscoveryHandler) *DiscoveryController {
func NewDiscoveryController(
crdInformer informers.CustomResourceDefinitionInformer,
versionHandler *versionDiscoveryHandler,
groupHandler *groupDiscoveryHandler,
resourceManager discoveryendpoint.ResourceManager,
) *DiscoveryController {
c := &DiscoveryController{
versionHandler: versionHandler,
groupHandler: groupHandler,
crdLister: crdInformer.Lister(),
crdsSynced: crdInformer.Informer().HasSynced,
versionHandler: versionHandler,
groupHandler: groupHandler,
resourceManager: resourceManager,
crdLister: crdInformer.Lister(),
crdsSynced: crdInformer.Informer().HasSynced,

queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "DiscoveryController"),
}
Expand All @@ -78,6 +87,7 @@ func (c *DiscoveryController) sync(version schema.GroupVersion) error {

apiVersionsForDiscovery := []metav1.GroupVersionForDiscovery{}
apiResourcesForDiscovery := []metav1.APIResource{}
aggregatedApiResourcesForDiscovery := []apidiscoveryv2beta1.APIResourceDiscovery{}
versionsForDiscoveryMap := map[metav1.GroupVersion]bool{}

crds, err := c.crdLister.List(labels.Everything())
Expand Down Expand Up @@ -146,6 +156,53 @@ func (c *DiscoveryController) sync(version schema.GroupVersion) error {
if err != nil {
return err
}

if c.resourceManager != nil {
var scope apidiscoveryv2beta1.ResourceScope
if crd.Spec.Scope == apiextensionsv1.NamespaceScoped {
scope = apidiscoveryv2beta1.ScopeNamespace
} else {
scope = apidiscoveryv2beta1.ScopeCluster
}
apiResourceDiscovery := apidiscoveryv2beta1.APIResourceDiscovery{
Resource: crd.Status.AcceptedNames.Plural,
SingularResource: crd.Status.AcceptedNames.Singular,
Scope: scope,
ResponseKind: &metav1.GroupVersionKind{
Group: version.Group,
Version: version.Version,
Kind: crd.Status.AcceptedNames.Kind,
},
Verbs: verbs,
ShortNames: crd.Status.AcceptedNames.ShortNames,
Categories: crd.Status.AcceptedNames.Categories,
}
if subresources != nil && subresources.Status != nil {
apiResourceDiscovery.Subresources = append(apiResourceDiscovery.Subresources, apidiscoveryv2beta1.APISubresourceDiscovery{
Subresource: "status",
ResponseKind: &metav1.GroupVersionKind{
Group: version.Group,
Version: version.Version,
Kind: crd.Status.AcceptedNames.Kind,
},
Verbs: metav1.Verbs([]string{"get", "patch", "update"}),
})
}
if subresources != nil && subresources.Scale != nil {
apiResourceDiscovery.Subresources = append(apiResourceDiscovery.Subresources, apidiscoveryv2beta1.APISubresourceDiscovery{
Subresource: "scale",
ResponseKind: &metav1.GroupVersionKind{
Group: autoscaling.GroupName,
Version: "v1",
Kind: "Scale",
},
Verbs: metav1.Verbs([]string{"get", "patch", "update"}),
})

}
aggregatedApiResourcesForDiscovery = append(aggregatedApiResourcesForDiscovery, apiResourceDiscovery)
}

if subresources != nil && subresources.Status != nil {
apiResourcesForDiscovery = append(apiResourcesForDiscovery, metav1.APIResource{
Name: crd.Status.AcceptedNames.Plural + "/status",
Expand All @@ -170,6 +227,10 @@ func (c *DiscoveryController) sync(version schema.GroupVersion) error {
if !foundGroup {
c.groupHandler.unsetDiscovery(version.Group)
c.versionHandler.unsetDiscovery(version)

if c.resourceManager != nil {
c.resourceManager.RemoveGroup(version.Group)
}
return nil
}

Expand All @@ -186,12 +247,30 @@ func (c *DiscoveryController) sync(version schema.GroupVersion) error {

if !foundVersion {
c.versionHandler.unsetDiscovery(version)

if c.resourceManager != nil {
c.resourceManager.RemoveGroupVersion(metav1.GroupVersion{
Group: version.Group,
Version: version.Version,
})
}
return nil
}
c.versionHandler.setDiscovery(version, discovery.NewAPIVersionHandler(Codecs, version, discovery.APIResourceListerFunc(func() []metav1.APIResource {
return apiResourcesForDiscovery
})))

sort.Slice(aggregatedApiResourcesForDiscovery[:], func(i, j int) bool {
return aggregatedApiResourcesForDiscovery[i].Resource < aggregatedApiResourcesForDiscovery[j].Resource
})
if c.resourceManager != nil {
c.resourceManager.AddGroupVersion(version.Group, apidiscoveryv2beta1.APIVersionDiscovery{
Version: version.Version,
Resources: aggregatedApiResourcesForDiscovery,
})
// Default priority for CRDs
c.resourceManager.SetGroupPriority(version.Group, 1000)
}
return nil
}

Expand Down
Loading

0 comments on commit d2c9e18

Please sign in to comment.