Skip to content

Commit dfcaafc

Browse files
committed
*: Pass ServicePort directly instead
1 parent 8b3f495 commit dfcaafc

File tree

5 files changed

+37
-50
lines changed

5 files changed

+37
-50
lines changed

internal/pkg/scaffold/cmd.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,13 @@ import (
5858
"github.com/operator-framework/operator-sdk/pkg/restmapper"
5959
sdkVersion "github.com/operator-framework/operator-sdk/version"
6060
"github.com/spf13/pflag"
61+
v1 "k8s.io/api/core/v1"
62+
"k8s.io/apimachinery/pkg/util/intstr"
6163
"sigs.k8s.io/controller-runtime/pkg/client/config"
6264
"sigs.k8s.io/controller-runtime/pkg/manager"
6365
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
6466
"sigs.k8s.io/controller-runtime/pkg/runtime/signals"
67+
"k8s.io/apimachinery/pkg/util/intstr"
6568
)
6669
6770
// Change below variables to serve metrics on different host or port.
@@ -150,14 +153,14 @@ func main() {
150153
if err = serveCRMetrics(cfg); err != nil {
151154
log.Info("Could not generate and serve custom resource metrics: ", err.Error())
152155
}
153-
156+
154157
// Add to the below struct any other metrics ports you want to expose.
155-
metricsService := []metrics.MetricService{
156-
{Port: operatorMetricsPort, PortName: metrics.CRPortName},
157-
{Port: metricsPort, PortName: metrics.OperatorPortName},
158+
servicePorts := []v1.ServicePort{
159+
{Port: metricsPort, Name: metrics.OperatorPortName, Protocol: v1.ProtocolTCP, TargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: metricsPort}},
160+
{Port: operatorMetricsPort, Name: metrics.CRPortName, Protocol: v1.ProtocolTCP, TargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: operatorMetricsPort}},
158161
}
159162
// Create Service object to expose the metrics port(s).
160-
_, err = metrics.CreateMetricsService(ctx, metricsService)
163+
_, err = metrics.CreateMetricsService(ctx, servicePorts)
161164
if err != nil {
162165
log.Info(err.Error())
163166
}

internal/pkg/scaffold/cmd_test.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ import (
5757
"github.com/operator-framework/operator-sdk/pkg/restmapper"
5858
sdkVersion "github.com/operator-framework/operator-sdk/version"
5959
"github.com/spf13/pflag"
60+
v1 "k8s.io/api/core/v1"
61+
"k8s.io/apimachinery/pkg/util/intstr"
6062
"sigs.k8s.io/controller-runtime/pkg/client/config"
6163
"sigs.k8s.io/controller-runtime/pkg/manager"
6264
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
@@ -151,12 +153,12 @@ func main() {
151153
}
152154
153155
// Add to the below struct any other metrics ports you want to expose.
154-
metricsService := []metrics.MetricService{
155-
{Port: operatorMetricsPort, PortName: metrics.CRPortName},
156-
{Port: metricsPort, PortName: metrics.OperatorPortName},
156+
servicePorts := []v1.ServicePort{
157+
{Port: metricsPort, Name: metrics.OperatorPortName, Protocol: v1.ProtocolTCP, TargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: metricsPort}},
158+
{Port: operatorMetricsPort, Name: metrics.CRPortName, Protocol: v1.ProtocolTCP, TargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: operatorMetricsPort}},
157159
}
158160
// Create Service object to expose the metrics port(s).
159-
_, err = metrics.CreateMetricsService(ctx, metricsService)
161+
_, err = metrics.CreateMetricsService(ctx, servicePorts)
160162
if err != nil {
161163
log.Info(err.Error())
162164
}

pkg/ansible/run.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,19 @@ import (
2828
"github.com/operator-framework/operator-sdk/pkg/leader"
2929
"github.com/operator-framework/operator-sdk/pkg/metrics"
3030
sdkVersion "github.com/operator-framework/operator-sdk/version"
31+
"k8s.io/api/core/v1"
3132
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33+
"k8s.io/apimachinery/pkg/util/intstr"
3234

3335
"sigs.k8s.io/controller-runtime/pkg/client/config"
3436
"sigs.k8s.io/controller-runtime/pkg/manager"
3537
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
3638
)
3739

38-
var log = logf.Log.WithName("cmd")
40+
var (
41+
log = logf.Log.WithName("cmd")
42+
metricsPort int32 = 8383
43+
)
3944

4045
func printVersion() {
4146
log.Info(fmt.Sprintf("Go Version: %s", runtime.Version()))
@@ -66,7 +71,7 @@ func Run(flags *aoflags.AnsibleOperatorFlags) error {
6671
// TODO: probably should expose the host & port as an environment variables
6772
mgr, err := manager.New(cfg, manager.Options{
6873
Namespace: namespace,
69-
MetricsBindAddress: "0.0.0.0:8383",
74+
MetricsBindAddress: fmt.Sprintf("0.0.0.0:%d", metricsPort),
7075
})
7176
if err != nil {
7277
log.Error(err, "Failed to create a new manager.")
@@ -85,11 +90,13 @@ func Run(flags *aoflags.AnsibleOperatorFlags) error {
8590
return err
8691
}
8792

88-
metricsService := []metrics.MetricService{
89-
{Port: 8383, PortName: metrics.OperatorPortName},
93+
// Add to the below struct any other metrics ports you want to expose.
94+
servicePorts := []v1.ServicePort{
95+
{Port: metricsPort, Name: metrics.OperatorPortName, Protocol: v1.ProtocolTCP, TargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: metricsPort}},
9096
}
97+
// Create Service object to expose the metrics port(s).
9198
// TODO: probably should expose the port as an environment variable
92-
_, err = metrics.CreateMetricsService(context.TODO(), metricsService)
99+
_, err = metrics.CreateMetricsService(context.TODO(), servicePorts)
93100
if err != nil {
94101
log.Error(err, "Exposing metrics port failed.")
95102
return err

pkg/helm/run.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ import (
2929
"github.com/operator-framework/operator-sdk/pkg/metrics"
3030
sdkVersion "github.com/operator-framework/operator-sdk/version"
3131

32+
"k8s.io/api/core/v1"
3233
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
34+
"k8s.io/apimachinery/pkg/util/intstr"
3335
"sigs.k8s.io/controller-runtime/pkg/client/config"
3436
"sigs.k8s.io/controller-runtime/pkg/manager"
3537
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
@@ -117,12 +119,11 @@ func Run(flags *hoflags.HelmOperatorFlags) error {
117119
return err
118120
}
119121

120-
// Add to the below struct any other metrics ports you want to expose.
121-
metricsService := []metrics.MetricService{
122-
{Port: metricsPort, PortName: metrics.OperatorPortName},
122+
servicePorts := []v1.ServicePort{
123+
{Port: metricsPort, Name: metrics.OperatorPortName, Protocol: v1.ProtocolTCP, TargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: metricsPort}},
123124
}
124125
// Create Service object to expose the metrics port(s).
125-
_, err = metrics.CreateMetricsService(ctx, metricsService)
126+
_, err = metrics.CreateMetricsService(ctx, servicePorts)
126127
if err != nil {
127128
log.Info(err.Error())
128129
}

pkg/metrics/metrics.go

+6-32
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2626
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2727
"k8s.io/apimachinery/pkg/types"
28-
"k8s.io/apimachinery/pkg/util/intstr"
2928
crclient "sigs.k8s.io/controller-runtime/pkg/client"
3029
"sigs.k8s.io/controller-runtime/pkg/client/config"
3130
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
@@ -42,23 +41,17 @@ const (
4241
CRPortName = "cr-metrics"
4342
)
4443

45-
//MetricService holds information about metrics port and port name.
46-
type MetricService struct {
47-
Port int32
48-
PortName string
49-
}
50-
5144
// CreateMetricsService creates a Kubernetes Service to expose the passed metrics
5245
// 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")
5649
}
5750
client, err := createClient()
5851
if err != nil {
5952
return nil, fmt.Errorf("failed to create new client: %v", err)
6053
}
61-
s, err := initOperatorService(ctx, client, ms)
54+
s, err := initOperatorService(ctx, client, servicePorts)
6255
if err != nil {
6356
if err == k8sutil.ErrNoNamespace {
6457
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
10497
}
10598

10699
// 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) {
108101
operatorName, err := k8sutil.GetOperatorName()
109102
if err != nil {
110103
return nil, err
@@ -113,19 +106,16 @@ func initOperatorService(ctx context.Context, client crclient.Client, ms []Metri
113106
if err != nil {
114107
return nil, err
115108
}
116-
117109
label := map[string]string{"name": operatorName}
118110

119-
ports := populateServicePorts(ms)
120-
121111
service := &v1.Service{
122112
ObjectMeta: metav1.ObjectMeta{
123113
Name: fmt.Sprintf("%s-metrics", operatorName),
124114
Namespace: namespace,
125115
Labels: label,
126116
},
127117
Spec: v1.ServiceSpec{
128-
Ports: ports,
118+
Ports: sp,
129119
Selector: label,
130120
},
131121
}
@@ -139,22 +129,6 @@ func initOperatorService(ctx context.Context, client crclient.Client, ms []Metri
139129
return service, nil
140130
}
141131

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-
158132
func getPodOwnerRef(ctx context.Context, client crclient.Client, ns string) (*metav1.OwnerReference, error) {
159133
// Get current Pod the operator is running in
160134
pod, err := k8sutil.GetPod(ctx, client, ns)

0 commit comments

Comments
 (0)