feature: Added command to list pods managed by fission for environment/function (#2207)

This commit is contained in:
Pradeep Lakshmi Narasimha
2021-10-10 10:52:42 +05:30
committed by GitHub
parent e930a2922c
commit c9da527d37
16 changed files with 403 additions and 32 deletions
+2
View File
@@ -214,6 +214,7 @@ func (api *API) GetHandler() http.Handler {
r.HandleFunc("/v2/functions/{function}", api.FunctionApiGet).Methods("GET")
r.HandleFunc("/v2/functions/{function}", api.FunctionApiUpdate).Methods("PUT")
r.HandleFunc("/v2/functions/{function}", api.FunctionApiDelete).Methods("DELETE")
r.HandleFunc("/v2/functions/{function}/pods", api.FunctionApiPodList).Methods("GET")
r.HandleFunc("/v2/triggers/http", api.HTTPTriggerApiList).Methods("GET")
r.HandleFunc("/v2/triggers/http", api.HTTPTriggerApiCreate).Methods("POST")
@@ -226,6 +227,7 @@ func (api *API) GetHandler() http.Handler {
r.HandleFunc("/v2/environments/{environment}", api.EnvironmentApiGet).Methods("GET")
r.HandleFunc("/v2/environments/{environment}", api.EnvironmentApiUpdate).Methods("PUT")
r.HandleFunc("/v2/environments/{environment}", api.EnvironmentApiDelete).Methods("DELETE")
r.HandleFunc("/v2/environments/{environment}/pods", api.EnvironmentApiPodList).Methods("GET")
r.HandleFunc("/v2/watches", api.WatchApiList).Methods("GET")
r.HandleFunc("/v2/watches", api.WatchApiCreate).Methods("POST")
+39 -2
View File
@@ -19,12 +19,13 @@ package v1
import (
"encoding/json"
"fmt"
"net/url"
"github.com/fission/fission/pkg/controller/client/rest"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
fv1 "github.com/fission/fission/pkg/apis/core/v1"
"github.com/fission/fission/pkg/controller/client/rest"
"github.com/fission/fission/pkg/generator/encoder"
v1generator "github.com/fission/fission/pkg/generator/v1"
)
@@ -40,6 +41,7 @@ type (
Update(env *fv1.Environment) (*metav1.ObjectMeta, error)
Delete(m *metav1.ObjectMeta) error
List(ns string) ([]fv1.Environment, error)
ListPods(m *metav1.ObjectMeta) ([]apiv1.Pod, error)
}
Environment struct {
@@ -163,3 +165,38 @@ func (c *Environment) List(ns string) ([]fv1.Environment, error) {
return envs, nil
}
func (c *Environment) ListPods(m *metav1.ObjectMeta) ([]apiv1.Pod, error) {
relativeUrl := fmt.Sprintf("environments/%s/pods", m.Name)
values := url.Values{}
if len(m.Labels) != 0 {
if envns, ok := m.Labels[fv1.ENVIRONMENT_NAMESPACE]; ok && len(envns) != 0 {
values.Add(fv1.ENVIRONMENT_NAMESPACE, envns)
}
if extype, ok := m.Labels[fv1.EXECUTOR_TYPE]; ok && len(extype) != 0 {
values.Add(fv1.EXECUTOR_TYPE, extype)
}
}
relativeUrl = fmt.Sprintf("%s?%s", relativeUrl, values.Encode())
resp, err := c.client.Get(relativeUrl)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := handleResponse(resp)
if err != nil {
return nil, err
}
pods := make([]apiv1.Pod, 0)
err = json.Unmarshal(body, &pods)
if err != nil {
return nil, err
}
return pods, nil
}
@@ -17,6 +17,7 @@ limitations under the License.
package fake
import (
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
fv1 "github.com/fission/fission/pkg/apis/core/v1"
@@ -50,3 +51,7 @@ func (c *FakeEnvironment) Delete(m *metav1.ObjectMeta) error {
func (c *FakeEnvironment) List(ns string) ([]fv1.Environment, error) {
return nil, nil
}
func (c *FakeEnvironment) ListPods(m *metav1.ObjectMeta) ([]apiv1.Pod, error) {
return nil, nil
}
@@ -17,6 +17,7 @@ limitations under the License.
package fake
import (
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
fv1 "github.com/fission/fission/pkg/apis/core/v1"
@@ -54,3 +55,7 @@ func (c *FakeFunction) Delete(m *metav1.ObjectMeta) error {
func (c *FakeFunction) List(functionNamespace string) ([]fv1.Function, error) {
return nil, nil
}
func (c *FakeFunction) ListPods(m *metav1.ObjectMeta) ([]apiv1.Pod, error) {
return nil, nil
}
+34
View File
@@ -19,7 +19,9 @@ package v1
import (
"encoding/json"
"fmt"
"net/url"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
fv1 "github.com/fission/fission/pkg/apis/core/v1"
@@ -38,6 +40,7 @@ type (
Update(f *fv1.Function) (*metav1.ObjectMeta, error)
Delete(m *metav1.ObjectMeta) error
List(functionNamespace string) ([]fv1.Function, error)
ListPods(m *metav1.ObjectMeta) ([]apiv1.Pod, error)
}
Function struct {
@@ -176,3 +179,34 @@ func (c *Function) List(functionNamespace string) ([]fv1.Function, error) {
return funcs, nil
}
func (c *Function) ListPods(m *metav1.ObjectMeta) ([]apiv1.Pod, error) {
relativeUrl := fmt.Sprintf("functions/%s/pods", m.Name)
values := url.Values{}
if len(m.Labels) != 0 {
if fns, ok := m.Labels[fv1.FUNCTION_NAMESPACE]; ok && len(fns) != 0 {
values.Add(fv1.FUNCTION_NAMESPACE, fns)
}
}
relativeUrl = fmt.Sprintf("%s?%s", relativeUrl, values.Encode())
resp, err := c.client.Get(relativeUrl)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := handleResponse(resp)
if err != nil {
return nil, err
}
pods := make([]apiv1.Pod, 0)
err = json.Unmarshal(body, &pods)
if err != nil {
return nil, err
}
return pods, nil
}
+43
View File
@@ -27,8 +27,10 @@ import (
"github.com/gorilla/mux"
"go.uber.org/zap"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
fv1 "github.com/fission/fission/pkg/apis/core/v1"
v1 "github.com/fission/fission/pkg/apis/core/v1"
ferror "github.com/fission/fission/pkg/error"
)
@@ -237,3 +239,44 @@ func (a *API) EnvironmentApiDelete(w http.ResponseWriter, r *http.Request) {
a.respondWithSuccess(w, []byte(""))
}
// EnvironmentApiPodList: Get list of pods currently available in environment
func (a *API) EnvironmentApiPodList(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
envName, ok := vars["environment"]
if !ok {
a.respondWithError(w, ferror.MakeError(http.StatusInternalServerError, "Error retrieving environment name"))
return
}
// label selector
selector := map[string]string{
fv1.ENVIRONMENT_NAME: envName,
}
ens := a.extractQueryParamFromRequest(r, v1.ENVIRONMENT_NAMESPACE)
if len(ens) != 0 {
selector[fv1.ENVIRONMENT_NAMESPACE] = ens
}
et := a.extractQueryParamFromRequest(r, v1.EXECUTOR_TYPE)
if len(et) != 0 {
selector[fv1.EXECUTOR_TYPE] = et
}
pods, err := a.kubernetesClient.CoreV1().Pods(metav1.NamespaceAll).List(r.Context(), metav1.ListOptions{
LabelSelector: labels.Set(selector).AsSelector().String(),
})
if err != nil {
a.respondWithError(w, err)
return
}
resp, err := json.Marshal(pods.Items)
if err != nil {
a.respondWithError(w, err)
return
}
a.respondWithSuccess(w, resp)
}
+37
View File
@@ -41,6 +41,7 @@ import (
"k8s.io/client-go/kubernetes"
fv1 "github.com/fission/fission/pkg/apis/core/v1"
v1 "github.com/fission/fission/pkg/apis/core/v1"
ferror "github.com/fission/fission/pkg/error"
)
@@ -369,3 +370,39 @@ func getContainerLog(kubernetesClient *kubernetes.Clientset, w http.ResponseWrit
return nil
}
// FunctionApiPodList: Get list of pods currently used by function
func (a *API) FunctionApiPodList(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
fnName, ok := vars["function"]
if !ok {
a.respondWithError(w, ferror.MakeError(http.StatusInternalServerError, "Error retrieving function name"))
return
}
// label selector
selector := map[string]string{
fv1.FUNCTION_NAME: fnName,
}
fns := a.extractQueryParamFromRequest(r, v1.FUNCTION_NAMESPACE)
if len(fns) != 0 {
selector[fv1.FUNCTION_NAMESPACE] = fns
}
pods, err := a.kubernetesClient.CoreV1().Pods(metav1.NamespaceAll).List(r.Context(), metav1.ListOptions{
LabelSelector: labels.Set(selector).AsSelector().String(),
})
if err != nil {
a.respondWithError(w, err)
return
}
resp, err := json.Marshal(pods.Items)
if err != nil {
a.respondWithError(w, err)
return
}
a.respondWithSuccess(w, resp)
}