Skip to content

Commit

Permalink
feat: add GET cluster-scoped CRs (#165)
Browse files Browse the repository at this point in the history
* get CRs for cluster-scoped resources
* get resource update
  • Loading branch information
nvanthao authored Jul 25, 2024
1 parent 16985f8 commit e49f9b5
Showing 1 changed file with 45 additions and 6 deletions.
51 changes: 45 additions & 6 deletions pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -930,12 +930,20 @@ func (h handler) getAPIsClusterResources(w http.ResponseWriter, r *http.Request)
dirName = filepath.Join(h.clusterData.ClusterResourcesDir, "custom-resources", g)
}

filenames, err = getJSONFileListFromDir(dirName)
if err != nil {
log.Errorf("failed to get %s files from dir: %v\n", resource, err)
w.WriteHeader(http.StatusInternalServerError)
return
filenames, _ = getJSONFileListFromDir(dirName)

// cluster-scoped resources have no directory
// e.g.
/* ├── clusterconfigs.k0s.k0sproject.io
│   ├── kube-system.json
│   └── kube-system.yaml
├── installations.embeddedcluster.replicated.com.json
├── installations.embeddedcluster.replicated.com.yaml */
if len(filenames) == 0 {
filename := dirName + ".json"
filenames = append(filenames, filename)
}

}

for _, fileName := range filenames {
Expand Down Expand Up @@ -1050,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 @@ -1062,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 @@ -1084,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 e49f9b5

Please sign in to comment.