-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathm_tenantgateway.go
118 lines (105 loc) · 3.77 KB
/
m_tenantgateway.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
// Copyright 2022 The kubegems.io Authors
//
// 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 webhooks
import (
"context"
"encoding/json"
"net/http"
v1 "k8s.io/api/admission/v1"
corev1 "k8s.io/api/core/v1"
gemlabels "kubegems.io/kubegems/pkg/apis/gems"
gemsv1beta1 "kubegems.io/kubegems/pkg/apis/gems/v1beta1"
"kubegems.io/kubegems/pkg/apis/networking"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
func (r *ResourceMutate) MutateTenantGateway(ctx context.Context, req admission.Request) admission.Response {
tg := &gemsv1beta1.TenantGateway{}
err := r.decoder.Decode(req, tg)
if err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
// ingressclassgvk, err := r.Client.RESTMapper().ResourceFor(schema.GroupVersionResource{
// Group: "networking.k8s.io",
// Resource: "ingressclasses",
// })
// if err != nil {
// return admission.Denied(fmt.Sprintf("get ingressclass gvk failed: %v", err))
// }
// // https://github.com/nginxinc/kubernetes-ingress/issues/1832
// tag := "1.11.1"
// if ingressclassgvk.Version == "v1" {
// // https://github.com/nginxinc/kubernetes-ingress/releases/tag/v2.0.0
// // This is the first version to support ingressclass v1
// // Don't upgrate it, or you'll get error like nginx.conf template not valid
// tag = "2.0.0"
// }
// r.Log.Info(fmt.Sprintf("use tag: %s because of ingressclass version is: %s", tag, ingressclassgvk.Version))
switch req.Operation {
case v1.Create, v1.Update:
tgDefault(tg, r.Repo, "v1.3.0")
modifyed, _ := json.Marshal(tg)
return admission.PatchResponseFromRaw(req.Object.Raw, modifyed)
default:
return admission.Allowed("pass")
}
}
func tgDefault(tg *gemsv1beta1.TenantGateway, repo, tag string) {
defaultReplicas := int32(1)
if tg.Labels == nil {
tg.Labels = make(map[string]string)
}
if tg.Labels[gemlabels.LabelTenant] == "" {
tg.Labels[gemlabels.LabelTenant] = tg.Spec.Tenant
}
if tg.Labels[networking.LabelIngressClass] == "" {
tg.Labels[networking.LabelIngressClass] = tg.Name + "-" + tg.Spec.Tenant
}
if tg.Spec.Replicas == nil || *tg.Spec.Replicas <= 0 {
tg.Spec.Replicas = &defaultReplicas
}
if tg.Spec.Type == "" {
tg.Spec.Type = corev1.ServiceTypeNodePort
}
if tg.Spec.IngressClass == "" {
tg.Spec.IngressClass = tg.Name + "-" + tg.Spec.Tenant
}
// nginx的pod上需添加租户信息,用于日志收集
if tg.Spec.Workload == nil {
tg.Spec.Workload = &gemsv1beta1.Workload{}
}
if tg.Spec.Workload.ExtraLabels == nil {
tg.Spec.Workload.ExtraLabels = make(map[string]string)
}
if tg.Spec.Workload.ExtraLabels[gemlabels.LabelTenant] == "" {
tg.Spec.Workload.ExtraLabels[gemlabels.LabelTenant] = tg.Spec.Tenant
}
if tg.Spec.Workload.ExtraLabels[gemlabels.LabelApplication] == "" {
tg.Spec.Workload.ExtraLabels[gemlabels.LabelApplication] = tg.Name
}
if tg.Spec.Workload.ExtraLabels[gemlabels.LabelGatewayType] == "" {
tg.Spec.Workload.ExtraLabels[gemlabels.LabelGatewayType] = "ingress-nginx" // 监控的servicemonitor 筛选 pod
}
if tg.Spec.BaseDomain == "" {
tg.Spec.BaseDomain = "*.kubegems.io"
}
if tg.Spec.Image.Repository == "" {
tg.Spec.Image.Repository = repo
}
if tg.Spec.Image.Tag == "" {
tg.Spec.Image.Tag = tag
}
if tg.Spec.Image.PullPolicy == "" {
tg.Spec.Image.PullPolicy = string(corev1.PullIfNotPresent)
}
}