Skip to content

Commit

Permalink
[Misc] Operator: Logs updated (#107)
Browse files Browse the repository at this point in the history
Several updates to logging

---------

Co-authored-by: i325261 <[email protected]>
  • Loading branch information
Pavan-SAP and anirudhprasad-sap authored Aug 1, 2024
1 parent 47bab51 commit 76335e0
Show file tree
Hide file tree
Showing 75 changed files with 208 additions and 101 deletions.
24 changes: 12 additions & 12 deletions internal/controller/reconcile-capapplication.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (c *Controller) handleCAPApplicationDependentResources(ctx context.Context,
return
}

// step 5 - check state of dependant resources
// step 5 - check state of dependent resources
if processing, err = c.checkPrimaryDomainResources(ctx, ca); err != nil || processing {
return
}
Expand Down Expand Up @@ -270,7 +270,7 @@ func (c *Controller) validateSecrets(ctx context.Context, ca *v1alpha1.CAPApplic
// waiting for secrets
message := fmt.Sprintf("waiting for secrets to get ready for %s %s.%s", v1alpha1.CAPApplicationKind, ca.Name, ca.Namespace)

util.LogInfo(message, string(ApplicationProcessing), ca, nil)
util.LogInfo("Waiting for secrets", string(Processing), ca, nil)
c.Event(ca, nil, corev1.EventTypeWarning, CAPApplicationEventMissingSecrets, EventActionProcessingSecrets, message)
ca.SetStatusWithReadyCondition(ca.Status.State, metav1.ConditionFalse, EventActionProcessingSecrets, message)
return true, nil
Expand Down Expand Up @@ -324,13 +324,13 @@ func (c *Controller) reconcileCAPApplicationProviderTenant(ctx context.Context,
},
}, metav1.CreateOptions{})
if err != nil {
util.LogError(err, "Error creating tenant subscription context secret", string(ApplicationProcessing), ca, nil, "tenantId", ca.Spec.Provider.TenantId)
util.LogError(err, "Error creating tenant subscription context secret", string(Processing), ca, nil, "tenantId", ca.Spec.Provider.TenantId)
ca.SetStatusWithReadyCondition(v1alpha1.CAPApplicationStateError, metav1.ConditionFalse, "ProviderTenantError", err.Error())
return false, err
}

// Create provider tenant
util.LogInfo("Creating Provider tenant", string(ApplicationProcessing), ca, nil, "tenantId", ca.Spec.Provider.TenantId)
util.LogInfo("Creating provider tenant", string(Processing), ca, nil, "tenantId", ca.Spec.Provider.TenantId)

if tenant, err = c.crdClient.SmeV1alpha1().CAPTenants(ca.Namespace).Create(
ctx, &v1alpha1.CAPTenant{
Expand Down Expand Up @@ -366,7 +366,7 @@ func (c *Controller) reconcileCAPApplicationProviderTenant(ctx context.Context,
}
_, err = c.kubeClient.CoreV1().Secrets(tenant.Namespace).Update(context.TODO(), secret, metav1.UpdateOptions{})
if err != nil {
util.LogError(err, "Error updating tenant subscription context secret", string(ApplicationProcessing), ca, nil, "tenantId", ca.Spec.Provider.TenantId)
util.LogError(err, "Error updating tenant subscription context secret", string(Processing), ca, nil, "tenantId", ca.Spec.Provider.TenantId)
ca.SetStatusWithReadyCondition(v1alpha1.CAPApplicationStateError, metav1.ConditionFalse, "ProviderTenantError", err.Error())
return false, err
}
Expand All @@ -382,7 +382,7 @@ func (c *Controller) reconcileCAPApplicationProviderTenant(ctx context.Context,
}

msg := fmt.Sprintf("provider %v not ready for %v %v.%v; waiting for it to be ready", v1alpha1.CAPTenantKind, v1alpha1.CAPApplicationKind, ca.Namespace, ca.Name)
util.LogInfo(msg, string(ApplicationProcessing), ca, tenant, "tenantId", ca.Spec.Provider.TenantId)
util.LogInfo("Waiting for provider tenant to be ready", string(Processing), ca, tenant, "tenantId", ca.Spec.Provider.TenantId)
ca.SetStatusWithReadyCondition(v1alpha1.CAPApplicationStateProcessing, metav1.ConditionFalse, EventActionProviderTenantProcessing, msg)
return true, nil
}
Expand All @@ -393,34 +393,34 @@ func (c *Controller) reconcileCAPApplicationProviderTenant(ctx context.Context,
func (c *Controller) handleCAPApplicationDeletion(ctx context.Context, ca *v1alpha1.CAPApplication) (*ReconcileResult, error) {
var err error

util.LogInfo("Attempting to delete CAPApplication", string(ApplicationDeleting), ca, nil)
util.LogInfo("Attempting to delete application", string(Deleting), ca, nil)
if ca.Status.State != v1alpha1.CAPApplicationStateDeleting {
ca.SetStatusWithReadyCondition(v1alpha1.CAPApplicationStateDeleting, metav1.ConditionFalse, "DeleteTriggered", "")
return NewReconcileResultWithResource(ResourceCAPApplication, ca.Name, ca.Namespace, 0), nil
}

// TODO: cleanup domain resources via reconciliation
util.LogInfo("Removing Primary Domain Certificate", string(ApplicationDeleting), ca, nil)
util.LogInfo("Removing primary domain certificate", string(Deleting), ca, nil)
if err = c.deletePrimaryDomainCertificate(ctx, ca); err != nil && !k8sErrors.IsNotFound(err) {
return nil, err
}

// delete CAPTenants - return if found in this loop, to verify deletion
var tenantFound bool
util.LogInfo("Deleting Tenants", string(ApplicationDeleting), ca, nil)
util.LogInfo("Deleting dependent tenants", string(Deleting), ca, nil)
if tenantFound, err = c.deleteTenants(ctx, ca); tenantFound || err != nil {
util.LogInfo("Could not delete; tenants exists for this application", string(ApplicationDeleting), ca, nil)
util.LogError(err, "Could not delete dependent tenant", string(Deleting), ca, nil)
return nil, err
}

util.LogInfo("Cleaning up secrets", string(ApplicationDeleting), ca, nil)
util.LogInfo("Cleaning up secrets", string(Deleting), ca, nil)
if err = c.cleanupPreservedSecrets(ca.Spec.BTP.Services, ca.Namespace); err != nil && !k8sErrors.IsNotFound(err) {
return nil, err
}

// delete CAPApplication
if removeFinalizer(&ca.Finalizers, FinalizerCAPApplication) {
util.LogInfo("Removing Finalizer; finished deleting this application", string(ApplicationDeleting), ca, nil)
util.LogInfo("Removing Finalizer; finished deleting this application", string(Deleting), ca, nil)
return nil, c.updateCAPApplication(ctx, ca)
}

Expand Down
42 changes: 24 additions & 18 deletions internal/controller/reconcile-capapplicationversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (c *Controller) updateCAPApplicationVersionStatus(ctx context.Context, cav
*cav = *cavUpdated
}
if statusErr != nil {
util.LogError(statusErr, "could not update status of application version", string(ApplicationVersionProcessing), cav, nil)
util.LogError(statusErr, "Could not update status of application version", string(Processing), cav, nil, "version", cav.Spec.Version)
}

return statusErr
Expand All @@ -107,7 +107,7 @@ func (c *Controller) handleCAPApplicationVersion(ctx context.Context, cav *v1alp
err := c.checkSecretsExist(ca.Spec.BTP.Services, ca.Namespace)
if err != nil {
// Requeue after 10s to check if secrets exist
util.LogInfo("Missing secrets; Check again if the required secrets exists after 10 seconds", string(ApplicationVersionProcessing), cav, nil)
util.LogInfo("Missing secrets; check again if the required secrets exists after 10 seconds", string(Processing), cav, nil, "version", cav.Spec.Version)
return NewReconcileResultWithResource(ResourceCAPApplicationVersion, cav.Name, cav.Namespace, 10*time.Second), c.updateCAPApplicationVersionStatus(ctx, cav, v1alpha1.CAPApplicationVersionStateProcessing, metav1.Condition{Type: string(v1alpha1.ConditionTypeReady), Status: "False", Reason: "WaitingForSecrets"})
}

Expand Down Expand Up @@ -187,8 +187,11 @@ func (c *Controller) processDeployments(ctx context.Context, ca *v1alpha1.CAPApp
return nil, err
}

// We do not want to wait until the deployments are actually "Ready", we only rely on Content Job completing successfully!
util.LogInfo("All deployments and services created successfully", string(ApplicationVersionReady), cav, nil)
// For now, we do not want to wait until the deployments are actually "Ready", we only rely on Content Job completing successfully!
if cav.Status.State == v1alpha1.CAPApplicationVersionStateProcessing {
// Only log if the state is still processing as cav might be reconciled again
util.LogInfo("All deployments and other resources created successfully", string(Ready), cav, nil, "version", cav.Spec.Version)
}
return nil, c.updateCAPApplicationVersionStatus(ctx, cav, v1alpha1.CAPApplicationVersionStateReady, metav1.Condition{Type: string(v1alpha1.ConditionTypeReady), Status: "True", Reason: "CreatedDeployments"})
}

Expand Down Expand Up @@ -254,7 +257,7 @@ func (c *Controller) handleContentDeployJob(ca *v1alpha1.CAPApplication, cav *v1
if err == nil {
contentDeployJob, err = c.kubeClient.BatchV1().Jobs(cav.Namespace).Create(context.TODO(), newContentDeploymentJob(ca, cav, workload, ownerRef, vcapSecretName), metav1.CreateOptions{})
if err == nil {
util.LogInfo("Content Job created successfully", string(ApplicationVersionProcessing), cav, contentDeployJob)
util.LogInfo("Content job created successfully", string(Processing), cav, contentDeployJob, "version", cav.Spec.Version)
}
}
}
Expand All @@ -274,7 +277,7 @@ func newContentDeploymentJob(ca *v1alpha1.CAPApplication, cav *v1alpha1.CAPAppli

contentJobName := getContentJobName(workload.Name, cav)

util.LogInfo("Creating content Job", string(ApplicationVersionProcessing), cav, nil, "contentJobName", contentJobName)
util.LogInfo("Creating content job", string(Processing), cav, nil, "contentJobName", contentJobName, "version", cav.Spec.Version)

return &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -375,7 +378,7 @@ func (c *Controller) updateServices(ca *v1alpha1.CAPApplication, cav *v1alpha1.C
if k8sErrors.IsNotFound(err) {
service, err = c.kubeClient.CoreV1().Services(cav.Namespace).Create(context.TODO(), newService(ca, cav, workloadServicePortInfo), metav1.CreateOptions{})
if err == nil {
util.LogInfo("Service created successfully", string(ApplicationVersionProcessing), cav, service)
util.LogInfo("Service created successfully", string(Processing), cav, service, "version", cav.Spec.Version)
}
}

Expand Down Expand Up @@ -403,7 +406,7 @@ func newService(ca *v1alpha1.CAPApplication, cav *v1alpha1.CAPApplicationVersion

labels := copyMaps(workload.Labels, getLabels(ca, cav, CategoryService, workloadServicePortInfo.DeploymentType, workloadServicePortInfo.WorkloadName+ServiceSuffix, true))

util.LogInfo("Creating service", string(ApplicationVersionProcessing), cav, nil, "serviceName", workloadServicePortInfo.WorkloadName+ServiceSuffix)
util.LogInfo("Creating service", string(Processing), cav, nil, "serviceName", workloadServicePortInfo.WorkloadName+ServiceSuffix, "version", cav.Spec.Version)

return &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -466,7 +469,7 @@ func (c *Controller) createNetworkPolicy(name string, spec networkingv1.NetworkP
networkPolicy, err := c.kubeClient.NetworkingV1().NetworkPolicies(cav.Namespace).Get(context.TODO(), name, metav1.GetOptions{})
// If the resource doesn't exist, we'll create it
if k8sErrors.IsNotFound(err) {
util.LogInfo("Creating network policy", string(ApplicationVersionProcessing), cav, nil, "networkPolicyName", name)
util.LogInfo("Creating network policy", string(Processing), cav, nil, "networkPolicyName", name, "version", cav.Spec.Version)
networkPolicy, err = c.kubeClient.NetworkingV1().NetworkPolicies(cav.Namespace).Create(context.TODO(), &networkingv1.NetworkPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand All @@ -478,7 +481,7 @@ func (c *Controller) createNetworkPolicy(name string, spec networkingv1.NetworkP
Spec: spec,
}, metav1.CreateOptions{})
if err == nil {
util.LogInfo("Network Policy created successfully", string(ApplicationVersionProcessing), cav, networkPolicy)
util.LogInfo("Network policy created successfully", string(Processing), cav, networkPolicy, "version", cav.Spec.Version)
}
}
return doChecks(err, networkPolicy, cav, "NetworkPolicy")
Expand Down Expand Up @@ -566,7 +569,7 @@ func (c *Controller) updateDeployment(ca *v1alpha1.CAPApplication, cav *v1alpha1
if err == nil {
workloadDeployment, err = c.kubeClient.AppsV1().Deployments(cav.Namespace).Create(context.TODO(), newDeployment(ca, cav, workload, ownerRef, vcapSecretName), metav1.CreateOptions{})
if err == nil {
util.LogInfo("Deployment created successfully", string(ApplicationVersionProcessing), cav, workloadDeployment)
util.LogInfo("Deployment created successfully", string(Processing), cav, workloadDeployment, "version", cav.Spec.Version)
}
}
}
Expand All @@ -592,7 +595,7 @@ func createDeployment(params *DeploymentParameters) *appsv1.Deployment {
annotations := copyMaps(params.WorkloadDetails.Annotations, getAnnotations(params.CA, params.CAV, true))
labels := copyMaps(params.WorkloadDetails.Labels, getLabels(params.CA, params.CAV, CategoryWorkload, string(params.WorkloadDetails.DeploymentDefinition.Type), workloadName, true))

util.LogInfo("Creating deployment", string(ApplicationVersionProcessing), params.CAV, nil, "deploymentName", workloadName)
util.LogInfo("Creating deployment", string(Processing), params.CAV, nil, "deploymentName", workloadName, "version", params.CAV.Spec.Version)

return &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -824,7 +827,7 @@ func doChecks(err error, obj metav1.Object, cav *v1alpha1.CAPApplicationVersion,
// attempt processing again later. This could have been caused by a
// temporary network failure, or any other transient reason.
if err != nil {
util.LogError(err, "Error during CAPApplicationVersion Processing", string(ApplicationVersionProcessing), cav, obj, "resource", res)
util.LogError(err, "Error during application version processing", string(Processing), cav, obj, "resource", res, "version", cav.Spec.Version)
return err
}

Expand Down Expand Up @@ -895,13 +898,16 @@ func (c *Controller) checkContentWorkloadStatus(ctx context.Context, cav *v1alph

// If the job is still running, set processing to true
if !cav.CheckFinishedJobs(job) {
util.LogInfo("Waiting for Content Job(s) to complete", string(ApplicationVersionProcessing), cav, nil, util.DependantName, job, util.DependantKind, "Job")
util.LogInfo("Waiting for content job(s) to complete", string(Processing), cav, nil, util.DependentName, job, util.DependentKind, "Job", "version", cav.Spec.Version)
return true, nil
}
}

// All Jobs are executed
util.LogInfo("Content Job(s) Completed", string(ApplicationVersionProcessing), cav, nil)
if cav.Status.State != v1alpha1.CAPApplicationVersionStateReady {
// Only log this state if cav is not already in Ready state as the resource might be reconciled again
util.LogInfo("Content job(s) completed", string(Processing), cav, nil, "version", cav.Spec.Version)
}
return false, nil
}

Expand All @@ -925,7 +931,7 @@ func (c *Controller) getRelevantTenantsForCAV(cav *v1alpha1.CAPApplicationVersio

func (c *Controller) deleteCAPApplicationVersion(ctx context.Context, cav *v1alpha1.CAPApplicationVersion) (*ReconcileResult, error) {
// Update State if it is not set yet
util.LogInfo("Deleting CAPApplicationVersion", string(ApplicationVersionDeleting), cav, nil)
util.LogInfo("Deleting application version", string(Deleting), cav, nil, "version", cav.Spec.Version)
if cav.Status.State != v1alpha1.CAPApplicationVersionStateDeleting {
var deleteCondition metav1.Condition
if len(cav.Status.Conditions) > 0 {
Expand All @@ -946,10 +952,10 @@ func (c *Controller) deleteCAPApplicationVersion(ctx context.Context, cav *v1alp
// Check if tenants exists
if len(tenants) > 0 {
// Requeue after 10s to check if all tenants are gone
util.LogInfo("Could not delete; tenants exists in this version", string(ApplicationVersionDeleting), cav, nil)
util.LogInfo("Could not delete; tenants exists in this version", string(Deleting), cav, nil, "version", cav.Spec.Version)
return NewReconcileResultWithResource(ResourceCAPApplicationVersion, cav.Name, cav.Namespace, 10*time.Second), nil
} else if removeFinalizer(&cav.Finalizers, FinalizerCAPApplicationVersion) { // All tenants are gone --> remove finalizer and process deletion
util.LogInfo("Removing Finalizer; finished deleting this version", string(ApplicationVersionDeleting), cav, nil)
util.LogInfo("Removing finalizer; finished deleting this version", string(Deleting), cav, nil, "version", cav.Spec.Version)
return nil, c.updateCAPApplicationVersion(ctx, cav)
}

Expand Down
6 changes: 3 additions & 3 deletions internal/controller/reconcile-capapplicationversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ func TestCAV_DeletingWithReadyTenants(t *testing.T) {
"testdata/capapplicationversion/cav-ready-deleting.yaml",
},
expectedResources: "testdata/capapplicationversion/expected/cav-deleting.yaml",
expectError: false, // cav is requeued until dependants are gone
expectError: false, // cav is requeued until dependents are gone
expectedRequeue: map[int][]NamespacedResourceKey{ResourceCAPApplicationVersion: {{Namespace: "default", Name: "test-cap-01-cav-v1"}}},
},
)
Expand All @@ -451,7 +451,7 @@ func TestCAV_DeletingWithUpgradingVersionTenants(t *testing.T) {
context.TODO(), t,
QueueItem{Key: ResourceCAPApplicationVersion, ResourceKey: NamespacedResourceKey{Namespace: "default", Name: "test-cap-01-cav-v1"}},
TestData{
description: "capapplication deleting with valid upgrading (version dependant) tenants",
description: "capapplication deleting with valid upgrading (version dependent) tenants",
initialResources: []string{
"testdata/common/capapplication.yaml",
"testdata/common/credential-secrets.yaml",
Expand All @@ -460,7 +460,7 @@ func TestCAV_DeletingWithUpgradingVersionTenants(t *testing.T) {
"testdata/capapplicationversion/cav-ready-deleting.yaml",
},
expectedResources: "testdata/capapplicationversion/expected/cav-deleting.yaml",
expectError: false, // cav is requeued until dependants are gone
expectError: false, // cav is requeued until dependents are gone
expectedRequeue: map[int][]NamespacedResourceKey{ResourceCAPApplicationVersion: {{Namespace: "default", Name: "test-cap-01-cav-v1"}}},
},
)
Expand Down
Loading

0 comments on commit 76335e0

Please sign in to comment.