Skip to content

Commit

Permalink
Use kind instead of declearing const resourceType
Browse files Browse the repository at this point in the history
  • Loading branch information
sethiyash committed Jan 3, 2024
1 parent 0174469 commit 02e4e67
Show file tree
Hide file tree
Showing 16 changed files with 46 additions and 50 deletions.
8 changes: 4 additions & 4 deletions cmd/controller/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ func Run(opts Options, runLog logr.Logger) error {
// initialize cluster access once - it contains a service account token cache which should be only setup once.
kubeconf := kubeconfig.NewKubeconfig(coreClient, runLog)
compInfo := componentinfo.NewComponentInfo(coreClient, kubeconf, Version)
appMetrics := &metrics.Metrics{ReconcileCountMetrics: countMetrics, ReconcileTimeMetrics: reconcileTimeMetrics}

cacheFolderApps := memdir.NewTmpDir("cache-appcr")
err = cacheFolderApps.Create()
Expand All @@ -200,7 +201,7 @@ func Run(opts Options, runLog logr.Logger) error {
CoreClient: coreClient,
AppClient: kcClient,
KcConfig: kcConfig,
AppMetrics: &metrics.Metrics{ReconcileCountMetrics: countMetrics, ReconcileTimeMetrics: reconcileTimeMetrics},
AppMetrics: appMetrics,
CmdRunner: sidecarCmdExec,
Kubeconf: kubeconf,
CompInfo: compInfo,
Expand Down Expand Up @@ -231,8 +232,7 @@ func Run(opts Options, runLog logr.Logger) error {
kcClient, opts.PackagingGlobalNS, runLog.WithName("handler"))

reconciler := pkginstall.NewReconciler(kcClient, pkgClient, coreClient, pkgToPkgInstallHandler,
runLog.WithName("pkgi"), compInfo, kcConfig, &metrics.Metrics{ReconcileCountMetrics: countMetrics,
ReconcileTimeMetrics: reconcileTimeMetrics})
runLog.WithName("pkgi"), compInfo, kcConfig, appMetrics)

ctrl, err := controller.New("pkgi", mgr, controller.Options{
Reconciler: reconciler,
Expand All @@ -259,7 +259,7 @@ func Run(opts Options, runLog logr.Logger) error {
CoreClient: coreClient,
AppClient: kcClient,
KcConfig: kcConfig,
AppMetrics: &metrics.Metrics{ReconcileCountMetrics: countMetrics, ReconcileTimeMetrics: reconcileTimeMetrics},
AppMetrics: appMetrics,
CmdRunner: sidecarCmdExec,
Kubeconf: kubeconf,
CacheFolder: cacheFolderPkgRepoApps,
Expand Down
1 change: 1 addition & 0 deletions pkg/app/app_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package app

import (
"fmt"

"github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
ctldep "github.com/vmware-tanzu/carvel-kapp-controller/pkg/deploy"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/exec"
Expand Down
1 change: 0 additions & 1 deletion pkg/app/app_fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const (
)

func (a *App) fetch(dstPath string) (string, exec.CmdRunResult) {
// fetch init stage
if len(a.app.Spec.Fetch) == 0 {
return "", exec.NewCmdRunResultWithErr(fmt.Errorf("Expected at least one fetch option"))
}
Expand Down
26 changes: 12 additions & 14 deletions pkg/app/app_reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

const appResourceType = "app"

// Reconcile is not expected to be called concurrently
func (a *App) Reconcile(force bool) (reconcile.Result, error) {
defer a.flushUpdateStatus("app reconciled")

var err error

a.appMetrics.ReconcileCountMetrics.InitMetrics(appResourceType, a.Name(), a.Namespace())
a.appMetrics.ReconcileCountMetrics.InitMetrics(a.app.Kind, a.Name(), a.Namespace())

timerOpts := ReconcileTimerOpts{
DefaultSyncPeriod: a.opts.DefaultSyncPeriod,
Expand Down Expand Up @@ -106,9 +104,9 @@ func (a *App) reconcileDeploy() error {

func (a *App) reconcileFetchTemplateDeploy() exec.CmdRunResult {
reconcileStartTime := time.Now()
a.isFirstReconcile = a.appMetrics.ReconcileCountMetrics.GetReconcileAttemptCounterValue("app", a.app.Name, a.app.Namespace) == 1
a.appMetrics.IsFirstReconcile = a.appMetrics.ReconcileCountMetrics.GetReconcileAttemptCounterValue(a.app.Kind, a.Name(), a.Namespace()) == 1
defer func() {
a.appMetrics.ReconcileTimeMetrics.RegisterOverallTime(appResourceType, a.app.Name, a.app.Namespace, a.isFirstReconcile,
a.appMetrics.ReconcileTimeMetrics.RegisterOverallTime(a.app.Kind, a.Name(), a.Namespace(), a.appMetrics.IsFirstReconcile,
time.Since(reconcileStartTime))
}()

Expand Down Expand Up @@ -138,7 +136,7 @@ func (a *App) reconcileFetchTemplateDeploy() exec.CmdRunResult {
UpdatedAt: metav1.NewTime(time.Now().UTC()),
}

a.appMetrics.ReconcileTimeMetrics.RegisterFetchTime(appResourceType, a.app.Name, a.app.Namespace, a.isFirstReconcile,
a.appMetrics.ReconcileTimeMetrics.RegisterFetchTime(a.app.Kind, a.Name(), a.Namespace(), a.appMetrics.IsFirstReconcile,
a.app.Status.Fetch.UpdatedAt.Sub(a.app.Status.Fetch.StartedAt.Time))

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

a.appMetrics.ReconcileTimeMetrics.RegisterTemplateTime(appResourceType, a.app.Name, a.app.Namespace, a.isFirstReconcile,
a.appMetrics.ReconcileTimeMetrics.RegisterTemplateTime(a.app.Kind, a.Name(), a.Namespace(), a.appMetrics.IsFirstReconcile,
a.app.Status.Template.UpdatedAt.Sub(templateStartTime))

err = a.updateStatus("marking template completed")
Expand Down Expand Up @@ -213,7 +211,7 @@ func (a *App) updateLastDeploy(result exec.CmdRunResult) exec.CmdRunResult {
},
}

a.appMetrics.ReconcileTimeMetrics.RegisterDeployTime(appResourceType, a.app.Name, a.app.Namespace, a.isFirstReconcile,
a.appMetrics.ReconcileTimeMetrics.RegisterDeployTime(a.app.Kind, a.Name(), a.Namespace(), a.appMetrics.IsFirstReconcile,
a.Status().Deploy.UpdatedAt.Sub(a.Status().Deploy.StartedAt.Time))

return result
Expand Down Expand Up @@ -267,7 +265,7 @@ func (a *App) setReconciling() {
Status: corev1.ConditionTrue,
})

a.appMetrics.ReconcileCountMetrics.RegisterReconcileAttempt(appResourceType, a.app.Name, a.app.Namespace)
a.appMetrics.ReconcileCountMetrics.RegisterReconcileAttempt(a.app.Kind, a.Name(), a.Namespace())
a.app.Status.FriendlyDescription = "Reconciling"
}

Expand All @@ -283,7 +281,7 @@ func (a *App) setReconcileCompleted(result exec.CmdRunResult) {
a.app.Status.ConsecutiveReconcileFailures++
a.app.Status.ConsecutiveReconcileSuccesses = 0
a.app.Status.FriendlyDescription = fmt.Sprintf("Reconcile failed: %s", result.ErrorStr())
a.appMetrics.ReconcileCountMetrics.RegisterReconcileFailure(appResourceType, a.app.Name, a.app.Namespace)
a.appMetrics.ReconcileCountMetrics.RegisterReconcileFailure(a.app.Kind, a.Name(), a.Namespace())
a.setUsefulErrorMessage(result)
} else {
a.app.Status.Conditions = append(a.app.Status.Conditions, v1alpha1.Condition{
Expand All @@ -294,7 +292,7 @@ func (a *App) setReconcileCompleted(result exec.CmdRunResult) {
a.app.Status.ConsecutiveReconcileSuccesses++
a.app.Status.ConsecutiveReconcileFailures = 0
a.app.Status.FriendlyDescription = "Reconcile succeeded"
a.appMetrics.ReconcileCountMetrics.RegisterReconcileSuccess(appResourceType, a.app.Name, a.app.Namespace)
a.appMetrics.ReconcileCountMetrics.RegisterReconcileSuccess(a.app.Kind, a.Name(), a.Namespace())
a.app.Status.UsefulErrorMessage = ""
}
}
Expand All @@ -307,7 +305,7 @@ func (a *App) setDeleting() {
Status: corev1.ConditionTrue,
})

a.appMetrics.ReconcileCountMetrics.RegisterReconcileDeleteAttempt(appResourceType, a.app.Name, a.app.Namespace)
a.appMetrics.ReconcileCountMetrics.RegisterReconcileDeleteAttempt(a.app.Kind, a.Name(), a.Namespace())
a.app.Status.FriendlyDescription = "Deleting"
}

Expand All @@ -323,10 +321,10 @@ func (a *App) setDeleteCompleted(result exec.CmdRunResult) {
a.app.Status.ConsecutiveReconcileFailures++
a.app.Status.ConsecutiveReconcileSuccesses = 0
a.app.Status.FriendlyDescription = fmt.Sprintf("Delete failed: %s", result.ErrorStr())
a.appMetrics.ReconcileCountMetrics.RegisterReconcileDeleteFailed(appResourceType, a.app.Name, a.app.Namespace)
a.appMetrics.ReconcileCountMetrics.RegisterReconcileDeleteFailed(a.app.Kind, a.Name(), a.Namespace())
a.setUsefulErrorMessage(result)
} else {
a.appMetrics.ReconcileCountMetrics.DeleteMetrics(appResourceType, a.app.Name, a.app.Namespace)
a.appMetrics.ReconcileCountMetrics.DeleteMetrics(a.app.Kind, a.Name(), a.Namespace())
}
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/app/app_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ package app

import (
"fmt"
"strings"

"github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/exec"
ctltpl "github.com/vmware-tanzu/carvel-kapp-controller/pkg/template"
"strings"
)

func (a *App) template(dirPath string) exec.CmdRunResult {
Expand Down
11 changes: 5 additions & 6 deletions pkg/app/crd_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,18 @@ import (
)

type CRDApp struct {
app *App
appModel *kcv1alpha1.App
log logr.Logger
countMetrics *metrics.ReconcileCountMetrics
appClient kcclient.Interface
app *App
appModel *kcv1alpha1.App
log logr.Logger
appClient kcclient.Interface
}

// NewCRDApp creates new CRD app
func NewCRDApp(appModel *kcv1alpha1.App, log logr.Logger, appMetrics *metrics.Metrics, appClient kcclient.Interface, fetchFactory fetch.Factory,
templateFactory template.Factory, deployFactory deploy.Factory,
compInfo ComponentInfo, opts Opts) *CRDApp {

crdApp := &CRDApp{appModel: appModel, log: log, countMetrics: appMetrics.ReconcileCountMetrics, appClient: appClient}
crdApp := &CRDApp{appModel: appModel, log: log, appClient: appClient}

crdApp.app = NewApp(*appModel, Hooks{
BlockDeletion: crdApp.blockDeletion,
Expand Down
1 change: 1 addition & 0 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ package metrics
type Metrics struct {
*ReconcileCountMetrics
*ReconcileTimeMetrics
IsFirstReconcile bool
}
5 changes: 3 additions & 2 deletions pkg/packageinstall/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ package packageinstall

import (
"fmt"
"sort"
"strings"

"github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
kcv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
pkgingv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/packaging/v1alpha1"
datapkgingv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apiserver/apis/datapackaging/v1alpha1"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/client/clientset/versioned/scheme"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sort"
"strings"
)

const (
Expand Down
15 changes: 6 additions & 9 deletions pkg/packageinstall/packageinstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package packageinstall
import (
"context"
"fmt"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/metrics"
"time"

"github.com/go-logr/logr"
Expand All @@ -18,6 +17,7 @@ import (
pkgclient "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apiserver/client/clientset/versioned"
kcclient "github.com/vmware-tanzu/carvel-kapp-controller/pkg/client/clientset/versioned"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/client/clientset/versioned/scheme"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/metrics"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/reconciler"
"github.com/vmware-tanzu/carvel-vendir/pkg/vendir/versions"
verv1alpha1 "github.com/vmware-tanzu/carvel-vendir/pkg/vendir/versions/v1alpha1"
Expand All @@ -42,7 +42,6 @@ const (
// PackageInstall to indicate that lower version of the package
// can be selected vs whats currently installed.
DowngradableAnnKey = "packaging.carvel.dev/downgradable"
packageInstallType = "pkgi"
)

// nolint: revive
Expand All @@ -58,8 +57,6 @@ type PackageInstallCR struct {
opts Opts

pkgMetrics *metrics.Metrics

firstReconcile bool
}

// nolint: revive
Expand All @@ -81,7 +78,7 @@ func (pi *PackageInstallCR) Reconcile() (reconcile.Result, error) {
func(st kcv1alpha1.GenericStatus) { pi.model.Status.GenericStatus = st },
}

pi.pkgMetrics.ReconcileCountMetrics.InitMetrics(packageInstallType, pi.model.Name, pi.model.Namespace)
pi.pkgMetrics.ReconcileCountMetrics.InitMetrics(pi.model.Kind, pi.model.Name, pi.model.Namespace)

var result reconcile.Result
var err error
Expand Down Expand Up @@ -109,13 +106,13 @@ func (pi *PackageInstallCR) Reconcile() (reconcile.Result, error) {

func (pi *PackageInstallCR) reconcile(modelStatus *reconciler.Status) (reconcile.Result, error) {
pi.log.Info("Reconciling")
pi.pkgMetrics.ReconcileCountMetrics.RegisterReconcileAttempt(packageInstallType, pi.model.Name, pi.model.Namespace)
pi.pkgMetrics.ReconcileCountMetrics.RegisterReconcileAttempt(pi.model.Kind, pi.model.Name, pi.model.Namespace)

reconcileStartTime := time.Now()
pi.firstReconcile = pi.pkgMetrics.ReconcileCountMetrics.GetReconcileAttemptCounterValue("pkgi", pi.model.Name, pi.model.Namespace) == 1
pi.pkgMetrics.IsFirstReconcile = pi.pkgMetrics.ReconcileCountMetrics.GetReconcileAttemptCounterValue("pkgi", pi.model.Name, pi.model.Namespace) == 1
defer func() {
pi.pkgMetrics.ReconcileTimeMetrics.RegisterOverallTime(packageInstallType, pi.model.Name, pi.model.Namespace,
pi.firstReconcile, time.Since(reconcileStartTime))
pi.pkgMetrics.ReconcileTimeMetrics.RegisterOverallTime(pi.model.Kind, pi.model.Name, pi.model.Namespace,
pi.pkgMetrics.IsFirstReconcile, time.Since(reconcileStartTime))
}()

err := pi.blockDeletion()
Expand Down
2 changes: 1 addition & 1 deletion pkg/packageinstall/packageinstall_deletion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package packageinstall

import (
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/metrics"
"testing"

"github.com/k14s/semver/v4"
Expand All @@ -13,6 +12,7 @@ import (
pkgingv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/packaging/v1alpha1"
fakeapiserver "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apiserver/client/clientset/versioned/fake"
fakekappctrl "github.com/vmware-tanzu/carvel-kapp-controller/pkg/client/clientset/versioned/fake"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/metrics"
versions "github.com/vmware-tanzu/carvel-vendir/pkg/vendir/versions/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
Expand Down
2 changes: 1 addition & 1 deletion pkg/packageinstall/packageinstall_downgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package packageinstall

import (
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/metrics"
"testing"

"github.com/k14s/semver/v4"
Expand All @@ -15,6 +14,7 @@ import (
datapkgingv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apiserver/apis/datapackaging/v1alpha1"
fakeapiserver "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apiserver/client/clientset/versioned/fake"
fakekappctrl "github.com/vmware-tanzu/carvel-kapp-controller/pkg/client/clientset/versioned/fake"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/metrics"
versions "github.com/vmware-tanzu/carvel-vendir/pkg/vendir/versions/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down
1 change: 1 addition & 0 deletions pkg/packageinstall/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package packageinstall
import (
"context"
"fmt"

"github.com/go-logr/logr"
kappctrlv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
pkgingv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/packaging/v1alpha1"
Expand Down
1 change: 0 additions & 1 deletion pkg/pkgrepository/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ type App struct {

log logr.Logger

isFirstReconcile bool
pendingStatusUpdate bool
flushAllStatusUpdates bool
}
Expand Down
1 change: 1 addition & 0 deletions pkg/pkgrepository/app_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package pkgrepository

import (
"fmt"

"github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
ctldep "github.com/vmware-tanzu/carvel-kapp-controller/pkg/deploy"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/exec"
Expand Down
2 changes: 1 addition & 1 deletion pkg/pkgrepository/app_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package pkgrepository

import (
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/metrics"
"path/filepath"

"github.com/go-logr/logr"
Expand All @@ -17,6 +16,7 @@ import (
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/fetch"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/kubeconfig"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/memdir"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/metrics"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/template"
"k8s.io/client-go/kubernetes"
)
Expand Down
Loading

0 comments on commit 02e4e67

Please sign in to comment.