/
services.go
168 lines (148 loc) · 5.13 KB
/
services.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// 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
//
// 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.
//
// Copyright 2019 Google LLC. This software is provided as-is,
// without warranty or representation for any use or purpose.
//
package controllers
import (
"context"
"fmt"
"reflect"
appconfig "github.com/GoogleCloudPlatform/anthos-appconfig/appconfigmgrv2/api/v1alpha1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
// reconcileServices reconciles kubernetes Service resources to stand in front of
// deployments which are managed outside the scope of this controller.
func (r *AppEnvConfigTemplateV2Reconciler) reconcileServices(
ctx context.Context,
in *appconfig.AppEnvConfigTemplateV2,
) error {
names := make(map[types.NamespacedName]bool)
for i, s := range services(in) {
if err := controllerutil.SetControllerReference(in, s, r.Scheme); err != nil {
return fmt.Errorf("setting controller reference for service[%v]: %v", i, err)
}
log.Info("Reconciling", "resource", "services", "index", i, "name", s.Name, "namespace", s.Namespace)
if err := r.reconcileService(ctx, s); err != nil {
return fmt.Errorf("reconciling service[%v]: %v", i, err)
}
names[types.NamespacedName{Name: s.Name, Namespace: s.Namespace}] = true
}
if err := r.garbageCollectServices(ctx, in, names); err != nil {
return fmt.Errorf("garbage collecting: %v", err)
}
return nil
}
func (r *AppEnvConfigTemplateV2Reconciler) reconcileService(
ctx context.Context,
s *corev1.Service,
) error {
found := &corev1.Service{}
err := r.Get(ctx, types.NamespacedName{Name: s.Name, Namespace: s.Namespace}, found)
if err != nil && errors.IsNotFound(err) {
log.Info("Creating", "resource", "services", "namespace", s.Namespace, "name", s.Name)
err = r.Create(ctx, s)
return err
} else if err != nil {
return err
}
if !reflect.DeepEqual(s.Spec, found.Spec) {
// ClusterIP is assigned after creation when it is not originally set
// so we will preserve the value.
s.Spec.ClusterIP = found.Spec.ClusterIP
if s.Spec.Type != corev1.ServiceTypeClusterIP {
for i := range s.Spec.Ports {
s.Spec.Ports[i].NodePort = found.Spec.Ports[i].NodePort
}
}
found.Spec = s.Spec
log.Info("Updating", "resource", "services", "namespace", s.Namespace, "name", s.Name)
if err := r.Update(ctx, found); err != nil {
return err
}
}
return nil
}
func (r *AppEnvConfigTemplateV2Reconciler) garbageCollectServices(
ctx context.Context,
in *appconfig.AppEnvConfigTemplateV2,
names map[types.NamespacedName]bool,
) error {
var list corev1.ServiceList
if err := r.List(ctx, &list, func(opt *client.ListOptions) {}); err != nil {
return fmt.Errorf("listing: %v", err)
}
for _, s := range list.Items {
if !metav1.IsControlledBy(&s, in) {
continue
}
if !names[types.NamespacedName{Name: s.Name, Namespace: s.Namespace}] {
log.Info("Deleting", "resource", "services", "namespace", s.Namespace, "name", s.Name)
if err := r.Delete(ctx, &s); err != nil {
return fmt.Errorf("deleting: %v", err)
}
}
}
return nil
}
// services returns a list of kube services that should exist.
// The number of services corresponds 1:1 with the number of .spec.services[]
// that are specified. Service selectors are based on `app` labels.
func services(t *appconfig.AppEnvConfigTemplateV2) []*corev1.Service {
var list []*corev1.Service
for i := range t.Spec.Services {
typ := corev1.ServiceTypeClusterIP
if t.Spec.Services[i].Ingress != nil {
typ = corev1.ServiceTypeNodePort
}
s := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: serviceName(t, i),
Namespace: t.Namespace,
},
Spec: corev1.ServiceSpec{
Type: typ,
Selector: map[string]string{
"app": t.Spec.Services[i].DeploymentApp,
},
Ports: []corev1.ServicePort{
{
// NOTE: Istio requires prefixed port names such as `http-___`.
Name: "http-default",
Protocol: t.Spec.Services[i].DeploymentPortProtocol,
Port: t.Spec.Services[i].ServicePort,
TargetPort: intstr.IntOrString{
IntVal: t.Spec.Services[i].DeploymentPort,
},
},
},
},
}
list = append(list, s)
}
return list
}
// serviceName returns the name of a given service where i is the index
// .spec.services[i] because the name is derived from the app config name
// and the service name.
func serviceName(t *appconfig.AppEnvConfigTemplateV2, i int) string {
return fmt.Sprintf("%v-%v", t.Name, t.Spec.Services[i].Name)
}