Skip to content

Commit 61f3551

Browse files
committed
[service-waiter] add cli to wait deployments server and public-api-server
1 parent 0e8f0de commit 61f3551

File tree

4 files changed

+314
-4
lines changed

4 files changed

+314
-4
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
10+
"github.com/spf13/cobra"
11+
"github.com/spf13/viper"
12+
13+
"github.com/gitpod-io/gitpod/common-go/log"
14+
)
15+
16+
var publicApiServerCmd = &cobra.Command{
17+
Use: "public-api-server",
18+
Short: "waits for deployment public-api-server to become latest build of current installer build",
19+
PreRun: func(cmd *cobra.Command, args []string) {
20+
err := viper.BindPFlags(cmd.Flags())
21+
if err != nil {
22+
log.WithError(err).Fatal("cannot bind Viper to pflags")
23+
}
24+
},
25+
Run: func(cmd *cobra.Command, args []string) {
26+
image := viper.GetString("image")
27+
timeout := getTimeout()
28+
logger := log.WithField("timeout", timeout.String()).WithField("image", image)
29+
if image == "" {
30+
logger.Fatal("Target image should be defined")
31+
}
32+
ctx, cancel := context.WithTimeout(cmd.Context(), timeout)
33+
defer cancel()
34+
35+
err := waitK8SDeploymentImage(ctx, logger, &deploymentWaiterConfig{
36+
namespace: "default",
37+
name: "public-api-server",
38+
deploymentName: "public-api-server",
39+
containerName: "public-api-server",
40+
targetImage: image,
41+
})
42+
43+
if err != nil {
44+
logger.WithError(err).Fatal("failed to wait service")
45+
} else {
46+
logger.Info("service is ready")
47+
}
48+
},
49+
}
50+
51+
func init() {
52+
rootCmd.AddCommand(publicApiServerCmd)
53+
publicApiServerCmd.Flags().String("image", "", "The latest image of current installer build")
54+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
18+
"k8s.io/client-go/kubernetes"
19+
"k8s.io/client-go/rest"
20+
)
21+
22+
var serverCmd = &cobra.Command{
23+
Use: "server",
24+
Short: "waits for deployment server to become latest build of current installer build",
25+
PreRun: func(cmd *cobra.Command, args []string) {
26+
err := viper.BindPFlags(cmd.Flags())
27+
if err != nil {
28+
log.WithError(err).Fatal("cannot bind Viper to pflags")
29+
}
30+
},
31+
Run: func(cmd *cobra.Command, args []string) {
32+
image := viper.GetString("image")
33+
timeout := getTimeout()
34+
logger := log.WithField("timeout", timeout.String()).WithField("image", image)
35+
if image == "" {
36+
logger.Fatal("Target image should be defined")
37+
}
38+
ctx, cancel := context.WithTimeout(cmd.Context(), timeout)
39+
defer cancel()
40+
41+
err := waitK8SDeploymentImage(ctx, logger, &deploymentWaiterConfig{
42+
namespace: "default",
43+
name: "server",
44+
deploymentName: "server",
45+
containerName: "server",
46+
targetImage: image,
47+
})
48+
49+
if err != nil {
50+
logger.WithError(err).Fatal("failed to wait service")
51+
} else {
52+
logger.Info("service is ready")
53+
}
54+
},
55+
}
56+
57+
type deploymentWaiterConfig struct {
58+
name string
59+
namespace string
60+
deploymentName string
61+
containerName string
62+
targetImage string
63+
}
64+
65+
func checkK8SDeploymentImage(ctx context.Context, k8sClient *kubernetes.Clientset, cfg *deploymentWaiterConfig) (bool, error) {
66+
deployment, err := k8sClient.AppsV1().Deployments(cfg.namespace).Get(ctx, cfg.deploymentName, metav1.GetOptions{})
67+
if err != nil {
68+
return false, fmt.Errorf("cannot get deployment info: %w", err)
69+
}
70+
for _, container := range deployment.Spec.Template.Spec.Containers {
71+
if container.Name == cfg.containerName {
72+
if container.Image != cfg.targetImage {
73+
return false, fmt.Errorf("image is not the same: %s != %s", container.Image, cfg.targetImage)
74+
}
75+
if deployment.Status.ReadyReplicas != *deployment.Spec.Replicas {
76+
return false, fmt.Errorf("not all pods are ready %d/%d", deployment.Status.ReadyReplicas, *deployment.Spec.Replicas)
77+
}
78+
return true, nil
79+
}
80+
}
81+
return false, fmt.Errorf("container %s not found", cfg.containerName)
82+
}
83+
84+
func waitK8SDeploymentImage(ctx context.Context, logger *logrus.Entry, cfg *deploymentWaiterConfig) error {
85+
k8sCfg, err := rest.InClusterConfig()
86+
if err != nil {
87+
return fmt.Errorf("cannot get in cluster config: %w", err)
88+
}
89+
k8sClient, err := kubernetes.NewForConfig(k8sCfg)
90+
if err != nil {
91+
return fmt.Errorf("cannot create k8s client: %w", err)
92+
}
93+
ok := false
94+
for {
95+
select {
96+
case <-ctx.Done():
97+
if ok {
98+
return nil
99+
}
100+
return ctx.Err()
101+
default:
102+
ok, err := checkK8SDeploymentImage(ctx, k8sClient, cfg)
103+
if err != nil {
104+
logger.WithError(err).WithField("component", cfg.name).Error("image check failed")
105+
continue
106+
}
107+
if ok {
108+
return nil
109+
}
110+
time.Sleep(time.Second)
111+
}
112+
}
113+
}
114+
115+
func init() {
116+
rootCmd.AddCommand(serverCmd)
117+
serverCmd.Flags().String("image", "", "The latest image of current installer build")
118+
}

components/service-waiter/go.mod

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,70 @@ require (
77
github.com/go-sql-driver/mysql v1.6.0
88
github.com/mitchellh/go-homedir v1.1.0
99
github.com/redis/go-redis/v9 v9.0.5
10+
github.com/sirupsen/logrus v1.9.3
1011
github.com/spf13/cobra v1.4.0
1112
github.com/spf13/viper v1.7.0
13+
k8s.io/apimachinery v0.27.3
14+
k8s.io/client-go v0.0.0-00010101000000-000000000000
1215
)
1316

1417
require (
1518
github.com/cespare/xxhash/v2 v2.2.0 // indirect
19+
github.com/davecgh/go-spew v1.1.1 // indirect
1620
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
21+
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
1722
github.com/fsnotify/fsnotify v1.4.9 // indirect
1823
github.com/gitpod-io/gitpod/components/scrubber v0.0.0-00010101000000-000000000000 // indirect
24+
github.com/go-logr/logr v1.2.3 // indirect
25+
github.com/go-openapi/jsonpointer v0.19.6 // indirect
26+
github.com/go-openapi/jsonreference v0.20.1 // indirect
27+
github.com/go-openapi/swag v0.22.3 // indirect
28+
github.com/gogo/protobuf v1.3.2 // indirect
1929
github.com/golang/protobuf v1.5.3 // indirect
30+
github.com/google/gnostic v0.5.7-v3refs // indirect
31+
github.com/google/go-cmp v0.5.9 // indirect
32+
github.com/google/gofuzz v1.1.0 // indirect
33+
github.com/google/uuid v1.3.0 // indirect
2034
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
2135
github.com/hashicorp/hcl v1.0.0 // indirect
2236
github.com/inconshreveable/mousetrap v1.0.0 // indirect
37+
github.com/josharian/intern v1.0.0 // indirect
38+
github.com/json-iterator/go v1.1.12 // indirect
2339
github.com/magiconair/properties v1.8.1 // indirect
40+
github.com/mailru/easyjson v0.7.7 // indirect
2441
github.com/mitchellh/mapstructure v1.1.2 // indirect
2542
github.com/mitchellh/reflectwalk v1.0.2 // indirect
43+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
44+
github.com/modern-go/reflect2 v1.0.2 // indirect
45+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
2646
github.com/pelletier/go-toml v1.2.0 // indirect
27-
github.com/sirupsen/logrus v1.9.3 // indirect
47+
github.com/rogpeppe/go-internal v1.11.0 // indirect
2848
github.com/spf13/afero v1.1.2 // indirect
2949
github.com/spf13/cast v1.3.0 // indirect
3050
github.com/spf13/jwalterweatherman v1.0.0 // indirect
3151
github.com/spf13/pflag v1.0.5 // indirect
3252
github.com/subosito/gotenv v1.2.0 // indirect
3353
golang.org/x/net v0.10.0 // indirect
54+
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783 // indirect
3455
golang.org/x/sys v0.11.0 // indirect
56+
golang.org/x/term v0.8.0 // indirect
3557
golang.org/x/text v0.9.0 // indirect
58+
golang.org/x/time v0.0.0-20220922220347-f3bd1da661af // indirect
59+
google.golang.org/appengine v1.6.7 // indirect
3660
google.golang.org/genproto v0.0.0-20221118155620-16455021b5e6 // indirect
3761
google.golang.org/grpc v1.52.3 // indirect
3862
google.golang.org/protobuf v1.30.0 // indirect
63+
gopkg.in/inf.v0 v0.9.1 // indirect
3964
gopkg.in/ini.v1 v1.51.0 // indirect
4065
gopkg.in/yaml.v2 v2.4.0 // indirect
66+
gopkg.in/yaml.v3 v3.0.1 // indirect
67+
k8s.io/api v0.27.3 // indirect
68+
k8s.io/klog/v2 v2.90.1 // indirect
69+
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f // indirect
70+
k8s.io/utils v0.0.0-20230209194617-a36077c30491 // indirect
71+
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
72+
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
73+
sigs.k8s.io/yaml v1.3.0 // indirect
4174
)
4275

4376
replace github.com/gitpod-io/gitpod/common-go => ../common-go // leeway

0 commit comments

Comments
 (0)