Skip to content

Commit

Permalink
Merge pull request #92 from kubernetes-sigs/revert-88-revert-84-webhook
Browse files Browse the repository at this point in the history
Revert "Revert "Webhook scaffolding""
  • Loading branch information
k8s-ci-robot authored Oct 9, 2018
2 parents 7bc3911 + cd4491f commit 0c39d47
Show file tree
Hide file tree
Showing 143 changed files with 8,468 additions and 1,208 deletions.
53 changes: 50 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cmd/controller-scaffold/cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ controller-scaffold project --domain k8s.io --license apache2 --owner "The Kuber
dkr,
&manager.APIs{},
&manager.Controller{},
&manager.Webhook{},
&manager.Config{Image: imgName},
&project.GitIgnore{},
&project.Kustomize{},
Expand Down
103 changes: 103 additions & 0 deletions cmd/controller-scaffold/cmd/webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
Copyright 2018 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 cmd

import (
"fmt"
"log"
"os"
"os/exec"

"github.com/spf13/cobra"
flag "github.com/spf13/pflag"

"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-tools/pkg/scaffold"
"sigs.k8s.io/controller-tools/pkg/scaffold/input"
"sigs.k8s.io/controller-tools/pkg/scaffold/resource"
"sigs.k8s.io/controller-tools/pkg/scaffold/webhook"
)

var res *resource.Resource
var operations []string
var server string
var webhookType string

// WebhookCmd represents the webhook command
var WebhookCmd = &cobra.Command{
Use: "webhook",
Short: "Scaffold a webhook server",
Long: `Scaffold a webhook server`,
Example: `webhook example: TBD`,
Run: func(cmd *cobra.Command, args []string) {
DieIfNoProject()

fmt.Println("Writing scaffold for you to edit...")

if len(res.Resource) == 0 {
gvr, _ := meta.UnsafeGuessKindToResource(schema.GroupVersionKind{
Group: res.Group, Version: res.Version, Kind: res.Kind})
res.Resource = gvr.Resource
}

err := (&scaffold.Scaffold{}).Execute(input.Options{},
&webhook.AdmissionHandler{Resource: res, Config: webhook.Config{Server: server, Type: webhookType, Operations: operations}},
&webhook.AdmissionWebhookBuilder{Resource: res, Config: webhook.Config{Server: server, Type: webhookType, Operations: operations}},
&webhook.AdmissionWebhooks{Resource: res, Config: webhook.Config{Server: server, Type: webhookType, Operations: operations}},
&webhook.AddAdmissionWebhookBuilderHandler{Resource: res, Config: webhook.Config{Server: server, Type: webhookType, Operations: operations}},
&webhook.Server{Resource: res, Config: webhook.Config{Server: server, Type: webhookType, Operations: operations}},
&webhook.AddServer{Resource: res, Config: webhook.Config{Server: server, Type: webhookType, Operations: operations}},
)
if err != nil {
log.Fatal(err)
}

if doMake {
fmt.Println("Running make...")
cm := exec.Command("make") // #nosec
cm.Stderr = os.Stderr
cm.Stdout = os.Stdout
if err := cm.Run(); err != nil {
log.Fatal(err)
}
}
},
}

func init() {
rootCmd.AddCommand(WebhookCmd)
WebhookCmd.Flags().StringVar(&server, "server", "default",
"name of the server")
WebhookCmd.Flags().StringVar(&webhookType, "type", "",
"webhook type, e.g. mutating or validating")
WebhookCmd.Flags().StringSliceVar(&operations, "operations", []string{"create"},
"the operations that the webhook will intercept, e.g. create, update, delete and connect")
WebhookCmd.Flags().BoolVar(&doMake, "make", true,
"if true, run make after generating files")
res = gvkForFlags(WebhookCmd.Flags())
}

// gvkForFlags registers flags for Resource fields and returns the Resource
func gvkForFlags(f *flag.FlagSet) *resource.Resource {
r := &resource.Resource{}
f.StringVar(&r.Group, "group", "", "resource Group")
f.StringVar(&r.Version, "version", "", "resource Version")
f.StringVar(&r.Kind, "kind", "", "resource Kind")
f.StringVar(&r.Resource, "resource", "", "resource Resource")
return r
}
5 changes: 5 additions & 0 deletions generated_golden.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ cd test
ln -s ../vendor vendor
../bin/controller-scaffold project --domain testproject.org --controller-tools-path ".." --license apache2 --owner "The Kubernetes authors" --dep=false
../bin/controller-scaffold api --group crew --version v1 --kind FirstMate --controller=true --resource=true --make=false
../bin/controller-scaffold webhook --group crew --version v1 --kind FirstMate --type=mutating --operations=create,update
../bin/controller-scaffold webhook --group crew --version v1 --kind FirstMate --type=mutating --operations=delete
../bin/controller-scaffold api --group ship --version v1beta1 --kind Frigate --example=false --controller=true --resource=true --make=false
../bin/controller-scaffold webhook --group ship --version v1beta1 --kind Frigate --type=validating --operations=update
../bin/controller-scaffold api --group creatures --version v2alpha1 --kind Kraken --namespaced=false --example=false --controller=true --resource=true --make=false
../bin/controller-scaffold webhook --group creatures --version v2alpha1 --kind Kraken --type=validating --operations=create
../bin/controller-scaffold api --group core --version v1 --kind Namespace --example=false --controller=true --resource=false --namespaced=false
../bin/controller-scaffold webhook --group core --version v1 --kind Namespace --type=mutating --operations=update
../bin/controller-scaffold api --group policy --version v1beta1 --kind HealthCheckPolicy --example=false --controller=true --resource=true --namespaced=false
make
rm -rf ./bin/
Expand Down
38 changes: 30 additions & 8 deletions pkg/scaffold/manager/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,44 +43,66 @@ var cmdTemplate = `{{ .Boilerplate }}
package main
import (
"log"
"flag"
"os"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/manager"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
"sigs.k8s.io/controller-runtime/pkg/runtime/signals"
"{{ .Repo }}/pkg/apis"
"{{ .Repo }}/pkg/controller"
"{{ .Repo }}/pkg/webhook"
)
func main() {
logf.SetLogger(logf.ZapLogger(false))
log := logf.Log.WithName("entrypoint")
// Get a config to talk to the apiserver
log.Info("setting up client for manager")
cfg, err := config.GetConfig()
if err != nil {
log.Fatal(err)
log.Error(err, "unable to set up client config")
os.Exit(1)
}
// Create a new Cmd to provide shared dependencies and start components
log.Info("setting up manager")
mgr, err := manager.New(cfg, manager.Options{})
if err != nil {
log.Fatal(err)
log.Error(err, "unable to set up overall controller manager")
os.Exit(1)
}
log.Printf("Registering Components.")
log.Info("Registering Components.")
// Setup Scheme for all resources
log.Info("setting up scheme")
if err := apis.AddToScheme(mgr.GetScheme()); err != nil {
log.Fatal(err)
log.Error(err, "unable add APIs to scheme")
os.Exit(1)
}
// Setup all Controllers
log.Info("Setting up controller")
if err := controller.AddToManager(mgr); err != nil {
log.Fatal(err)
log.Error(err, "unable to register controllers to the manager")
os.Exit(1)
}
log.Printf("Starting the Cmd.")
log.Info("setting up webhooks")
if err := webhook.AddToManager(mgr); err != nil {
log.Error(err, "unable to register webhooks to the manager")
os.Exit(1)
}
// Start the Cmd
log.Fatal(mgr.Start(signals.SetupSignalHandler()))
log.Info("Starting the Cmd.")
if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
log.Error(err, "unable to run the manager")
os.Exit(1)
}
}
`
27 changes: 27 additions & 0 deletions pkg/scaffold/manager/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,40 @@ spec:
- command:
- /root/manager
image: {{ .Image }}
imagePullPolicy: Always
name: manager
env:
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: SECRET_NAME
value: $(WEBHOOK_SECRET_NAME)
resources:
limits:
cpu: 100m
memory: 30Mi
requests:
cpu: 100m
memory: 20Mi
ports:
- containerPort: 9876
name: webhook-server
protocol: TCP
volumeMounts:
- mountPath: /tmp/cert
name: cert
readOnly: true
terminationGracePeriodSeconds: 10
volumes:
- name: cert
secret:
defaultMode: 420
secretName: webhook-server-secret
---
apiVersion: v1
kind: Secret
metadata:
name: webhook-server-secret
namespace: system
`
Loading

0 comments on commit 0c39d47

Please sign in to comment.