Skip to content
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

wip (draft pr) - reconcile sap-btp-manager secret changes #932

Closed
wants to merge 28 commits into from

Conversation

ukff
Copy link
Contributor

@ukff ukff commented Dec 18, 2024

No description provided.

@kyma-bot kyma-bot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. cla: yes Indicates the PR's author has signed the CLA. labels Dec 18, 2024
Copy link

Add one of following labels

- kind/feature -> Use it when you want to submit a new feature

- kind/enhancement -> Use it when you modify or improve an existing feature

- kind/bug -> Use it when you fix a bug

@kyma-bot kyma-bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Dec 18, 2024
@ukff ukff force-pushed the supportsecretchanges2 branch from 15ac9a8 to 810a601 Compare December 19, 2024 08:06
}

if labels != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have already assigned labels and data to the structure fields.

@@ -498,26 +510,69 @@ func (r *BtpOperatorReconciler) reconcileResources(ctx context.Context, s *corev

r.deleteCreationTimestamp(resourcesToApply...)

logger.Info(fmt.Sprintf("check if deployment: %s in %s needs to be restarted.", DeploymentName, resolvedNamespace))
clusterIdSecretChanged := r.checkIfOperatorNeedRestart(ctx, resolvedNamespace, baseSecret, &logger)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checkIfOperatorNeedRestart detects if clusterId was changed so the name is misleading.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the other hand the implementation check both cases, so possibly the variable name is inappropriate.

Namespace: inNamespace,
},
}
exists := make(chan bool)
Copy link
Contributor

@jaroslaw-pieszka jaroslaw-pieszka Dec 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use more informative channel name?
exists usually is used as boolean flag.

go r.checkResourceExistence(ctx, obj, exists, time.Second*10)
ok := <-exists
if !ok {
return fmt.Errorf("secret: %s in %s didnt appear in given time", clusterIdSecretName, inNamespace)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return fmt.Errorf("secret: %s in %s didnt appear in given time", clusterIdSecretName, inNamespace)
return fmt.Errorf("secret: %s in %s did not appear in given time", clusterIdSecretName, inNamespace)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still appear is not the best way to describe what we are expecting.

Namespace: ChartNamespace,
},
}
err := r.Get(context.TODO(), client.ObjectKeyFromObject(currentConfigMap), currentConfigMap)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-existent config map is a different story. Restart would not help, since map is necessary for operator to start.

return true
}

return !reflect.DeepEqual(baseSecret.Data["management_namespace"], []byte(currentConfigMap.Data["RELEASE_NAMESPACE"])) ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we compare management_namespace with release namespace?

}

func (r *BtpOperatorReconciler) checkIfOperatorNeedRestart(ctx context.Context, resolvedNamespace string, baseSecret *corev1.Secret, logger *logr.Logger) bool {
detectConfigMapChanges := func() bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functional objects seem to be overkill here.

}
err := r.Client.Get(ctx, client.ObjectKeyFromObject(&clusterIdSecret), &clusterIdSecret)
if k8serrors.IsNotFound(err) {
logger.Info(fmt.Sprintf("secret %s in %s not found, so restart is needed", clusterIdSecretName, resolvedNamespace))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How restart would help in this case?

logger.Info(fmt.Sprintf("secret %s in %s not found, so restart is needed", clusterIdSecretName, resolvedNamespace))
return true
}
return !reflect.DeepEqual(baseSecret.Data["cluster_id"], clusterIdSecret.Data["INITIAL_CLUSTER_ID"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why DeepEqual?
Simple comparison is not enough?

return unstructured.SetNestedField(u.Object, string(secret.Data["cluster_id"]), "data", "CLUSTER_ID")
func (r *BtpOperatorReconciler) setConfigMapValues(baseSecret *corev1.Secret, configMap *unstructured.Unstructured, resolvedNamespace string) error {
err := unstructured.SetNestedField(configMap.Object, resolvedNamespace, "data", "MANAGEMENT_NAMESPACE")
err = unstructured.SetNestedField(configMap.Object, resolvedNamespace, "data", "RELEASE_NAMESPACE")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK we should support change of management namespace not release namespace.

func (r *BtpOperatorReconciler) setConfigMapValues(secret *corev1.Secret, u *unstructured.Unstructured) error {
return unstructured.SetNestedField(u.Object, string(secret.Data["cluster_id"]), "data", "CLUSTER_ID")
func (r *BtpOperatorReconciler) setConfigMapValues(baseSecret *corev1.Secret, configMap *unstructured.Unstructured, resolvedNamespace string) error {
err := unstructured.SetNestedField(configMap.Object, resolvedNamespace, "data", "MANAGEMENT_NAMESPACE")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We overwrite error in the next line. So named variable is not needed.

}

func (r *BtpOperatorReconciler) setSecretValues(secret *corev1.Secret, u *unstructured.Unstructured) error {
for k := range secret.Data {
if err := unstructured.SetNestedField(u.Object, base64.StdEncoding.EncodeToString(secret.Data[k]), "data", k); err != nil {
return err
for _, r := range requiredSecretKeys {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is over-engineered.
We just need to change one value of the map.
I think no loop is needed, at least not two nested loops.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why it is over engineered? we want only fields from requiresSecretsKeys. We dont want add any more keys to base secret than are required (for example namespace)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will discuss it later on.

@@ -2026,3 +2139,84 @@ func (r *BtpOperatorReconciler) reconcileResourcesWithoutChangingCrState(ctx con
logger.Error(err, "resources reconciliation failed")
}
}

func (r *BtpOperatorReconciler) restartOperator(ctx context.Context, inNamespace string, logger *logr.Logger) error {
logger.Info(fmt.Sprintf("restarting of deployment: %s in %s started", DeploymentName, inNamespace))
Copy link
Contributor

@jaroslaw-pieszka jaroslaw-pieszka Dec 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
logger.Info(fmt.Sprintf("restarting of deployment: %s in %s started", DeploymentName, inNamespace))
logger.Info(fmt.Sprintf("restarting deployment: %s in %s restarted", DeploymentName, inNamespace))

@@ -2026,3 +2139,84 @@ func (r *BtpOperatorReconciler) reconcileResourcesWithoutChangingCrState(ctx con
logger.Error(err, "resources reconciliation failed")
}
}

func (r *BtpOperatorReconciler) restartOperator(ctx context.Context, inNamespace string, logger *logr.Logger) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This secret with cluster_id is always located in release namespace.

func (r *BtpOperatorReconciler) restartOperator(ctx context.Context, inNamespace string, logger *logr.Logger) error {
logger.Info(fmt.Sprintf("restarting of deployment: %s in %s started", DeploymentName, inNamespace))

clusterIdSecret := &corev1.Secret{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be extracted to function with the proper responsibility - deletion of the secret.

if !k8serrors.IsNotFound(err) {
return "", "", false, fmt.Errorf("failed to get configmap %s in %s, %w", ConfigName, ChartNamespace, err)
}
currentNamespace := currentConfigMap.Data["management_namespace"]
Copy link
Contributor

@jaroslaw-pieszka jaroslaw-pieszka Dec 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The key in the map is in uppercase: MANAGEMENT_NAMESPACE

return nil
}

func (r *BtpOperatorReconciler) resolveNamespace(ctx context.Context, secret *corev1.Secret, logger *logr.Logger) (string, string, bool, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not clear what is the responsibility here.

@@ -486,8 +491,15 @@ func (r *BtpOperatorReconciler) reconcileResources(ctx context.Context, s *corev
}
logger.Info(fmt.Sprintf("got %d module resources to apply based on %s directory", len(resourcesToApply), r.getResourcesToApplyPath()))

resolvedNamespace, _, namespaceChanged, err := r.resolveNamespace(ctx, baseSecret, &logger)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too many responsibilities for this function.
And why to return value which is ignored (always).

err := unstructured.SetNestedField(configMap.Object, resolvedNamespace, "data", "MANAGEMENT_NAMESPACE")
err = unstructured.SetNestedField(configMap.Object, resolvedNamespace, "data", "RELEASE_NAMESPACE")

err = unstructured.SetNestedField(configMap.Object, string(baseSecret.Data["cluster_id"]), "data", "CLUSTER_ID")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we extract literals, so commonly used: CLUSTER_ID et al.

@@ -475,7 +480,7 @@ func (r *BtpOperatorReconciler) deleteResources(ctx context.Context, us []*unstr
return nil
}

func (r *BtpOperatorReconciler) reconcileResources(ctx context.Context, s *corev1.Secret) error {
func (r *BtpOperatorReconciler) reconcileResources(ctx context.Context, baseSecret *corev1.Secret) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about State of the resource?
Is it in line with what is happening?
Restart could take some time, operator is not operational and we enter reconcileResources either from Ready state or from Processing state.

@ukff
Copy link
Contributor Author

ukff commented Dec 31, 2024

@jaroslaw-pieszka this pr is draft and wip. it was not to be supposed to review now :D but i will takie into considerations

@ukff ukff closed this Dec 31, 2024
@ukff ukff changed the title wip - reconcile sap-btp-manager secret changes wip (draft pr) - reconcile sap-btp-manager secret changes Jan 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Indicates the PR's author has signed the CLA. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants