-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_defaults.go
151 lines (126 loc) · 4.54 KB
/
app_defaults.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package v1alpha1
import (
"context"
"encoding/json"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"knative.dev/pkg/apis"
)
var (
defaultMem = resource.MustParse("1Gi")
defaultStorage = resource.MustParse("1Gi")
// CPU isn't defaulted in Cloud Foundry so we assume apps are I/O bound
// and roughly 10 should run on a single core machine.
defaultCPU = resource.MustParse("100m")
)
const (
// DefaultHealthCheckProbeTimeout holds the default timeout to be applied to
// healthchecks in seconds. This matches Cloud Foundry's default timeout.
DefaultHealthCheckProbeTimeout = 60
// DefaultHealthCheckProbeEndpoint is the default endpoint to use for HTTP
// Get health checks.
DefaultHealthCheckProbeEndpoint = "/"
)
// SetDefaults implements apis.Defaultable
func (k *App) SetDefaults(ctx context.Context) {
k.Spec.SetDefaults(ctx)
}
// SetDefaults implements apis.Defaultable
func (k *AppSpec) SetDefaults(ctx context.Context) {
k.SetSourceDefaults(ctx)
k.Template.SetDefaults(ctx)
k.SetServiceBindingDefaults(ctx)
}
// SetSourceDefaults implements apis.Defaultable for the embedded SourceSpec.
func (k *AppSpec) SetSourceDefaults(ctx context.Context) {
// If the app source has changed without changing the UpdateRequests,
// update it.
if base := apis.GetBaseline(ctx); base != nil {
if old, ok := base.(*App); ok {
// If the update is a post rather than a patch, pick up where the last
// source left off.
if k.Source.UpdateRequests == 0 {
k.Source.UpdateRequests = old.Spec.Source.UpdateRequests
}
if k.Source.NeedsUpdateRequestsIncrement(old.Spec.Source) {
k.Source.UpdateRequests++
}
}
}
}
// SetServiceBindingDefaults sets the defaults for an AppSpec's ServiceBindings.
func (k *AppSpec) SetServiceBindingDefaults(ctx context.Context) {
for i := range k.ServiceBindings {
binding := &k.ServiceBindings[i]
binding.SetDefaults(ctx)
}
}
// SetDefaults sets the defaults for an AppSpecServiceBinding.
func (k *AppSpecServiceBinding) SetDefaults(ctx context.Context) {
if k.BindingName == "" {
k.BindingName = k.Instance
}
if string(k.Parameters) == "" {
k.Parameters = json.RawMessage("null")
}
}
// SetDefaults implements apis.Defaultable
func (k *AppSpecTemplate) SetDefaults(ctx context.Context) {
// We require at least one container, so if there isn't one, set a blank
// one.
if len(k.Spec.Containers) == 0 {
k.Spec.Containers = append(k.Spec.Containers, corev1.Container{})
}
container := &k.Spec.Containers[0]
SetKfAppContainerDefaults(ctx, container)
}
// SetKfAppContainerDefaults sets the defaults for an application container.
// This function MAY be context sensitive in the future.
func SetKfAppContainerDefaults(_ context.Context, container *corev1.Container) {
// Default the probe to a TCP connection if unspecified
if container.ReadinessProbe == nil {
container.ReadinessProbe = &corev1.Probe{
Handler: corev1.Handler{
TCPSocket: &corev1.TCPSocketAction{},
},
}
}
readinessProbe := container.ReadinessProbe
// Default the probe timeout
if readinessProbe.TimeoutSeconds == 0 {
readinessProbe.TimeoutSeconds = DefaultHealthCheckProbeTimeout
}
// If the probe is HTTP, default the path
if http := readinessProbe.HTTPGet; http != nil {
if http.Path == "" {
http.Path = DefaultHealthCheckProbeEndpoint
}
}
// Set default disk, RAM, and CPU limits on the application if they have not been custom set
if container.Resources.Requests == nil {
container.Resources.Requests = v1.ResourceList{}
}
if _, exists := container.Resources.Requests[corev1.ResourceMemory]; !exists {
container.Resources.Requests[corev1.ResourceMemory] = defaultMem
}
if _, exists := container.Resources.Requests[corev1.ResourceEphemeralStorage]; !exists {
container.Resources.Requests[corev1.ResourceEphemeralStorage] = defaultStorage
}
if _, exists := container.Resources.Requests[corev1.ResourceCPU]; !exists {
container.Resources.Requests[corev1.ResourceCPU] = defaultCPU
}
}