/
util.go
144 lines (126 loc) · 4.03 KB
/
util.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
/*
Copyright 2021.
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
http://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 webhook
import (
"context"
"errors"
"strconv"
"strings"
"text/template"
configv1 "github.com/tinyzimmer/caddy-injector/api/v1"
"github.com/Masterminds/sprig"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func shouldInject(pod *corev1.Pod) bool {
if annotations := pod.GetAnnotations(); annotations != nil {
_, ok := annotations[InjectAnnotation]
return ok
}
return false
}
func getDNSNames(pod *corev1.Pod) []string {
if annotations := pod.GetAnnotations(); annotations != nil {
if names, ok := annotations[DNSNamesAnnotation]; ok {
return strings.Split(names, ",")
}
}
return []string{"localhost"}
}
func getHTTPSPort(pod *corev1.Pod) int32 {
if annotations := pod.GetAnnotations(); annotations != nil {
if port, ok := annotations[HTTPSPortAnnotation]; ok {
p, err := strconv.Atoi(port)
if err == nil {
return int32(p)
}
webhookLog.Error(err, "Could not coerce port annotation to integer, falling back to default")
}
}
return defaultHTTPSPort
}
func getHTTPPort(pod *corev1.Pod) (int32, error) {
// Check for an annotation override
if annotations := pod.GetAnnotations(); annotations != nil {
if port, ok := annotations[HTTPPortAnnotation]; ok {
p, err := strconv.Atoi(port)
return int32(p), err
}
}
// If there is only one container and port, use that
if len(pod.Spec.Containers) == 1 && len(pod.Spec.Containers[0].Ports) == 1 {
return pod.Spec.Containers[0].Ports[0].ContainerPort, nil
}
// Look for a port named "http"
for _, container := range pod.Spec.Containers {
for _, port := range container.Ports {
if strings.ToLower(port.Name) == "http" {
return port.ContainerPort, nil
}
}
}
// Give up
return 0, errors.New("could not determine HTTP port for pod")
}
func getCaddyfileTemplate(ctx context.Context, c client.Client, pod *corev1.Pod) (*template.Template, error) {
if annotations := pod.GetAnnotations(); annotations != nil {
name := pod.GetName()
if name == "" {
name = pod.GenerateName
}
if inlineTemplate, ok := annotations[InlineCaddyfileAnnotation]; ok {
return template.New(name).Funcs(sprig.TxtFuncMap()).Parse(inlineTemplate)
}
if crTemplate, ok := annotations[CaddyfileTemplateAnnotation]; ok {
var tmpl configv1.CaddyfileTemplate
if err := c.Get(ctx, types.NamespacedName{Name: crTemplate, Namespace: metav1.NamespaceAll}, &tmpl); err != nil {
return nil, err
}
return template.New(name).Funcs(sprig.TxtFuncMap()).Parse(tmpl.Data)
}
}
return defaultCaddyfileTmpl, nil
}
func getIssuerKind(pod *corev1.Pod) string {
annotations := pod.GetAnnotations()
if _, ok := annotations[CertManagerIssuerAnnotation]; ok {
return "Issuer"
}
// Assume cluster issuer since a check was already performed for one
// of the annotations existing.
return "ClusterIssuer"
}
func getIssuerName(pod *corev1.Pod) string {
annotations := pod.GetAnnotations()
if issuer, ok := annotations[CertManagerIssuerAnnotation]; ok {
return issuer
}
if clusterIssuer, ok := annotations[CertManagerClusterIssuerAnnotation]; ok {
return clusterIssuer
}
// Shouldn't fire since one of the two annotations exists already
return ""
}
func useCertManager(pod *corev1.Pod) bool {
if annotations := pod.GetAnnotations(); annotations != nil {
if _, ok := annotations[CertManagerIssuerAnnotation]; ok {
return true
}
if _, ok := annotations[CertManagerClusterIssuerAnnotation]; ok {
return true
}
}
return false
}