This repository has been archived by the owner on May 25, 2022. It is now read-only.
/
services.go
93 lines (81 loc) · 3.23 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
// Copyright (c) 2020, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
package managed
import (
"context"
"github.com/golang/glog"
"github.com/verrazzano/verrazzano-operator/pkg/constants"
"github.com/verrazzano/verrazzano-operator/pkg/monitoring"
"github.com/verrazzano/verrazzano-operator/pkg/types"
"github.com/verrazzano/verrazzano-operator/pkg/util"
"github.com/verrazzano/verrazzano-operator/pkg/util/diff"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
// CreateServices creates/updates services needed for each managed cluster.
func CreateServices(mbPair *types.ModelBindingPair, filteredConnections map[string]*util.ManagedClusterConnection) error {
glog.V(6).Infof("Creating/updating Service for VerrazzanoBinding %s", mbPair.Binding.Name)
// If binding is not System binding, skip creating Services
if mbPair.Binding.Name != constants.VmiSystemBindingName {
glog.V(6).Infof("Skip creating Services for VerrazzanoApplicationBinding %s", mbPair.Binding.Name)
return nil
}
// Construct services for each ManagedCluster
for clusterName := range mbPair.ManagedClusters {
managedClusterConnection := filteredConnections[clusterName]
managedClusterConnection.Lock.RLock()
defer managedClusterConnection.Lock.RUnlock()
// Construct the set of expected Services
newServices := newServices(clusterName)
// Create or update Services
for _, newService := range newServices {
existingService, err := managedClusterConnection.ServiceLister.Services(newService.Namespace).Get(newService.Name)
if existingService != nil {
specDiffs := diff.CompareIgnoreTargetEmpties(existingService, newService)
if specDiffs != "" {
glog.V(6).Infof("Service %s : Spec differences %s", newService.Name, specDiffs)
glog.V(4).Infof("Updating Service %s in cluster %s", newService.Name, clusterName)
_, err = managedClusterConnection.KubeClient.CoreV1().Services(newService.Namespace).Update(context.TODO(), newService, metav1.UpdateOptions{})
}
} else {
glog.V(4).Infof("Creating Service %s in cluster %s", newService.Name, clusterName)
_, err = managedClusterConnection.KubeClient.CoreV1().Services(newService.Namespace).Create(context.TODO(), newService, metav1.CreateOptions{})
}
if err != nil {
return err
}
}
}
return nil
}
// Constructs the necessary Service for the specified ManagedCluster
func newServices(managedClusterName string) []*corev1.Service {
roleLabels := monitoring.GetNodeExporterLabels(managedClusterName)
labels := map[string]string{
constants.ServiceAppLabel: constants.NodeExporterName,
}
var Services []*corev1.Service
service := &corev1.Service{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{
Name: constants.NodeExporterName,
Labels: roleLabels,
Namespace: constants.MonitoringNamespace,
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: "metrics",
Port: 9100,
TargetPort: intstr.FromInt(9100),
Protocol: "TCP",
},
},
Selector: labels,
Type: "ClusterIP",
},
}
Services = append(Services, service)
return Services
}