Skip to content

Commit

Permalink
refactor service reconciler
Browse files Browse the repository at this point in the history
  • Loading branch information
maciaszczykm committed Jan 28, 2025
1 parent 34f9387 commit d3fab9a
Showing 1 changed file with 14 additions and 37 deletions.
51 changes: 14 additions & 37 deletions go/controller/internal/controller/servicedeployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ func (r *ServiceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_
}

attr, result, err := r.genServiceAttributes(ctx, service, repository.Status.ID)
if result != nil {
utils.MarkCondition(service.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, defaultErrMessage(err, ""))
return *result, nil // If the result is set, then error is ignored to requeue.
}
if err != nil {
utils.MarkCondition(service.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, err.Error())
return ctrl.Result{}, err
}
if result != nil {
utils.MarkCondition(service.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, "")
return *result, nil
}

existingService, err := r.ConsoleClient.GetService(*cluster.Status.ID, service.ConsoleName())
if err != nil && !errors.IsNotFound(err) {
Expand Down Expand Up @@ -248,8 +248,6 @@ func updateStatus(r *v1alpha1.ServiceDeployment, existingService *console.Servic
}

func (r *ServiceReconciler) genServiceAttributes(ctx context.Context, service *v1alpha1.ServiceDeployment, repositoryId *string) (*console.ServiceDeploymentAttributes, *ctrl.Result, error) {
log := log.FromContext(ctx)

syncConfigAttributes, err := service.Spec.SyncConfig.Attributes()
if err != nil {
return nil, nil, err
Expand All @@ -268,6 +266,7 @@ func (r *ServiceReconciler) genServiceAttributes(ctx context.Context, service *v
Kustomize: service.Spec.Kustomize.Attributes(),
Dependencies: service.Spec.DependenciesAttribute(),
SyncConfig: syncConfigAttributes,
Configuration: make([]*console.ConfigAttributes, 0),
}

if id, ok := service.GetAnnotations()[InventoryAnnotation]; ok && id != "" {
Expand All @@ -279,43 +278,25 @@ func (r *ServiceReconciler) genServiceAttributes(ctx context.Context, service *v
for _, imp := range service.Spec.Imports {
stackID, err := r.getStackID(ctx, imp.StackRef)
if err != nil {
log.Error(err, "error while getting stack ID", "error", err, "stackName", imp.StackRef.Name)
return nil, &requeue, nil
return nil, &requeue, fmt.Errorf("error while getting stack ID: %s", imp.StackRef.Name)
}
attr.Imports = append(attr.Imports, &console.ServiceImportAttributes{StackID: *stackID})
}
}

if service.Spec.ConfigurationRef != nil {
attr.Configuration = make([]*console.ConfigAttributes, 0)
secret := &corev1.Secret{}
name := types.NamespacedName{Name: service.Spec.ConfigurationRef.Name, Namespace: service.Spec.ConfigurationRef.Namespace}
err := r.Get(ctx, name, secret)
if err != nil {
log.Error(err, "error while getting configuration secret", "error", err,
"secretName", service.Spec.ConfigurationRef.Name, "secretNamespace", service.Spec.ConfigurationRef.Namespace)
return nil, &requeue, nil
if err = r.Get(ctx, types.NamespacedName{Name: service.Spec.ConfigurationRef.Name, Namespace: service.Spec.ConfigurationRef.Namespace}, secret); err != nil {
return nil, &requeue, fmt.Errorf("error while getting configuration secret: %s", err.Error())
}
for k, v := range secret.Data {
value := string(v)
attr.Configuration = append(attr.Configuration, &console.ConfigAttributes{
Name: k,
Value: &value,
})
attr.Configuration = append(attr.Configuration, &console.ConfigAttributes{Name: k, Value: lo.ToPtr(string(v))})
}
}

if len(service.Spec.Configuration) > 0 {
if attr.Configuration == nil {
attr.Configuration = make([]*console.ConfigAttributes, 0)
}

for k, v := range service.Spec.Configuration {
value := v
attr.Configuration = append(attr.Configuration, &console.ConfigAttributes{
Name: k,
Value: lo.ToPtr(value),
})
attr.Configuration = append(attr.Configuration, &console.ConfigAttributes{Name: k, Value: lo.ToPtr(v)})
}
}

Expand Down Expand Up @@ -352,14 +333,12 @@ func (r *ServiceReconciler) genServiceAttributes(ctx context.Context, service *v
if service.Spec.RepositoryRef != nil {
ref := service.Spec.RepositoryRef
var repo v1alpha1.GitRepository
if err := r.Get(ctx, client.ObjectKey{Name: ref.Name, Namespace: ref.Namespace}, &repo); err != nil {
log.Error(err, "error while getting repository", "error", err, "repoName", ref.Name, "repoNamespace", ref.Namespace)
return nil, &requeue, nil
if err = r.Get(ctx, client.ObjectKey{Name: ref.Name, Namespace: ref.Namespace}, &repo); err != nil {
return nil, &requeue, fmt.Errorf("error while getting repository: %s", err.Error())
}

if repo.Status.ID == nil {
log.Error(err, "repository is not ready yet", "repoName", ref.Name, "repoNamespace", ref.Namespace)
return nil, &requeue, nil
return nil, &requeue, fmt.Errorf("repository is not ready yet")
}

attr.Helm.RepositoryID = repo.Status.ID
Expand All @@ -368,9 +347,7 @@ func (r *ServiceReconciler) genServiceAttributes(ctx context.Context, service *v
if service.Spec.Helm.ValuesConfigMapRef != nil {
val, err := utils.GetConfigMapData(ctx, r.Client, service.GetNamespace(), service.Spec.Helm.ValuesConfigMapRef)
if err != nil {
log.Error(err, "error while getting values config map", "error", err, "configMapName",
service.Spec.Helm.ValuesConfigMapRef.Name)
return nil, &requeue, nil
return nil, &requeue, fmt.Errorf("error while getting values config map: %s", err.Error())
}
attr.Helm.Values = &val
}
Expand Down

0 comments on commit d3fab9a

Please sign in to comment.