|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2018 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | +// Author Ewout Prangsma |
| 21 | +// |
| 22 | + |
| 23 | +package main |
| 24 | + |
| 25 | +import ( |
| 26 | + "io" |
| 27 | + "os" |
| 28 | + "path/filepath" |
| 29 | + "time" |
| 30 | + |
| 31 | + "github.com/spf13/cobra" |
| 32 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 33 | + |
| 34 | + "github.com/arangodb/kube-arangodb/pkg/util/constants" |
| 35 | + "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" |
| 36 | +) |
| 37 | + |
| 38 | +var ( |
| 39 | + cmdLifecycle = &cobra.Command{ |
| 40 | + Use: "lifecycle", |
| 41 | + Run: cmdUsage, |
| 42 | + Hidden: true, |
| 43 | + } |
| 44 | + |
| 45 | + cmdLifecyclePreStop = &cobra.Command{ |
| 46 | + Use: "preStop", |
| 47 | + Run: cmdLifecyclePreStopRun, |
| 48 | + Hidden: true, |
| 49 | + } |
| 50 | + cmdLifecycleCopy = &cobra.Command{ |
| 51 | + Use: "copy", |
| 52 | + Run: cmdLifecycleCopyRun, |
| 53 | + Hidden: true, |
| 54 | + } |
| 55 | + |
| 56 | + lifecycleCopyOptions struct { |
| 57 | + TargetDir string |
| 58 | + } |
| 59 | +) |
| 60 | + |
| 61 | +func init() { |
| 62 | + cmdMain.AddCommand(cmdLifecycle) |
| 63 | + cmdLifecycle.AddCommand(cmdLifecyclePreStop) |
| 64 | + cmdLifecycle.AddCommand(cmdLifecycleCopy) |
| 65 | + |
| 66 | + cmdLifecycleCopy.Flags().StringVar(&lifecycleCopyOptions.TargetDir, "target", "", "Target directory to copy the executable to") |
| 67 | +} |
| 68 | + |
| 69 | +// Wait until all finalizers of the current pod have been removed. |
| 70 | +func cmdLifecyclePreStopRun(cmd *cobra.Command, args []string) { |
| 71 | + cliLog.Info().Msgf("Starting arangodb-operator, lifecycle preStop, version %s build %s", projectVersion, projectBuild) |
| 72 | + |
| 73 | + // Get environment |
| 74 | + namespace := os.Getenv(constants.EnvOperatorPodNamespace) |
| 75 | + if len(namespace) == 0 { |
| 76 | + cliLog.Fatal().Msgf("%s environment variable missing", constants.EnvOperatorPodNamespace) |
| 77 | + } |
| 78 | + name := os.Getenv(constants.EnvOperatorPodName) |
| 79 | + if len(name) == 0 { |
| 80 | + cliLog.Fatal().Msgf("%s environment variable missing", constants.EnvOperatorPodName) |
| 81 | + } |
| 82 | + |
| 83 | + // Create kubernetes client |
| 84 | + kubecli, err := k8sutil.NewKubeClient() |
| 85 | + if err != nil { |
| 86 | + cliLog.Fatal().Err(err).Msg("Failed to create Kubernetes client") |
| 87 | + } |
| 88 | + |
| 89 | + pods := kubecli.CoreV1().Pods(namespace) |
| 90 | + recentErrors := 0 |
| 91 | + for { |
| 92 | + p, err := pods.Get(name, metav1.GetOptions{}) |
| 93 | + if k8sutil.IsNotFound(err) { |
| 94 | + cliLog.Warn().Msg("Pod not found") |
| 95 | + return |
| 96 | + } else if err != nil { |
| 97 | + recentErrors++ |
| 98 | + cliLog.Error().Err(err).Msg("Failed to get pod") |
| 99 | + if recentErrors > 20 { |
| 100 | + cliLog.Fatal().Err(err).Msg("Too many recent errors") |
| 101 | + return |
| 102 | + } |
| 103 | + } else { |
| 104 | + // We got our pod |
| 105 | + finalizerCount := len(p.GetFinalizers()) |
| 106 | + if finalizerCount == 0 { |
| 107 | + // No more finalizers, we're done |
| 108 | + cliLog.Info().Msg("All finalizers gone, we can stop now") |
| 109 | + return |
| 110 | + } |
| 111 | + cliLog.Info().Msgf("Waiting for %d more finalizers to be removed", finalizerCount) |
| 112 | + } |
| 113 | + // Wait a bit |
| 114 | + time.Sleep(time.Second) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +// Copy the executable to a given place. |
| 119 | +func cmdLifecycleCopyRun(cmd *cobra.Command, args []string) { |
| 120 | + cliLog.Info().Msgf("Starting arangodb-operator, lifecycle copy, version %s build %s", projectVersion, projectBuild) |
| 121 | + |
| 122 | + exePath, err := os.Executable() |
| 123 | + if err != nil { |
| 124 | + cliLog.Fatal().Err(err).Msg("Failed to get executable path") |
| 125 | + } |
| 126 | + |
| 127 | + // Open source |
| 128 | + rd, err := os.Open(exePath) |
| 129 | + if err != nil { |
| 130 | + cliLog.Fatal().Err(err).Msg("Failed to open executable file") |
| 131 | + } |
| 132 | + defer rd.Close() |
| 133 | + |
| 134 | + // Open target |
| 135 | + targetPath := filepath.Join(lifecycleCopyOptions.TargetDir, filepath.Base(exePath)) |
| 136 | + wr, err := os.Create(targetPath) |
| 137 | + if err != nil { |
| 138 | + cliLog.Fatal().Err(err).Msg("Failed to create target file") |
| 139 | + } |
| 140 | + defer wr.Close() |
| 141 | + |
| 142 | + if _, err := io.Copy(wr, rd); err != nil { |
| 143 | + cliLog.Fatal().Err(err).Msg("Failed to copy") |
| 144 | + } |
| 145 | + |
| 146 | + // Set file mode |
| 147 | + if err := os.Chmod(targetPath, 0755); err != nil { |
| 148 | + cliLog.Fatal().Err(err).Msg("Failed to chmod") |
| 149 | + } |
| 150 | +} |
0 commit comments