-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployer.go
242 lines (230 loc) · 6.8 KB
/
deployer.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package k8s
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path"
"time"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer/yaml"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/discovery"
"k8s.io/client-go/discovery/cached/memory"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
"k8s.io/client-go/restmapper"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/portforward"
"k8s.io/client-go/transport/spdy"
"k8s.io/utils/ptr"
)
type Deployer struct {
client *dynamic.DynamicClient
decoder runtime.Serializer
mapper *restmapper.DeferredDiscoveryRESTMapper
config *rest.Config
k8sConfig Config
}
func (d Deployer) Cleanup(ctx context.Context) {
log.Info("Cleaning up remote resources")
namespaceRes := schema.GroupVersionResource{Group: "", Version: "v1", Resource: "namespaces"}
if err := d.client.Resource(namespaceRes).Delete(ctx, d.k8sConfig.Namespace, metav1.DeleteOptions{}); err != nil {
log.Error("Unable to do cleanup. Please do the cleanup manually", "err", err)
}
}
func (d Deployer) Deploy(ctx context.Context) error {
if err := d.DeployRemoteComponents(ctx); err != nil {
return err
}
if readChanChan, err := d.ForwardPorts(ctx, d.k8sConfig.ControlServerPort, d.k8sConfig.PortalPort); err != nil {
log.Error("Error forwarding ports", "err", err)
return err
} else {
go func() {
for r := range readChanChan {
<-r
printConnectionDetails(fmt.Sprintf("%s.%s:%s", d.k8sConfig.AppName, d.k8sConfig.Namespace, d.k8sConfig.ServicePort))
}
}()
}
return nil
}
func (d Deployer) deploy(
ctx context.Context,
manifest string,
) error {
var obj unstructured.Unstructured
_, gvk, err := d.decoder.Decode([]byte(manifest), nil, &obj)
if err != nil {
return err
}
mapping, err := d.mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
return err
}
var dr dynamic.ResourceInterface
if mapping.Scope.Name() == meta.RESTScopeNameNamespace {
// namespaced resources should specify the namespace
dr = d.client.Resource(mapping.Resource).Namespace(obj.GetNamespace())
} else {
// for cluster-wide resources
dr = d.client.Resource(mapping.Resource)
}
data, err := json.Marshal(obj.Object)
if err != nil {
return err
}
_, err = dr.Patch(
ctx,
obj.GetName(),
types.ApplyPatchType,
data,
metav1.PatchOptions{
FieldManager: d.k8sConfig.AppName + "-k8s",
Force: ptr.To(true),
},
)
if err != nil {
return err
}
return nil
}
func NewDeployer(k8sConfig Config) Deployer {
cfg, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: k8sConfig.Kubeconfig},
&clientcmd.ConfigOverrides{CurrentContext: k8sConfig.KubeContext},
).ClientConfig()
if err != nil {
log.Fatal("Error building k8s config", "err", err)
}
dc, err := discovery.NewDiscoveryClientForConfig(cfg)
if err != nil {
log.Fatal("Error creating discovery client", "err", err)
}
mapper := restmapper.NewDeferredDiscoveryRESTMapper(memory.NewMemCacheClient(dc))
client, err := dynamic.NewForConfig(cfg)
if err != nil {
log.Fatal("Error building k8s config", "err", err)
}
var decoder = yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme)
deployer := Deployer{
client: client,
decoder: decoder,
mapper: mapper,
config: cfg,
k8sConfig: k8sConfig,
}
return deployer
}
func (d Deployer) DeployRemoteComponents(ctx context.Context) error {
log.Info("Deploying remote resources")
var (
err error
tmpl string
)
log.Info("Deploying new namespace", "name", d.k8sConfig.Namespace)
tmpl, err = executeTemplate(Namespace, d.k8sConfig)
if err != nil {
return err
}
if err = d.deploy(ctx, tmpl); err != nil {
log.Error("Error deploying remote components", "err", err)
return err
}
log.Info("Deploying new deployment", "namespace", d.k8sConfig.Namespace)
tmpl, err = executeTemplate(Deployment, d.k8sConfig)
if err != nil {
return err
}
if err = d.deploy(ctx, tmpl); err != nil {
log.Error("Error deploying remote components", "err", err)
return err
}
log.Info("Deploying new service", "namespace", d.k8sConfig.Namespace)
tmpl, err = executeTemplate(Service, d.k8sConfig)
if err != nil {
return err
}
if err = d.deploy(ctx, tmpl); err != nil {
log.Error("Error deploying remote components", "err", err)
return err
}
return nil
}
func (d Deployer) getPodName(ctx context.Context, k8sConfig Config) string {
log.Info("Getting pod details")
for {
namespaceRes := schema.GroupVersionResource{Group: "", Version: "v1", Resource: "pods"}
list, err := d.client.Resource(namespaceRes).Namespace(k8sConfig.Namespace).List(ctx, metav1.ListOptions{})
if err != nil {
log.Warn("Error getting pod list", "err", err)
}
for _, u := range list.Items {
podStatus, _, err := unstructured.NestedFieldCopy(u.Object, "status", "phase")
if err != nil {
log.Warn("Error getting pod details", "err", err)
}
if podStatus == "Running" {
podName := u.GetName()
log.Info("Pod is Running", "name", podName)
return podName
}
}
time.Sleep(time.Second * 2)
log.Info("Pod not ready yet")
}
}
func (d Deployer) ForwardPorts(ctx context.Context, ports ...string) (chan chan struct{}, error) {
transport, upgrader, err := spdy.RoundTripperFor(d.config)
if err != nil {
return nil, err
}
url, err := url.Parse(d.config.Host)
if err != nil {
return nil, err
}
readChanChan := make(chan chan struct{})
go func() {
for {
podName := d.getPodName(ctx, d.k8sConfig)
url.Path = path.Join("api", "v1", "namespaces", d.k8sConfig.Namespace, "pods", podName, "portforward")
dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, http.MethodPost, url)
var formattedPorts []string
for _, p := range ports {
formattedPorts = append(formattedPorts, fmt.Sprintf("%s:%s", p, p))
}
readyChan := make(chan struct{})
readChanChan <- readyChan
forwarder, err := portforward.New(dialer, formattedPorts, nil, readyChan, io.Discard, os.Stderr)
if err != nil {
log.Error("Error creating new forwarder. Retrying", "err", err)
} else {
if err := forwarder.ForwardPorts(); err != nil {
log.Error("Error forwarding ports. Retrying", "err", err)
}
}
time.Sleep(time.Second * 5)
}
}()
return readChanChan, nil
}
var connectionDetailsStyle = lipgloss.NewStyle().
Border(lipgloss.NormalBorder()).
Foreground(lipgloss.AdaptiveColor{Light: "236", Dark: "253"}).
PaddingTop(1).
PaddingBottom(1).
PaddingLeft(2).
PaddingRight(2)
func printConnectionDetails(details string) {
fmt.Println(connectionDetailsStyle.Render(details))
}