This repository has been archived by the owner on Dec 11, 2021. It is now read-only.
forked from operator-framework/operator-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
199 lines (174 loc) · 5.77 KB
/
metrics.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright 2018 The Operator-SDK Authors
//
// 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.
package metrics
import (
"context"
"fmt"
"github.com/operator-framework/operator-sdk/pkg/k8sutil"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
crclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
)
var log = logf.Log.WithName("metrics")
var trueVar = true
const (
// PrometheusPortName defines the port name used in the metrics Service.
PrometheusPortName = "metrics"
)
// ExposeMetricsPort creates a Kubernetes Service to expose the passed metrics port.
func ExposeMetricsPort(ctx context.Context, port int32) (*v1.Service, error) {
client, err := createClient()
if err != nil {
return nil, fmt.Errorf("failed to create new client: %v", err)
}
// We do not need to check the validity of the port, as controller-runtime
// would error out and we would never get to this stage.
s, err := initOperatorService(ctx, client, port, PrometheusPortName)
if err != nil {
if err == k8sutil.ErrNoNamespace {
log.Info("Skipping metrics Service creation; not running in a cluster.")
return nil, nil
}
return nil, fmt.Errorf("failed to initialize service object for metrics: %v", err)
}
service, err := createOrUpdateService(ctx, client, s)
if err != nil {
return nil, fmt.Errorf("failed to create or get service for metrics: %v", err)
}
return service, nil
}
func createOrUpdateService(ctx context.Context, client crclient.Client, s *v1.Service) (*v1.Service, error) {
if err := client.Create(ctx, s); err != nil {
if !apierrors.IsAlreadyExists(err) {
return nil, err
}
// Service already exists, we want to update it
// as we do not know if any fields might have changed.
existingService := &v1.Service{}
err := client.Get(ctx, types.NamespacedName{
Name: s.Name,
Namespace: s.Namespace,
}, existingService)
s.ResourceVersion = existingService.ResourceVersion
if existingService.Spec.Type == v1.ServiceTypeClusterIP {
s.Spec.ClusterIP = existingService.Spec.ClusterIP
}
err = client.Update(ctx, s)
if err != nil {
return nil, err
}
log.V(1).Info("Metrics Service object updated", "Service.Name", s.Name, "Service.Namespace", s.Namespace)
return existingService, nil
}
log.Info("Metrics Service object created", "Service.Name", s.Name, "Service.Namespace", s.Namespace)
return s, nil
}
// initOperatorService returns the static service which exposes specified port.
func initOperatorService(ctx context.Context, client crclient.Client, port int32, portName string) (*v1.Service, error) {
operatorName, err := k8sutil.GetOperatorName()
if err != nil {
return nil, err
}
namespace, err := k8sutil.GetOperatorNamespace()
if err != nil {
return nil, err
}
label := map[string]string{"name": operatorName}
service := &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: operatorName,
Namespace: namespace,
Labels: label,
},
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "v1",
},
Spec: v1.ServiceSpec{
Ports: []v1.ServicePort{
{
Port: port,
Protocol: v1.ProtocolTCP,
TargetPort: intstr.IntOrString{
Type: intstr.Int,
IntVal: port,
},
Name: portName,
},
},
Selector: label,
},
}
ownRef, err := getPodOwnerRef(ctx, client, namespace)
if err != nil {
return nil, err
}
service.SetOwnerReferences([]metav1.OwnerReference{*ownRef})
return service, nil
}
func getPodOwnerRef(ctx context.Context, client crclient.Client, ns string) (*metav1.OwnerReference, error) {
// Get current Pod the operator is running in
pod, err := k8sutil.GetPod(ctx, client, ns)
if err != nil {
return nil, err
}
podOwnerRefs := metav1.NewControllerRef(pod, pod.GroupVersionKind())
// Get Owner that the Pod belongs to
ownerRef := metav1.GetControllerOf(pod)
finalOwnerRef, err := findFinalOwnerRef(ctx, client, ns, ownerRef)
if err != nil {
return nil, err
}
if finalOwnerRef != nil {
return finalOwnerRef, nil
}
// Default to returning Pod as the Owner
return podOwnerRefs, nil
}
// findFinalOwnerRef tries to locate the final controller/owner based on the owner reference provided.
func findFinalOwnerRef(ctx context.Context, client crclient.Client, ns string, ownerRef *metav1.OwnerReference) (*metav1.OwnerReference, error) {
if ownerRef == nil {
return nil, nil
}
obj := &unstructured.Unstructured{}
obj.SetAPIVersion(ownerRef.APIVersion)
obj.SetKind(ownerRef.Kind)
err := client.Get(ctx, types.NamespacedName{Namespace: ns, Name: ownerRef.Name}, obj)
if err != nil {
return nil, err
}
newOwnerRef := metav1.GetControllerOf(obj)
if newOwnerRef != nil {
return findFinalOwnerRef(ctx, client, ns, newOwnerRef)
}
log.V(1).Info("Pods owner found", "Kind", ownerRef.Kind, "Name", ownerRef.Name, "Namespace", ns)
return ownerRef, nil
}
func createClient() (crclient.Client, error) {
config, err := config.GetConfig()
if err != nil {
return nil, err
}
client, err := crclient.New(config, crclient.Options{})
if err != nil {
return nil, err
}
return client, nil
}