Skip to content

Commit

Permalink
get resource update
Browse files Browse the repository at this point in the history
  • Loading branch information
nvanthao committed Jul 25, 2024
1 parent cb6416f commit 03744c6
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,25 @@ func (h handler) getAPIsClusterResource(w http.ResponseWriter, r *http.Request)

resource := mux.Vars(r)["resource"]
name := mux.Vars(r)["name"]
group := mux.Vars(r)["group"]
asTable := strings.Contains(r.Header.Get("Accept"), "as=Table") // who needs parsing
setResponse := func(d runtime.Object) {
if asTable {
table, err := toTable(d, r)
if err != nil {
log.Warn("could not convert to table: ", err)
} else {
d = table
}
}
JSON(w, http.StatusOK, d)
}
fileName := filepath.Join(h.clusterData.ClusterResourcesDir, fmt.Sprintf("%s.json", sbctlutil.GetSBCompatibleResourceName(resource)))

if !fileExists(fileName) {
fileName = filepath.Join(h.clusterData.ClusterResourcesDir, "custom-resources", fmt.Sprintf("%s.%s.json", resource, group))
}

data, err := readFileAndLog(fileName)
if err != nil {
log.Error("failed to load file", err)
Expand All @@ -1070,7 +1088,7 @@ func (h handler) getAPIsClusterResource(w http.ResponseWriter, r *http.Request)
return
}

decoded, _, err := sbctl.Decode(resource, data)
decoded, gvk, err := sbctl.Decode(resource, data)
if err != nil {
log.Error("failed to decode wrapped", resource, ":", err)
w.WriteHeader(http.StatusInternalServerError)
Expand All @@ -1092,6 +1110,19 @@ func (h handler) getAPIsClusterResource(w http.ResponseWriter, r *http.Request)
return
}
}
default:
uObjList, err := sbctl.ToUnstructuredList(decoded)
if err != nil {
log.Error("failed to convert type to unstructured: ", gvk)
return
}
for _, item := range uObjList.Items {
if item.GetName() == name {
item := item
setResponse(&item)
return
}
}
}
JSON(w, http.StatusNotFound, errorNotFound)
}
Expand Down

0 comments on commit 03744c6

Please sign in to comment.