mirror of
https://github.com/GlueOps/autoglue.git
synced 2026-02-13 21:00:06 +01:00
taints
This commit is contained in:
@@ -4,6 +4,27 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type createNodePoolRequest struct {
|
||||
Name string `json:"name"`
|
||||
ServerIDs []string `json:"server_ids"`
|
||||
}
|
||||
|
||||
type updateNodePoolRequest struct {
|
||||
Name *string `json:"name"`
|
||||
}
|
||||
|
||||
type attachServersRequest struct {
|
||||
ServerIDs []string `json:"server_ids"`
|
||||
}
|
||||
|
||||
type attachLabelsRequest struct {
|
||||
LabelIDs []string `json:"label_ids"`
|
||||
}
|
||||
|
||||
type attachTaintsRequest struct {
|
||||
TaintIDs []string `json:"taint_ids"`
|
||||
}
|
||||
|
||||
type nodePoolResponse struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
@@ -12,34 +33,10 @@ type nodePoolResponse struct {
|
||||
|
||||
type serverBrief struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Hostname string `json:"hostname"`
|
||||
IP string `json:"ip"`
|
||||
Role string `json:"role"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
type createNodePoolRequest struct {
|
||||
Name string `json:"name"`
|
||||
ServerIDs []string `json:"server_ids,omitempty"` // optional initial servers
|
||||
}
|
||||
|
||||
type updateNodePoolRequest struct {
|
||||
Name *string `json:"name,omitempty"`
|
||||
}
|
||||
|
||||
type attachServersRequest struct {
|
||||
ServerIDs []string `json:"server_ids"`
|
||||
}
|
||||
|
||||
type taintBrief struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Key string `json:"key"`
|
||||
Value string `json:"value"`
|
||||
Effect string `json:"effect"`
|
||||
}
|
||||
|
||||
type attachTaintsRequest struct {
|
||||
TaintIDs []string `json:"taint_ids"`
|
||||
Hostname string `json:"hostname,omitempty"`
|
||||
IP string `json:"ip,omitempty"`
|
||||
Role string `json:"role,omitempty"`
|
||||
Status string `json:"status,omitempty"`
|
||||
}
|
||||
|
||||
type labelBrief struct {
|
||||
@@ -48,16 +45,9 @@ type labelBrief struct {
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
type attachLabelsRequest struct {
|
||||
LabelIDs []string `json:"label_ids"`
|
||||
}
|
||||
|
||||
type annotationBrief struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Key string `json:"key"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
type attachAnnotationsRequest struct {
|
||||
AnnotationIDs []string `json:"annotation_ids"`
|
||||
type taintBrief struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Key string `json:"key"`
|
||||
Value string `json:"value"`
|
||||
Effect string `json:"effect"`
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@ package nodepools
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/glueops/autoglue/internal/db"
|
||||
"github.com/glueops/autoglue/internal/db/models"
|
||||
@@ -11,14 +9,14 @@ import (
|
||||
)
|
||||
|
||||
func toResp(ng models.NodePool, includeServers bool) nodePoolResponse {
|
||||
resp := nodePoolResponse{
|
||||
out := nodePoolResponse{
|
||||
ID: ng.ID,
|
||||
Name: ng.Name,
|
||||
}
|
||||
if includeServers {
|
||||
resp.Servers = make([]serverBrief, 0, len(ng.Servers))
|
||||
out.Servers = make([]serverBrief, 0, len(ng.Servers))
|
||||
for _, s := range ng.Servers {
|
||||
resp.Servers = append(resp.Servers, serverBrief{
|
||||
out.Servers = append(out.Servers, serverBrief{
|
||||
ID: s.ID,
|
||||
Hostname: s.Hostname,
|
||||
IP: s.IPAddress,
|
||||
@@ -27,17 +25,17 @@ func toResp(ng models.NodePool, includeServers bool) nodePoolResponse {
|
||||
})
|
||||
}
|
||||
}
|
||||
return resp
|
||||
return out
|
||||
}
|
||||
|
||||
func parseUUIDs(ids []string) ([]uuid.UUID, error) {
|
||||
out := make([]uuid.UUID, 0, len(ids))
|
||||
for _, s := range ids {
|
||||
u, err := uuid.Parse(strings.TrimSpace(s))
|
||||
for _, raw := range ids {
|
||||
id, err := uuid.Parse(raw)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, u)
|
||||
out = append(out, id)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -50,15 +48,25 @@ func ensureServersBelongToOrg(orgID uuid.UUID, ids []uuid.UUID) error {
|
||||
return err
|
||||
}
|
||||
if count != int64(len(ids)) {
|
||||
return fmt.Errorf("some servers do not belong to this organization")
|
||||
return errors.New("some servers do not belong to org")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ensureLabelsBelongToOrg(orgID uuid.UUID, ids []uuid.UUID) error {
|
||||
var count int64
|
||||
if err := db.DB.Model(&models.Label{}).
|
||||
Where("organization_id = ? AND id IN ?", orgID, ids).
|
||||
Count(&count).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if count != int64(len(ids)) {
|
||||
return errors.New("some labels do not belong to org")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ensureTaintsBelongToOrg(orgID uuid.UUID, ids []uuid.UUID) error {
|
||||
if len(ids) == 0 {
|
||||
return nil
|
||||
}
|
||||
var count int64
|
||||
if err := db.DB.Model(&models.Taint{}).
|
||||
Where("organization_id = ? AND id IN ?", orgID, ids).
|
||||
@@ -66,39 +74,7 @@ func ensureTaintsBelongToOrg(orgID uuid.UUID, ids []uuid.UUID) error {
|
||||
return err
|
||||
}
|
||||
if count != int64(len(ids)) {
|
||||
return errors.New("some taints not in organization")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ensureLabelsBelongToOrg(orgID uuid.UUID, ids []uuid.UUID) error {
|
||||
if len(ids) == 0 {
|
||||
return nil
|
||||
}
|
||||
var cnt int64
|
||||
if err := db.DB.Model(&models.Label{}).
|
||||
Where("organization_id = ? AND id IN ?", orgID, ids).
|
||||
Count(&cnt).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if cnt != int64(len(ids)) {
|
||||
return errors.New("one or more labels not in organization")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ensureAnnotationsBelongToOrg(orgID uuid.UUID, ids []uuid.UUID) error {
|
||||
if len(ids) == 0 {
|
||||
return nil
|
||||
}
|
||||
var cnt int64
|
||||
if err := db.DB.Model(&models.Annotation{}).
|
||||
Where("organization_id = ? AND id IN ?", orgID, ids).
|
||||
Count(&cnt).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if cnt != int64(len(ids)) {
|
||||
return errors.New("one or more annotations not in organization")
|
||||
return errors.New("some taints do not belong to org")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package taints
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/glueops/autoglue/internal/db"
|
||||
@@ -9,6 +10,19 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
var allowedEffects = map[string]struct{}{
|
||||
"NoSchedule": {},
|
||||
"PreferNoSchedule": {},
|
||||
"NoExecute": {},
|
||||
}
|
||||
|
||||
// includeNodePools returns true when the query param requests linked pools.
|
||||
// Accepts both "node_pools" and "node_groups" for compatibility.
|
||||
func includeNodePools(r *http.Request) bool {
|
||||
inc := strings.TrimSpace(r.URL.Query().Get("include"))
|
||||
return strings.EqualFold(inc, "node_pools") || strings.EqualFold(inc, "node_groups")
|
||||
}
|
||||
|
||||
func toResp(t models.Taint, include bool) taintResponse {
|
||||
resp := taintResponse{
|
||||
ID: t.ID,
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/glueops/autoglue/internal/db"
|
||||
@@ -15,9 +16,11 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// ---------- Handlers ----------
|
||||
|
||||
// ListTaints godoc
|
||||
// @Summary List node taints (org scoped)
|
||||
// @Description Returns node taints for the organization in X-Org-ID. Filters: `key`, `value`, and `q` (key contains). Add `include=node_groups` to include linked node groups.
|
||||
// @Description Returns node taints for the organization in X-Org-ID. Filters: `key`, `value`, and `q` (key contains). Add `include=node_pools` to include linked node pools.
|
||||
// @Tags taints
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
@@ -40,37 +43,38 @@ func ListTaints(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
q := db.DB.Where("organization_id = ?", ac.OrganizationID)
|
||||
|
||||
if key := strings.TrimSpace(r.URL.Query().Get("key")); key != "" {
|
||||
q = q.Where("key = ?", key)
|
||||
q = q.Where(`key = ?`, key)
|
||||
}
|
||||
if val := strings.TrimSpace(r.URL.Query().Get("value")); val != "" {
|
||||
q = q.Where("value = ?", val)
|
||||
q = q.Where(`value = ?`, val)
|
||||
}
|
||||
if needle := strings.TrimSpace(r.URL.Query().Get("q")); needle != "" {
|
||||
q = q.Where("name ILIKE ?", "%"+needle+"%")
|
||||
q = q.Where(`key ILIKE ?`, "%"+needle+"%")
|
||||
}
|
||||
|
||||
includePools := strings.EqualFold(strings.TrimSpace(r.URL.Query().Get("include")), "node_pools")
|
||||
if includePools {
|
||||
withPools := includeNodePools(r)
|
||||
if withPools {
|
||||
q = q.Preload("NodePools")
|
||||
}
|
||||
|
||||
var rows []models.Taint
|
||||
if err := q.Order("created_at DESC").Find(&rows).Error; err != nil {
|
||||
http.Error(w, "failed to list taints", http.StatusInternalServerError)
|
||||
http.Error(w, "failed to list node taints", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
out := make([]taintResponse, 0, len(rows))
|
||||
for _, np := range rows {
|
||||
out = append(out, toResp(np, includePools))
|
||||
for _, t := range rows {
|
||||
out = append(out, toResp(t, withPools))
|
||||
}
|
||||
_ = response.JSON(w, http.StatusOK, out)
|
||||
}
|
||||
|
||||
// GetTaint godoc
|
||||
// @Summary Get node taint by ID (org scoped)
|
||||
// @Description Returns one taint. Add `include=node_groups` to include node groups.
|
||||
// @Description Returns one taint. Add `include=node_pools` to include node pools.
|
||||
// @Tags taints
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
@@ -94,28 +98,27 @@ func GetTaint(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
id, err := uuid.Parse(chi.URLParam(r, "id"))
|
||||
if err != nil {
|
||||
http.Error(w, "invalid taint id", http.StatusBadRequest)
|
||||
http.Error(w, "invalid id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
include := strings.EqualFold(strings.TrimSpace(r.URL.Query().Get("include")), "node_pools")
|
||||
withPools := includeNodePools(r)
|
||||
|
||||
var t models.Taint
|
||||
q := db.DB.Where("id = ? AND organization_id = ?", id, ac.OrganizationID)
|
||||
if include {
|
||||
if withPools {
|
||||
q = q.Preload("NodePools")
|
||||
}
|
||||
|
||||
if err := q.First(&t).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
http.Error(w, "taint not found", http.StatusNotFound)
|
||||
http.Error(w, "not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
http.Error(w, "failed to find taint", http.StatusInternalServerError)
|
||||
http.Error(w, "fetch failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
_ = response.JSON(w, http.StatusOK, toResp(t, include))
|
||||
_ = response.JSON(w, http.StatusOK, toResp(t, withPools))
|
||||
}
|
||||
|
||||
// CreateTaint godoc
|
||||
@@ -141,8 +144,20 @@ func CreateTaint(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var req createTaintRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil || req.Key == "" || req.Value == "" || req.Effect == "" {
|
||||
http.Error(w, "invalid json or missing key/value/effect", http.StatusBadRequest)
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, "invalid json", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
req.Key = strings.TrimSpace(req.Key)
|
||||
req.Value = strings.TrimSpace(req.Value)
|
||||
req.Effect = strings.TrimSpace(req.Effect)
|
||||
|
||||
if req.Key == "" || req.Effect == "" {
|
||||
http.Error(w, "invalid json or missing key/effect", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if _, ok := allowedEffects[req.Effect]; !ok {
|
||||
http.Error(w, "invalid effect", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -152,33 +167,39 @@ func CreateTaint(w http.ResponseWriter, r *http.Request) {
|
||||
Value: req.Value,
|
||||
Effect: req.Effect,
|
||||
}
|
||||
|
||||
if err := db.DB.Create(&t).Error; err != nil {
|
||||
http.Error(w, "failed to create taint", http.StatusInternalServerError)
|
||||
http.Error(w, "create failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// optional initial links
|
||||
if len(req.NodePoolIDs) > 0 {
|
||||
ids, err := parseUUIDs(req.NodePoolIDs)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid node pool IDs", http.StatusBadRequest)
|
||||
http.Error(w, "invalid node_pool_ids", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if err := ensureNodePoolsBelongToOrg(ac.OrganizationID, ids); err != nil {
|
||||
http.Error(w, "invalid node pool IDs for this organization", http.StatusBadRequest)
|
||||
http.Error(w, "invalid node_pool_ids for this organization", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var nps []models.NodePool
|
||||
if err := db.DB.Where("id in ? AND organization_id = ?", ids, ac.OrganizationID).Find(&nps).Error; err != nil {
|
||||
http.Error(w, "node pools not found for this organization", http.StatusInternalServerError)
|
||||
var pools []models.NodePool
|
||||
if err := db.DB.Where("id IN ? AND organization_id = ?", ids, ac.OrganizationID).
|
||||
Find(&pools).Error; err != nil {
|
||||
http.Error(w, "create failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if err := db.DB.Model(&t).Association("NodePools").Append(&nps); err != nil {
|
||||
http.Error(w, "attach node pools failed", http.StatusInternalServerError)
|
||||
if len(pools) != len(ids) {
|
||||
http.Error(w, "invalid node_pool_ids", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if err := db.DB.Model(&t).Association("NodePools").Append(&pools); err != nil {
|
||||
http.Error(w, "create failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
_ = response.JSON(w, http.StatusCreated, toResp(t, false))
|
||||
}
|
||||
|
||||
@@ -205,7 +226,6 @@ func UpdateTaint(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "organization required", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
id, err := uuid.Parse(chi.URLParam(r, "id"))
|
||||
if err != nil {
|
||||
http.Error(w, "invalid id", http.StatusBadRequest)
|
||||
@@ -213,7 +233,8 @@ func UpdateTaint(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var t models.Taint
|
||||
if err := db.DB.Where("id = ? AND organization_id = ?", id, ac.OrganizationID).First(&t).Error; err != nil {
|
||||
if err := db.DB.Where("id = ? AND organization_id = ?", id, ac.OrganizationID).
|
||||
First(&t).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
http.Error(w, "not found", http.StatusNotFound)
|
||||
return
|
||||
@@ -227,6 +248,7 @@ func UpdateTaint(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "invalid json", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if req.Key != nil {
|
||||
t.Key = strings.TrimSpace(*req.Key)
|
||||
}
|
||||
@@ -234,7 +256,16 @@ func UpdateTaint(w http.ResponseWriter, r *http.Request) {
|
||||
t.Value = strings.TrimSpace(*req.Value)
|
||||
}
|
||||
if req.Effect != nil {
|
||||
t.Effect = strings.TrimSpace(*req.Effect)
|
||||
e := strings.TrimSpace(*req.Effect)
|
||||
if e == "" {
|
||||
http.Error(w, "invalid json", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if _, ok := allowedEffects[e]; !ok {
|
||||
http.Error(w, "invalid effect", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
t.Effect = e
|
||||
}
|
||||
|
||||
if err := db.DB.Save(&t).Error; err != nil {
|
||||
@@ -265,18 +296,17 @@ func DeleteTaint(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "organization required", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
id, err := uuid.Parse(chi.URLParam(r, "id"))
|
||||
if err != nil {
|
||||
http.Error(w, "invalid id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if err := db.DB.Where("id = ? AND organization_id = ?", id, ac.OrganizationID).Delete(&models.Taint{}).Error; err != nil {
|
||||
if err := db.DB.Where("id = ? AND organization_id = ?", id, ac.OrganizationID).
|
||||
Delete(&models.Taint{}).Error; err != nil {
|
||||
http.Error(w, "delete failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
response.NoContent(w)
|
||||
}
|
||||
|
||||
@@ -304,7 +334,6 @@ func AddTaintToNodePool(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "organization required", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
taintID, err := uuid.Parse(chi.URLParam(r, "id"))
|
||||
if err != nil {
|
||||
http.Error(w, "invalid id", http.StatusBadRequest)
|
||||
@@ -312,8 +341,7 @@ func AddTaintToNodePool(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var t models.Taint
|
||||
if err := db.DB.
|
||||
Where("id = ? AND organization_id = ?", taintID, ac.OrganizationID).
|
||||
if err := db.DB.Where("id = ? AND organization_id = ?", taintID, ac.OrganizationID).
|
||||
First(&t).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
http.Error(w, "not found", http.StatusNotFound)
|
||||
@@ -323,9 +351,7 @@ func AddTaintToNodePool(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
var in struct {
|
||||
NodePoolIDs []string `json:"node_pool_ids"`
|
||||
}
|
||||
var in addTaintToPoolRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&in); err != nil || len(in.NodePoolIDs) == 0 {
|
||||
http.Error(w, "invalid json or empty node_pool_ids", http.StatusBadRequest)
|
||||
return
|
||||
@@ -341,20 +367,43 @@ func AddTaintToNodePool(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
var pools []models.NodePool
|
||||
if err := db.DB.
|
||||
Where("id IN ? AND organization_id = ?", ids, ac.OrganizationID).
|
||||
Find(&pools).Error; err != nil {
|
||||
// Fetch existing links to avoid duplicates
|
||||
var existing []models.NodePool
|
||||
if err := db.DB.Model(&t).Association("NodePools").Find(&existing); err != nil {
|
||||
http.Error(w, "attach failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if err := db.DB.Model(&t).Association("NodePools").Append(&pools); err != nil {
|
||||
http.Error(w, "attach failed", http.StatusInternalServerError)
|
||||
return
|
||||
existingIDs := make([]uuid.UUID, 0, len(existing))
|
||||
for _, p := range existing {
|
||||
existingIDs = append(existingIDs, p.ID)
|
||||
}
|
||||
|
||||
includePools := strings.EqualFold(strings.TrimSpace(r.URL.Query().Get("include")), "node_pools")
|
||||
if includePools {
|
||||
toFetch := make([]uuid.UUID, 0, len(ids))
|
||||
for _, id := range ids {
|
||||
if !slices.Contains(existingIDs, id) {
|
||||
toFetch = append(toFetch, id)
|
||||
}
|
||||
}
|
||||
|
||||
if len(toFetch) > 0 {
|
||||
var toAttach []models.NodePool
|
||||
if err := db.DB.Where("id IN ? AND organization_id = ?", toFetch, ac.OrganizationID).
|
||||
Find(&toAttach).Error; err != nil {
|
||||
http.Error(w, "attach failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if len(toAttach) != len(toFetch) {
|
||||
http.Error(w, "invalid node_pool_ids", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if err := db.DB.Model(&t).Association("NodePools").Append(&toAttach); err != nil {
|
||||
http.Error(w, "attach failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
withPools := includeNodePools(r)
|
||||
if withPools {
|
||||
if err := db.DB.Preload("NodePools").
|
||||
First(&t, "id = ? AND organization_id = ?", taintID, ac.OrganizationID).Error; err != nil {
|
||||
http.Error(w, "fetch failed", http.StatusInternalServerError)
|
||||
@@ -362,7 +411,7 @@ func AddTaintToNodePool(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
_ = response.JSON(w, http.StatusOK, toResp(t, includePools))
|
||||
_ = response.JSON(w, http.StatusOK, toResp(t, withPools))
|
||||
}
|
||||
|
||||
// RemoveTaintFromNodePool godoc
|
||||
@@ -426,7 +475,6 @@ func RemoveTaintFromNodePool(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "detach failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
response.NoContent(w)
|
||||
}
|
||||
|
||||
@@ -460,9 +508,10 @@ func ListNodePoolsWithTaint(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Ensure the taint exists and belongs to this org
|
||||
// Load the taint and its pools using GORM's mapping (avoids guessing join table name)
|
||||
var t models.Taint
|
||||
if err := db.DB.Where("id = ? AND organization_id = ?", taintID, ac.OrganizationID).
|
||||
Preload("NodePools").
|
||||
First(&t).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
http.Error(w, "not found", http.StatusNotFound)
|
||||
@@ -472,24 +521,18 @@ func ListNodePoolsWithTaint(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Build query for pools linked via join table
|
||||
q := db.DB.Model(&models.NodePool{}).
|
||||
Joins("JOIN taint_node_pools tnp ON tnp.node_pool_id = node_pools.id").
|
||||
Where("tnp.taint_id = ? AND node_pools.organization_id = ?", taintID, ac.OrganizationID)
|
||||
|
||||
if needle := strings.TrimSpace(r.URL.Query().Get("q")); needle != "" {
|
||||
q = q.Where("node_pools.name ILIKE ?", "%"+needle+"%")
|
||||
needle := strings.TrimSpace(r.URL.Query().Get("q"))
|
||||
out := make([]nodePoolResponse, 0, len(t.NodePools))
|
||||
for _, p := range t.NodePools {
|
||||
if needle != "" && !strings.Contains(strings.ToLower(p.Name), strings.ToLower(needle)) {
|
||||
continue
|
||||
}
|
||||
out = append(out, nodePoolResponse{
|
||||
ID: p.ID,
|
||||
Name: p.Name,
|
||||
// Servers intentionally omitted here; this endpoint doesn't include them by default.
|
||||
})
|
||||
}
|
||||
|
||||
var pools []models.NodePool
|
||||
if err := q.Order("node_pools.created_at DESC").Find(&pools).Error; err != nil {
|
||||
http.Error(w, "fetch failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// If you have a serializer like toNodePoolResp, use it; otherwise return models with JSON tags.
|
||||
//out := make([]nodePoolResponse, 0, len(pools))
|
||||
//for _, p := range pools { out = append(out, toNodePoolResp(p)) }
|
||||
|
||||
_ = response.JSON(w, http.StatusOK, pools)
|
||||
_ = response.JSON(w, http.StatusOK, out)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user