github.com/kiali/kiali@v1.84.0/handlers/namespaces.go (about) 1 package handlers 2 3 import ( 4 "io" 5 "net/http" 6 "strings" 7 8 "github.com/gorilla/mux" 9 10 "github.com/kiali/kiali/log" 11 "github.com/kiali/kiali/models" 12 ) 13 14 func NamespaceList(w http.ResponseWriter, r *http.Request) { 15 business, err := getBusiness(r) 16 if err != nil { 17 log.Error(err) 18 RespondWithError(w, http.StatusInternalServerError, err.Error()) 19 return 20 } 21 22 namespaces, err := business.Namespace.GetNamespaces(r.Context()) 23 if err != nil { 24 log.Error(err) 25 RespondWithError(w, http.StatusInternalServerError, err.Error()) 26 return 27 } 28 29 RespondWithJSON(w, http.StatusOK, namespaces) 30 } 31 32 func NamespaceInfo(w http.ResponseWriter, r *http.Request) { 33 query := r.URL.Query() 34 vars := mux.Vars(r) 35 namespace := vars["namespace"] 36 cluster := clusterNameFromQuery(query) 37 38 business, err := getBusiness(r) 39 if err != nil { 40 log.Error(err) 41 RespondWithError(w, http.StatusInternalServerError, err.Error()) 42 return 43 } 44 45 namespaceInfo, err := business.Namespace.GetClusterNamespace(r.Context(), namespace, cluster) 46 if err != nil { 47 log.Error(err) 48 RespondWithError(w, http.StatusInternalServerError, err.Error()) 49 return 50 } 51 52 RespondWithJSON(w, http.StatusOK, namespaceInfo) 53 } 54 55 // NamespaceValidationSummary is the API handler to fetch validations summary to be displayed. 56 // It is related to all the Istio Objects within the namespace 57 func NamespaceValidationSummary(w http.ResponseWriter, r *http.Request) { 58 query := r.URL.Query() 59 vars := mux.Vars(r) 60 namespace := vars["namespace"] 61 62 cluster := clusterNameFromQuery(query) 63 64 business, err := getBusiness(r) 65 if err != nil { 66 log.Error(err) 67 RespondWithError(w, http.StatusInternalServerError, err.Error()) 68 return 69 } 70 71 var validationSummary models.IstioValidationSummary 72 istioConfigValidationResults := models.IstioValidations{} 73 var errValidations error 74 75 // If cluster is not set, is because we need a unified validations view (E.g. in the Summary graph) 76 clusters, _ := business.Mesh.GetClusters() 77 if len(clusters) == 1 { 78 istioConfigValidationResults, errValidations = business.Validations.GetValidations(r.Context(), cluster, namespace, "", "") 79 } else { 80 for _, cl := range clusters { 81 _, errNs := business.Namespace.GetClusterNamespace(r.Context(), namespace, cl.Name) 82 if errNs == nil { 83 clusterIstioConfigValidationResults, _ := business.Validations.GetValidations(r.Context(), cl.Name, namespace, "", "") 84 istioConfigValidationResults = istioConfigValidationResults.MergeValidations(clusterIstioConfigValidationResults) 85 } 86 } 87 } 88 89 if errValidations != nil { 90 log.Error(errValidations) 91 RespondWithError(w, http.StatusInternalServerError, errValidations.Error()) 92 } else { 93 validationSummary = *istioConfigValidationResults.SummarizeValidation(namespace, cluster) 94 } 95 96 RespondWithJSON(w, http.StatusOK, validationSummary) 97 } 98 99 // ConfigValidationSummary is the API handler to fetch validations summary to be displayed. 100 // It is related to all the Istio Objects within given namespaces 101 func ConfigValidationSummary(w http.ResponseWriter, r *http.Request) { 102 params := r.URL.Query() 103 namespaces := params.Get("namespaces") // csl of namespaces 104 nss := []string{} 105 if len(namespaces) > 0 { 106 nss = strings.Split(namespaces, ",") 107 } 108 cluster := clusterNameFromQuery(params) 109 110 business, err := getBusiness(r) 111 if err != nil { 112 log.Error(err) 113 RespondWithError(w, http.StatusInternalServerError, err.Error()) 114 return 115 } 116 117 if len(nss) == 0 { 118 loadedNamespaces, _ := business.Namespace.GetClusterNamespaces(r.Context(), cluster) 119 for _, ns := range loadedNamespaces { 120 nss = append(nss, ns.Name) 121 } 122 } 123 124 istioConfigValidationResults, errValidations := business.Validations.GetValidations(r.Context(), cluster, "", "", "") 125 if errValidations != nil { 126 log.Error(errValidations) 127 RespondWithError(w, http.StatusInternalServerError, errValidations.Error()) 128 return 129 } 130 131 validationSummaries := []models.IstioValidationSummary{} 132 for _, ns := range nss { 133 validationSummaries = append(validationSummaries, *istioConfigValidationResults.SummarizeValidation(ns, cluster)) 134 } 135 136 RespondWithJSON(w, http.StatusOK, validationSummaries) 137 } 138 139 // NamespaceUpdate is the API to perform a patch on a Namespace configuration 140 func NamespaceUpdate(w http.ResponseWriter, r *http.Request) { 141 params := mux.Vars(r) 142 business, err := getBusiness(r) 143 if err != nil { 144 RespondWithError(w, http.StatusInternalServerError, "Namespace initialization error: "+err.Error()) 145 return 146 } 147 namespace := params["namespace"] 148 body, err := io.ReadAll(r.Body) 149 if err != nil { 150 RespondWithError(w, http.StatusBadRequest, "Update request with bad update patch: "+err.Error()) 151 } 152 jsonPatch := string(body) 153 154 query := r.URL.Query() 155 cluster := clusterNameFromQuery(query) 156 157 ns, err := business.Namespace.UpdateNamespace(r.Context(), namespace, jsonPatch, cluster) 158 if err != nil { 159 handleErrorResponse(w, err) 160 return 161 } 162 audit(r, "UPDATE on Namespace: "+namespace+" Patch: "+jsonPatch) 163 RespondWithJSON(w, http.StatusOK, ns) 164 }