Skip to content

Commit

Permalink
Redeploy agent when fleet-agent deployment is deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
Daishan committed Apr 27, 2021
1 parent 3639a36 commit aba667b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
10 changes: 5 additions & 5 deletions pkg/apis/fleet.cattle.io/v1alpha1/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ type Cluster struct {
}

type ClusterSpec struct {
Paused bool `json:"paused,omitempty"`
ClientID string `json:"clientID,omitempty"`
KubeConfigSecret string `json:"kubeConfigSecret,omitempty"`
RedeployAgentGeneration int64 `json:"redeployAgentGeneration,omitempty"`
AgentEnvVars []v1.EnvVar `json:"agentEnvVars,omitempty"`
Paused bool `json:"paused,omitempty"`
ClientID string `json:"clientID,omitempty"`
KubeConfigSecret string `json:"kubeConfigSecret,omitempty"`
RedeployAgentGeneration int64 `json:"redeployAgentGeneration,omitempty"`
AgentEnvVars []v1.EnvVar `json:"agentEnvVars,omitempty"`
}

type ClusterStatus struct {
Expand Down
24 changes: 19 additions & 5 deletions pkg/controllers/cluster/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,31 @@ func RegisterImport(
fleetcontrollers.RegisterClusterStatusHandler(ctx, clusters, "Imported", "import-cluster", h.importCluster)
}

func agentDeployed(cluster *fleet.Cluster) bool {
func agentDeployed(ctx context.Context, namespace string, kc kubernetes.Interface, cluster *fleet.Cluster) bool {
if cluster.Status.AgentDeployedGeneration == nil {
return false
}
return *cluster.Status.AgentDeployedGeneration == cluster.Spec.RedeployAgentGeneration

if *cluster.Status.AgentDeployedGeneration != cluster.Spec.RedeployAgentGeneration {
return false
}

if kc != nil {
dp, err := kc.AppsV1().Deployments(namespace).Get(ctx, "fleet-agent", metav1.GetOptions{})
if err != nil || dp.DeletionTimestamp != nil {
return false
}
}

return true
}

func (i *importHandler) OnChange(key string, cluster *fleet.Cluster) (_ *fleet.Cluster, err error) {
if cluster == nil {
return cluster, nil
}

if cluster.Spec.KubeConfigSecret == "" || agentDeployed(cluster) {
if cluster.Spec.KubeConfigSecret == "" || agentDeployed(i.ctx, i.systemNamespace, nil, cluster) {
return cluster, nil
}

Expand Down Expand Up @@ -132,11 +144,9 @@ func (i *importHandler) deleteOldAgent(cluster *fleet.Cluster, kc kubernetes.Int

func (i *importHandler) importCluster(cluster *fleet.Cluster, status fleet.ClusterStatus) (_ fleet.ClusterStatus, err error) {
if cluster.Spec.KubeConfigSecret == "" ||
agentDeployed(cluster) ||
cluster.Spec.ClientID == "" {
return status, nil
}

secret, err := i.secrets.Get(cluster.Namespace, cluster.Spec.KubeConfigSecret)
if err != nil {
return status, err
Expand Down Expand Up @@ -170,6 +180,10 @@ func (i *importHandler) importCluster(cluster *fleet.Cluster, status fleet.Clust
return status, err
}

if agentDeployed(i.ctx, i.systemNamespace, kc, cluster) {
return status, nil
}

if _, err = kc.Discovery().ServerVersion(); err != nil {
return status, err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/controllers/manageagent/manageagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ func Register(ctx context.Context,
relatedresource.WatchClusterScoped(ctx, "manage-agent-resolver", h.resolveNS, namespace, clusters)
}

func (h *handler) resolveNS(namespace, name string, obj runtime.Object) ([]relatedresource.Key, error) {
if _, ok := obj.(*fleet.Cluster); ok {
if _, err := h.bundleCache.Get(namespace, agentBundleName); err != nil {
func (h *handler) resolveNS(namespace, _ string, obj runtime.Object) ([]relatedresource.Key, error) {
if cluster, ok := obj.(*fleet.Cluster); ok {
if _, err := h.bundleCache.Get(namespace, name.SafeConcatName(agentBundleName, cluster.Name)); err != nil {
return []relatedresource.Key{{Name: namespace}}, nil
}
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/helmdeployer/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,11 @@ func (h *helm) deleteByRelease(bundleID, releaseName string) error {
return err
}

if strings.HasPrefix(bundleID, "fleet-agent") {
// Never uninstall the fleet-agent, just "forget" it
return deleteHistory(cfg, bundleID)
}

u := action.NewUninstall(&cfg)
_, err = u.Run(releaseName)
return err
Expand Down Expand Up @@ -514,7 +519,7 @@ func (h *helm) delete(bundleID string, options fleet.BundleDeploymentOptions, dr
return err
}

if bundleID == "fleet-agent" {
if strings.HasPrefix(bundleID, "fleet-agent") {
// Never uninstall the fleet-agent, just "forget" it
return deleteHistory(cfg, bundleID)
}
Expand Down

0 comments on commit aba667b

Please sign in to comment.