-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
generate dummy admission controller when specifying `--admission-cont…
…roller` extend Config fields w/ admission controller and post-start hooks
- Loading branch information
1 parent
8a7491d
commit 93b61fc
Showing
10 changed files
with
529 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package generators | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"k8s.io/klog" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"text/template" | ||
|
||
"k8s.io/gengo/generator" | ||
) | ||
|
||
var _ generator.Generator = &apiGenerator{} | ||
|
||
type admissionGenerator struct { | ||
generator.DefaultGen | ||
projectRootPath string | ||
admissionKinds []string | ||
} | ||
|
||
func CreateAdmissionGenerator(apis *APIs, filename string, projectRootPath string, outputBase string) generator.Generator { | ||
admissionKinds := []string{} | ||
// filter out those resources created w/ `--admission-controller` flag | ||
for _, group := range apis.Groups { | ||
for _, version := range group.Versions { | ||
for _, resource := range version.Resources { | ||
resourceAdmissionControllerPkg := filepath.Join(outputBase, projectRootPath, "plugin", "admission", strings.ToLower(resource.Kind)) | ||
// if "<repo>/plugin/admission" package is present in the project, add it to the generated installation function | ||
if _, err := os.Stat(resourceAdmissionControllerPkg); err == nil { | ||
admissionKinds = append(admissionKinds, resource.Kind) | ||
klog.V(5).Infof("found existing admission controller for resource: %v/%v", resource.Group, resource.Kind) | ||
} | ||
} | ||
} | ||
} | ||
|
||
return &admissionGenerator{ | ||
generator.DefaultGen{OptionalName: filename}, | ||
projectRootPath, | ||
admissionKinds, | ||
} | ||
} | ||
|
||
func (d *admissionGenerator) Imports(c *generator.Context) []string { | ||
imports := []string{ | ||
"github.com/kubernetes-incubator/apiserver-builder-alpha/pkg/cmd/server", | ||
"k8s.io/client-go/rest", | ||
`genericserver "k8s.io/apiserver/pkg/server"`, | ||
} | ||
for _, kind := range d.admissionKinds { | ||
imports = append(imports, fmt.Sprintf( | ||
`. "%s/plugin/admission/%s"`, d.projectRootPath, strings.ToLower(kind))) | ||
} | ||
imports = append(imports, | ||
fmt.Sprintf(`aggregatedclientset "%s/pkg/client/clientset_generated/clientset"`, d.projectRootPath)) | ||
imports = append(imports, | ||
fmt.Sprintf(`aggregatedinformerfactory "%s/pkg/client/informers_generated/externalversions"`, d.projectRootPath)) | ||
imports = append(imports, | ||
fmt.Sprintf(`intializer "%s/plugin/admission"`, d.projectRootPath)) | ||
return imports | ||
} | ||
|
||
type AdmissionGeneratorParam struct { | ||
Kind string | ||
} | ||
|
||
func (d *admissionGenerator) Finalize(context *generator.Context, w io.Writer) error { | ||
if len(d.admissionKinds) == 0 { | ||
return nil | ||
} | ||
|
||
temp := template.Must(template.New("admission-install-template").Parse(AdmissionsInstallTemplate)) | ||
return temp.Execute(w, &struct { | ||
Admissions []string | ||
}{ | ||
Admissions: d.admissionKinds, | ||
}) | ||
} | ||
|
||
var AdmissionsInstallTemplate = ` | ||
func init() { | ||
server.AggregatedAdmissionInitializerGetter = GetAggregatedResourceAdmissionControllerInitializer | ||
{{ range .Admissions -}} | ||
server.AggregatedAdmissionPlugins["{{.}}"] = New{{.}}Plugin() | ||
{{ end }} | ||
} | ||
func GetAggregatedResourceAdmissionControllerInitializer(config *rest.Config) (admission.PluginInitializer, genericserver.PostStartHookFunc) { | ||
// init aggregated resource clients | ||
aggregatedResourceClient := aggregatedclientset.NewForConfigOrDie(config) | ||
aggregatedInformerFactory := aggregatedinformerfactory.NewSharedInformerFactory(aggregatedResourceClient, 0) | ||
aggregatedResourceInitializer := intializer.New(aggregatedResourceClient, aggregatedInformerFactory) | ||
return aggregatedResourceInitializer, func(context genericserver.PostStartHookContext) error { | ||
aggregatedInformerFactory.Start(context.StopCh) | ||
return nil | ||
} | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
|
||
/* | ||
Copyright YEAR 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 deeponeadmission | ||
|
||
import ( | ||
"fmt" | ||
aggregatedadmission "github.com/kubernetes-incubator/apiserver-builder-alpha/example/plugin/admission" | ||
aggregatedinformerfactory "github.com/kubernetes-incubator/apiserver-builder-alpha/example/pkg/client/informers_generated/externalversions" | ||
aggregatedclientset "github.com/kubernetes-incubator/apiserver-builder-alpha/example/pkg/client/clientset_generated/clientset" | ||
genericadmissioninitializer "k8s.io/apiserver/pkg/admission/initializer" | ||
"k8s.io/client-go/informers" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/apiserver/pkg/admission" | ||
) | ||
|
||
var _ admission.Interface = &deeponePlugin{} | ||
var _ admission.MutationInterface = &deeponePlugin{} | ||
var _ admission.ValidationInterface = &deeponePlugin{} | ||
var _ genericadmissioninitializer.WantsExternalKubeInformerFactory = &deeponePlugin{} | ||
var _ genericadmissioninitializer.WantsExternalKubeClientSet = &deeponePlugin{} | ||
var _ aggregatedadmission.WantsAggregatedResourceInformerFactory = &deeponePlugin{} | ||
var _ aggregatedadmission.WantsAggregatedResourceClientSet = &deeponePlugin{} | ||
|
||
func NewDeepOnePlugin() *deeponePlugin { | ||
return &deeponePlugin{ | ||
Handler: admission.NewHandler(admission.Create, admission.Update), | ||
} | ||
} | ||
|
||
type deeponePlugin struct { | ||
*admission.Handler | ||
} | ||
|
||
func (p *deeponePlugin) ValidateInitialization() error { | ||
return nil | ||
} | ||
|
||
func (p *deeponePlugin) Admit(a admission.Attributes) error { | ||
fmt.Println("admitting deepones") | ||
return nil | ||
} | ||
|
||
func (p *deeponePlugin) Validate(a admission.Attributes) error { | ||
return nil | ||
} | ||
|
||
func (p *deeponePlugin) SetAggregatedResourceInformerFactory(aggregatedinformerfactory.SharedInformerFactory) {} | ||
|
||
func (p *deeponePlugin) SetAggregatedResourceClientSet(aggregatedclientset.Interface) {} | ||
|
||
func (p *deeponePlugin) SetExternalKubeInformerFactory(informers.SharedInformerFactory) {} | ||
|
||
func (p *deeponePlugin) SetExternalKubeClientSet(kubernetes.Interface) {} |
Oops, something went wrong.