Skip to content

Commit 1631608

Browse files
committed
final refactoring
Signed-off-by: sethiyash <[email protected]>
1 parent 09d6cd0 commit 1631608

22 files changed

+292
-275
lines changed

cmd/controller/run.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ func Run(opts Options, runLog logr.Logger) error {
9696
}
9797

9898
runLog.Info("setting up metrics")
99-
appMetrics := metrics.NewAppMetrics()
100-
appMetrics.RegisterAllMetrics()
99+
countMetrics := metrics.NewCountMetrics()
100+
countMetrics.RegisterAllMetrics()
101101

102102
reconcileTimeMetrics := metrics.NewReconcileTimeMetrics()
103103
reconcileTimeMetrics.RegisterAllMetrics()
@@ -197,15 +197,15 @@ func Run(opts Options, runLog logr.Logger) error {
197197
}
198198
{ // add controller for apps
199199
appFactory := app.CRDAppFactory{
200-
CoreClient: coreClient,
201-
AppClient: kcClient,
202-
KcConfig: kcConfig,
203-
AppMetrics: appMetrics,
204-
TimeMetrics: reconcileTimeMetrics,
205-
CmdRunner: sidecarCmdExec,
206-
Kubeconf: kubeconf,
207-
CompInfo: compInfo,
208-
CacheFolder: cacheFolderApps,
200+
CoreClient: coreClient,
201+
AppClient: kcClient,
202+
KcConfig: kcConfig,
203+
CountMetrics: countMetrics,
204+
TimeMetrics: reconcileTimeMetrics,
205+
CmdRunner: sidecarCmdExec,
206+
Kubeconf: kubeconf,
207+
CompInfo: compInfo,
208+
CacheFolder: cacheFolderApps,
209209
}
210210
reconciler := app.NewReconciler(kcClient, runLog.WithName("app"),
211211
appFactory, refTracker, updateStatusTracker, compInfo)
@@ -232,7 +232,7 @@ func Run(opts Options, runLog logr.Logger) error {
232232
kcClient, opts.PackagingGlobalNS, runLog.WithName("handler"))
233233

234234
reconciler := pkginstall.NewReconciler(kcClient, pkgClient, coreClient, pkgToPkgInstallHandler,
235-
runLog.WithName("pkgi"), compInfo, kcConfig, reconcileTimeMetrics)
235+
runLog.WithName("pkgi"), compInfo, kcConfig, countMetrics, reconcileTimeMetrics)
236236

237237
ctrl, err := controller.New("pkgi", mgr, controller.Options{
238238
Reconciler: reconciler,
@@ -256,14 +256,14 @@ func Run(opts Options, runLog logr.Logger) error {
256256

257257
{ // add controller for pkgrepositories
258258
appFactory := pkgrepository.AppFactory{
259-
CoreClient: coreClient,
260-
AppClient: kcClient,
261-
KcConfig: kcConfig,
262-
AppMetrics: appMetrics,
263-
TimeMetrics: reconcileTimeMetrics,
264-
CmdRunner: sidecarCmdExec,
265-
Kubeconf: kubeconf,
266-
CacheFolder: cacheFolderPkgRepoApps,
259+
CoreClient: coreClient,
260+
AppClient: kcClient,
261+
KcConfig: kcConfig,
262+
CountMetrics: countMetrics,
263+
TimeMetrics: reconcileTimeMetrics,
264+
CmdRunner: sidecarCmdExec,
265+
Kubeconf: kubeconf,
266+
CacheFolder: cacheFolderPkgRepoApps,
267267
}
268268

269269
reconciler := pkgrepository.NewReconciler(kcClient, coreClient,

pkg/app/app.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,24 @@ type App struct {
5555
memoizedKubernetesVersion string
5656
memoizedKubernetesAPIs []string
5757

58-
log logr.Logger
59-
opts Opts
60-
appMetrics *metrics.AppMetrics
61-
timeMetrics *metrics.ReconcileTimeMetrics
58+
log logr.Logger
59+
opts Opts
60+
countMetrics *metrics.ReconcileCountMetrics
61+
timeMetrics *metrics.ReconcileTimeMetrics
6262

63-
isFirstReconcile string
63+
isFirstReconcile bool
6464
pendingStatusUpdate bool
6565
flushAllStatusUpdates bool
6666
metadata *deploy.Meta
6767
}
6868

6969
func NewApp(app v1alpha1.App, hooks Hooks,
7070
fetchFactory fetch.Factory, templateFactory template.Factory,
71-
deployFactory deploy.Factory, log logr.Logger, opts Opts, appMetrics *metrics.AppMetrics, timeMetrics *metrics.ReconcileTimeMetrics, compInfo ComponentInfo) *App {
71+
deployFactory deploy.Factory, log logr.Logger, opts Opts, appMetrics *metrics.ReconcileCountMetrics, timeMetrics *metrics.ReconcileTimeMetrics, compInfo ComponentInfo) *App {
7272

7373
return &App{app: app, appPrev: *(app.DeepCopy()), hooks: hooks,
7474
fetchFactory: fetchFactory, templateFactory: templateFactory,
75-
deployFactory: deployFactory, log: log, opts: opts, appMetrics: appMetrics, timeMetrics: timeMetrics, compInfo: compInfo}
75+
deployFactory: deployFactory, log: log, opts: opts, countMetrics: appMetrics, timeMetrics: timeMetrics, compInfo: compInfo}
7676
}
7777

7878
func (a *App) Name() string { return a.app.Name }

pkg/app/app_factory.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type CRDAppFactory struct {
2626
CoreClient kubernetes.Interface
2727
AppClient kcclient.Interface
2828
KcConfig *config.Config
29-
AppMetrics *metrics.AppMetrics
29+
CountMetrics *metrics.ReconcileCountMetrics
3030
TimeMetrics *metrics.ReconcileTimeMetrics
3131
VendirConfigHook func(vendirconf.Config) vendirconf.Config
3232
KbldAllowBuild bool
@@ -49,7 +49,7 @@ func (f *CRDAppFactory) NewCRDApp(app *kcv1alpha1.App, log logr.Logger) *CRDApp
4949
templateFactory := template.NewFactory(f.CoreClient, fetchFactory, f.KbldAllowBuild, f.CmdRunner)
5050
deployFactory := deploy.NewFactory(f.CoreClient, f.Kubeconf, f.KcConfig, f.CmdRunner, log)
5151

52-
return NewCRDApp(app, log, f.AppMetrics, f.TimeMetrics, f.AppClient, fetchFactory, templateFactory, deployFactory, f.CompInfo, Opts{
52+
return NewCRDApp(app, log, f.CountMetrics, f.TimeMetrics, f.AppClient, fetchFactory, templateFactory, deployFactory, f.CompInfo, Opts{
5353
DefaultSyncPeriod: f.KcConfig.AppDefaultSyncPeriod(),
5454
MinimumSyncPeriod: f.KcConfig.AppMinimumSyncPeriod(),
5555
})

pkg/app/app_fetch.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ const (
2020
)
2121

2222
func (a *App) fetch(dstPath string) (string, exec.CmdRunResult) {
23-
// update metrics with the total fetch time
24-
reconcileStartTS := time.Now()
25-
defer func() {
26-
a.timeMetrics.RegisterFetchTime(a.app.Kind, a.app.Name, a.app.Namespace, "", time.Since(reconcileStartTS))
27-
}()
28-
2923
// fetch init stage
3024
if len(a.app.Spec.Fetch) == 0 {
3125
return "", exec.NewCmdRunResultWithErr(fmt.Errorf("Expected at least one fetch option"))

pkg/app/app_reconcile.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ import (
1515
"sigs.k8s.io/controller-runtime/pkg/reconcile"
1616
)
1717

18+
const appResourceType = "app"
19+
1820
// Reconcile is not expected to be called concurrently
1921
func (a *App) Reconcile(force bool) (reconcile.Result, error) {
2022
defer a.flushUpdateStatus("app reconciled")
2123

2224
var err error
2325

24-
a.appMetrics.InitMetrics(a.Name(), a.Namespace())
26+
a.countMetrics.InitMetrics(appResourceType, a.Name(), a.Namespace())
2527

2628
timerOpts := ReconcileTimerOpts{
2729
DefaultSyncPeriod: a.opts.DefaultSyncPeriod,
@@ -104,13 +106,9 @@ func (a *App) reconcileDeploy() error {
104106

105107
func (a *App) reconcileFetchTemplateDeploy() exec.CmdRunResult {
106108
reconcileStartTS := time.Now()
107-
a.isFirstReconcile = "false"
108-
if a.appMetrics.GetReconcileAttemptCounterValue(a.app.Name, a.app.Namespace) == 1 {
109-
a.isFirstReconcile = "true"
110-
}
111-
109+
a.isFirstReconcile = a.countMetrics.GetReconcileAttemptCounterValue("app", a.app.Name, a.app.Namespace) == 1
112110
defer func() {
113-
a.timeMetrics.RegisterOverallTime(a.app.Kind, a.app.Name, a.app.Namespace, a.isFirstReconcile,
111+
a.timeMetrics.RegisterOverallTime(appResourceType, a.app.Name, a.app.Namespace, a.isFirstReconcile,
114112
time.Since(reconcileStartTS))
115113
}()
116114

@@ -140,7 +138,7 @@ func (a *App) reconcileFetchTemplateDeploy() exec.CmdRunResult {
140138
UpdatedAt: metav1.NewTime(time.Now().UTC()),
141139
}
142140

143-
a.timeMetrics.RegisterFetchTime(a.app.Kind, a.app.Name, a.app.Namespace, a.isFirstReconcile,
141+
a.timeMetrics.RegisterFetchTime(appResourceType, a.app.Name, a.app.Namespace, a.isFirstReconcile,
144142
a.app.Status.Fetch.UpdatedAt.Sub(a.app.Status.Fetch.StartedAt.Time))
145143

146144
err := a.updateStatus("marking fetch completed")
@@ -164,7 +162,7 @@ func (a *App) reconcileFetchTemplateDeploy() exec.CmdRunResult {
164162
UpdatedAt: metav1.NewTime(time.Now().UTC()),
165163
}
166164

167-
a.timeMetrics.RegisterTemplateTime(a.app.Kind, a.app.Name, a.app.Namespace, a.isFirstReconcile,
165+
a.timeMetrics.RegisterTemplateTime(appResourceType, a.app.Name, a.app.Namespace, a.isFirstReconcile,
168166
a.app.Status.Template.UpdatedAt.Sub(templateStartTime))
169167

170168
err = a.updateStatus("marking template completed")
@@ -215,7 +213,7 @@ func (a *App) updateLastDeploy(result exec.CmdRunResult) exec.CmdRunResult {
215213
},
216214
}
217215

218-
a.timeMetrics.RegisterDeployTime(a.app.Kind, a.app.Name, a.app.Namespace, a.isFirstReconcile,
216+
a.timeMetrics.RegisterDeployTime(appResourceType, a.app.Name, a.app.Namespace, a.isFirstReconcile,
219217
a.Status().Deploy.UpdatedAt.Sub(a.Status().Deploy.StartedAt.Time))
220218

221219
return result
@@ -269,7 +267,7 @@ func (a *App) setReconciling() {
269267
Status: corev1.ConditionTrue,
270268
})
271269

272-
a.appMetrics.RegisterReconcileAttempt(a.app.Name, a.app.Namespace)
270+
a.countMetrics.RegisterReconcileAttempt(appResourceType, a.app.Name, a.app.Namespace)
273271
a.app.Status.FriendlyDescription = "Reconciling"
274272
}
275273

@@ -285,7 +283,7 @@ func (a *App) setReconcileCompleted(result exec.CmdRunResult) {
285283
a.app.Status.ConsecutiveReconcileFailures++
286284
a.app.Status.ConsecutiveReconcileSuccesses = 0
287285
a.app.Status.FriendlyDescription = fmt.Sprintf("Reconcile failed: %s", result.ErrorStr())
288-
a.appMetrics.RegisterReconcileFailure(a.app.Name, a.app.Namespace)
286+
a.countMetrics.RegisterReconcileFailure(appResourceType, a.app.Name, a.app.Namespace)
289287
a.setUsefulErrorMessage(result)
290288
} else {
291289
a.app.Status.Conditions = append(a.app.Status.Conditions, v1alpha1.Condition{
@@ -296,7 +294,7 @@ func (a *App) setReconcileCompleted(result exec.CmdRunResult) {
296294
a.app.Status.ConsecutiveReconcileSuccesses++
297295
a.app.Status.ConsecutiveReconcileFailures = 0
298296
a.app.Status.FriendlyDescription = "Reconcile succeeded"
299-
a.appMetrics.RegisterReconcileSuccess(a.app.Name, a.app.Namespace)
297+
a.countMetrics.RegisterReconcileSuccess(appResourceType, a.app.Name, a.app.Namespace)
300298
a.app.Status.UsefulErrorMessage = ""
301299
}
302300
}
@@ -309,7 +307,7 @@ func (a *App) setDeleting() {
309307
Status: corev1.ConditionTrue,
310308
})
311309

312-
a.appMetrics.RegisterReconcileDeleteAttempt(a.app.Name, a.app.Namespace)
310+
a.countMetrics.RegisterReconcileDeleteAttempt(appResourceType, a.app.Name, a.app.Namespace)
313311
a.app.Status.FriendlyDescription = "Deleting"
314312
}
315313

@@ -325,10 +323,10 @@ func (a *App) setDeleteCompleted(result exec.CmdRunResult) {
325323
a.app.Status.ConsecutiveReconcileFailures++
326324
a.app.Status.ConsecutiveReconcileSuccesses = 0
327325
a.app.Status.FriendlyDescription = fmt.Sprintf("Delete failed: %s", result.ErrorStr())
328-
a.appMetrics.RegisterReconcileDeleteFailed(a.app.Name, a.app.Namespace)
326+
a.countMetrics.RegisterReconcileDeleteFailed(appResourceType, a.app.Name, a.app.Namespace)
329327
a.setUsefulErrorMessage(result)
330328
} else {
331-
a.appMetrics.DeleteMetrics(a.app.Name, a.app.Namespace)
329+
a.countMetrics.DeleteMetrics(appResourceType, a.app.Name, a.app.Namespace)
332330
}
333331
}
334332

pkg/app/app_reconcile_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
func Test_NoInspectReconcile_IfNoDeployAttempted(t *testing.T) {
3131
log := logf.Log.WithName("kc")
3232
var (
33-
appMetrics = metrics.NewAppMetrics()
33+
appMetrics = metrics.NewCountMetrics()
3434
timeMetrics = metrics.NewReconcileTimeMetrics()
3535
)
3636

@@ -90,7 +90,7 @@ func Test_NoInspectReconcile_IfNoDeployAttempted(t *testing.T) {
9090
func Test_NoInspectReconcile_IfInspectNotEnabled(t *testing.T) {
9191
log := logf.Log.WithName("kc")
9292
var (
93-
appMetrics = metrics.NewAppMetrics()
93+
appMetrics = metrics.NewCountMetrics()
9494
timeMetrics = metrics.NewReconcileTimeMetrics()
9595
)
9696

@@ -171,7 +171,7 @@ func Test_NoInspectReconcile_IfInspectNotEnabled(t *testing.T) {
171171
func Test_TemplateError_DisplayedInStatus_UsefulErrorMessageProperty(t *testing.T) {
172172
log := logf.Log.WithName("kc")
173173
var (
174-
appMetrics = metrics.NewAppMetrics()
174+
appMetrics = metrics.NewCountMetrics()
175175
timeMetrics = metrics.NewReconcileTimeMetrics()
176176
)
177177

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.NewAppMetrics(), metrics.NewReconcileTimeMetrics(), fakeInfo)
64+
app := NewApp(appEmpty, Hooks{}, fetchFac, tmpFac, deployFac, log, Opts{}, metrics.NewCountMetrics(), metrics.NewReconcileTimeMetrics(), fakeInfo)
6565

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

pkg/app/crd_app.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,20 @@ import (
2121
)
2222

2323
type CRDApp struct {
24-
app *App
25-
appModel *kcv1alpha1.App
26-
log logr.Logger
27-
appMetrics *metrics.AppMetrics
28-
appClient kcclient.Interface
24+
app *App
25+
appModel *kcv1alpha1.App
26+
log logr.Logger
27+
countMetrics *metrics.ReconcileCountMetrics
28+
appClient kcclient.Interface
2929
}
3030

3131
// NewCRDApp creates new CRD app
32-
func NewCRDApp(appModel *kcv1alpha1.App, log logr.Logger, appMetrics *metrics.AppMetrics,
32+
func NewCRDApp(appModel *kcv1alpha1.App, log logr.Logger, appMetrics *metrics.ReconcileCountMetrics,
3333
timeMetrics *metrics.ReconcileTimeMetrics, appClient kcclient.Interface, fetchFactory fetch.Factory,
3434
templateFactory template.Factory, deployFactory deploy.Factory,
3535
compInfo ComponentInfo, opts Opts) *CRDApp {
3636

37-
crdApp := &CRDApp{appModel: appModel, log: log, appMetrics: appMetrics, appClient: appClient}
37+
crdApp := &CRDApp{appModel: appModel, log: log, countMetrics: appMetrics, appClient: appClient}
3838

3939
crdApp.app = NewApp(*appModel, Hooks{
4040
BlockDeletion: crdApp.blockDeletion,

0 commit comments

Comments
 (0)