1
0
Fork 0
mirror of https://github.com/arangodb/kube-arangodb.git synced 2024-12-14 11:57:37 +00:00
kube-arangodb/pkg/server/server.go

191 lines
5.8 KiB
Go
Raw Normal View History

2018-04-05 11:46:04 +00:00
//
// DISCLAIMER
//
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
//
// 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.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Ewout Prangsma
//
package server
import (
"crypto/tls"
"fmt"
"net/http"
"strings"
2018-04-05 11:46:04 +00:00
"time"
certificates "github.com/arangodb-helper/go-certificates"
2018-07-02 07:54:19 +00:00
"github.com/gin-gonic/gin"
assets "github.com/jessevdk/go-assets"
2018-07-02 07:54:19 +00:00
"github.com/prometheus/client_golang/prometheus"
2018-07-02 10:40:32 +00:00
"github.com/rs/zerolog"
2018-04-05 11:46:04 +00:00
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
2018-07-02 07:54:19 +00:00
"github.com/arangodb/kube-arangodb/dashboard"
2018-07-02 07:54:19 +00:00
"github.com/arangodb/kube-arangodb/pkg/util/probe"
2018-04-05 11:46:04 +00:00
)
// Config settings for the Server
type Config struct {
2018-07-02 16:24:42 +00:00
Namespace string
2018-04-05 11:46:04 +00:00
Address string // Address to listen on
TLSSecretName string // Name of secret containing TLS certificate
TLSSecretNamespace string // Namespace of secret containing TLS certificate
PodName string // Name of the Pod we're running in
PodIP string // IP address of the Pod we're running in
}
2018-07-02 07:54:19 +00:00
// Dependencies of the Server
type Dependencies struct {
2018-07-02 10:40:32 +00:00
Log zerolog.Logger
2018-07-02 07:54:19 +00:00
LivenessProbe *probe.LivenessProbe
DeploymentProbe *probe.ReadyProbe
DeploymentReplicationProbe *probe.ReadyProbe
StorageProbe *probe.ReadyProbe
2018-07-02 10:40:32 +00:00
Operators Operators
}
// Operators is the API provided to the server for accessing the various operators.
type Operators interface {
// Return the deployment operator (if any)
DeploymentOperator() DeploymentOperator
2018-07-02 07:54:19 +00:00
}
2018-04-05 11:46:04 +00:00
// Server is the HTTPS server for the operator.
type Server struct {
2018-07-02 16:24:42 +00:00
cfg Config
2018-07-02 07:54:19 +00:00
deps Dependencies
2018-04-05 11:46:04 +00:00
httpServer *http.Server
}
// NewServer creates a new server, fetching/preparing a TLS certificate.
2018-07-02 07:54:19 +00:00
func NewServer(cli corev1.CoreV1Interface, cfg Config, deps Dependencies) (*Server, error) {
2018-04-05 11:46:04 +00:00
httpServer := &http.Server{
Addr: cfg.Address,
ReadTimeout: time.Second * 30,
ReadHeaderTimeout: time.Second * 15,
WriteTimeout: time.Second * 30,
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
}
var cert, key string
if cfg.TLSSecretName != "" && cfg.TLSSecretNamespace != "" {
// Load TLS certificate from secret
s, err := cli.Secrets(cfg.TLSSecretNamespace).Get(cfg.TLSSecretName, metav1.GetOptions{})
if err != nil {
return nil, maskAny(err)
}
certBytes, found := s.Data[v1.TLSCertKey]
if !found {
return nil, maskAny(fmt.Errorf("No %s found in secret %s", v1.TLSCertKey, cfg.TLSSecretName))
}
keyBytes, found := s.Data[v1.TLSPrivateKeyKey]
if !found {
return nil, maskAny(fmt.Errorf("No %s found in secret %s", v1.TLSPrivateKeyKey, cfg.TLSSecretName))
}
cert = string(certBytes)
key = string(keyBytes)
} else {
// Secret not specified, create our own TLS certificate
options := certificates.CreateCertificateOptions{
CommonName: cfg.PodName,
Hosts: []string{cfg.PodName, cfg.PodIP},
ValidFrom: time.Now(),
ValidFor: time.Hour * 24 * 365 * 10,
IsCA: false,
ECDSACurve: "P256",
}
var err error
cert, key, err = certificates.CreateCertificate(options, nil)
if err != nil {
return nil, maskAny(err)
}
}
tlsConfig, err := createTLSConfig(cert, key)
if err != nil {
return nil, maskAny(err)
}
tlsConfig.BuildNameToCertificate()
httpServer.TLSConfig = tlsConfig
2018-07-02 10:40:32 +00:00
// Builder server
s := &Server{
2018-07-02 16:24:42 +00:00
cfg: cfg,
2018-07-02 10:40:32 +00:00
deps: deps,
httpServer: httpServer,
}
2018-07-02 07:54:19 +00:00
// Build router
r := gin.New()
r.Use(gin.Recovery())
r.GET("/health", gin.WrapF(deps.LivenessProbe.LivenessHandler))
r.GET("/ready/deployment", gin.WrapF(deps.DeploymentProbe.ReadyHandler))
r.GET("/ready/deployment-replication", gin.WrapF(deps.DeploymentReplicationProbe.ReadyHandler))
r.GET("/ready/storage", gin.WrapF(deps.StorageProbe.ReadyHandler))
r.GET("/metrics", gin.WrapH(prometheus.Handler()))
2018-07-02 10:40:32 +00:00
api := r.Group("/api")
{
api.GET("/operators", s.handleGetOperators)
// Deployment operator
api.GET("/deployment", s.handleGetDeployments)
2018-07-05 15:16:36 +00:00
api.GET("/deployment/:name", s.handleGetDeploymentDetails)
2018-07-02 10:40:32 +00:00
}
// Dashboard
2018-07-02 13:41:44 +00:00
r.GET("/", createAssetFileHandler(dashboard.Assets.Files["index.html"]))
for path, file := range dashboard.Assets.Files {
localPath := "/" + strings.TrimPrefix(path, "/")
2018-07-02 13:41:44 +00:00
r.GET(localPath, createAssetFileHandler(file))
}
2018-07-02 07:54:19 +00:00
httpServer.Handler = r
2018-07-02 10:40:32 +00:00
return s, nil
2018-04-05 11:46:04 +00:00
}
2018-07-02 13:41:44 +00:00
// createAssetFileHandler creates a gin handler to serve the content
// of the given asset file.
func createAssetFileHandler(file *assets.File) func(c *gin.Context) {
return func(c *gin.Context) {
http.ServeContent(c.Writer, c.Request, file.Name(), file.ModTime(), file)
}
}
2018-04-05 11:46:04 +00:00
// Run the server until the program stops.
func (s *Server) Run() error {
2018-07-02 10:40:32 +00:00
s.deps.Log.Info().Msgf("Serving on %s", s.httpServer.Addr)
2018-04-05 11:46:04 +00:00
if err := s.httpServer.ListenAndServeTLS("", ""); err != nil && err != http.ErrServerClosed {
return maskAny(err)
}
return nil
}
// createTLSConfig creates a TLS config based on given config
func createTLSConfig(cert, key string) (*tls.Config, error) {
var result *tls.Config
c, err := tls.X509KeyPair([]byte(cert), []byte(key))
if err != nil {
return nil, maskAny(err)
}
result = &tls.Config{
Certificates: []tls.Certificate{c},
}
return result, nil
}