diff --git a/cluster/addons.go b/cluster/addons.go index e1e9ab3b..24a51fa3 100644 --- a/cluster/addons.go +++ b/cluster/addons.go @@ -186,18 +186,7 @@ func (c *Cluster) deployK8sAddOns(ctx context.Context, data map[string]interface func (c *Cluster) deployUserAddOns(ctx context.Context) error { log.Infof(ctx, "[addons] Setting up user addons") if c.Addons != "" { - addonJobExists, err := addons.AddonJobExists(UserAddonJobName, c.LocalKubeConfigPath, c.K8sWrapTransport) - if err != nil { - return err - } - if addonJobExists { - log.Infof(ctx, "[addons] Removing old user addons") - if err := c.doAddonDelete(ctx, UserAddonResourceName, false); err != nil { - return err - } - - log.Infof(ctx, "[addons] Old user addons removed successfully") - } + // deletes existing job if resource name is rke-user-addon and redeploy user addons if err := c.doAddonDeploy(ctx, c.Addons, UserAddonResourceName, false); err != nil { return err } @@ -216,6 +205,7 @@ func (c *Cluster) deployUserAddOns(ctx context.Context) error { } } if len(c.AddonsInclude) > 0 { + // deletes existing job if resource name is rke-user-includes-addons and redeploy user addons if err := c.deployAddonsInclude(ctx); err != nil { return err } @@ -512,7 +502,7 @@ func (c *Cluster) doAddonDeploy(ctx context.Context, addonYaml, resourceName str return &addonError{fmt.Sprintf("Failed to generate addon execute job: %v", err), isCritical} } - if err = c.ApplySystemAddonExecuteJob(addonJob, addonUpdated); err != nil { + if err = c.ApplySystemAddonExecuteJob(addonJob, resourceName, addonUpdated); err != nil { return &addonError{fmt.Sprintf("%v", err), isCritical} } return nil @@ -531,7 +521,7 @@ func (c *Cluster) doAddonDelete(ctx context.Context, resourceName string, isCrit if err != nil { return &addonError{fmt.Sprintf("Failed to generate addon delete job: %v", err), isCritical} } - if err := k8s.ApplyK8sSystemJob(deleteJob, c.LocalKubeConfigPath, c.K8sWrapTransport, c.AddonJobTimeout*2, false); err != nil { + if err := k8s.ApplyK8sSystemJob(deleteJob, c.LocalKubeConfigPath, c.K8sWrapTransport, c.AddonJobTimeout*2, resourceName, false); err != nil { return &addonError{fmt.Sprintf("%v", err), isCritical} } // At this point, the addon should be deleted. We need to clean up by deleting the deploy and delete jobs. @@ -575,8 +565,8 @@ func (c *Cluster) StoreAddonConfigMap(ctx context.Context, addonYaml string, add } } -func (c *Cluster) ApplySystemAddonExecuteJob(addonJob string, addonUpdated bool) error { - return k8s.ApplyK8sSystemJob(addonJob, c.LocalKubeConfigPath, c.K8sWrapTransport, c.AddonJobTimeout, addonUpdated) +func (c *Cluster) ApplySystemAddonExecuteJob(addonJob, resourceName string, addonUpdated bool) error { + return k8s.ApplyK8sSystemJob(addonJob, c.LocalKubeConfigPath, c.K8sWrapTransport, c.AddonJobTimeout, resourceName, addonUpdated) } func (c *Cluster) deployIngress(ctx context.Context, data map[string]interface{}) error { diff --git a/k8s/job.go b/k8s/job.go index 355f7913..a3fbcac2 100644 --- a/k8s/job.go +++ b/k8s/job.go @@ -13,13 +13,18 @@ import ( "k8s.io/client-go/transport" ) +const ( + UserAddonResourceName = "rke-user-addon" + UserAddonsIncludeResourceName = "rke-user-includes-addons" +) + type JobStatus struct { Completed bool Created bool Removing bool } -func ApplyK8sSystemJob(jobYaml, kubeConfigPath string, k8sWrapTransport transport.WrapperFunc, timeout int, addonUpdated bool) error { +func ApplyK8sSystemJob(jobYaml, kubeConfigPath string, k8sWrapTransport transport.WrapperFunc, timeout int, resourceName string, addonUpdated bool) error { job := v1.Job{} if err := DecodeYamlResource(&job, jobYaml); err != nil { return err @@ -49,10 +54,11 @@ func ApplyK8sSystemJob(jobYaml, kubeConfigPath string, k8sWrapTransport transpor return err } - // if the addon configMap is updated, or the previous job is not completed, - // I will remove the existing job first, if any - if addonUpdated || (jobStatus.Created && !jobStatus.Completed) { - logrus.Debugf("[k8s] replacing job %s.. ", job.Name) + // remove the existing job if addon config has changed or the job isn't completed + // always remove the existing job for user addons to rerun kubectl apply for user addons + if addonUpdated || (jobStatus.Created && !jobStatus.Completed) || + resourceName == UserAddonResourceName || resourceName == UserAddonsIncludeResourceName { + logrus.Debugf("[k8s] replacing job %s for %s", job.Name, resourceName) if err := DeleteK8sSystemJob(jobYaml, k8sClient, timeout); err != nil { return err }