-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge admission webhook and operator into one binary (#329)
- Loading branch information
Showing
5 changed files
with
188 additions
and
193 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,97 +1,39 @@ | ||
package cmds | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"time" | ||
"io" | ||
|
||
"github.com/appscode/go/log" | ||
stringz "github.com/appscode/go/strings" | ||
v "github.com/appscode/go/version" | ||
"github.com/appscode/kutil/discovery" | ||
"github.com/appscode/pat" | ||
api "github.com/appscode/stash/apis/stash" | ||
cs "github.com/appscode/stash/client" | ||
"github.com/appscode/stash/pkg/controller" | ||
"github.com/appscode/stash/pkg/docker" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"github.com/appscode/stash/pkg/cmds/server" | ||
"github.com/spf13/cobra" | ||
crd_cs "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1beta1" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/tools/clientcmd" | ||
) | ||
|
||
func NewCmdRun() *cobra.Command { | ||
var ( | ||
masterURL string | ||
kubeconfigPath string | ||
address = ":56790" | ||
opts = controller.Options{ | ||
DockerRegistry: docker.ACRegistry, | ||
StashImageTag: stringz.Val(v.Version.Version, "canary"), | ||
ResyncPeriod: 10 * time.Minute, | ||
MaxNumRequeues: 5, | ||
NumThreads: 2, | ||
} | ||
scratchDir = "/tmp" | ||
) | ||
func NewCmdRun(out, errOut io.Writer, stopCh <-chan struct{}) *cobra.Command { | ||
o := server.NewStashOptions(out, errOut) | ||
|
||
cmd := &cobra.Command{ | ||
Use: "run", | ||
Short: "Run Stash operator", | ||
Short: "Launch Stash Controller", | ||
Long: "Launch Stash Controller", | ||
DisableAutoGenTag: true, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
config, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfigPath) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
kubeClient := kubernetes.NewForConfigOrDie(config) | ||
stashClient := cs.NewForConfigOrDie(config) | ||
crdClient := crd_cs.NewForConfigOrDie(config) | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
log.Infof("Starting operator version %s+%s ...", v.Version.Version, v.Version.CommitHash) | ||
|
||
// get kube api server version | ||
opts.KubectlImageTag, err = discovery.GetBaseVersion(kubeClient.Discovery()) | ||
if err != nil { | ||
log.Fatalf("Failed to detect server version, reason: %s\n", err) | ||
if err := o.Complete(); err != nil { | ||
return err | ||
} | ||
|
||
ctrl := controller.New(kubeClient, crdClient, stashClient, opts) | ||
err = ctrl.Setup() | ||
if err != nil { | ||
log.Fatalln(err) | ||
if err := o.Validate(args); err != nil { | ||
return err | ||
} | ||
|
||
log.Infof("Starting operator version %s+%s ...", v.Version.Version, v.Version.CommitHash) | ||
// Now let's start the controller | ||
stop := make(chan struct{}) | ||
defer close(stop) | ||
go ctrl.Run(stop) | ||
|
||
m := pat.New() | ||
m.Get("/metrics", promhttp.Handler()) | ||
|
||
pattern := fmt.Sprintf("/%s/v1beta1/namespaces/%s/restics/%s/metrics", api.GroupName, PathParamNamespace, PathParamName) | ||
log.Infof("URL pattern: %s", pattern) | ||
exporter := &PrometheusExporter{ | ||
kubeClient: kubeClient, | ||
stashClient: stashClient.StashV1alpha1(), | ||
scratchDir: scratchDir, | ||
if err := o.Run(stopCh); err != nil { | ||
return err | ||
} | ||
m.Get(pattern, exporter) | ||
|
||
http.Handle("/", m) | ||
log.Infoln("Listening on", address) | ||
log.Fatal(http.ListenAndServe(address, nil)) | ||
return nil | ||
}, | ||
} | ||
cmd.Flags().StringVar(&masterURL, "master", masterURL, "The address of the Kubernetes API server (overrides any value in kubeconfig)") | ||
cmd.Flags().StringVar(&kubeconfigPath, "kubeconfig", kubeconfigPath, "Path to kubeconfig file with authorization information (the master location is set by the master flag).") | ||
cmd.Flags().StringVar(&address, "address", address, "Address to listen on for web interface and telemetry.") | ||
cmd.Flags().BoolVar(&opts.EnableRBAC, "rbac", opts.EnableRBAC, "Enable RBAC for operator") | ||
cmd.Flags().StringVar(&scratchDir, "scratch-dir", scratchDir, "Directory used to store temporary files. Use an `emptyDir` in Kubernetes.") | ||
cmd.Flags().DurationVar(&opts.ResyncPeriod, "resync-period", opts.ResyncPeriod, "If non-zero, will re-list this often. Otherwise, re-list will be delayed aslong as possible (until the upstream source closes the watch or times out.") | ||
cmd.Flags().StringVar(&opts.StashImageTag, "image-tag", opts.StashImageTag, "Image tag for sidecar, init-container, check-job and recovery-job") | ||
cmd.Flags().StringVar(&opts.DockerRegistry, "docker-registry", opts.DockerRegistry, "Docker image registry for sidecar, init-container, check-job, recovery-job and kubectl-job") | ||
|
||
o.AddFlags(cmd.Flags()) | ||
|
||
return cmd | ||
} |
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,81 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
stringz "github.com/appscode/go/strings" | ||
v "github.com/appscode/go/version" | ||
"github.com/appscode/kutil/discovery" | ||
cs "github.com/appscode/stash/client" | ||
hookapi "github.com/appscode/stash/pkg/admission/api" | ||
"github.com/appscode/stash/pkg/admission/plugin" | ||
"github.com/appscode/stash/pkg/controller" | ||
"github.com/appscode/stash/pkg/docker" | ||
"github.com/spf13/pflag" | ||
crd_cs "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1beta1" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
type ControllerOptions struct { | ||
EnableRBAC bool | ||
StashImageTag string | ||
DockerRegistry string | ||
MaxNumRequeues int | ||
NumThreads int | ||
ScratchDir string | ||
OpsAddress string | ||
ResyncPeriod time.Duration | ||
} | ||
|
||
func NewControllerOptions() *ControllerOptions { | ||
return &ControllerOptions{ | ||
DockerRegistry: docker.ACRegistry, | ||
StashImageTag: stringz.Val(v.Version.Version, "canary"), | ||
MaxNumRequeues: 5, | ||
NumThreads: 2, | ||
ScratchDir: "/tmp", | ||
OpsAddress: ":56790", | ||
ResyncPeriod: 10 * time.Minute, | ||
} | ||
} | ||
|
||
func (s *ControllerOptions) AddFlags(fs *pflag.FlagSet) { | ||
fs.StringVar(&s.OpsAddress, "ops-address", s.OpsAddress, "Address to listen on for web interface and telemetry.") | ||
fs.BoolVar(&s.EnableRBAC, "rbac", s.EnableRBAC, "Enable RBAC for operator") | ||
fs.StringVar(&s.ScratchDir, "scratch-dir", s.ScratchDir, "Directory used to store temporary files. Use an `emptyDir` in Kubernetes.") | ||
fs.StringVar(&s.StashImageTag, "image-tag", s.StashImageTag, "Image tag for sidecar, init-container, check-job and recovery-job") | ||
fs.StringVar(&s.DockerRegistry, "docker-registry", s.DockerRegistry, "Docker image registry for sidecar, init-container, check-job, recovery-job and kubectl-job") | ||
fs.DurationVar(&s.ResyncPeriod, "resync-period", s.ResyncPeriod, "If non-zero, will re-list this often. Otherwise, re-list will be delayed aslong as possible (until the upstream source closes the watch or times out.") | ||
} | ||
|
||
func (s *ControllerOptions) ApplyTo(cfg *controller.ControllerConfig) error { | ||
var err error | ||
|
||
cfg.EnableRBAC = s.EnableRBAC | ||
cfg.StashImageTag = s.StashImageTag | ||
cfg.DockerRegistry = s.DockerRegistry | ||
cfg.MaxNumRequeues = s.MaxNumRequeues | ||
cfg.NumThreads = s.NumThreads | ||
cfg.OpsAddress = s.OpsAddress | ||
cfg.ResyncPeriod = s.ResyncPeriod | ||
|
||
if cfg.KubeClient, err = kubernetes.NewForConfig(cfg.ClientConfig); err != nil { | ||
return err | ||
} | ||
if cfg.StashClient, err = cs.NewForConfig(cfg.ClientConfig); err != nil { | ||
return err | ||
} | ||
if cfg.CRDClient, err = crd_cs.NewForConfig(cfg.ClientConfig); err != nil { | ||
return err | ||
} | ||
cfg.AdmissionHooks = []hookapi.AdmissionHook{&plugin.CRDValidator{}} | ||
|
||
// get kube api server version | ||
cfg.KubectlImageTag, err = discovery.GetBaseVersion(cfg.KubeClient.Discovery()) | ||
if err != nil { | ||
return fmt.Errorf("failed to detect server version, reason: %s", err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.