Skip to content

Commit

Permalink
use packagerepo status instead of app status
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhelfand authored and ewrenn8 committed Jun 11, 2021
1 parent d0d744d commit a21d8af
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 175 deletions.
16 changes: 0 additions & 16 deletions config/crds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1178,22 +1178,6 @@ spec:
type: object
friendlyDescription:
type: string
inspect:
properties:
error:
type: string
exitCode:
type: integer
stderr:
type: string
stdout:
type: string
updatedAt:
format: date-time
type: string
type: object
managedAppName:
type: string
observedGeneration:
format: int64
type: integer
Expand Down
17 changes: 16 additions & 1 deletion pkg/apis/packaging/v1alpha1/package_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type PackageRepository struct {

Spec PackageRepositorySpec `json:"spec"`
// +optional
Status v1alpha1.AppStatus `json:"status,omitempty"`
Status PackageRepositoryStatus `json:"status,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down Expand Up @@ -57,3 +57,18 @@ type PackageRepositoryFetch struct {
// +optional
ImgpkgBundle *v1alpha1.AppFetchImgpkgBundle `json:"imgpkgBundle,omitempty"`
}

type PackageRepositoryStatus struct {
// +optional
Fetch *v1alpha1.AppStatusFetch `json:"fetch,omitempty"`
// +optional
Template *v1alpha1.AppStatusTemplate `json:"template,omitempty"`
// +optional
Deploy *v1alpha1.AppStatusDeploy `json:"deploy,omitempty"`
// +optional
ConsecutiveReconcileSuccesses int `json:"consecutiveReconcileSuccesses,omitempty"`
// +optional
ConsecutiveReconcileFailures int `json:"consecutiveReconcileFailures,omitempty"`
// +optional
v1alpha1.GenericStatus `json:",inline"`
}
32 changes: 32 additions & 0 deletions pkg/apis/packaging/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 1 addition & 32 deletions pkg/pkgrepository/app_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/exec"
)

// TODO: Remove changedFunc logic
func (a *App) deploy(tplOutput string, changedFunc func(exec.CmdRunResult)) exec.CmdRunResult {
err := a.blockDeletion()
if err != nil {
Expand Down Expand Up @@ -88,38 +89,6 @@ func (a *App) delete(changedFunc func(exec.CmdRunResult)) exec.CmdRunResult {
return result
}

func (a *App) inspect() exec.CmdRunResult {
if len(a.app.Spec.Deploy) != 1 {
return exec.NewCmdRunResultWithErr(fmt.Errorf("Expected exactly one deploy option"))
}

var result exec.CmdRunResult

for _, dep := range a.app.Spec.Deploy {
switch {
case dep.Kapp != nil:
cancelCh, closeCancelCh := a.newCancelCh()
defer closeCancelCh()

kapp, err := a.newKapp(*dep.Kapp, cancelCh)
if err != nil {
return exec.NewCmdRunResultWithErr(fmt.Errorf("Preparing kapp: %s", err))
}

result = kapp.Inspect()

default:
result.AttachErrorf("%s", fmt.Errorf("Unsupported way to inspect"))
}

if result.Error != nil {
break
}
}

return result
}

func (a *App) newKapp(kapp v1alpha1.AppDeployKapp, cancelCh chan struct{}) (*ctldep.Kapp, error) {
genericOpts := ctldep.GenericOpts{Name: a.app.Name, Namespace: a.app.Namespace}
return a.deployFactory.NewKappPrivileged(kapp, genericOpts, cancelCh)
Expand Down
26 changes: 5 additions & 21 deletions pkg/pkgrepository/app_reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,6 @@ func (a *App) reconcileDeploy() error {
result := a.reconcileFetchTemplateDeploy()
a.setReconcileCompleted(result)

// Reconcile inspect regardless of deploy success
// but don't inspect if deploy never attempted
if a.app.Status.Deploy != nil {
_ = a.reconcileInspect()
}

return a.updateStatus("marking reconcile completed")
}

Expand Down Expand Up @@ -147,7 +141,11 @@ func (a *App) reconcileFetchTemplateDeploy() exec.CmdRunResult {

a.resetLastDeployStartedAt()

return a.updateLastDeploy(a.deploy(tplResult.Stdout, a.updateLastDeployNoReturn))
// Deployment should not take long time, so no
// need to regularly update status. Passing in
// empty func for a.deploy so only updateLastDeploy
// sets status for Deploy portion of status.
return a.updateLastDeploy(a.deploy(tplResult.Stdout, func(result exec.CmdRunResult) {}))
}

func (a *App) updateLastDeploy(result exec.CmdRunResult) exec.CmdRunResult {
Expand Down Expand Up @@ -186,20 +184,6 @@ func (a *App) resetLastDeployStartedAt() {
a.app.Status.Deploy.StartedAt = metav1.NewTime(time.Now().UTC())
}

func (a *App) reconcileInspect() error {
inspectResult := a.inspect().WithFriendlyYAMLStrings()

a.app.Status.Inspect = &v1alpha1.AppStatusInspect{
Stdout: inspectResult.Stdout,
Stderr: inspectResult.Stderr,
ExitCode: inspectResult.ExitCode,
Error: inspectResult.ErrorStr(),
UpdatedAt: metav1.NewTime(time.Now().UTC()),
}

return a.updateStatus("marking inspect completed")
}

func (a *App) markObservedLatest() {
a.app.Status.ObservedGeneration = a.app.Generation
}
Expand Down
28 changes: 13 additions & 15 deletions pkg/pkgrepository/crd_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ package pkgrepository
import (
"context"
"fmt"
pkgingv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/packaging/v1alpha1"
"reflect"

"github.com/go-logr/logr"
kcv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
pkgingv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/packaging/v1alpha1"
kcclient "github.com/vmware-tanzu/carvel-kapp-controller/pkg/client/clientset/versioned"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/deploy"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/fetch"
Expand Down Expand Up @@ -38,7 +36,6 @@ func NewCRDApp(appModel *kcv1alpha1.App, packageRepo *pkgingv1alpha1.PackageRepo
BlockDeletion: crdApp.blockDeletion,
UnblockDeletion: crdApp.unblockDeletion,
UpdateStatus: crdApp.updateStatus,
WatchChanges: crdApp.watchChanges,
}, fetchFactory, templateFactory, deployFactory, log)

return crdApp
Expand Down Expand Up @@ -81,18 +78,23 @@ func (a *CRDApp) updateStatus(desc string) error {
}

func (a *CRDApp) updateStatusOnce() error {
existingApp, err := a.appClient.PackagingV1alpha1().PackageRepositories(a.pkgrModel.Namespace).Get(context.Background(), a.pkgrModel.Name, metav1.GetOptions{})
existingRepo, err := a.appClient.PackagingV1alpha1().PackageRepositories(a.pkgrModel.Namespace).Get(context.Background(), a.pkgrModel.Name, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("Fetching app: %s", err)
}

if !reflect.DeepEqual(existingApp.Status, a.app.Status()) {
existingRepo.Status = pkgingv1alpha1.PackageRepositoryStatus{
Fetch: a.app.Status().Fetch,
Template: a.app.Status().Template,
Deploy: a.app.Status().Deploy,
GenericStatus: a.app.Status().GenericStatus,
ConsecutiveReconcileSuccesses: a.app.Status().ConsecutiveReconcileSuccesses,
ConsecutiveReconcileFailures: a.app.Status().ConsecutiveReconcileFailures,
}

existingApp.Status = a.app.Status()
_, err = a.appClient.PackagingV1alpha1().PackageRepositories(existingApp.Namespace).UpdateStatus(context.Background(), existingApp, metav1.UpdateOptions{})
if err != nil {
return err
}
_, err = a.appClient.PackagingV1alpha1().PackageRepositories(existingRepo.Namespace).UpdateStatus(context.Background(), existingRepo, metav1.UpdateOptions{})
if err != nil {
return err
}

return nil
Expand Down Expand Up @@ -120,10 +122,6 @@ func (a *CRDApp) Reconcile(force bool) (reconcile.Result, error) {
return a.app.Reconcile(force)
}

func (a *CRDApp) watchChanges(callback func(kcv1alpha1.App), cancelCh chan struct{}) error {
return NewCRDAppWatcher(*a.appModel, a.appClient).Watch(callback, cancelCh)
}

func (a *CRDApp) ResourceRefs() map[reftracker.RefKey]struct{} {
return a.app.SecretRefs()
}
87 changes: 0 additions & 87 deletions pkg/pkgrepository/crd_app_watcher.go

This file was deleted.

19 changes: 16 additions & 3 deletions pkg/pkgrepository/package_repo_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ package pkgrepository
import (
kcv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
pkgingv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/packaging/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"time"
)

func NewPackageRepoApp(pkgRepository *pkgingv1alpha1.PackageRepository) (*kcv1alpha1.App, error) {
desiredApp := &kcv1alpha1.App{}

desiredApp.Name = pkgRepository.Name
desiredApp.Namespace = pkgRepository.Namespace
desiredApp.Status = pkgRepository.Status
desiredApp.DeletionTimestamp = pkgRepository.DeletionTimestamp

desiredApp.Spec = kcv1alpha1.AppSpec{
Expand All @@ -32,14 +33,26 @@ func NewPackageRepoApp(pkgRepository *pkgingv1alpha1.PackageRepository) (*kcv1al
Deploy: []kcv1alpha1.AppDeploy{{
Kapp: &kcv1alpha1.AppDeployKapp{},
}},
SyncPeriod: pkgRepository.Spec.SyncPeriod,
Paused: pkgRepository.Spec.Paused,
Paused: pkgRepository.Spec.Paused,
}

if pkgRepository.Spec.SyncPeriod == nil {
desiredApp.Spec.SyncPeriod = &metav1.Duration{Duration: time.Minute * 5}
}

if desiredApp.Spec.Fetch[0].ImgpkgBundle != nil {
desiredApp.Spec.Template = append(desiredApp.Spec.Template,
kcv1alpha1.AppTemplate{Kbld: &kcv1alpha1.AppTemplateKbld{Paths: []string{"-", ".imgpkg/images.yml"}}})
}

desiredApp.Status = kcv1alpha1.AppStatus{
Fetch: pkgRepository.Status.Fetch,
Template: pkgRepository.Status.Template,
Deploy: pkgRepository.Status.Deploy,
GenericStatus: pkgRepository.Status.GenericStatus,
ConsecutiveReconcileSuccesses: pkgRepository.Status.ConsecutiveReconcileSuccesses,
ConsecutiveReconcileFailures: pkgRepository.Status.ConsecutiveReconcileFailures,
}

return desiredApp, nil
}

0 comments on commit a21d8af

Please sign in to comment.