-
Notifications
You must be signed in to change notification settings - Fork 88
/
embedded_cluster_node_join_command.go
185 lines (160 loc) · 5.96 KB
/
embedded_cluster_node_join_command.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
package handlers
import (
"encoding/json"
"fmt"
"net/http"
"github.com/replicatedhq/kots/pkg/embeddedcluster"
"github.com/replicatedhq/kots/pkg/k8sutil"
"github.com/replicatedhq/kots/pkg/kotsutil"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/store"
"github.com/replicatedhq/kots/pkg/util"
)
type GenerateEmbeddedClusterNodeJoinCommandResponse struct {
Command []string `json:"command"`
}
type GetEmbeddedClusterNodeJoinCommandResponse struct {
ClusterID string `json:"clusterID"`
K0sJoinCommand string `json:"k0sJoinCommand"`
K0sToken string `json:"k0sToken"`
K0sUnsupportedOverrides string `json:"k0sUnsupportedOverrides"`
EndUserK0sConfigOverrides string `json:"endUserK0sConfigOverrides"`
MetricsBaseURL string `json:"metricsBaseURL"`
EmbeddedClusterVersion string `json:"embeddedClusterVersion"`
AirgapRegistryAddress string `json:"airgapRegistryAddress"`
IsAirgap bool `json:"isAirgap"`
}
type GenerateEmbeddedClusterNodeJoinCommandRequest struct {
Roles []string `json:"roles"`
}
func (h *Handler) GenerateEmbeddedClusterNodeJoinCommand(w http.ResponseWriter, r *http.Request) {
if !util.IsEmbeddedCluster() {
logger.Errorf("not an embedded cluster")
w.WriteHeader(http.StatusBadRequest)
return
}
generateEmbeddedClusterNodeJoinCommandRequest := GenerateEmbeddedClusterNodeJoinCommandRequest{}
if err := json.NewDecoder(r.Body).Decode(&generateEmbeddedClusterNodeJoinCommandRequest); err != nil {
logger.Error(fmt.Errorf("failed to decode request body: %w", err))
w.WriteHeader(http.StatusBadRequest)
return
}
token, err := store.GetStore().SetEmbeddedClusterInstallCommandRoles(generateEmbeddedClusterNodeJoinCommandRequest.Roles)
if err != nil {
logger.Error(fmt.Errorf("failed to set k0s install command roles: %w", err))
w.WriteHeader(http.StatusInternalServerError)
return
}
client, err := k8sutil.GetClientset()
if err != nil {
logger.Error(fmt.Errorf("failed to get clientset: %w", err))
w.WriteHeader(http.StatusInternalServerError)
return
}
apps, err := store.GetStore().ListInstalledApps()
if err != nil {
logger.Error(fmt.Errorf("failed to list installed apps: %w", err))
w.WriteHeader(http.StatusInternalServerError)
return
}
if len(apps) == 0 {
logger.Error(fmt.Errorf("no installed apps found"))
w.WriteHeader(http.StatusInternalServerError)
return
}
app := apps[0]
nodeJoinCommand, err := embeddedcluster.GenerateAddNodeCommand(r.Context(), client, token, app.IsAirgap)
if err != nil {
logger.Error(fmt.Errorf("failed to generate add node command: %w", err))
w.WriteHeader(http.StatusInternalServerError)
return
}
JSON(w, http.StatusOK, GenerateEmbeddedClusterNodeJoinCommandResponse{
Command: []string{nodeJoinCommand},
})
}
// this function relies on the token being valid for authentication
func (h *Handler) GetEmbeddedClusterNodeJoinCommand(w http.ResponseWriter, r *http.Request) {
if !util.IsEmbeddedCluster() {
logger.Errorf("not an embedded cluster")
w.WriteHeader(http.StatusBadRequest)
return
}
// read query string, ensure that the token is valid
token := r.URL.Query().Get("token")
roles, err := store.GetStore().GetEmbeddedClusterInstallCommandRoles(token)
if err != nil {
logger.Error(fmt.Errorf("failed to get k0s install command roles: %w", err))
w.WriteHeader(http.StatusInternalServerError)
return
}
// use roles to generate join token etc
client, err := k8sutil.GetClientset()
if err != nil {
logger.Error(fmt.Errorf("failed to get clientset: %w", err))
w.WriteHeader(http.StatusInternalServerError)
return
}
k0sRole := "worker"
controllerRoleName, err := embeddedcluster.ControllerRoleName(r.Context())
if err != nil {
logger.Error(fmt.Errorf("failed to get controller role name: %w", err))
w.WriteHeader(http.StatusInternalServerError)
return
}
for _, role := range roles {
if role == controllerRoleName {
k0sRole = "controller"
break
}
}
// sort roles by name, but put controller first
roles = embeddedcluster.SortRoles(controllerRoleName, roles)
k0sToken, err := embeddedcluster.GenerateAddNodeToken(r.Context(), client, k0sRole)
if err != nil {
logger.Error(fmt.Errorf("failed to generate add node token: %w", err))
w.WriteHeader(http.StatusInternalServerError)
return
}
k0sJoinCommand, err := embeddedcluster.GenerateK0sJoinCommand(r.Context(), client, roles)
if err != nil {
logger.Error(fmt.Errorf("failed to generate k0s join command: %w", err))
w.WriteHeader(http.StatusInternalServerError)
return
}
logger.Infof("k0s join command: %q", k0sJoinCommand)
clusterID, err := embeddedcluster.ClusterID(client)
if err != nil {
logger.Error(fmt.Errorf("failed to get cluster id: %w", err))
w.WriteHeader(http.StatusInternalServerError)
return
}
// extracts the configuration overrides from the current active installation object.
install, err := embeddedcluster.GetCurrentInstallation(r.Context())
if err != nil {
logger.Error(fmt.Errorf("failed to get current install: %w", err))
w.WriteHeader(http.StatusInternalServerError)
return
}
endUserK0sConfigOverrides := install.Spec.EndUserK0sConfigOverrides
var k0sUnsupportedOverrides, ecVersion string
if install.Spec.Config != nil {
k0sUnsupportedOverrides = install.Spec.Config.UnsupportedOverrides.K0s
ecVersion = install.Spec.Config.Version
}
airgapRegistryAddress := ""
if install.Spec.AirGap {
airgapRegistryAddress, _, _ = kotsutil.GetEmbeddedRegistryCreds(client)
}
JSON(w, http.StatusOK, GetEmbeddedClusterNodeJoinCommandResponse{
ClusterID: clusterID,
K0sJoinCommand: k0sJoinCommand,
K0sToken: k0sToken,
K0sUnsupportedOverrides: k0sUnsupportedOverrides,
EndUserK0sConfigOverrides: endUserK0sConfigOverrides,
MetricsBaseURL: install.Spec.MetricsBaseURL,
EmbeddedClusterVersion: ecVersion,
AirgapRegistryAddress: airgapRegistryAddress,
IsAirgap: install.Spec.AirGap,
})
}