-
Notifications
You must be signed in to change notification settings - Fork 3
/
workspace_status_controller.go
216 lines (180 loc) · 5.92 KB
/
workspace_status_controller.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
package controllers
import (
"context"
"strconv"
"time"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/source"
cosmov1alpha1 "github.com/cosmo-workspace/cosmo/api/core/v1alpha1"
wsv1alpha1 "github.com/cosmo-workspace/cosmo/api/workspace/v1alpha1"
"github.com/cosmo-workspace/cosmo/pkg/clog"
"github.com/cosmo-workspace/cosmo/pkg/kosmo"
"github.com/cosmo-workspace/cosmo/pkg/wsnet"
)
const (
WsStatControllerFieldManager string = "cosmo-workspace-status-controller"
)
// WorkspaceStatusReconciler reconciles a Workspace object
type WorkspaceStatusReconciler struct {
kosmo.Client
Recorder record.EventRecorder
Scheme *runtime.Scheme
}
//+kubebuilder:rbac:groups=workspace.cosmo.cosmo-workspace.github.io,resources=workspaces,verbs=get;list;watch
//+kubebuilder:rbac:groups=workspace.cosmo.cosmo-workspace.github.io,resources=workspaces/status,verbs=get;update;patch
func (r *WorkspaceStatusReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := clog.FromContext(ctx).WithName("WorkspaceStatusReconciler")
ctx = clog.IntoContext(ctx, log)
log.Debug().Info("start reconcile")
requeue := false
key := r.getWorkspaceNamespacedName(ctx, req.NamespacedName)
var ws wsv1alpha1.Workspace
if err := r.Get(ctx, key, &ws); err != nil {
return ctrl.Result{}, ignoreNotFound(err)
}
current := ws.DeepCopy()
log.DebugAll().DumpObject(r.Scheme, &ws, "before workspace")
// sync workspace config with template
cfg, err := r.GetWorkspaceConfig(ctx, ws.Spec.Template.Name)
if err != nil {
return ctrl.Result{}, err
}
ws.Status.Config = cfg
// fetch child pod status
if urlMap, err := r.GenWorkspaceURLMap(ctx, ws); err == nil {
log.Debug().Info("workspace urlmap", "urlmap", urlMap)
ws.Status.URLs = urlMap
} else {
log.Info("failed to gen urlmap", "error", err, "ws", ws.Name, "urlbase", cfg.URLBase, "logLevel", "warn")
}
// set workspace phase
pods, err := r.ListWorkspacePods(ctx, ws)
if err != nil {
log.Info("failed to list instance pods", "error", err, "ws", ws.Name, "logLevel", "warn")
ws.Status.Phase = "NotRunning"
} else {
if len(pods) > 0 {
if *ws.Spec.Replicas == 0 {
ws.Status.Phase = "Stopping"
requeue = true
} else {
ws.Status.Phase = kosmo.PodStatusReason(pods[0])
}
} else {
if *ws.Spec.Replicas > 0 {
ws.Status.Phase = "Starting"
requeue = true
} else {
ws.Status.Phase = "Stopped"
}
}
}
// update workspace status
if !equality.Semantic.DeepEqual(*current, ws) {
log.Debug().PrintObjectDiff(*current, ws)
if err := r.Status().Update(ctx, &ws); err != nil {
return ctrl.Result{}, err
}
log.Info("workspace status updated", "ws", ws.Name)
}
log.Info("finish reconcile")
if requeue {
return ctrl.Result{RequeueAfter: time.Second * 5}, nil
}
return ctrl.Result{}, nil
}
func (r *WorkspaceStatusReconciler) SetupWithManager(mgr ctrl.Manager) error {
c, err := ctrl.NewControllerManagedBy(mgr).
For(&wsv1alpha1.Workspace{}).
Owns(&cosmov1alpha1.Instance{}).
Build(r)
if err != nil {
return err
}
// watch pods which has "cosmo/instance" label
predi, _ := predicate.LabelSelectorPredicate(metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: cosmov1alpha1.LabelKeyInstance,
Operator: metav1.LabelSelectorOpExists,
},
},
})
err = c.Watch(&source.Kind{Type: &corev1.Pod{}}, &handler.EnqueueRequestForObject{}, predi)
if err != nil {
return err
}
err = c.Watch(&source.Kind{Type: &corev1.Service{}}, &handler.EnqueueRequestForObject{}, predi)
if err != nil {
return err
}
return nil
}
func (r *WorkspaceStatusReconciler) getWorkspaceNamespacedName(ctx context.Context, req types.NamespacedName) types.NamespacedName {
var pod corev1.Pod
if err := r.Get(ctx, req, &pod); err == nil {
// request is Pod with "cosmo/instance" label
return types.NamespacedName{Name: pod.Labels[cosmov1alpha1.LabelKeyInstance], Namespace: pod.GetNamespace()}
}
var svc corev1.Service
if err := r.Get(ctx, req, &svc); err == nil {
// request is Service with "cosmo/instance" label
return types.NamespacedName{Name: svc.Labels[cosmov1alpha1.LabelKeyInstance], Namespace: svc.GetNamespace()}
}
// request is Workspace
return req
}
func (r *WorkspaceStatusReconciler) GenWorkspaceURLMap(ctx context.Context, ws wsv1alpha1.Workspace) (map[string]string, error) {
log := clog.FromContext(ctx).WithCaller()
urlbase := wsnet.URLBase(ws.Status.Config.URLBase)
svc, _, err := r.GetWorkspaceServicesAndIngress(ctx, ws)
if err != nil {
return nil, err
}
urlvarsMap := make(map[string]wsnet.URLVars)
for _, netRule := range ws.Spec.Network {
urlvars := wsnet.URLVars{}
urlvars.PortName = netRule.PortName
urlvars.PortNumber = strconv.Itoa(netRule.PortNumber)
if netRule.Group != nil {
urlvars.NetRuleGroup = *netRule.Group
}
urlvars.InstanceName = ws.Name
urlvars.WorkspaceName = ws.Name
urlvars.Namespace = ws.Namespace
urlvars.UserID = wsv1alpha1.UserIDByNamespace(ws.Namespace)
urlvars.IngressPath = netRule.HTTPPath
// node port
for _, p := range svc.Spec.Ports {
if p.Name == netRule.PortName {
urlvars.NodePortNumber = strconv.Itoa(int(p.NodePort))
}
}
// load balancer
if svc.Status.LoadBalancer.Size() > 0 {
lb := svc.Status.LoadBalancer.Ingress[0]
if lb.Hostname != "" {
urlvars.LoadBalancer = lb.Hostname
} else {
urlvars.LoadBalancer = lb.IP
}
}
urlvarsMap[netRule.PortName] = urlvars
}
urlMap := make(map[string]string)
for name, urlvars := range urlvarsMap {
if log.DebugAll().Enabled() {
urlvars.Dump(log)
}
urlMap[name] = urlbase.GenURL(urlvars)
}
return urlMap, nil
}