forked from metal-stack/csi-driver-lvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Vicente-Cheng/add-webhook
Add StorageClass Validator
- Loading branch information
Showing
2,027 changed files
with
206,631 additions
and
46,078 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/harvester/webhook/pkg/config" | ||
"github.com/harvester/webhook/pkg/server" | ||
"github.com/harvester/webhook/pkg/server/admission" | ||
"github.com/rancher/wrangler/pkg/generated/controllers/core" | ||
ctlstorage "github.com/rancher/wrangler/v3/pkg/generated/controllers/storage" | ||
"github.com/rancher/wrangler/v3/pkg/kubeconfig" | ||
"github.com/rancher/wrangler/v3/pkg/signals" | ||
"github.com/sirupsen/logrus" | ||
"github.com/urfave/cli/v2" | ||
"k8s.io/client-go/rest" | ||
|
||
"github.com/harvester/csi-driver-lvm/pkg/webhook/storageclass" | ||
) | ||
|
||
const webhookName = "harvester-csi-driver-lvm-webhook" | ||
|
||
func main() { | ||
var options config.Options | ||
var logLevel string | ||
|
||
flags := []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "loglevel", | ||
Usage: "Specify log level", | ||
EnvVars: []string{"LOGLEVEL"}, | ||
Value: "info", | ||
Destination: &logLevel, | ||
}, | ||
&cli.IntFlag{ | ||
Name: "threadiness", | ||
EnvVars: []string{"THREADINESS"}, | ||
Usage: "Specify controller threads", | ||
Value: 5, | ||
Destination: &options.Threadiness, | ||
}, | ||
&cli.IntFlag{ | ||
Name: "https-port", | ||
EnvVars: []string{"WEBHOOK_SERVER_HTTPS_PORT"}, | ||
Usage: "HTTPS listen port", | ||
Value: 8443, | ||
Destination: &options.HTTPSListenPort, | ||
}, | ||
&cli.StringFlag{ | ||
Name: "namespace", | ||
EnvVars: []string{"NAMESPACE"}, | ||
Destination: &options.Namespace, | ||
Usage: "The harvester namespace", | ||
Value: "harvester-system", | ||
Required: true, | ||
}, | ||
&cli.StringFlag{ | ||
Name: "controller-user", | ||
EnvVars: []string{"CONTROLLER_USER_NAME"}, | ||
Destination: &options.ControllerUsername, | ||
Value: "harvester-csi-driver-lvm-webhook", | ||
Usage: "The harvester controller username", | ||
}, | ||
&cli.StringFlag{ | ||
Name: "gc-user", | ||
EnvVars: []string{"GARBAGE_COLLECTION_USER_NAME"}, | ||
Destination: &options.GarbageCollectionUsername, | ||
Usage: "The system username that performs garbage collection", | ||
Value: "system:serviceaccount:kube-system:generic-garbage-collector", | ||
}, | ||
} | ||
|
||
cfg, err := kubeconfig.GetNonInteractiveClientConfig(os.Getenv("KUBECONFIG")).ClientConfig() | ||
if err != nil { | ||
logrus.Fatal(err) | ||
} | ||
|
||
ctx := signals.SetupSignalContext() | ||
|
||
app := cli.NewApp() | ||
app.Flags = flags | ||
app.Action = func(_ *cli.Context) error { | ||
setLogLevel(logLevel) | ||
err := runWebhookServer(ctx, cfg, &options) | ||
return err | ||
} | ||
|
||
if err := app.Run(os.Args); err != nil { | ||
logrus.Fatalf("run webhook server failed: %v", err) | ||
} | ||
} | ||
|
||
func runWebhookServer(ctx context.Context, cfg *rest.Config, options *config.Options) error { | ||
storageFactory, err := ctlstorage.NewFactoryFromConfig(cfg) | ||
if err != nil { | ||
return err | ||
} | ||
coreFactory, err := core.NewFactoryFromConfig(cfg) | ||
if err != nil { | ||
return err | ||
} | ||
nodeClient := coreFactory.Core().V1().Node() | ||
storageclassClient := storageFactory.Storage().V1().StorageClass() | ||
webhookServer := server.NewWebhookServer(ctx, cfg, webhookName, options) | ||
|
||
storageclassValidator := storageclass.NewStorageClassValidator(storageclassClient, nodeClient) | ||
|
||
var validators = []admission.Validator{ | ||
storageclassValidator, | ||
} | ||
|
||
if err := webhookServer.RegisterValidators(validators...); err != nil { | ||
return err | ||
} | ||
|
||
if err := webhookServer.Start(); err != nil { | ||
return err | ||
} | ||
|
||
<-ctx.Done() | ||
|
||
return nil | ||
} | ||
|
||
func setLogLevel(level string) { | ||
ll, err := logrus.ParseLevel(level) | ||
if err != nil { | ||
ll = logrus.DebugLevel | ||
} | ||
// set global log level | ||
logrus.SetLevel(ll) | ||
} |
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.