2020-06-26 06:53:24 +00:00
|
|
|
//
|
|
|
|
// DISCLAIMER
|
|
|
|
//
|
2024-02-08 14:25:48 +00:00
|
|
|
// Copyright 2016-2024 ArangoDB GmbH, Cologne, Germany
|
2020-06-26 06:53:24 +00:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
|
2022-01-04 18:46:53 +00:00
|
|
|
package cmd
|
2020-06-26 06:53:24 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2022-09-19 15:21:13 +00:00
|
|
|
"encoding/json"
|
2020-06-26 06:53:24 +00:00
|
|
|
"fmt"
|
2022-12-22 09:49:51 +00:00
|
|
|
"io"
|
2020-06-26 06:53:24 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
2022-09-19 15:21:13 +00:00
|
|
|
"strconv"
|
2020-06-26 06:53:24 +00:00
|
|
|
|
2022-07-11 11:49:47 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2022-09-19 15:21:13 +00:00
|
|
|
"github.com/arangodb/go-driver"
|
2020-06-26 06:53:24 +00:00
|
|
|
"github.com/arangodb/go-driver/jwt"
|
2022-07-11 11:49:47 +00:00
|
|
|
|
2022-09-19 15:21:13 +00:00
|
|
|
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
|
2023-12-12 15:39:35 +00:00
|
|
|
shared "github.com/arangodb/kube-arangodb/pkg/apis/shared"
|
2022-09-19 15:21:13 +00:00
|
|
|
"github.com/arangodb/kube-arangodb/pkg/deployment/client"
|
|
|
|
"github.com/arangodb/kube-arangodb/pkg/deployment/features"
|
2020-06-26 06:53:24 +00:00
|
|
|
"github.com/arangodb/kube-arangodb/pkg/deployment/pod"
|
2022-09-19 15:21:13 +00:00
|
|
|
"github.com/arangodb/kube-arangodb/pkg/deployment/resources"
|
2022-12-08 07:59:50 +00:00
|
|
|
"github.com/arangodb/kube-arangodb/pkg/util"
|
2020-06-26 06:53:24 +00:00
|
|
|
"github.com/arangodb/kube-arangodb/pkg/util/constants"
|
2024-02-08 14:25:48 +00:00
|
|
|
"github.com/arangodb/kube-arangodb/pkg/util/errors"
|
2020-06-26 06:53:24 +00:00
|
|
|
)
|
|
|
|
|
2022-12-08 07:59:50 +00:00
|
|
|
const (
|
|
|
|
ProbePort util.EnvironmentVariable = "ARANGODB_SERVER_PORT"
|
|
|
|
)
|
|
|
|
|
2020-06-26 06:53:24 +00:00
|
|
|
var (
|
|
|
|
cmdLifecycleProbe = &cobra.Command{
|
|
|
|
Use: "probe",
|
2022-09-19 15:21:13 +00:00
|
|
|
Run: cmdLifecycleProbeRun,
|
|
|
|
}
|
|
|
|
cmdLifecycleProbeLiveness = &cobra.Command{
|
|
|
|
Use: "liveness",
|
|
|
|
Run: cmdLifecycleProbeRun,
|
|
|
|
}
|
|
|
|
cmdLifecycleProbeReadiness = &cobra.Command{
|
|
|
|
Use: "readiness",
|
|
|
|
Run: cmdLifecycleProbeRun,
|
|
|
|
}
|
|
|
|
cmdLifecycleProbeStartUp = &cobra.Command{
|
|
|
|
Use: "startup",
|
|
|
|
Run: cmdLifecycleProbeRun,
|
2020-06-26 06:53:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
probeInput struct {
|
2022-09-19 15:21:13 +00:00
|
|
|
Endpoint string
|
|
|
|
JWTPath string
|
|
|
|
ArangoDBVersion string
|
|
|
|
ServerGroup string
|
|
|
|
DeploymentMode string
|
|
|
|
SSL bool
|
|
|
|
Auth bool
|
|
|
|
Enterprise bool
|
2020-06-26 06:53:24 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
f := cmdLifecycleProbe.PersistentFlags()
|
|
|
|
|
2022-09-19 15:21:13 +00:00
|
|
|
cmdLifecycleProbe.AddCommand(cmdLifecycleProbeLiveness)
|
|
|
|
cmdLifecycleProbe.AddCommand(cmdLifecycleProbeReadiness)
|
|
|
|
cmdLifecycleProbe.AddCommand(cmdLifecycleProbeStartUp)
|
|
|
|
|
2020-06-26 06:53:24 +00:00
|
|
|
f.BoolVarP(&probeInput.SSL, "ssl", "", false, "Determines if SSL is enabled")
|
|
|
|
f.BoolVarP(&probeInput.Auth, "auth", "", false, "Determines if authentication is enabled")
|
2022-09-19 15:21:13 +00:00
|
|
|
f.StringVarP(&probeInput.Endpoint, "endpoint", "", client.ServerApiVersionEndpoint, "Endpoint (path) to call for lifecycle probe")
|
|
|
|
f.MarkDeprecated("endpoint", "Endpoint is chosen automatically by the lifecycle process")
|
2022-03-28 13:53:58 +00:00
|
|
|
f.StringVarP(&probeInput.JWTPath, "jwt", "", shared.ClusterJWTSecretVolumeMountDir, "Path to the JWT tokens")
|
2022-09-19 15:21:13 +00:00
|
|
|
f.StringVar(&probeInput.ArangoDBVersion, "arangodb-version", os.Getenv(resources.ArangoDBOverrideVersionEnv),
|
|
|
|
"Version of the ArangoDB")
|
|
|
|
f.StringVar(&probeInput.ServerGroup, "serverGroup", os.Getenv(resources.ArangoDBOverrideServerGroupEnv),
|
|
|
|
"Name of the group where a server belongs to")
|
|
|
|
f.StringVar(&probeInput.DeploymentMode, "deploymentMode", os.Getenv(resources.ArangoDBOverrideDeploymentModeEnv),
|
|
|
|
"A deployment mode (Cluster, Single, ActiveFailover)")
|
|
|
|
enterprise, _ := strconv.ParseBool(os.Getenv(resources.ArangoDBOverrideEnterpriseEnv))
|
|
|
|
f.BoolVar(&probeInput.Enterprise, "enterprise", enterprise, "Determines if ArangoDB is enterprise")
|
2020-06-26 06:53:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func probeClient() *http.Client {
|
|
|
|
tr := &http.Transport{}
|
|
|
|
|
|
|
|
if probeInput.SSL {
|
|
|
|
tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: tr,
|
|
|
|
}
|
|
|
|
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
func probeEndpoint(endpoint string) string {
|
|
|
|
proto := "http"
|
|
|
|
if probeInput.SSL {
|
|
|
|
proto = "https"
|
|
|
|
}
|
|
|
|
|
2022-12-08 07:59:50 +00:00
|
|
|
port := ProbePort.GetOrDefault(fmt.Sprintf("%d", shared.ArangoPort))
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s://%s:%s%s", proto, "127.0.0.1", port, endpoint)
|
2020-06-26 06:53:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func readJWTFile(file string) ([]byte, error) {
|
|
|
|
p := path.Join(probeInput.JWTPath, file)
|
|
|
|
log.Info().Str("path", p).Msgf("Try to use file")
|
|
|
|
|
|
|
|
f, err := os.Open(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
2022-12-22 09:49:51 +00:00
|
|
|
data, err := io.ReadAll(f)
|
2020-06-26 06:53:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getJWTToken() ([]byte, error) {
|
|
|
|
// Try read default one
|
|
|
|
if token, err := readJWTFile(constants.SecretKeyToken); err == nil {
|
|
|
|
log.Info().Str("token", constants.SecretKeyToken).Msgf("Using JWT Token")
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try read active one
|
|
|
|
if token, err := readJWTFile(pod.ActiveJWTKey); err == nil {
|
|
|
|
log.Info().Str("token", pod.ActiveJWTKey).Msgf("Using JWT Token")
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
2022-12-22 09:49:51 +00:00
|
|
|
if files, err := os.ReadDir(probeInput.JWTPath); err == nil {
|
2020-06-26 06:53:24 +00:00
|
|
|
for _, file := range files {
|
|
|
|
if token, err := readJWTFile(file.Name()); err == nil {
|
|
|
|
log.Info().Str("token", file.Name()).Msgf("Using JWT Token")
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.Errorf("Unable to find any token")
|
|
|
|
}
|
|
|
|
|
|
|
|
func addAuthHeader(req *http.Request) error {
|
|
|
|
if !probeInput.Auth {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
token, err := getJWTToken()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
header, err := jwt.CreateArangodJwtAuthorizationHeader(string(token), "probe")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Add("Authorization", header)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-19 15:21:13 +00:00
|
|
|
func doRequest(endpoint string) (*http.Response, error) {
|
2020-06-26 06:53:24 +00:00
|
|
|
client := probeClient()
|
|
|
|
|
2022-09-19 15:21:13 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, probeEndpoint(endpoint), nil)
|
2020-06-26 06:53:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := addAuthHeader(req); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.Do(req)
|
|
|
|
}
|
|
|
|
|
2022-09-19 15:21:13 +00:00
|
|
|
func cmdLifecycleProbeRun(cmd *cobra.Command, _ []string) {
|
|
|
|
if err := cmdLifecycleProbeRunE(cmd); err != nil {
|
2020-06-26 06:53:24 +00:00
|
|
|
log.Error().Err(err).Msgf("Fatal")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-19 15:21:13 +00:00
|
|
|
func cmdLifecycleProbeRunE(cmd *cobra.Command) error {
|
|
|
|
endpoint := getEndpoint(api.ProbeType(cmd.Use))
|
|
|
|
resp, err := doRequest(endpoint)
|
2020-06-26 06:53:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-19 15:21:13 +00:00
|
|
|
if resp.Body != nil {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
}
|
2020-06-26 06:53:24 +00:00
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
if resp.Body != nil {
|
2022-12-22 09:49:51 +00:00
|
|
|
if data, err := io.ReadAll(resp.Body); err == nil {
|
2020-06-26 06:53:24 +00:00
|
|
|
return errors.Errorf("Unexpected code: %d - %s", resp.StatusCode, string(data))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.Errorf("Unexpected code: %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
2022-09-19 15:21:13 +00:00
|
|
|
if endpoint == client.ServerStatusEndpoint {
|
|
|
|
// When server status endpoint is used then HTTP status code 200 is not enough.
|
|
|
|
// The progress should be also checked.
|
|
|
|
if resp.Body == nil {
|
|
|
|
return errors.Errorf("Expected body from the \"%s\" endpoint", endpoint)
|
|
|
|
}
|
2022-12-22 09:49:51 +00:00
|
|
|
data, err := io.ReadAll(resp.Body)
|
2022-09-19 15:21:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Errorf("Failed to read body from the \"%s\" endpoint", endpoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
status := client.ServerStatus{}
|
|
|
|
if err = json.Unmarshal(data, &status); err != nil {
|
|
|
|
return errors.Errorf("Failed to unmarshal %s into server status", string(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
if progress, ok := status.GetProgress(); !ok {
|
|
|
|
return errors.Errorf("server not ready: %s", progress)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-26 06:53:24 +00:00
|
|
|
log.Info().Msgf("Check passed")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-09-19 15:21:13 +00:00
|
|
|
|
|
|
|
// getEndpoint returns endpoint to the ArangoDB instance where readiness should be checked.
|
|
|
|
func getEndpoint(probeType api.ProbeType) string {
|
|
|
|
if probeType == api.ProbeTypeReadiness {
|
|
|
|
if probeInput.DeploymentMode == string(api.DeploymentModeActiveFailover) {
|
|
|
|
v := driver.Version(probeInput.ArangoDBVersion)
|
|
|
|
if features.FailoverLeadership().Supported(v, probeInput.Enterprise) {
|
|
|
|
return client.ServerApiVersionEndpoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.ServerAvailabilityEndpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
if probeInput.ServerGroup == api.ServerGroupDBServersString {
|
|
|
|
v := driver.Version(probeInput.ArangoDBVersion)
|
|
|
|
if features.Version310().Supported(v, probeInput.Enterprise) {
|
|
|
|
return client.ServerStatusEndpoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.ServerApiVersionEndpoint
|
|
|
|
}
|