-
Notifications
You must be signed in to change notification settings - Fork 479
/
Copy pathreplica_reader.go
106 lines (82 loc) · 2.74 KB
/
replica_reader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// License: OpenFaaS Community Edition (CE) EULA
// Copyright (c) 2017,2019-2024 OpenFaaS Author(s)
// Copyright (c) Alex Ellis 2017. All rights reserved.
// Copyright 2020 OpenFaaS Author(s)
package handlers
import (
"encoding/json"
"fmt"
"log"
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/openfaas/faas-netes/pkg/k8s"
types "github.com/openfaas/faas-provider/types"
"k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/client-go/listers/apps/v1"
klog "k8s.io/klog"
)
// MaxReplicas licensed for OpenFaaS CE is 5/5
// a license for OpenFaaS Standard is required to increase this limit.
const MaxReplicas = 5
// MaxFunctions licensed for OpenFaaS CE is 15
// a license for OpenFaaS Standard is required to increase this limit.
const MaxFunctions = 15
// MakeReplicaReader reads the amount of replicas for a deployment
func MakeReplicaReader(defaultNamespace string, lister v1.DeploymentLister) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
functionName := vars["name"]
q := r.URL.Query()
namespace := q.Get("namespace")
lookupNamespace := defaultNamespace
if len(namespace) > 0 {
lookupNamespace = namespace
}
if lookupNamespace != defaultNamespace {
http.Error(w, fmt.Sprintf("namespace must be: %s", defaultNamespace), http.StatusBadRequest)
return
}
s := time.Now()
function, err := getService(lookupNamespace, functionName, lister)
if err != nil {
log.Printf("Unable to fetch service: %s", functionName)
w.WriteHeader(http.StatusInternalServerError)
http.Error(w, fmt.Sprintf("Unable to fetch service: %s", functionName), http.StatusInternalServerError)
return
}
if function == nil {
w.WriteHeader(http.StatusNotFound)
return
}
d := time.Since(s)
log.Printf("Replicas: %s.%s, (%d/%d) %dms", functionName, lookupNamespace, function.AvailableReplicas, function.Replicas, d.Milliseconds())
functionBytes, err := json.Marshal(function)
if err != nil {
klog.Errorf("Failed to marshal function: %s", err.Error())
http.Error(w, "Failed to marshal function", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(functionBytes)
}
}
// getService returns a function/service or nil if not found
func getService(functionNamespace string, functionName string, lister v1.DeploymentLister) (*types.FunctionStatus, error) {
item, err := lister.Deployments(functionNamespace).
Get(functionName)
if err != nil {
if errors.IsNotFound(err) {
return nil, nil
}
return nil, err
}
if item != nil {
function := k8s.AsFunctionStatus(*item)
if function != nil {
return function, nil
}
}
return nil, fmt.Errorf("function: %s not found", functionName)
}