Skip to content

Commit 68d3e5d

Browse files
committed
making metrics port configurable and constructor for metrics
Signed-off-by: sethiyash <[email protected]>
1 parent 621f947 commit 68d3e5d

File tree

12 files changed

+40
-59
lines changed

12 files changed

+40
-59
lines changed

cmd/controller/run.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,6 @@ func Run(opts Options, runLog logr.Logger) error {
9595
return fmt.Errorf("Building packaging client: %s", err)
9696
}
9797

98-
runLog.Info("setting up metrics")
99-
countMetrics := metrics.NewCountMetrics()
100-
countMetrics.RegisterAllMetrics()
101-
102-
reconcileTimeMetrics := metrics.NewReconcileTimeMetrics()
103-
reconcileTimeMetrics.RegisterAllMetrics()
104-
10598
var server *apiserver.APIServer
10699
if opts.StartAPIServer {
107100
// assign bindPort to env var KAPPCTRL_API_PORT if available
@@ -189,7 +182,11 @@ func Run(opts Options, runLog logr.Logger) error {
189182
// initialize cluster access once - it contains a service account token cache which should be only setup once.
190183
kubeconf := kubeconfig.NewKubeconfig(coreClient, runLog)
191184
compInfo := componentinfo.NewComponentInfo(coreClient, kubeconf, Version)
192-
appMetrics := &metrics.Metrics{ReconcileCountMetrics: countMetrics, ReconcileTimeMetrics: reconcileTimeMetrics}
185+
186+
runLog.Info("setting up metrics")
187+
appMetrics := metrics.NewMetrics()
188+
appMetrics.ReconcileTimeMetrics.RegisterAllMetrics()
189+
appMetrics.ReconcileCountMetrics.RegisterAllMetrics()
193190

194191
cacheFolderApps := memdir.NewTmpDir("cache-appcr")
195192
err = cacheFolderApps.Create()

config/config/deployment.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ spec:
5252
- containerPort: #@ data.values.apiPort
5353
name: api
5454
protocol: TCP
55-
- containerPort: 8080
55+
- containerPort: #@ data.values.metricsPort
5656
name: metrics
5757
protocol: TCP
5858
securityContext:

config/values-schema.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ dangerousEnablePprof: false
1717
tlsCipherSuites: ""
1818
#@schema/desc "API port"
1919
apiPort: 8443
20+
#@schema/desc "Metrics port"
21+
metricsPort: 8080
2022
#@schema/desc "The coreDNSIP will be injected into /etc/resolv.conf of kapp-controller pod"
2123
coreDNSIP: ""
2224
#@schema/desc "HostNetwork of kapp-controller deployment."

pkg/app/app_reconcile_test.go

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ import (
2929

3030
func Test_NoInspectReconcile_IfNoDeployAttempted(t *testing.T) {
3131
log := logf.Log.WithName("kc")
32-
var (
33-
appMetrics = metrics.NewCountMetrics()
34-
timeMetrics = metrics.NewReconcileTimeMetrics()
35-
)
3632

3733
// The url under fetch is invalid, which will cause this
3834
// app to fail before deploy.
@@ -55,7 +51,7 @@ func Test_NoInspectReconcile_IfNoDeployAttempted(t *testing.T) {
5551
tmpFac := template.NewFactory(k8scs, fetchFac, false, exec.NewPlainCmdRunner())
5652
deployFac := deploy.NewFactory(k8scs, kubeconfig.NewKubeconfig(k8scs, log), nil, exec.NewPlainCmdRunner(), log)
5753

58-
crdApp := NewCRDApp(&app, log, &metrics.Metrics{ReconcileCountMetrics: appMetrics, ReconcileTimeMetrics: timeMetrics}, kappcs, fetchFac, tmpFac, deployFac, FakeComponentInfo{}, Opts{MinimumSyncPeriod: 30 * time.Second})
54+
crdApp := NewCRDApp(&app, log, metrics.NewMetrics(), kappcs, fetchFac, tmpFac, deployFac, FakeComponentInfo{}, Opts{MinimumSyncPeriod: 30 * time.Second})
5955
_, err := crdApp.Reconcile(false)
6056
assert.Nil(t, err, "unexpected error with reconciling", err)
6157

@@ -89,10 +85,6 @@ func Test_NoInspectReconcile_IfNoDeployAttempted(t *testing.T) {
8985

9086
func Test_NoInspectReconcile_IfInspectNotEnabled(t *testing.T) {
9187
log := logf.Log.WithName("kc")
92-
var (
93-
appMetrics = metrics.NewCountMetrics()
94-
timeMetrics = metrics.NewReconcileTimeMetrics()
95-
)
9688

9789
app := v1alpha1.App{
9890
ObjectMeta: metav1.ObjectMeta{
@@ -125,7 +117,7 @@ func Test_NoInspectReconcile_IfInspectNotEnabled(t *testing.T) {
125117
tmpFac := template.NewFactory(k8scs, fetchFac, false, exec.NewPlainCmdRunner())
126118
deployFac := deploy.NewFactory(k8scs, kubeconfig.NewKubeconfig(k8scs, log), nil, exec.NewPlainCmdRunner(), log)
127119

128-
crdApp := NewCRDApp(&app, log, &metrics.Metrics{ReconcileCountMetrics: appMetrics, ReconcileTimeMetrics: timeMetrics}, kappcs, fetchFac, tmpFac, deployFac, FakeComponentInfo{}, Opts{MinimumSyncPeriod: 30 * time.Second})
120+
crdApp := NewCRDApp(&app, log, metrics.NewMetrics(), kappcs, fetchFac, tmpFac, deployFac, FakeComponentInfo{}, Opts{MinimumSyncPeriod: 30 * time.Second})
129121
_, err := crdApp.Reconcile(false)
130122
assert.Nil(t, err, "unexpected error with reconciling", err)
131123

@@ -170,10 +162,6 @@ func Test_NoInspectReconcile_IfInspectNotEnabled(t *testing.T) {
170162

171163
func Test_TemplateError_DisplayedInStatus_UsefulErrorMessageProperty(t *testing.T) {
172164
log := logf.Log.WithName("kc")
173-
var (
174-
appMetrics = metrics.NewCountMetrics()
175-
timeMetrics = metrics.NewReconcileTimeMetrics()
176-
)
177165

178166
fetchInline := map[string]string{
179167
"file.yml": `foo: #@ data.values.nothere`,
@@ -200,7 +188,7 @@ func Test_TemplateError_DisplayedInStatus_UsefulErrorMessageProperty(t *testing.
200188
tmpFac := template.NewFactory(k8scs, fetchFac, false, exec.NewPlainCmdRunner())
201189
deployFac := deploy.NewFactory(k8scs, kubeconfig.NewKubeconfig(k8scs, log), nil, exec.NewPlainCmdRunner(), log)
202190

203-
crdApp := NewCRDApp(&app, log, &metrics.Metrics{ReconcileCountMetrics: appMetrics, ReconcileTimeMetrics: timeMetrics}, kappcs, fetchFac, tmpFac, deployFac, FakeComponentInfo{}, Opts{MinimumSyncPeriod: 30 * time.Second})
191+
crdApp := NewCRDApp(&app, log, metrics.NewMetrics(), kappcs, fetchFac, tmpFac, deployFac, FakeComponentInfo{}, Opts{MinimumSyncPeriod: 30 * time.Second})
204192
_, err := crdApp.Reconcile(false)
205193
assert.Nil(t, err, "Unexpected error with reconciling", err)
206194

pkg/app/app_template_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func Test_BuildAdditionalDownwardAPIValues_MemoizedCallCount(t *testing.T) {
6161
K8sAPIsCount: &k8sAPIsCallCount,
6262
KCVersionCount: &kcVersionCallCount,
6363
}
64-
app := NewApp(appEmpty, Hooks{}, fetchFac, tmpFac, deployFac, log, Opts{}, &metrics.Metrics{}, fakeInfo)
64+
app := NewApp(appEmpty, Hooks{}, fetchFac, tmpFac, deployFac, log, Opts{}, metrics.NewMetrics(), fakeInfo)
6565

6666
dir, err := os.MkdirTemp("", "temp")
6767
assert.NoError(t, err)

pkg/app/app_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func Test_SecretRefs_RetrievesAllSecretRefs(t *testing.T) {
6565
tmpFac := template.NewFactory(k8scs, fetchFac, false, exec.NewPlainCmdRunner())
6666
deployFac := deploy.NewFactory(k8scs, kubeconfig.NewKubeconfig(k8scs, log), nil, exec.NewPlainCmdRunner(), log)
6767

68-
app := apppkg.NewApp(appWithRefs, apppkg.Hooks{}, fetchFac, tmpFac, deployFac, log, apppkg.Opts{}, &metrics.Metrics{}, FakeComponentInfo{})
68+
app := apppkg.NewApp(appWithRefs, apppkg.Hooks{}, fetchFac, tmpFac, deployFac, log, apppkg.Opts{}, metrics.NewMetrics(), FakeComponentInfo{})
6969

7070
out := app.SecretRefs()
7171
assert.Truef(t, reflect.DeepEqual(out, expected), "Expected: %s\nGot: %s\n", expected, out)
@@ -89,7 +89,7 @@ func Test_SecretRefs_RetrievesNoSecretRefs_WhenNonePresent(t *testing.T) {
8989
tmpFac := template.NewFactory(k8scs, fetchFac, false, exec.NewPlainCmdRunner())
9090
deployFac := deploy.NewFactory(k8scs, kubeconfig.NewKubeconfig(k8scs, log), nil, exec.NewPlainCmdRunner(), log)
9191

92-
app := apppkg.NewApp(appEmpty, apppkg.Hooks{}, fetchFac, tmpFac, deployFac, log, apppkg.Opts{}, &metrics.Metrics{}, FakeComponentInfo{})
92+
app := apppkg.NewApp(appEmpty, apppkg.Hooks{}, fetchFac, tmpFac, deployFac, log, apppkg.Opts{}, metrics.NewMetrics(), FakeComponentInfo{})
9393

9494
out := app.SecretRefs()
9595
assert.Equal(t, 0, len(out), "No SecretRefs to be returned")
@@ -127,7 +127,7 @@ func Test_ConfigMapRefs_RetrievesAllConfigMapRefs(t *testing.T) {
127127
tmpFac := template.NewFactory(k8scs, fetchFac, false, exec.NewPlainCmdRunner())
128128
deployFac := deploy.NewFactory(k8scs, kubeconfig.NewKubeconfig(k8scs, log), nil, exec.NewPlainCmdRunner(), log)
129129

130-
app := apppkg.NewApp(appWithRefs, apppkg.Hooks{}, fetchFac, tmpFac, deployFac, log, apppkg.Opts{}, &metrics.Metrics{}, FakeComponentInfo{})
130+
app := apppkg.NewApp(appWithRefs, apppkg.Hooks{}, fetchFac, tmpFac, deployFac, log, apppkg.Opts{}, metrics.NewMetrics(), FakeComponentInfo{})
131131

132132
out := app.ConfigMapRefs()
133133
assert.Truef(t, reflect.DeepEqual(out, expected), "Expected: %s\nGot: %s\n", expected, out)
@@ -151,7 +151,7 @@ func Test_ConfigMapRefs_RetrievesNoConfigMapRefs_WhenNonePresent(t *testing.T) {
151151
tmpFac := template.NewFactory(k8scs, fetchFac, false, exec.NewPlainCmdRunner())
152152
deployFac := deploy.NewFactory(k8scs, kubeconfig.NewKubeconfig(k8scs, log), nil, exec.NewPlainCmdRunner(), log)
153153

154-
app := apppkg.NewApp(appEmpty, apppkg.Hooks{}, fetchFac, tmpFac, deployFac, log, apppkg.Opts{}, &metrics.Metrics{}, FakeComponentInfo{})
154+
app := apppkg.NewApp(appEmpty, apppkg.Hooks{}, fetchFac, tmpFac, deployFac, log, apppkg.Opts{}, metrics.NewMetrics(), FakeComponentInfo{})
155155

156156
out := app.ConfigMapRefs()
157157
assert.Lenf(t, out, 0, "Expected: %s\nGot: %s\n", "No ConfigMapRefs to be returned", out)

pkg/metrics/metrics.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,12 @@ type Metrics struct {
99
*ReconcileTimeMetrics
1010
IsFirstReconcile bool
1111
}
12+
13+
// NewMetrics is a factory function that returns a new instance of Metrics.
14+
func NewMetrics() *Metrics {
15+
return &Metrics{
16+
ReconcileCountMetrics: NewCountMetrics(),
17+
ReconcileTimeMetrics: NewReconcileTimeMetrics(),
18+
IsFirstReconcile: false,
19+
}
20+
}

pkg/metrics/reconcile_time_metrics.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
package metrics
66

77
import (
8-
"github.com/prometheus/client_golang/prometheus"
9-
"sigs.k8s.io/controller-runtime/pkg/metrics"
108
"strconv"
119
"sync"
1210
"time"
11+
12+
"github.com/prometheus/client_golang/prometheus"
13+
"sigs.k8s.io/controller-runtime/pkg/metrics"
1314
)
1415

1516
// ReconcileTimeMetrics holds reconcile time metrics

pkg/packageinstall/packageinstall_deletion_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func Test_PackageInstallDeletion(t *testing.T) {
6868

6969
ip := NewPackageInstallCR(pkgInstall, log, appClient, pkgClient, coreClient,
7070
FakeComponentInfo{KCVersion: semver.MustParse("0.42.31337")}, Opts{},
71-
&metrics.Metrics{ReconcileCountMetrics: metrics.NewCountMetrics(), ReconcileTimeMetrics: metrics.NewReconcileTimeMetrics()})
71+
metrics.NewMetrics())
7272
_, err := ip.Reconcile()
7373
assert.Nil(t, err)
7474

pkg/packageinstall/packageinstall_downgrade_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ import (
2626

2727
func Test_PackageInstallVersionDowngrades(t *testing.T) {
2828
log := logf.Log.WithName("kc")
29-
var reconcileTimeMetrics = metrics.NewReconcileTimeMetrics()
30-
var reconcileCountMetrics = metrics.NewCountMetrics()
3129

3230
pkg1 := datapkgingv1alpha1.Package{
3331
ObjectMeta: metav1.ObjectMeta{
@@ -103,7 +101,7 @@ func Test_PackageInstallVersionDowngrades(t *testing.T) {
103101

104102
ip := NewPackageInstallCR(pkgInstall, log, appClient, pkgClient, fakek8s,
105103
FakeComponentInfo{KCVersion: semver.MustParse("0.42.31337")}, Opts{},
106-
&metrics.Metrics{ReconcileCountMetrics: reconcileCountMetrics, ReconcileTimeMetrics: reconcileTimeMetrics})
104+
metrics.NewMetrics())
107105
_, err := ip.Reconcile()
108106
assert.Nil(t, err)
109107

@@ -154,7 +152,7 @@ func Test_PackageInstallVersionDowngrades(t *testing.T) {
154152

155153
ip := NewPackageInstallCR(pkgInstall, log, appClient, pkgClient, fakek8s,
156154
FakeComponentInfo{KCVersion: semver.MustParse("0.42.31337")}, Opts{},
157-
&metrics.Metrics{ReconcileCountMetrics: reconcileCountMetrics, ReconcileTimeMetrics: reconcileTimeMetrics})
155+
metrics.NewMetrics())
158156
_, err := ip.Reconcile()
159157
assert.Nil(t, err)
160158

@@ -205,7 +203,7 @@ func Test_PackageInstallVersionDowngrades(t *testing.T) {
205203

206204
ip := NewPackageInstallCR(pkgInstall, log, appClient, pkgClient, fakek8s,
207205
FakeComponentInfo{KCVersion: semver.MustParse("0.42.31337")}, Opts{},
208-
&metrics.Metrics{ReconcileCountMetrics: reconcileCountMetrics, ReconcileTimeMetrics: reconcileTimeMetrics})
206+
metrics.NewMetrics())
209207
_, err := ip.Reconcile()
210208
assert.Nil(t, err)
211209

@@ -262,7 +260,7 @@ func Test_PackageInstallVersionDowngrades(t *testing.T) {
262260

263261
ip := NewPackageInstallCR(pkgInstall, log, appClient, pkgClient, fakek8s,
264262
FakeComponentInfo{KCVersion: semver.MustParse("0.42.31337")}, Opts{},
265-
&metrics.Metrics{ReconcileCountMetrics: reconcileCountMetrics, ReconcileTimeMetrics: reconcileTimeMetrics})
263+
metrics.NewMetrics())
266264
_, err := ip.Reconcile()
267265
assert.Nil(t, err)
268266

0 commit comments

Comments
 (0)