Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for namespace to commands #519

Merged
merged 1 commit into from
Sep 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions handlers/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,23 @@ import (
)

// MakeDeleteHandler delete a function
func MakeDeleteHandler(functionNamespace string, clientset *kubernetes.Clientset) http.HandlerFunc {
func MakeDeleteHandler(defaultNamespace string, clientset *kubernetes.Clientset) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()

q := r.URL.Query()
namespace := q.Get("namespace")

lookupNamespace := defaultNamespace

if len(namespace) > 0 {
lookupNamespace = namespace
}

if lookupNamespace == "kube-system" {
http.Error(w, "unable to list within the kube-system namespace", http.StatusUnauthorized)
}

body, _ := ioutil.ReadAll(r.Body)

request := requests.DeleteFunctionRequest{}
Expand All @@ -37,7 +50,7 @@ func MakeDeleteHandler(functionNamespace string, clientset *kubernetes.Clientset

// This makes sure we don't delete non-labelled deployments
deployment, findDeployErr := clientset.AppsV1().
Deployments(functionNamespace).
Deployments(lookupNamespace).
Get(request.FunctionName, getOpts)

if findDeployErr != nil {
Expand All @@ -52,7 +65,7 @@ func MakeDeleteHandler(functionNamespace string, clientset *kubernetes.Clientset
}

if isFunction(deployment) {
deleteFunction(functionNamespace, clientset, request, w)
deleteFunction(lookupNamespace, clientset, request, w)
} else {
w.WriteHeader(http.StatusBadRequest)

Expand Down
16 changes: 14 additions & 2 deletions handlers/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,22 @@ import (
)

// MakeFunctionReader handler for reading functions deployed in the cluster as deployments.
func MakeFunctionReader(functionNamespace string, clientset *kubernetes.Clientset) http.HandlerFunc {
func MakeFunctionReader(defaultNamespace string, clientset *kubernetes.Clientset) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
namespace := q.Get("namespace")

functions, err := getServiceList(functionNamespace, clientset)
lookupNamespace := defaultNamespace

if len(namespace) > 0 {
lookupNamespace = namespace
}

if lookupNamespace == "kube-system" {
http.Error(w, "unable to list within the kube-system namespace", http.StatusUnauthorized)
}

functions, err := getServiceList(lookupNamespace, clientset)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
Expand Down
20 changes: 16 additions & 4 deletions handlers/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,23 @@ import (
)

// MakeUpdateHandler update specified function
func MakeUpdateHandler(functionNamespace string, factory k8s.FunctionFactory) http.HandlerFunc {
func MakeUpdateHandler(defaultNamespace string, factory k8s.FunctionFactory) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {

defer r.Body.Close()

q := r.URL.Query()
namespace := q.Get("namespace")

lookupNamespace := defaultNamespace

if len(namespace) > 0 {
lookupNamespace = namespace
}

if lookupNamespace == "kube-system" {
http.Error(w, "unable to list within the kube-system namespace", http.StatusUnauthorized)
}

body, _ := ioutil.ReadAll(r.Body)

request := types.FunctionDeployment{}
Expand All @@ -33,12 +45,12 @@ func MakeUpdateHandler(functionNamespace string, factory k8s.FunctionFactory) ht
}

annotations := buildAnnotations(request)
if err, status := updateDeploymentSpec(functionNamespace, factory, request, annotations); err != nil {
if err, status := updateDeploymentSpec(lookupNamespace, factory, request, annotations); err != nil {
w.WriteHeader(status)
w.Write([]byte(err.Error()))
}

if err, status := updateService(functionNamespace, factory, request, annotations); err != nil {
if err, status := updateService(lookupNamespace, factory, request, annotations); err != nil {
w.WriteHeader(status)
w.Write([]byte(err.Error()))
}
Expand Down