|
| 1 | +// Copyright (c) 2023 Gitpod GmbH. All rights reserved. |
| 2 | +// Licensed under the GNU Affero General Public License (AGPL). |
| 3 | +// See License.AGPL.txt in the project root for license information. |
| 4 | + |
| 5 | +package cmd |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/sirupsen/logrus" |
| 13 | + "github.com/spf13/cobra" |
| 14 | + "github.com/spf13/viper" |
| 15 | + |
| 16 | + "github.com/gitpod-io/gitpod/common-go/log" |
| 17 | + corev1 "k8s.io/api/core/v1" |
| 18 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 19 | + "k8s.io/client-go/kubernetes" |
| 20 | + "k8s.io/client-go/rest" |
| 21 | +) |
| 22 | + |
| 23 | +var componentCmdOpt struct { |
| 24 | + image string |
| 25 | + namespace string |
| 26 | + component string |
| 27 | + labels string |
| 28 | +} |
| 29 | + |
| 30 | +var componentCmd = &cobra.Command{ |
| 31 | + Use: "component", |
| 32 | + Short: "waits for component to become latest build of current installer build", |
| 33 | + PreRun: func(cmd *cobra.Command, args []string) { |
| 34 | + err := viper.BindPFlags(cmd.Flags()) |
| 35 | + if err != nil { |
| 36 | + log.WithError(err).Fatal("cannot bind Viper to pflags") |
| 37 | + } |
| 38 | + }, |
| 39 | + Run: func(cmd *cobra.Command, args []string) { |
| 40 | + if componentCmdOpt.image == "" { |
| 41 | + log.Errorf("target image is empty, skip service waiter %s", componentCmdOpt.component) |
| 42 | + return |
| 43 | + } |
| 44 | + timeout := getTimeout() |
| 45 | + log.WithField("timeout", timeout.String()).WithFields(logrus.Fields{"image": componentCmdOpt.image, "component": componentCmdOpt.component, "namespace": componentCmdOpt.namespace, "labels": componentCmdOpt.labels}).Info("start to wait component") |
| 46 | + ctx, cancel := context.WithTimeout(cmd.Context(), timeout) |
| 47 | + defer cancel() |
| 48 | + |
| 49 | + err := waitPodsImage(ctx) |
| 50 | + |
| 51 | + if err != nil { |
| 52 | + log.WithError(err).Fatal("failed to wait service") |
| 53 | + } else { |
| 54 | + log.Info("service is ready") |
| 55 | + } |
| 56 | + }, |
| 57 | +} |
| 58 | + |
| 59 | +func checkPodsImage(ctx context.Context, k8sClient *kubernetes.Clientset) (bool, error) { |
| 60 | + pods, err := k8sClient.CoreV1().Pods(componentCmdOpt.namespace).List(ctx, metav1.ListOptions{ |
| 61 | + LabelSelector: componentCmdOpt.labels, |
| 62 | + }) |
| 63 | + if err != nil { |
| 64 | + return false, fmt.Errorf("cannot get pod list: %w", err) |
| 65 | + } |
| 66 | + if len(pods.Items) == 0 { |
| 67 | + return false, fmt.Errorf("no pods found") |
| 68 | + } |
| 69 | + readyCount := 0 |
| 70 | + for _, pod := range pods.Items { |
| 71 | + for _, container := range pod.Spec.Containers { |
| 72 | + if container.Name == componentCmdOpt.component { |
| 73 | + if container.Image != componentCmdOpt.image { |
| 74 | + return false, fmt.Errorf("image is not the same: %s != %s", container.Image, componentCmdOpt.image) |
| 75 | + } |
| 76 | + for _, condition := range pod.Status.Conditions { |
| 77 | + if condition.Type == corev1.PodReady { |
| 78 | + if condition.Status == corev1.ConditionTrue { |
| 79 | + readyCount += 1 |
| 80 | + } else { |
| 81 | + return false, fmt.Errorf("pod is not ready") |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + log.Infof("ready pods: %d/%d", readyCount, len(pods.Items)) |
| 89 | + return readyCount == len(pods.Items), nil |
| 90 | +} |
| 91 | + |
| 92 | +func waitPodsImage(ctx context.Context) error { |
| 93 | + k8sCfg, err := rest.InClusterConfig() |
| 94 | + if err != nil { |
| 95 | + return fmt.Errorf("cannot get in cluster config: %w", err) |
| 96 | + } |
| 97 | + k8sClient, err := kubernetes.NewForConfig(k8sCfg) |
| 98 | + if err != nil { |
| 99 | + return fmt.Errorf("cannot create k8s client: %w", err) |
| 100 | + } |
| 101 | + ok := false |
| 102 | + for { |
| 103 | + select { |
| 104 | + case <-ctx.Done(): |
| 105 | + if ok { |
| 106 | + return nil |
| 107 | + } |
| 108 | + return ctx.Err() |
| 109 | + default: |
| 110 | + ok, err := checkPodsImage(ctx, k8sClient) |
| 111 | + if err != nil { |
| 112 | + log.WithError(err).Error("image check failed") |
| 113 | + time.Sleep(1 * time.Second) |
| 114 | + continue |
| 115 | + } |
| 116 | + if ok { |
| 117 | + return nil |
| 118 | + } |
| 119 | + time.Sleep(1 * time.Second) |
| 120 | + } |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func init() { |
| 125 | + rootCmd.AddCommand(componentCmd) |
| 126 | + componentCmd.Flags().StringVar(&componentCmdOpt.image, "image", "", "The latest image of current installer build") |
| 127 | + componentCmd.Flags().StringVar(&componentCmdOpt.namespace, "namespace", "", "The namespace of deployment") |
| 128 | + componentCmd.Flags().StringVar(&componentCmdOpt.component, "component", "", "Component name of deployment") |
| 129 | + componentCmd.Flags().StringVar(&componentCmdOpt.labels, "labels", "", "Labels of deployment") |
| 130 | + |
| 131 | + _ = componentCmd.MarkFlagRequired("namespace") |
| 132 | + _ = componentCmd.MarkFlagRequired("component") |
| 133 | + _ = componentCmd.MarkFlagRequired("labels") |
| 134 | +} |
0 commit comments