Skip to content

Commit 68e6562

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

File tree

4 files changed

+316
-4
lines changed

4 files changed

+316
-4
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
// TODO: make sure there's only one source for those vars in installer and service-waiter
37+
namespace: "default",
38+
name: "public-api-server",
39+
deploymentName: "public-api-server",
40+
containerName: "public-api-server",
41+
targetImage: image,
42+
})
43+
44+
if err != nil {
45+
logger.WithError(err).Fatal("failed to wait service")
46+
} else {
47+
logger.Info("service is ready")
48+
}
49+
},
50+
}
51+
52+
func init() {
53+
rootCmd.AddCommand(publicApiServerCmd)
54+
publicApiServerCmd.Flags().String("image", "", "The latest image of current installer build")
55+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
// TODO: make sure there's only one source for those vars in installer and service-waiter
43+
namespace: "default",
44+
name: "server",
45+
deploymentName: "server",
46+
containerName: "server",
47+
targetImage: image,
48+
})
49+
50+
if err != nil {
51+
logger.WithError(err).Fatal("failed to wait service")
52+
} else {
53+
logger.Info("service is ready")
54+
}
55+
},
56+
}
57+
58+
type deploymentWaiterConfig struct {
59+
name string
60+
namespace string
61+
deploymentName string
62+
containerName string
63+
targetImage string
64+
}
65+
66+
func checkK8SDeploymentImage(ctx context.Context, k8sClient *kubernetes.Clientset, cfg *deploymentWaiterConfig) (bool, error) {
67+
deployment, err := k8sClient.AppsV1().Deployments(cfg.namespace).Get(ctx, cfg.deploymentName, metav1.GetOptions{})
68+
if err != nil {
69+
return false, fmt.Errorf("cannot get deployment info: %w", err)
70+
}
71+
for _, container := range deployment.Spec.Template.Spec.Containers {
72+
if container.Name == cfg.containerName {
73+
if container.Image != cfg.targetImage {
74+
return false, fmt.Errorf("image is not the same: %s != %s", container.Image, cfg.targetImage)
75+
}
76+
if deployment.Status.ReadyReplicas != *deployment.Spec.Replicas {
77+
return false, fmt.Errorf("not all pods are ready %d/%d", deployment.Status.ReadyReplicas, *deployment.Spec.Replicas)
78+
}
79+
return true, nil
80+
}
81+
}
82+
return false, fmt.Errorf("container %s not found", cfg.containerName)
83+
}
84+
85+
func waitK8SDeploymentImage(ctx context.Context, logger *logrus.Entry, cfg *deploymentWaiterConfig) error {
86+
k8sCfg, err := rest.InClusterConfig()
87+
if err != nil {
88+
return fmt.Errorf("cannot get in cluster config: %w", err)
89+
}
90+
k8sClient, err := kubernetes.NewForConfig(k8sCfg)
91+
if err != nil {
92+
return fmt.Errorf("cannot create k8s client: %w", err)
93+
}
94+
ok := false
95+
for {
96+
select {
97+
case <-ctx.Done():
98+
if ok {
99+
return nil
100+
}
101+
return ctx.Err()
102+
default:
103+
ok, err := checkK8SDeploymentImage(ctx, k8sClient, cfg)
104+
if err != nil {
105+
logger.WithError(err).WithField("component", cfg.name).Error("image check failed")
106+
continue
107+
}
108+
if ok {
109+
return nil
110+
}
111+
time.Sleep(time.Second)
112+
}
113+
}
114+
}
115+
116+
func init() {
117+
rootCmd.AddCommand(serverCmd)
118+
serverCmd.Flags().String("image", "", "The latest image of current installer build")
119+
}

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)