fix: restore NSReconciler, archive upload create/edit, fix version label both places; v1.3.45
- tenant.go: restore StartNSReconciler (sync FISSION_RESOURCE_NAMESPACES every 30s) - main.go: call nsm.StartNSReconciler on startup - handlers.go: handleCreateFunctionFromArchive (multipart), handleUpdateFunctionArchive (PUT /archive) source-type annotation, source_type in GET response - functions.js: submitCreate/submitEdit archive mode, openEdit uses source_type - index.html: version label updated in BOTH places (line 103 and 403) to v1.3.45 - console.yaml: image v1.3.45 - doc: archive-and-edit-modal-flow.md
This commit is contained in:
@@ -3,6 +3,8 @@ package cloud
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -10,6 +12,7 @@ import (
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/client-go/dynamic"
|
||||
|
||||
"fission-console/internal/fission"
|
||||
@@ -140,6 +143,171 @@ func (m *NSManager) provisionNamespace(ctx context.Context, ns string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
var deployGVR = schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"}
|
||||
|
||||
// fissionSystemNS возвращает namespace где живут компоненты Fission (router, executor, ...).
|
||||
func fissionSystemNS() string {
|
||||
if ns := os.Getenv("FISSION_SYSTEM_NAMESPACE"); ns != "" {
|
||||
return ns
|
||||
}
|
||||
return "fission"
|
||||
}
|
||||
|
||||
// StartNSReconciler запускает фоновый goroutine который синхронизирует FISSION_RESOURCE_NAMESPACES
|
||||
// в router и executor со всеми реально существующими пользовательскими namespace-ами.
|
||||
//
|
||||
// Почему reconciler, а не прямой патч в EnsureUserNS:
|
||||
// - Прямой патч → rolling restart при каждом новом логине → cold start для всех
|
||||
// - Reconciler батчит изменения, не делает лишних патчей если список не изменился
|
||||
// - Автоматически убирает ghost namespace-ы (удалённые вручную через kubectl)
|
||||
func (m *NSManager) StartNSReconciler(interval time.Duration) {
|
||||
go func() {
|
||||
ticker := time.NewTicker(interval)
|
||||
defer ticker.Stop()
|
||||
log.Printf("cloud.NSReconciler: started, interval=%v", interval)
|
||||
for range ticker.C {
|
||||
m.reconcileNSList()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// reconcileNSList синхронизирует FISSION_RESOURCE_NAMESPACES в router и executor.
|
||||
// Читает все Active namespace-ы с меткой managed-by=fission-console,
|
||||
// сравнивает с текущим значением в router, патчит только если есть разница.
|
||||
func (m *NSManager) reconcileNSList() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
|
||||
sysNS := fissionSystemNS()
|
||||
|
||||
// Шаг 1: реальные namespace-ы с нашей меткой (только Active)
|
||||
nsList, err := m.dyn.Resource(fission.NamespaceGVR).List(ctx, metav1.ListOptions{
|
||||
LabelSelector: "managed-by=fission-console",
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("cloud.NSReconciler: list namespaces: %v", err)
|
||||
return
|
||||
}
|
||||
desired := map[string]struct{}{"default": {}}
|
||||
for _, ns := range nsList.Items {
|
||||
phase, _, _ := unstructured.NestedString(ns.Object, "status", "phase")
|
||||
if phase == "Active" {
|
||||
desired[ns.GetName()] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
// Шаг 2: текущее значение из router (источник истины)
|
||||
routerDep, err := m.dyn.Resource(deployGVR).Namespace(sysNS).Get(ctx, "router", metav1.GetOptions{})
|
||||
if err != nil {
|
||||
log.Printf("cloud.NSReconciler: get router deployment: %v", err)
|
||||
return
|
||||
}
|
||||
currentVal := currentFissionResourceNS(routerDep)
|
||||
|
||||
// Шаг 3: сравниваем
|
||||
currentSet := map[string]struct{}{}
|
||||
for _, p := range strings.Split(currentVal, ",") {
|
||||
if t := strings.TrimSpace(p); t != "" {
|
||||
currentSet[t] = struct{}{}
|
||||
}
|
||||
}
|
||||
same := len(currentSet) == len(desired)
|
||||
if same {
|
||||
for k := range desired {
|
||||
if _, ok := currentSet[k]; !ok {
|
||||
same = false
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if same {
|
||||
return // ничего не изменилось — нет патча, нет rolling restart
|
||||
}
|
||||
|
||||
// Шаг 4: строим новое значение
|
||||
parts := make([]string, 0, len(desired))
|
||||
for ns := range desired {
|
||||
parts = append(parts, ns)
|
||||
}
|
||||
sort.Strings(parts)
|
||||
newVal := strings.Join(parts, ",")
|
||||
|
||||
// Шаг 5: патчим router и executor
|
||||
for _, depName := range []string{"router", "executor"} {
|
||||
if patchErr := m.patchDeployFissionNS(ctx, sysNS, depName, newVal); patchErr != nil {
|
||||
log.Printf("cloud.NSReconciler: patch %s: %v", depName, patchErr)
|
||||
}
|
||||
}
|
||||
log.Printf("cloud.NSReconciler: synced FISSION_RESOURCE_NAMESPACES: %q → %q", currentVal, newVal)
|
||||
}
|
||||
|
||||
// currentFissionResourceNS читает текущий FISSION_RESOURCE_NAMESPACES из deployment.
|
||||
func currentFissionResourceNS(dep *unstructured.Unstructured) string {
|
||||
containers, _, _ := unstructured.NestedSlice(dep.Object, "spec", "template", "spec", "containers")
|
||||
for _, c := range containers {
|
||||
cont, ok := c.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
envs, _, _ := unstructured.NestedSlice(cont, "env")
|
||||
for _, e := range envs {
|
||||
env, ok := e.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if env["name"] == "FISSION_RESOURCE_NAMESPACES" {
|
||||
if v, ok := env["value"].(string); ok && v != "" {
|
||||
return v
|
||||
}
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
return "default"
|
||||
}
|
||||
|
||||
// patchDeployFissionNS обновляет FISSION_RESOURCE_NAMESPACES в указанном deployment.
|
||||
func (m *NSManager) patchDeployFissionNS(ctx context.Context, sysNS, depName, newVal string) error {
|
||||
dep, err := m.dyn.Resource(deployGVR).Namespace(sysNS).Get(ctx, depName, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
containers, _, _ := unstructured.NestedSlice(dep.Object, "spec", "template", "spec", "containers")
|
||||
if len(containers) == 0 {
|
||||
return nil
|
||||
}
|
||||
cont, ok := containers[0].(map[string]any)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
envs, _, _ := unstructured.NestedSlice(cont, "env")
|
||||
updated := false
|
||||
for i, e := range envs {
|
||||
env, ok := e.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if env["name"] == "FISSION_RESOURCE_NAMESPACES" {
|
||||
if env["value"] != newVal {
|
||||
env["value"] = newVal
|
||||
envs[i] = env
|
||||
updated = true
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
if !updated {
|
||||
envs = append(envs, map[string]any{"name": "FISSION_RESOURCE_NAMESPACES", "value": newVal})
|
||||
}
|
||||
cont["env"] = envs
|
||||
containers[0] = cont
|
||||
if err := unstructured.SetNestedSlice(dep.Object, containers, "spec", "template", "spec", "containers"); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = m.dyn.Resource(deployGVR).Namespace(sysNS).Update(ctx, dep, metav1.UpdateOptions{})
|
||||
return err
|
||||
}
|
||||
|
||||
// StartExpiryReaper запускает фоновый goroutine для удаления функций с истёкшим TTL.
|
||||
// Interval — как часто проверять. Рекомендуемое значение: 5 минут.
|
||||
func (m *NSManager) StartExpiryReaper(interval time.Duration) {
|
||||
|
||||
Reference in New Issue
Block a user