2018-05-11 12:58:49 +00:00
|
|
|
//
|
|
|
|
// DISCLAIMER
|
|
|
|
//
|
2022-01-10 11:35:49 +00:00
|
|
|
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
|
2018-05-11 12:58:49 +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
|
2018-05-11 12:58:49 +00:00
|
|
|
|
|
|
|
import (
|
2021-03-23 15:47:28 +00:00
|
|
|
"context"
|
2018-05-11 12:58:49 +00:00
|
|
|
"io"
|
2021-10-29 07:41:41 +00:00
|
|
|
"net"
|
2018-05-11 12:58:49 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-10-29 07:41:41 +00:00
|
|
|
"strconv"
|
2018-05-11 12:58:49 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2022-06-30 18:39:07 +00:00
|
|
|
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2018-05-11 12:58:49 +00:00
|
|
|
|
2022-03-28 13:53:58 +00:00
|
|
|
"github.com/arangodb/kube-arangodb/pkg/apis/shared"
|
2022-07-11 11:49:47 +00:00
|
|
|
"github.com/arangodb/kube-arangodb/pkg/handlers/utils"
|
2018-05-11 12:58:49 +00:00
|
|
|
"github.com/arangodb/kube-arangodb/pkg/util/constants"
|
|
|
|
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
|
2022-02-28 18:53:01 +00:00
|
|
|
"github.com/arangodb/kube-arangodb/pkg/util/kclient"
|
2022-07-11 11:49:47 +00:00
|
|
|
"github.com/arangodb/kube-arangodb/pkg/util/retry"
|
|
|
|
"github.com/arangodb/kube-arangodb/pkg/version"
|
2018-05-11 12:58:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
cmdLifecycle = &cobra.Command{
|
|
|
|
Use: "lifecycle",
|
2022-01-04 18:46:53 +00:00
|
|
|
Run: executeUsage,
|
2018-05-11 12:58:49 +00:00
|
|
|
Hidden: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdLifecyclePreStop = &cobra.Command{
|
|
|
|
Use: "preStop",
|
2021-10-24 23:09:40 +00:00
|
|
|
Hidden: true,
|
|
|
|
}
|
|
|
|
cmdLifecyclePreStopFinalizers = &cobra.Command{
|
|
|
|
Use: "finalizers",
|
|
|
|
Run: cmdLifecyclePreStopRunFinalizer,
|
2018-05-11 12:58:49 +00:00
|
|
|
Hidden: true,
|
|
|
|
}
|
2021-10-29 07:41:41 +00:00
|
|
|
cmdLifecyclePreStopPort = &cobra.Command{
|
|
|
|
Use: "port",
|
|
|
|
Hidden: true,
|
|
|
|
}
|
2018-05-11 12:58:49 +00:00
|
|
|
cmdLifecycleCopy = &cobra.Command{
|
|
|
|
Use: "copy",
|
|
|
|
Run: cmdLifecycleCopyRun,
|
|
|
|
Hidden: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
lifecycleCopyOptions struct {
|
|
|
|
TargetDir string
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cmdMain.AddCommand(cmdLifecycle)
|
2021-10-24 23:09:40 +00:00
|
|
|
|
2021-10-29 07:41:41 +00:00
|
|
|
var preStopPort cmdLifecyclePreStopRunPort
|
|
|
|
|
|
|
|
cmdLifecyclePreStopPort.RunE = preStopPort.run
|
|
|
|
|
|
|
|
f := cmdLifecyclePreStopPort.Flags()
|
|
|
|
|
|
|
|
f.DurationVar(&preStopPort.timeout, "timeout", 6*60*time.Minute, "PreStopTimeout")
|
|
|
|
|
|
|
|
cmdLifecyclePreStop.AddCommand(cmdLifecyclePreStopFinalizers, cmdLifecyclePreStopPort)
|
2021-10-24 23:09:40 +00:00
|
|
|
|
2018-05-11 12:58:49 +00:00
|
|
|
cmdLifecycle.AddCommand(cmdLifecyclePreStop)
|
|
|
|
cmdLifecycle.AddCommand(cmdLifecycleCopy)
|
2020-06-26 06:53:24 +00:00
|
|
|
cmdLifecycle.AddCommand(cmdLifecycleProbe)
|
2021-11-10 13:48:41 +00:00
|
|
|
cmdLifecycle.AddCommand(cmdLifecycleWait)
|
2022-08-08 10:03:06 +00:00
|
|
|
cmdLifecycle.AddCommand(cmdLifecycleStartup)
|
2018-05-11 12:58:49 +00:00
|
|
|
|
|
|
|
cmdLifecycleCopy.Flags().StringVar(&lifecycleCopyOptions.TargetDir, "target", "", "Target directory to copy the executable to")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until all finalizers of the current pod have been removed.
|
2021-10-24 23:09:40 +00:00
|
|
|
func cmdLifecyclePreStopRunFinalizer(cmd *cobra.Command, args []string) {
|
2022-06-14 07:26:07 +00:00
|
|
|
logger.Info("Starting arangodb-operator (%s), lifecycle preStop, version %s build %s", version.GetVersionV1().Edition.Title(), version.GetVersionV1().Version, version.GetVersionV1().Build)
|
2018-05-11 12:58:49 +00:00
|
|
|
|
|
|
|
// Get environment
|
|
|
|
namespace := os.Getenv(constants.EnvOperatorPodNamespace)
|
|
|
|
if len(namespace) == 0 {
|
2022-06-14 07:26:07 +00:00
|
|
|
logger.Fatal("%s environment variable missing", constants.EnvOperatorPodNamespace)
|
2018-05-11 12:58:49 +00:00
|
|
|
}
|
|
|
|
name := os.Getenv(constants.EnvOperatorPodName)
|
|
|
|
if len(name) == 0 {
|
2022-06-14 07:26:07 +00:00
|
|
|
logger.Fatal("%s environment variable missing", constants.EnvOperatorPodName)
|
2018-05-11 12:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create kubernetes client
|
2022-02-28 18:53:01 +00:00
|
|
|
client, ok := kclient.GetDefaultFactory().Client()
|
|
|
|
if !ok {
|
2022-06-14 07:26:07 +00:00
|
|
|
logger.Fatal("Client not initialised")
|
2018-05-11 12:58:49 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 18:53:01 +00:00
|
|
|
pods := client.Kubernetes().CoreV1().Pods(namespace)
|
2018-05-11 12:58:49 +00:00
|
|
|
recentErrors := 0
|
|
|
|
for {
|
2022-06-30 18:39:07 +00:00
|
|
|
p, err := pods.Get(context.Background(), name, meta.GetOptions{})
|
2018-05-11 12:58:49 +00:00
|
|
|
if k8sutil.IsNotFound(err) {
|
2022-06-14 07:26:07 +00:00
|
|
|
logger.Warn("Pod not found")
|
2018-05-11 12:58:49 +00:00
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
recentErrors++
|
2022-06-14 07:26:07 +00:00
|
|
|
logger.Err(err).Error("Failed to get pod")
|
2018-05-11 12:58:49 +00:00
|
|
|
if recentErrors > 20 {
|
2022-06-14 07:26:07 +00:00
|
|
|
logger.Err(err).Fatal("Too many recent errors")
|
2018-05-11 12:58:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We got our pod
|
|
|
|
finalizerCount := len(p.GetFinalizers())
|
|
|
|
if finalizerCount == 0 {
|
|
|
|
// No more finalizers, we're done
|
2022-06-14 07:26:07 +00:00
|
|
|
logger.Info("All finalizers gone, we can stop now")
|
2018-05-11 12:58:49 +00:00
|
|
|
return
|
|
|
|
}
|
2022-06-14 07:26:07 +00:00
|
|
|
logger.Info("Waiting for %d more finalizers to be removed", finalizerCount)
|
2018-05-11 12:58:49 +00:00
|
|
|
}
|
|
|
|
// Wait a bit
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the executable to a given place.
|
|
|
|
func cmdLifecycleCopyRun(cmd *cobra.Command, args []string) {
|
2022-06-14 07:26:07 +00:00
|
|
|
logger.Info("Starting arangodb-operator (%s), lifecycle copy, version %s build %s", version.GetVersionV1().Edition.Title(), version.GetVersionV1().Version, version.GetVersionV1().Build)
|
2018-05-11 12:58:49 +00:00
|
|
|
|
|
|
|
exePath, err := os.Executable()
|
|
|
|
if err != nil {
|
2022-06-14 07:26:07 +00:00
|
|
|
logger.Err(err).Fatal("Failed to get executable path")
|
2018-05-11 12:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open source
|
|
|
|
rd, err := os.Open(exePath)
|
|
|
|
if err != nil {
|
2022-06-14 07:26:07 +00:00
|
|
|
logger.Err(err).Fatal("Failed to open executable file")
|
2018-05-11 12:58:49 +00:00
|
|
|
}
|
|
|
|
defer rd.Close()
|
|
|
|
|
|
|
|
// Open target
|
|
|
|
targetPath := filepath.Join(lifecycleCopyOptions.TargetDir, filepath.Base(exePath))
|
|
|
|
wr, err := os.Create(targetPath)
|
|
|
|
if err != nil {
|
2022-06-14 07:26:07 +00:00
|
|
|
logger.Err(err).Fatal("Failed to create target file")
|
2018-05-11 12:58:49 +00:00
|
|
|
}
|
|
|
|
defer wr.Close()
|
|
|
|
|
|
|
|
if _, err := io.Copy(wr, rd); err != nil {
|
2022-06-14 07:26:07 +00:00
|
|
|
logger.Err(err).Fatal("Failed to copy")
|
2018-05-11 12:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set file mode
|
|
|
|
if err := os.Chmod(targetPath, 0755); err != nil {
|
2022-06-14 07:26:07 +00:00
|
|
|
logger.Err(err).Fatal("Failed to chmod")
|
2018-05-11 12:58:49 +00:00
|
|
|
}
|
2018-08-30 06:23:13 +00:00
|
|
|
|
2022-06-14 07:26:07 +00:00
|
|
|
logger.Info("Executable copied to %s", targetPath)
|
2018-05-11 12:58:49 +00:00
|
|
|
}
|
2021-10-29 07:41:41 +00:00
|
|
|
|
|
|
|
type cmdLifecyclePreStopRunPort struct {
|
|
|
|
timeout time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until port 8529 is closed.
|
|
|
|
func (c *cmdLifecyclePreStopRunPort) run(cmd *cobra.Command, args []string) error {
|
2022-03-28 13:53:58 +00:00
|
|
|
address := net.JoinHostPort("127.0.0.1", strconv.Itoa(shared.ArangoPort))
|
2021-10-29 07:41:41 +00:00
|
|
|
|
|
|
|
// Get environment
|
|
|
|
namespace := os.Getenv(constants.EnvOperatorPodNamespace)
|
|
|
|
if len(namespace) == 0 {
|
2022-06-14 07:26:07 +00:00
|
|
|
logger.Fatal("%s environment variable missing", constants.EnvOperatorPodNamespace)
|
2021-10-29 07:41:41 +00:00
|
|
|
}
|
|
|
|
name := os.Getenv(constants.EnvOperatorPodName)
|
|
|
|
if len(name) == 0 {
|
2022-06-14 07:26:07 +00:00
|
|
|
logger.Fatal("%s environment variable missing", constants.EnvOperatorPodName)
|
2021-10-29 07:41:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create kubernetes client
|
2022-02-28 18:53:01 +00:00
|
|
|
client, ok := kclient.GetDefaultFactory().Client()
|
|
|
|
if !ok {
|
2022-06-14 07:26:07 +00:00
|
|
|
logger.Fatal("Client not initialised")
|
2021-10-29 07:41:41 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 18:53:01 +00:00
|
|
|
pods := client.Kubernetes().CoreV1().Pods(namespace)
|
2021-10-29 07:41:41 +00:00
|
|
|
|
|
|
|
recentErrors := 0
|
|
|
|
|
|
|
|
return retry.NewTimeout(func() error {
|
|
|
|
conn, err := net.DialTimeout("tcp", address, 500*time.Millisecond)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return retry.Interrput()
|
|
|
|
}
|
|
|
|
|
|
|
|
conn.Close()
|
|
|
|
|
2022-06-30 18:39:07 +00:00
|
|
|
p, err := pods.Get(context.Background(), name, meta.GetOptions{})
|
2021-10-29 07:41:41 +00:00
|
|
|
if k8sutil.IsNotFound(err) {
|
2022-06-14 07:26:07 +00:00
|
|
|
logger.Warn("Pod not found")
|
2021-10-29 07:41:41 +00:00
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
recentErrors++
|
2022-06-14 07:26:07 +00:00
|
|
|
logger.Err(err).Error("Failed to get pod")
|
2021-10-29 07:41:41 +00:00
|
|
|
if recentErrors > 20 {
|
2022-06-14 07:26:07 +00:00
|
|
|
logger.Err(err).Fatal("Too many recent errors")
|
2021-10-29 07:41:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We got our pod
|
|
|
|
finalizers := utils.StringList(p.GetFinalizers())
|
|
|
|
if !finalizers.Has(constants.FinalizerPodGracefulShutdown) {
|
|
|
|
return retry.Interrput()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}).Timeout(125*time.Millisecond, c.timeout)
|
|
|
|
}
|