mirror of
https://github.com/openfaas/faas.git
synced 2025-06-08 16:26:47 +00:00
Return labels in functions list endpoint
Signed-off-by: Alex Ellis <alexellis2@gmail.com>
This commit is contained in:
parent
c1dfd9cfa2
commit
b2c579370a
@ -73,18 +73,30 @@ func makeSpec(request *requests.CreateFunctionRequest, maxRestarts uint64, resta
|
||||
} else {
|
||||
constraints = linuxOnlyConstraints
|
||||
}
|
||||
labels := map[string]string{"function": "true"}
|
||||
|
||||
labels := map[string]string{
|
||||
"com.openfaas.function": request.Service,
|
||||
"function": "true", // backwards-compatible
|
||||
}
|
||||
|
||||
if request.Labels != nil {
|
||||
for k, v := range *request.Labels {
|
||||
labels[k] = v
|
||||
}
|
||||
}
|
||||
fmt.Println(labels)
|
||||
|
||||
nets := []swarm.NetworkAttachmentConfig{
|
||||
{Target: request.Network},
|
||||
{
|
||||
Target: request.Network,
|
||||
},
|
||||
}
|
||||
|
||||
spec := swarm.ServiceSpec{
|
||||
Annotations: swarm.Annotations{
|
||||
Name: request.Service,
|
||||
Labels: labels,
|
||||
},
|
||||
TaskTemplate: swarm.TaskSpec{
|
||||
RestartPolicy: &swarm.RestartPolicy{
|
||||
MaxAttempts: &maxRestarts,
|
||||
@ -100,10 +112,6 @@ func makeSpec(request *requests.CreateFunctionRequest, maxRestarts uint64, resta
|
||||
Constraints: constraints,
|
||||
},
|
||||
},
|
||||
Annotations: swarm.Annotations{
|
||||
Name: request.Service,
|
||||
Labels: labels,
|
||||
},
|
||||
}
|
||||
|
||||
// TODO: request.EnvProcess should only be set if it's not nil, otherwise we override anything in the Docker image already
|
||||
|
@ -50,12 +50,15 @@ func MakeFunctionReader(metricsOptions metrics.MetricOptions, c client.ServiceAP
|
||||
}
|
||||
}
|
||||
|
||||
// Required (copy by value)
|
||||
labels := service.Spec.Annotations.Labels
|
||||
f := requests.Function{
|
||||
Name: service.Spec.Name,
|
||||
Image: service.Spec.TaskTemplate.ContainerSpec.Image,
|
||||
InvocationCount: 0,
|
||||
Replicas: *service.Spec.Mode.Replicated.Replicas,
|
||||
EnvProcess: envProcess,
|
||||
Labels: &labels,
|
||||
}
|
||||
|
||||
functions = append(functions, f)
|
||||
|
@ -81,33 +81,34 @@ func updateSpec(request *requests.CreateFunctionRequest, spec *swarm.ServiceSpec
|
||||
constraints = linuxOnlyConstraints
|
||||
}
|
||||
|
||||
nets := []swarm.NetworkAttachmentConfig{
|
||||
{Target: request.Network},
|
||||
}
|
||||
|
||||
spec.TaskTemplate.RestartPolicy.MaxAttempts = &maxRestarts
|
||||
spec.TaskTemplate.RestartPolicy.Condition = swarm.RestartPolicyConditionAny
|
||||
spec.TaskTemplate.RestartPolicy.Delay = &restartDelay
|
||||
spec.TaskTemplate.ContainerSpec.Image = request.Image
|
||||
spec.TaskTemplate.ContainerSpec.Labels = map[string]string{
|
||||
"function": "true",
|
||||
"uid": fmt.Sprintf("%d", time.Now().Nanosecond()),
|
||||
"function": "true",
|
||||
"com.openfaas.function": request.Service,
|
||||
"com.openfaas.uid": fmt.Sprintf("%d", time.Now().Nanosecond()),
|
||||
}
|
||||
|
||||
if request.Labels != nil {
|
||||
for k, v := range *request.Labels {
|
||||
spec.TaskTemplate.ContainerSpec.Labels[k] = v
|
||||
spec.Annotations.Labels[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
spec.TaskTemplate.Networks = nets
|
||||
spec.TaskTemplate.Networks = []swarm.NetworkAttachmentConfig{
|
||||
{
|
||||
Target: request.Network,
|
||||
},
|
||||
}
|
||||
|
||||
spec.TaskTemplate.Placement = &swarm.Placement{
|
||||
Constraints: constraints,
|
||||
}
|
||||
|
||||
spec.Annotations = swarm.Annotations{
|
||||
Name: request.Service,
|
||||
}
|
||||
spec.Annotations.Name = request.Service
|
||||
|
||||
spec.RollbackConfig = &swarm.UpdateConfig{
|
||||
FailureAction: "pause",
|
||||
|
@ -67,6 +67,10 @@ type Function struct {
|
||||
InvocationCount float64 `json:"invocationCount"` // TODO: shouldn't this be int64?
|
||||
Replicas uint64 `json:"replicas"`
|
||||
EnvProcess string `json:"envProcess"`
|
||||
|
||||
// Labels are metadata for functions which may be used by the
|
||||
// back-end for making scheduling or routing decisions
|
||||
Labels *map[string]string `json:"labels"`
|
||||
}
|
||||
|
||||
// AsyncReport is the report from a function executed on a queue worker.
|
||||
|
@ -116,6 +116,10 @@ func TestReaderSuccessReturnsCorrectBodyWithZeroFunctions(t *testing.T) {
|
||||
|
||||
func TestReaderSuccessReturnsCorrectBodyWithOneFunction(t *testing.T) {
|
||||
replicas := uint64(5)
|
||||
labels := map[string]string{
|
||||
"function": "bar",
|
||||
}
|
||||
|
||||
services := []swarm.Service{
|
||||
swarm.Service{
|
||||
Spec: swarm.ServiceSpec{
|
||||
@ -125,17 +129,16 @@ func TestReaderSuccessReturnsCorrectBodyWithOneFunction(t *testing.T) {
|
||||
},
|
||||
},
|
||||
Annotations: swarm.Annotations{
|
||||
Name: "bar",
|
||||
Name: "bar",
|
||||
Labels: labels,
|
||||
},
|
||||
TaskTemplate: swarm.TaskSpec{
|
||||
ContainerSpec: swarm.ContainerSpec{
|
||||
Env: []string{
|
||||
"fprocess=bar",
|
||||
},
|
||||
Image: "foo/bar:latest",
|
||||
Labels: map[string]string{
|
||||
"function": "bar",
|
||||
},
|
||||
Image: "foo/bar:latest",
|
||||
Labels: labels,
|
||||
},
|
||||
},
|
||||
},
|
||||
@ -159,6 +162,9 @@ func TestReaderSuccessReturnsCorrectBodyWithOneFunction(t *testing.T) {
|
||||
InvocationCount: 0,
|
||||
Replicas: 5,
|
||||
EnvProcess: "bar",
|
||||
Labels: &map[string]string{
|
||||
"function": "bar",
|
||||
},
|
||||
},
|
||||
}
|
||||
marshalled, _ := json.Marshal(functions)
|
||||
|
Loading…
x
Reference in New Issue
Block a user