Skip to content

Commit

Permalink
Split deployManager into packages
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Manno committed Oct 30, 2023
1 parent 3c71c75 commit eb3ccfe
Show file tree
Hide file tree
Showing 12 changed files with 477 additions and 330 deletions.
24 changes: 19 additions & 5 deletions integrationtests/agent/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
"github.com/rancher/fleet/integrationtests/utils"
"github.com/rancher/fleet/internal/cmd/agent/controllers/bundledeployment"
"github.com/rancher/fleet/internal/cmd/agent/deployer"
"github.com/rancher/fleet/internal/cmd/agent/deployer/cleanup"
"github.com/rancher/fleet/internal/cmd/agent/deployer/driftdetect"
"github.com/rancher/fleet/internal/cmd/agent/deployer/monitor"
"github.com/rancher/fleet/internal/cmd/agent/trigger"
"github.com/rancher/fleet/internal/helmdeployer"
"github.com/rancher/fleet/internal/manifest"
Expand Down Expand Up @@ -146,18 +149,29 @@ func registerBundleDeploymentController(cfg *rest.Config, namespace string, look
wranglerApply, err := apply.NewForConfig(cfg)
Expect(err).ToNot(HaveOccurred())

deployManager := deployer.NewManager(
namespace,
namespace,
deployManager := deployer.New(
lookup,
helmDeployer)
cleanup := cleanup.New(
namespace,
namespace,
factory.Fleet().V1alpha1().BundleDeployment().Cache(),
factory.Fleet().V1alpha1().BundleDeployment(),
lookup,
helmDeployer)
monitor := monitor.New(
namespace,
namespace,
namespace,
helmDeployer,
wranglerApply)
driftdetect := driftdetect.New(
namespace,
namespace,
namespace,
helmDeployer,
wranglerApply)

bundledeployment.Register(ctx, trig, mapper, dyn, deployManager, factory.Fleet().V1alpha1().BundleDeployment())
bundledeployment.Register(ctx, trig, mapper, dyn, deployManager, cleanup, monitor, driftdetect, factory.Fleet().V1alpha1().BundleDeployment())

err = factory.Start(ctx, 50)
Expect(err).ToNot(HaveOccurred())
Expand Down
54 changes: 34 additions & 20 deletions internal/cmd/agent/controllers/bundledeployment/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import (
"github.com/sirupsen/logrus"

"github.com/rancher/fleet/internal/cmd/agent/deployer"
"github.com/rancher/fleet/internal/cmd/agent/deployer/cleanup"
"github.com/rancher/fleet/internal/cmd/agent/deployer/driftdetect"
"github.com/rancher/fleet/internal/cmd/agent/deployer/monitor"
"github.com/rancher/fleet/internal/cmd/agent/trigger"
"github.com/rancher/fleet/internal/helmdeployer"
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
Expand All @@ -38,28 +41,39 @@ var nsResource = schema.GroupVersionResource{Group: "", Version: "v1", Resource:
type handler struct {
cleanupOnce sync.Once

ctx context.Context
trigger *trigger.Trigger
deployManager *deployer.Manager
bdController fleetcontrollers.BundleDeploymentController
restMapper meta.RESTMapper
dynamic dynamic.Interface
ctx context.Context
trigger *trigger.Trigger

Deployer *deployer.Deployer
Monitor *monitor.Monitor
DriftDetect *driftdetect.DriftDetect
Cleanup *cleanup.Cleanup

bdController fleetcontrollers.BundleDeploymentController
restMapper meta.RESTMapper
dynamic dynamic.Interface
}

func Register(ctx context.Context,
trigger *trigger.Trigger,
restMapper meta.RESTMapper,
dynamic dynamic.Interface,
deployManager *deployer.Manager,
deployer *deployer.Deployer,
cleanup *cleanup.Cleanup,
monitor *monitor.Monitor,
driftDetect *driftdetect.DriftDetect,
bdController fleetcontrollers.BundleDeploymentController) {

h := &handler{
ctx: ctx,
trigger: trigger,
deployManager: deployManager,
bdController: bdController,
restMapper: restMapper,
dynamic: dynamic,
ctx: ctx,
trigger: trigger,
Deployer: deployer,
Cleanup: cleanup,
Monitor: monitor,
DriftDetect: driftDetect,
bdController: bdController,
restMapper: restMapper,
dynamic: dynamic,
}

fleetcontrollers.RegisterBundleDeploymentStatusHandler(ctx,
Expand All @@ -75,12 +89,12 @@ func Register(ctx context.Context,
h.MonitorBundle)

bdController.OnChange(ctx, "bundle-trigger", h.Trigger)
bdController.OnChange(ctx, "bundle-cleanup", h.Cleanup)
bdController.OnChange(ctx, "bundle-cleanup", h.CleanupReleases)
}

func (h *handler) garbageCollect() {
for {
if err := h.deployManager.Cleanup(); err != nil {
if err := h.Cleanup.Cleanup(); err != nil {
logrus.Errorf("failed to cleanup orphaned releases: %v", err)
}
select {
Expand All @@ -91,15 +105,15 @@ func (h *handler) garbageCollect() {
}
}

func (h *handler) Cleanup(key string, bd *fleet.BundleDeployment) (*fleet.BundleDeployment, error) {
func (h *handler) CleanupReleases(key string, bd *fleet.BundleDeployment) (*fleet.BundleDeployment, error) {
h.cleanupOnce.Do(func() {
go h.garbageCollect()
})

if bd != nil {
return bd, nil
}
return nil, h.deployManager.Delete(key)
return nil, h.Deployer.Delete(key)
}

func (h *handler) DeployBundle(bd *fleet.BundleDeployment, status fleet.BundleDeploymentStatus) (fleet.BundleDeploymentStatus, error) {
Expand All @@ -115,7 +129,7 @@ func (h *handler) DeployBundle(bd *fleet.BundleDeployment, status fleet.BundleDe
}

logrus.Infof("Deploying bundle %s/%s", bd.Namespace, bd.Name)
release, err := h.deployManager.Deploy(bd)
release, err := h.Deployer.Deploy(bd)
if err != nil {
// When an error from DeployBundle is returned it causes DeployBundle
// to requeue and keep trying to deploy on a loop. If there is something
Expand Down Expand Up @@ -356,7 +370,7 @@ func (h *handler) Trigger(key string, bd *fleet.BundleDeployment) (*fleet.Bundle

logrus.Debugf("Triggering for bundledeployment '%s'", key)

resources, err := h.deployManager.AllResources(bd)
resources, err := h.DriftDetect.AllResources(bd)
if err != nil {
return bd, err
}
Expand Down Expand Up @@ -436,7 +450,7 @@ func (h *handler) MonitorBundle(bd *fleet.BundleDeployment, status fleet.BundleD
return status, nil
}

err := h.deployManager.UpdateBundleDeploymentStatus(h.restMapper, bd)
err := h.Monitor.UpdateBundleDeploymentStatus(h.restMapper, bd)
if err != nil {

// Returning an error will cause MonitorBundle to requeue in a loop.
Expand Down
22 changes: 18 additions & 4 deletions internal/cmd/agent/controllers/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (

"github.com/rancher/fleet/internal/cmd/agent/controllers/bundledeployment"
"github.com/rancher/fleet/internal/cmd/agent/deployer"
"github.com/rancher/fleet/internal/cmd/agent/deployer/cleanup"
"github.com/rancher/fleet/internal/cmd/agent/deployer/driftdetect"
"github.com/rancher/fleet/internal/cmd/agent/deployer/monitor"
"github.com/rancher/fleet/internal/cmd/agent/trigger"
"github.com/rancher/fleet/internal/helmdeployer"
"github.com/rancher/fleet/internal/manifest"
Expand Down Expand Up @@ -90,14 +93,25 @@ func Register(ctx context.Context,
trigger.New(ctx, appCtx.restMapper, appCtx.Dynamic),
appCtx.restMapper,
appCtx.Dynamic,
deployer.NewManager(
deployer.New(
manifest.NewLookup(appCtx.Fleet.Content()),
helmDeployer),
cleanup.New(
fleetNamespace,
defaultNamespace,
labelPrefix,
agentScope,
appCtx.Fleet.BundleDeployment().Cache(),
appCtx.Fleet.BundleDeployment(),
manifest.NewLookup(appCtx.Fleet.Content()),
helmDeployer),
monitor.New(
defaultNamespace,
labelPrefix,
agentScope,
helmDeployer,
appCtx.Apply),
driftdetect.New(
defaultNamespace,
labelPrefix,
agentScope,
helmDeployer,
appCtx.Apply),
appCtx.Fleet.BundleDeployment())
Expand Down
81 changes: 81 additions & 0 deletions internal/cmd/agent/deployer/cleanup/cleanup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package cleanup

import (
"github.com/rancher/fleet/internal/helmdeployer"
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
fleetcontrollers "github.com/rancher/fleet/pkg/generated/controllers/fleet.cattle.io/v1alpha1"

"github.com/sirupsen/logrus"

apierror "k8s.io/apimachinery/pkg/api/errors"
)

type Cleanup struct {
fleetNamespace string
defaultNamespace string
bundleDeploymentCache fleetcontrollers.BundleDeploymentCache
deployer *helmdeployer.Helm
bundleDeploymentController fleetcontrollers.BundleDeploymentController
}

func New(fleetNamespace string,
defaultNamespace string,
bundleDeploymentCache fleetcontrollers.BundleDeploymentCache,
bundleDeploymentController fleetcontrollers.BundleDeploymentController,
deployer *helmdeployer.Helm) *Cleanup {
return &Cleanup{
fleetNamespace: fleetNamespace,
defaultNamespace: defaultNamespace,
bundleDeploymentCache: bundleDeploymentCache,
deployer: deployer,
bundleDeploymentController: bundleDeploymentController,
}
}

func (m *Cleanup) Cleanup() error {
deployed, err := m.deployer.ListDeployments()
if err != nil {
return err
}

for _, deployed := range deployed {
bundleDeployment, err := m.bundleDeploymentCache.Get(m.fleetNamespace, deployed.BundleID)
if apierror.IsNotFound(err) {
// found a helm secret, but no bundle deployment, so uninstall the release
logrus.Infof("Deleting orphan bundle ID %s, release %s", deployed.BundleID, deployed.ReleaseName)
if err := m.deployer.Delete(deployed.BundleID, deployed.ReleaseName); err != nil {
return err
}

return nil
} else if err != nil {
return err
}

key := m.releaseKey(bundleDeployment)
if key != deployed.ReleaseName {
// found helm secret and bundle deployment for BundleID, but release name doesn't match, so delete the release
logrus.Infof("Deleting unknown bundle ID %s, release %s, expecting release %s", deployed.BundleID, deployed.ReleaseName, key)
if err := m.deployer.Delete(deployed.BundleID, deployed.ReleaseName); err != nil {
return err
}
}
}

return nil
}

// releaseKey returns a deploymentKey from namespace+releaseName
func (m *Cleanup) releaseKey(bd *fleet.BundleDeployment) string {
ns := m.defaultNamespace
if bd.Spec.Options.TargetNamespace != "" {
ns = bd.Spec.Options.TargetNamespace
} else if bd.Spec.Options.DefaultNamespace != "" {
ns = bd.Spec.Options.DefaultNamespace
}

if bd.Spec.Options.Helm == nil || bd.Spec.Options.Helm.ReleaseName == "" {
return ns + "/" + bd.Name
}
return ns + "/" + bd.Spec.Options.Helm.ReleaseName
}
51 changes: 51 additions & 0 deletions internal/cmd/agent/deployer/deployer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package deployer

import (
"github.com/rancher/fleet/internal/helmdeployer"
"github.com/rancher/fleet/internal/manifest"
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"

"github.com/rancher/wrangler/v2/pkg/kv"
)

type Deployer struct {
lookup manifest.Lookup
deployer *helmdeployer.Helm
}

func New(lookup manifest.Lookup, deployer *helmdeployer.Helm) *Deployer {
return &Deployer{
lookup: lookup,
deployer: deployer,
}
}

func (m *Deployer) Delete(bundleDeploymentKey string) error {
_, name := kv.RSplit(bundleDeploymentKey, "/")
return m.deployer.Delete(name, "")
}

// Deploy the bundle deployment, i.e. with helmdeployer.
// This loads the manifest and the contents from the upstream cluster.
func (m *Deployer) Deploy(bd *fleet.BundleDeployment) (string, error) {
if bd.Spec.DeploymentID == bd.Status.AppliedDeploymentID {
if ok, err := m.deployer.EnsureInstalled(bd.Name, bd.Status.Release); err != nil {
return "", err
} else if ok {
return bd.Status.Release, nil
}
}
manifestID, _ := kv.Split(bd.Spec.DeploymentID, ":")
manifest, err := m.lookup.Get(manifestID)
if err != nil {
return "", err
}

manifest.Commit = bd.Labels["fleet.cattle.io/commit"]
resource, err := m.deployer.Deploy(bd.Name, manifest, bd.Spec.Options)
if err != nil {
return "", err
}

return resource.ID, nil
}
Loading

0 comments on commit eb3ccfe

Please sign in to comment.