-
-
Notifications
You must be signed in to change notification settings - Fork 21
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 #83 from K-Phoen/svc
Svc
- Loading branch information
Showing
193 changed files
with
33,556 additions
and
251 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 |
---|---|---|
@@ -1,93 +1,43 @@ | ||
package main | ||
|
||
import ( | ||
"crypto/tls" | ||
"net/http" | ||
"time" | ||
"fmt" | ||
|
||
"github.com/K-Phoen/dark/internal" | ||
"github.com/K-Phoen/dark/internal/pkg/dashboards" | ||
"github.com/K-Phoen/grabana" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/tools/clientcmd" | ||
"k8s.io/klog" | ||
"github.com/K-Phoen/dark/internal/pkg/worker" | ||
"github.com/voi-oss/svc" | ||
"go.uber.org/zap" | ||
|
||
// enables GCP auth | ||
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" | ||
|
||
clientset "github.com/K-Phoen/dark/internal/pkg/generated/clientset/versioned" | ||
informers "github.com/K-Phoen/dark/internal/pkg/generated/informers/externalversions" | ||
"github.com/K-Phoen/dark/internal/pkg/signals" | ||
environment "github.com/caarlos0/env/v6" | ||
"github.com/go-playground/validator/v10" | ||
) | ||
|
||
type config struct { | ||
MasterURL string `env:"K8S_MASTER_URL"` | ||
KubeConfig string `env:"K8S_CONFIG"` | ||
|
||
GrafanaHost string `env:"GRAFANA_HOST" validate:"required"` | ||
GrafanaToken string `env:"GRAFANA_TOKEN" validate:"required"` | ||
|
||
InsecureSkipVerify bool `env:"INSECURE_SKIP_VERIFY"` | ||
} | ||
|
||
func (cfg *config) loadFromEnv() error { | ||
if err := environment.Parse(cfg); err != nil { | ||
return err | ||
} | ||
if err := validator.New().Struct(*cfg); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
// serviceVersion will be populated by the build script with the sha of the last git commit. | ||
var serviceVersion = "snapshot" | ||
|
||
func main() { | ||
cfg := config{} | ||
if err := cfg.loadFromEnv(); err != nil { | ||
klog.Fatalf("Error loading configuration: %s", err.Error()) | ||
} | ||
cfg := worker.Config{} | ||
|
||
restCfg, err := clientcmd.BuildConfigFromFlags(cfg.MasterURL, cfg.KubeConfig) | ||
if err != nil { | ||
klog.Fatalf("Error building kubeconfig: %s", err.Error()) | ||
// Read up global configs | ||
if err := svc.LoadFromEnv(&cfg); err != nil { | ||
panic(fmt.Sprintf("could not load configuration: %s", err)) | ||
} | ||
|
||
kubeClient, err := kubernetes.NewForConfig(restCfg) | ||
if err != nil { | ||
klog.Fatalf("Error building kubernetes clientset: %s", err.Error()) | ||
// SVC supervisor Init | ||
options := []svc.Option{ | ||
svc.WithMetrics(), | ||
svc.WithHealthz(), | ||
svc.WithMetricsHandler(), | ||
svc.WithHTTPServer("9090"), | ||
svc.WithStackdriverLogger(zap.InfoLevel), | ||
} | ||
|
||
darkClient, err := clientset.NewForConfig(restCfg) | ||
if err != nil { | ||
klog.Fatalf("Error building dark clientset: %s", err.Error()) | ||
} | ||
|
||
darkInformerFactory := informers.NewSharedInformerFactory(darkClient, time.Second*30) | ||
|
||
grabanaClient := grabana.NewClient(makeHTTPClient(cfg), cfg.GrafanaHost, grabana.WithAPIToken(cfg.GrafanaToken)) | ||
dashboardCreator := dashboards.NewCreator(grabanaClient) | ||
|
||
controller := internal.NewController(kubeClient, darkClient, darkInformerFactory.Controller().V1().GrafanaDashboards(), dashboardCreator) | ||
|
||
// set up signals so we handle the first shutdown signal gracefully | ||
stopCh := signals.SetupSignalHandler() | ||
// SVC supervisor Init | ||
service, err := svc.New("dark", serviceVersion, options...) | ||
svc.MustInit(service, err) | ||
|
||
// notice that there is no need to run Start methods in a separate goroutine. (i.e. go kubeInformerFactory.Start(stopCh) | ||
// Start method is non-blocking and runs all registered informers in a dedicated goroutine. | ||
darkInformerFactory.Start(stopCh) | ||
// Workers definition | ||
service.AddWorker("dashboards-controller", worker.New(cfg)) | ||
|
||
if err = controller.Run(2, stopCh); err != nil { | ||
klog.Fatalf("Error running controller: %s", err.Error()) | ||
} | ||
} | ||
|
||
func makeHTTPClient(cfg config) *http.Client { | ||
return &http.Client{ | ||
Transport: &http.Transport{ | ||
TLSClientConfig: &tls.Config{InsecureSkipVerify: cfg.InsecureSkipVerify}, | ||
}, | ||
Timeout: 10 * time.Second, // Large, but better than no timeout. | ||
} | ||
// Service main loop | ||
service.Run() | ||
} |
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.