forked from ServiceWeaver/weaver-kube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.go
163 lines (144 loc) · 5.14 KB
/
deploy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package impl
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"github.com/ServiceWeaver/weaver/runtime"
"github.com/ServiceWeaver/weaver/runtime/bin"
"github.com/ServiceWeaver/weaver/runtime/codegen"
"github.com/ServiceWeaver/weaver/runtime/version"
"github.com/google/uuid"
"sigs.k8s.io/yaml"
)
// Deploy generates a Kubernetes YAML file and corresponding Docker image to
// deploy the Service Weaver application specified by the provided kube.yaml
// config file.
func Deploy(ctx context.Context, configFilename string) error {
// Read the config file.
contents, err := os.ReadFile(configFilename)
if err != nil {
return fmt.Errorf("read deployment config file %q: %w", configFilename, err)
}
// Parse and validate the deployment config.
config := &kubeConfig{}
if err := yaml.Unmarshal(contents, config); err != nil {
return fmt.Errorf("parse deployment config file %q: %w", configFilename, err)
}
if config.AppConfig == "" {
return fmt.Errorf("app config file not specified")
}
// Ensure that the app config path is resolved relative to the deployment config path.
dir := filepath.Dir(configFilename)
if !filepath.IsAbs(config.AppConfig) {
appConfigPath, err := filepath.Abs(filepath.Join(dir, config.AppConfig))
if err != nil {
return err
}
config.AppConfig = appConfigPath
}
// Read the app config file.
contents, err = os.ReadFile(config.AppConfig)
if err != nil {
return fmt.Errorf("read app config file %q: %w", config.AppConfig, err)
}
// Parse and validate the app config.
app, err := runtime.ParseConfig(config.AppConfig, string(contents), codegen.ComponentConfigValidator)
if err != nil {
return fmt.Errorf("parse app config file %q: %w", config.AppConfig, err)
}
if _, err := os.Stat(app.Binary); errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("binary %q doesn't exist", app.Binary)
}
if err := checkVersionCompatibility(app.Binary); err != nil {
return err
}
if config.Repo == "" {
fmt.Fprintln(os.Stderr, "No container repo specified in the config file. The container image will only be accessible locally. See `weaver kube deploy --help` for details.")
}
if config.Namespace == "" {
config.Namespace = "default"
}
if config.ServiceAccount == "" {
config.ServiceAccount = "default"
}
binListeners, err := bin.ReadListeners(app.Binary)
if err != nil {
return fmt.Errorf("cannot read listeners from binary %s: %w", app.Binary, err)
}
allListeners := make(map[string]struct{})
for _, c := range binListeners {
for _, l := range c.Listeners {
allListeners[l] = struct{}{}
}
}
for _, lis := range config.Listeners {
if _, ok := allListeners[lis.Name]; !ok {
return fmt.Errorf("listener %s specified in the config not found in the binary", lis.Name)
}
}
// Unique app deployment identifier.
depId := uuid.New().String()
// Build the docker image for the deployment.
opts := dockerOptions{image: config.Image, repo: config.Repo}
image, err := buildAndUploadDockerImage(ctx, app, depId, opts)
if err != nil {
return err
}
// Generate the kube deployment information.
return generateYAMLs(app, config, depId, image)
}
// checkVersionCompatibility checks that the `weaver kube` binary is compatible
// with the application binary being deployed.
func checkVersionCompatibility(appBinary string) error {
versions, err := bin.ReadVersions(appBinary)
if err != nil {
return fmt.Errorf("read versions: %w", err)
}
selfVersion, _, err := ToolVersion()
if err != nil {
return fmt.Errorf("read weaver-kube version: %w", err)
}
// Try to relativize the binary, defaulting to the absolute path if there
// are any errors.
relativize := func(bin string) string {
cwd, err := os.Getwd()
if err != nil {
return bin
}
rel, err := filepath.Rel(cwd, bin)
if err != nil {
return bin
}
return rel
}
if versions.DeployerVersion != version.DeployerVersion {
return fmt.Errorf(`
ERROR: The binary you're trying to deploy (%q) was built with
github.com/ServiceWeaver/weaver module version %s. However, the 'weaver-kube'
binary you're using was built with weaver module version %s. These versions are
incompatible.
We recommend updating both the weaver module your application is built with and
updating the 'weaver-kube' command by running the following.
go get github.com/ServiceWeaver/weaver@latest
go install github.com/kidus-tiliksew/weaver-kube/cmd/weaver-kube@latest
Then, re-build your code and re-run 'weaver-kube deploy'. If the problem
persists, please file an issue at https://github.com/ServiceWeaver/weaver/issues`,
relativize(appBinary), versions.ModuleVersion, selfVersion)
}
return nil
}