-
Notifications
You must be signed in to change notification settings - Fork 347
/
infra_resource.go
145 lines (121 loc) · 3.89 KB
/
infra_resource.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
// Copyright Envoy Gateway Authors
// SPDX-License-Identifier: Apache-2.0
// The full text of the Apache license is available in the LICENSE file at
// the root of the repo.
package kubernetes
import (
"context"
"reflect"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"github.com/envoyproxy/gateway/internal/infrastructure/kubernetes/resource"
)
// createOrUpdateServiceAccount creates a ServiceAccount in the kube api server based on the
// provided ResourceRender, if it doesn't exist and updates it if it does.
func (i *Infra) createOrUpdateServiceAccount(ctx context.Context, r ResourceRender) error {
sa, err := r.ServiceAccount()
if err != nil {
return err
}
current := &corev1.ServiceAccount{}
key := types.NamespacedName{
Namespace: sa.Namespace,
Name: sa.Name,
}
return i.Client.CreateOrUpdate(ctx, key, current, sa, func() bool {
return true
})
}
// createOrUpdateConfigMap creates a ConfigMap in the Kube api server based on the provided
// ResourceRender, if it doesn't exist and updates it if it does.
func (i *Infra) createOrUpdateConfigMap(ctx context.Context, r ResourceRender) error {
cm, err := r.ConfigMap()
if err != nil {
return err
}
if cm == nil {
return nil
}
current := &corev1.ConfigMap{}
key := types.NamespacedName{
Namespace: cm.Namespace,
Name: cm.Name,
}
return i.Client.CreateOrUpdate(ctx, key, current, cm, func() bool {
return !reflect.DeepEqual(cm.Data, current.Data)
})
}
// createOrUpdateDeployment creates a Deployment in the kube api server based on the provided
// ResourceRender, if it doesn't exist and updates it if it does.
func (i *Infra) createOrUpdateDeployment(ctx context.Context, r ResourceRender) error {
deployment, err := r.Deployment()
if err != nil {
return err
}
current := &appsv1.Deployment{}
key := types.NamespacedName{
Namespace: deployment.Namespace,
Name: deployment.Name,
}
return i.Client.CreateOrUpdate(ctx, key, current, deployment, func() bool {
return !reflect.DeepEqual(deployment.Spec, current.Spec)
})
}
// createOrUpdateRateLimitService creates a Service in the kube api server based on the provided ResourceRender,
// if it doesn't exist or updates it if it does.
func (i *Infra) createOrUpdateService(ctx context.Context, r ResourceRender) error {
svc, err := r.Service()
if err != nil {
return err
}
current := &corev1.Service{}
key := types.NamespacedName{
Namespace: svc.Namespace,
Name: svc.Name,
}
return i.Client.CreateOrUpdate(ctx, key, current, svc, func() bool {
return !resource.CompareSvc(svc, current)
})
}
// deleteServiceAccount deletes the ServiceAccount in the kube api server, if it exists.
func (i *Infra) deleteServiceAccount(ctx context.Context, r ResourceRender) error {
sa := &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Namespace: i.Namespace,
Name: r.Name(),
},
}
return i.Client.Delete(ctx, sa)
}
// deleteDeployment deletes the Envoy Deployment in the kube api server, if it exists.
func (i *Infra) deleteDeployment(ctx context.Context, r ResourceRender) error {
deployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Namespace: i.Namespace,
Name: r.Name(),
},
}
return i.Client.Delete(ctx, deployment)
}
// deleteConfigMap deletes the ConfigMap in the kube api server, if it exists.
func (i *Infra) deleteConfigMap(ctx context.Context, r ResourceRender) error {
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: i.Namespace,
Name: r.Name(),
},
}
return i.Client.Delete(ctx, cm)
}
// deleteService deletes the Service in the kube api server, if it exists.
func (i *Infra) deleteService(ctx context.Context, r ResourceRender) error {
svc := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Namespace: i.Namespace,
Name: r.Name(),
},
}
return i.Client.Delete(ctx, svc)
}