@@ -25,7 +25,6 @@ import (
25
25
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26
26
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
27
27
"k8s.io/apimachinery/pkg/types"
28
- "k8s.io/apimachinery/pkg/util/intstr"
29
28
crclient "sigs.k8s.io/controller-runtime/pkg/client"
30
29
"sigs.k8s.io/controller-runtime/pkg/client/config"
31
30
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
@@ -42,23 +41,17 @@ const (
42
41
CRPortName = "cr-metrics"
43
42
)
44
43
45
- //MetricService holds information about metrics port and port name.
46
- type MetricService struct {
47
- Port int32
48
- PortName string
49
- }
50
-
51
44
// CreateMetricsService creates a Kubernetes Service to expose the passed metrics
52
45
// port(s) with the given name(s).
53
- func CreateMetricsService (ctx context.Context , ms []MetricService ) (* v1.Service , error ) {
54
- if len (ms ) < 1 {
55
- return nil , fmt .Errorf ("failed to create metrics Serice; MetricsService empty" )
46
+ func CreateMetricsService (ctx context.Context , servicePorts []v1. ServicePort ) (* v1.Service , error ) {
47
+ if len (servicePorts ) < 1 {
48
+ return nil , fmt .Errorf ("failed to create metrics Serice; service ports were empty" )
56
49
}
57
50
client , err := createClient ()
58
51
if err != nil {
59
52
return nil , fmt .Errorf ("failed to create new client: %v" , err )
60
53
}
61
- s , err := initOperatorService (ctx , client , ms )
54
+ s , err := initOperatorService (ctx , client , servicePorts )
62
55
if err != nil {
63
56
if err == k8sutil .ErrNoNamespace {
64
57
log .Info ("Skipping metrics Service creation; not running in a cluster." )
@@ -104,7 +97,7 @@ func createOrUpdateService(ctx context.Context, client crclient.Client, s *v1.Se
104
97
}
105
98
106
99
// initOperatorService returns the static service which exposes specified port(s).
107
- func initOperatorService (ctx context.Context , client crclient.Client , ms []MetricService ) (* v1.Service , error ) {
100
+ func initOperatorService (ctx context.Context , client crclient.Client , sp []v1. ServicePort ) (* v1.Service , error ) {
108
101
operatorName , err := k8sutil .GetOperatorName ()
109
102
if err != nil {
110
103
return nil , err
@@ -113,19 +106,16 @@ func initOperatorService(ctx context.Context, client crclient.Client, ms []Metri
113
106
if err != nil {
114
107
return nil , err
115
108
}
116
-
117
109
label := map [string ]string {"name" : operatorName }
118
110
119
- ports := populateServicePorts (ms )
120
-
121
111
service := & v1.Service {
122
112
ObjectMeta : metav1.ObjectMeta {
123
113
Name : fmt .Sprintf ("%s-metrics" , operatorName ),
124
114
Namespace : namespace ,
125
115
Labels : label ,
126
116
},
127
117
Spec : v1.ServiceSpec {
128
- Ports : ports ,
118
+ Ports : sp ,
129
119
Selector : label ,
130
120
},
131
121
}
@@ -139,22 +129,6 @@ func initOperatorService(ctx context.Context, client crclient.Client, ms []Metri
139
129
return service , nil
140
130
}
141
131
142
- func populateServicePorts (ms []MetricService ) []v1.ServicePort {
143
- var servicePorts []v1.ServicePort
144
- for _ , v := range ms {
145
- servicePorts = append (servicePorts , v1.ServicePort {
146
- Port : v .Port ,
147
- Protocol : v1 .ProtocolTCP ,
148
- TargetPort : intstr.IntOrString {
149
- Type : intstr .Int ,
150
- IntVal : v .Port ,
151
- },
152
- Name : v .PortName ,
153
- })
154
- }
155
- return servicePorts
156
- }
157
-
158
132
func getPodOwnerRef (ctx context.Context , client crclient.Client , ns string ) (* metav1.OwnerReference , error ) {
159
133
// Get current Pod the operator is running in
160
134
pod , err := k8sutil .GetPod (ctx , client , ns )
0 commit comments