This repository has been archived by the owner on Apr 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 532
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate off of openshift/generic-admission-server
That library os not actively maintained, anymore, and (worse) blocks updating k8s dependencies to v0.18 and beyond because of breaking changes to client-go (context.Context pass to most of the public functions now). Therefore, I refactored the webhook to rely on controller-runtime. This also simplifies much of the webhook's code.
- Loading branch information
Max Jonas Werner
committed
Aug 5, 2020
1 parent
32dba94
commit 98ff2a1
Showing
12 changed files
with
166 additions
and
244 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
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,105 @@ | ||
package app | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/healthz" | ||
|
||
"k8s.io/client-go/tools/clientcmd" | ||
|
||
"github.com/spf13/cobra" | ||
"k8s.io/component-base/logs" | ||
"k8s.io/klog" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
"sigs.k8s.io/controller-runtime/pkg/manager/signals" | ||
ctrwebhook "sigs.k8s.io/controller-runtime/pkg/webhook" | ||
"sigs.k8s.io/kubefed/pkg/controller/webhook/federatedtypeconfig" | ||
"sigs.k8s.io/kubefed/pkg/controller/webhook/kubefedcluster" | ||
"sigs.k8s.io/kubefed/pkg/controller/webhook/kubefedconfig" | ||
"sigs.k8s.io/kubefed/pkg/version" | ||
) | ||
|
||
const ( | ||
defaultPort = 443 | ||
) | ||
|
||
var ( | ||
certDir, kubeconfig, masterURL string | ||
port int | ||
) | ||
|
||
// NewWebhookCommand creates a *cobra.Command object with default parameters | ||
func NewWebhookCommand(stopChan <-chan struct{}) *cobra.Command { | ||
verFlag := false | ||
|
||
cmd := &cobra.Command{ | ||
Use: "webhook", | ||
Short: "Start a kubefed webhook server", | ||
Long: "Start a kubefed webhook server", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Fprintf(os.Stdout, "KubeFed webhook version: %s\n", fmt.Sprintf("%#v", version.Get())) | ||
if verFlag { | ||
os.Exit(0) | ||
} | ||
// PrintFlags(cmd.Flags()) | ||
|
||
if err := Run(stopChan); err != nil { | ||
fmt.Fprintf(os.Stderr, "%v\n", err) | ||
os.Exit(1) | ||
} | ||
}, | ||
} | ||
|
||
// Add the command line flags from other dependencies(klog, kubebuilder, etc.) | ||
cmd.Flags().AddGoFlagSet(flag.CommandLine) | ||
|
||
cmd.Flags().StringVar(&kubeconfig, "kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.") | ||
cmd.Flags().StringVar(&masterURL, "master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.") | ||
cmd.Flags().StringVar(&certDir, "cert-dir", "", "The directory where the TLS certs are located.") | ||
cmd.Flags().IntVar(&port, "secure-port", defaultPort, "The port on which to serve HTTPS.") | ||
|
||
return cmd | ||
} | ||
|
||
// Run runs the webhook with options. This should never exit. | ||
func Run(stopChan <-chan struct{}) error { | ||
logs.InitLogs() | ||
defer logs.FlushLogs() | ||
|
||
config, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfig) | ||
if err != nil { | ||
klog.Fatalf("error setting up webhook's config: %s", err) | ||
} | ||
mgr, err := manager.New(config, manager.Options{ | ||
Port: port, | ||
CertDir: certDir, | ||
}) | ||
if err != nil { | ||
klog.Fatalf("error setting up webhook manager: %s", err) | ||
} | ||
hookServer := mgr.GetWebhookServer() | ||
|
||
hookServer.Register("/validate-federatedtypeconfigs", &ctrwebhook.Admission{Handler: &federatedtypeconfig.FederatedTypeConfigAdmissionHook{}}) | ||
hookServer.Register("/validate-kubefedcluster", &ctrwebhook.Admission{Handler: &kubefedcluster.KubeFedClusterAdmissionHook{}}) | ||
hookServer.Register("/validate-kubefedconfig", &ctrwebhook.Admission{Handler: &kubefedconfig.KubeFedConfigValidator{}}) | ||
hookServer.Register("/default-kubefedconfig", &ctrwebhook.Admission{Handler: &kubefedconfig.KubeFedConfigDefaulter{}}) | ||
|
||
if err != nil { | ||
klog.Fatalf("error getting clientset: %s", err) | ||
} | ||
|
||
hookServer.WebhookMux.Handle("/readyz", http.StripPrefix("/readyz", &healthz.Handler{ | ||
Checks: map[string]healthz.Checker{ | ||
"ping": healthz.Ping, | ||
}, | ||
})) | ||
|
||
if err := mgr.Start(signals.SetupSignalHandler()); err != nil { | ||
klog.Fatalf("unable to run manager: %s", err) | ||
} | ||
|
||
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
Oops, something went wrong.