-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mario Manno
committed
Oct 30, 2023
1 parent
3c71c75
commit eb3ccfe
Showing
12 changed files
with
477 additions
and
330 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.