@@ -19,12 +19,14 @@ import (
19
19
"fmt"
20
20
"os"
21
21
22
+ v1 "k8s.io/api/core/v1"
22
23
"k8s.io/apimachinery/pkg/api/meta"
23
24
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24
25
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
25
26
"k8s.io/apimachinery/pkg/runtime"
26
27
"k8s.io/apimachinery/pkg/runtime/schema"
27
28
"k8s.io/apimachinery/pkg/runtime/serializer"
29
+ intstr "k8s.io/apimachinery/pkg/util/intstr"
28
30
cgoscheme "k8s.io/client-go/kubernetes/scheme"
29
31
)
30
32
@@ -147,3 +149,53 @@ func GetWatchNamespace() (string, error) {
147
149
}
148
150
return ns , nil
149
151
}
152
+
153
+ // GetOperatorName return the operator name
154
+ func GetOperatorName () (string , error ) {
155
+ operatorName , found := os .LookupEnv (OperatorNameEnvVar )
156
+ if ! found {
157
+ return "" , fmt .Errorf ("%s must be set" , OperatorNameEnvVar )
158
+ }
159
+ if len (operatorName ) == 0 {
160
+ return "" , fmt .Errorf ("%s must not be empty" , OperatorNameEnvVar )
161
+ }
162
+ return operatorName , nil
163
+ }
164
+
165
+ // InitOperatorService return the static service which expose operator metrics
166
+ func InitOperatorService () (* v1.Service , error ) {
167
+ operatorName , err := GetOperatorName ()
168
+ if err != nil {
169
+ return nil , err
170
+ }
171
+ namespace , err := GetWatchNamespace ()
172
+ if err != nil {
173
+ return nil , err
174
+ }
175
+ service := & v1.Service {
176
+ ObjectMeta : metav1.ObjectMeta {
177
+ Name : operatorName ,
178
+ Namespace : namespace ,
179
+ Labels : map [string ]string {"name" : operatorName },
180
+ },
181
+ TypeMeta : metav1.TypeMeta {
182
+ Kind : "Service" ,
183
+ APIVersion : "v1" ,
184
+ },
185
+ Spec : v1.ServiceSpec {
186
+ Ports : []v1.ServicePort {
187
+ {
188
+ Port : PrometheusMetricsPort ,
189
+ Protocol : v1 .ProtocolTCP ,
190
+ TargetPort : intstr.IntOrString {
191
+ Type : intstr .String ,
192
+ StrVal : PrometheusMetricsPortName ,
193
+ },
194
+ Name : PrometheusMetricsPortName ,
195
+ },
196
+ },
197
+ Selector : map [string ]string {"name" : operatorName },
198
+ },
199
+ }
200
+ return service , nil
201
+ }
0 commit comments