|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2016-2021 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 | + |
| 21 | +package main |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "fmt" |
| 26 | + "os" |
| 27 | + "time" |
| 28 | + |
| 29 | + v1 "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" |
| 30 | + "github.com/arangodb/kube-arangodb/pkg/util" |
| 31 | + "github.com/arangodb/kube-arangodb/pkg/util/constants" |
| 32 | + |
| 33 | + "github.com/spf13/cobra" |
| 34 | +) |
| 35 | + |
| 36 | +const ( |
| 37 | + ArgDeploymentWatchTimeout = "watch-timeout" |
| 38 | + WatchDefaultTimeout = time.Minute * 5 |
| 39 | + WatchCheckInterval = time.Second * 5 |
| 40 | +) |
| 41 | + |
| 42 | +var ( |
| 43 | + cmdLifecycleWait = &cobra.Command{ |
| 44 | + Use: "wait", |
| 45 | + Short: "Wait for specific ArangoDeployment", |
| 46 | + Long: "Wait for ArangoDeployment till it will reach UpToDate condition", |
| 47 | + Run: cmdLifecycleWaitCheck, |
| 48 | + } |
| 49 | +) |
| 50 | + |
| 51 | +func init() { |
| 52 | + var deploymentName string |
| 53 | + var watchTimeout time.Duration |
| 54 | + |
| 55 | + cmdLifecycleWait.Flags().StringVarP(&deploymentName, ArgDeploymentName, "d", "", |
| 56 | + "Name of ArangoDeployment to watch - necessary when more than one deployment exist within one namespace") |
| 57 | + cmdLifecycleWait.Flags().DurationVarP(&watchTimeout, ArgDeploymentWatchTimeout, "t", WatchDefaultTimeout, |
| 58 | + "Watch timeout") |
| 59 | +} |
| 60 | + |
| 61 | +func cmdLifecycleWaitCheck(cmd *cobra.Command, _ []string) { |
| 62 | + ctx := util.CreateSignalContext(context.Background()) |
| 63 | + |
| 64 | + deploymentName, err := cmd.Flags().GetString(ArgDeploymentName) |
| 65 | + if err != nil { |
| 66 | + cliLog.Fatal().Err(err).Msg(fmt.Sprintf("error parsing argument: %s", ArgDeploymentName)) |
| 67 | + } |
| 68 | + watchTimeout, err := cmd.Flags().GetDuration(ArgDeploymentWatchTimeout) |
| 69 | + if err != nil { |
| 70 | + cliLog.Fatal().Err(err).Msg(fmt.Sprintf("error parsing argument: %s", ArgDeploymentWatchTimeout)) |
| 71 | + } |
| 72 | + |
| 73 | + for { |
| 74 | + d, err := getDeployment(ctx, os.Getenv(constants.EnvOperatorPodNamespace), deploymentName) |
| 75 | + if err != nil { |
| 76 | + cliLog.Fatal().Err(err).Msg(fmt.Sprintf("error getting ArangoDeployment: %s", d.Name)) |
| 77 | + } |
| 78 | + |
| 79 | + isUpToDate, err := d.IsUpToDate() |
| 80 | + if err != nil { |
| 81 | + cliLog.Err(err).Msg(fmt.Sprintf("error checking Status for ArangoDeployment: %s", d.Name)) |
| 82 | + } |
| 83 | + |
| 84 | + if isUpToDate { |
| 85 | + cliLog.Info().Msg(fmt.Sprintf("ArangoDeployment: %s is %s", d.Name, v1.ConditionTypeUpToDate)) |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + select { |
| 90 | + case <-ctx.Done(): |
| 91 | + return |
| 92 | + case <-time.After(WatchCheckInterval): |
| 93 | + cliLog.Info().Msg(fmt.Sprintf("ArangoDeployment: %s is not ready yet. Waiting...", d.Name)) |
| 94 | + continue |
| 95 | + case <-time.After(watchTimeout): |
| 96 | + cliLog.Error().Msg(fmt.Sprintf("ArangoDeployment: %s is not %s yet - operation timed out!", d.Name, v1.ConditionTypeUpToDate)) |
| 97 | + return |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments