-
Notifications
You must be signed in to change notification settings - Fork 70
Feat: add .status.observedGeneration to Configuration #269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -133,7 +133,7 @@ func (r *ConfigurationReconciler) Reconcile(ctx context.Context, req ctrl.Reques | |
|
|
||
| var tfExecutionJob = &batchv1.Job{} | ||
| if err := r.Client.Get(ctx, client.ObjectKey{Name: meta.ApplyJobName, Namespace: meta.Namespace}, tfExecutionJob); err == nil { | ||
| if tfExecutionJob.Status.Succeeded == int32(1) { | ||
| if !meta.EnvChanged && tfExecutionJob.Status.Succeeded == int32(1) { | ||
| if err := meta.updateApplyStatus(ctx, r.Client, types.Available, types.MessageCloudResourceDeployed); err != nil { | ||
| return ctrl.Result{}, err | ||
| } | ||
|
|
@@ -200,6 +200,7 @@ type TFConfigurationMeta struct { | |
| RemoteGit string | ||
| RemoteGitPath string | ||
| ConfigurationChanged bool | ||
| EnvChanged bool | ||
| ConfigurationCMName string | ||
| BackendSecretName string | ||
| ApplyJobName string | ||
|
|
@@ -277,7 +278,7 @@ func (r *ConfigurationReconciler) terraformApply(ctx context.Context, namespace | |
| return errors.Wrap(err, types.ErrUpdateTerraformApplyJob) | ||
| } | ||
|
|
||
| if tfExecutionJob.Status.Succeeded == int32(1) { | ||
| if !meta.EnvChanged && tfExecutionJob.Status.Succeeded == int32(1) { | ||
| if err := meta.updateApplyStatus(ctx, k8sClient, types.Available, types.MessageCloudResourceDeployed); err != nil { | ||
| return err | ||
| } | ||
|
|
@@ -461,6 +462,28 @@ func (r *ConfigurationReconciler) preCheck(ctx context.Context, configuration *v | |
| return err | ||
| } | ||
|
|
||
| // Check whether env changes | ||
| if err := meta.prepareTFVariables(configuration); err != nil { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great, I did move it to here in another pending PR. |
||
| return err | ||
| } | ||
|
|
||
| var variableInSecret v1.Secret | ||
| if err := k8sClient.Get(ctx, client.ObjectKey{Name: meta.VariableSecretName, Namespace: meta.Namespace}, &variableInSecret); err == nil { | ||
| for k, v := range variableInSecret.Data { | ||
| if val, ok := meta.VariableSecretData[k]; !ok || !bytes.Equal(v, val) { | ||
| meta.EnvChanged = true | ||
| klog.Info("Job's env changed") | ||
| if err := meta.updateApplyStatus(ctx, k8sClient, types.ConfigurationReloading, types.ConfigurationReloadingAsVariableChanged); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| break | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch! |
||
| } | ||
| } | ||
| } else if !kerrors.IsNotFound(err) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What will happen if it's not
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. system err or runtime err
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 如果 not found 在这里返回,会无法继续进行;后面会在配置变更后删除 secrets object |
||
| return err | ||
| } | ||
|
|
||
| // Apply ClusterRole | ||
| return createTerraformExecutorClusterRole(ctx, k8sClient, fmt.Sprintf("%s-%s", meta.Namespace, ClusterRoleName)) | ||
| } | ||
|
|
@@ -472,6 +495,7 @@ func (meta *TFConfigurationMeta) updateApplyStatus(ctx context.Context, k8sClien | |
| State: state, | ||
| Message: message, | ||
| } | ||
| configuration.Status.ObservedGeneration = configuration.Generation | ||
| if state == types.Available { | ||
| outputs, err := meta.getTFOutputs(ctx, k8sClient, configuration) | ||
| if err != nil { | ||
|
|
@@ -539,31 +563,11 @@ func (meta *TFConfigurationMeta) assembleAndTriggerJob(ctx context.Context, k8sC | |
|
|
||
| // updateTerraformJob will set deletion finalizer to the Terraform job if its envs are changed, which will result in | ||
| // deleting the job. Finally, a new Terraform job will be generated | ||
| func (meta *TFConfigurationMeta) updateTerraformJobIfNeeded(ctx context.Context, k8sClient client.Client, configuration v1beta1.Configuration, | ||
| func (meta *TFConfigurationMeta) updateTerraformJobIfNeeded(ctx context.Context, k8sClient client.Client, _ v1beta1.Configuration, | ||
| job batchv1.Job) error { | ||
| if err := meta.prepareTFVariables(&configuration); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // check whether env changes | ||
| var variableInSecret v1.Secret | ||
| if err := k8sClient.Get(ctx, client.ObjectKey{Name: meta.VariableSecretName, Namespace: meta.Namespace}, &variableInSecret); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| var envChanged bool | ||
| for k, v := range variableInSecret.Data { | ||
| if val, ok := meta.VariableSecretData[k]; !ok || !bytes.Equal(v, val) { | ||
| envChanged = true | ||
| klog.Info("Job's env changed") | ||
| if err := meta.updateApplyStatus(ctx, k8sClient, types.ConfigurationReloading, types.ConfigurationReloadingAsVariableChanged); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // if either one changes, delete the job | ||
| if envChanged || meta.ConfigurationChanged { | ||
| if meta.EnvChanged || meta.ConfigurationChanged { | ||
| klog.InfoS("about to delete job", "Name", job.Name, "Namespace", job.Namespace) | ||
| var j batchv1.Job | ||
| if err := k8sClient.Get(ctx, client.ObjectKey{Name: job.Name, Namespace: job.Namespace}, &j); err == nil { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这一块不能删除,它解决的是这个场景:
Configuration 部署过程中,job 已经完成了,状态还是
ProvisioningAndChecking,这时候删除 Configuration,状态就无法到达Avaialable,导致状态 block,无法部署成功也无法删除。There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里需要判断一下是否是当前 spec 对应的 job ,如果新的 spec 进来会后会拿到上一次的 job