func()

in pkg/controller/stackconfigpolicy/controller.go [395:480]


func (r *ReconcileStackConfigPolicy) reconcileKibanaResources(ctx context.Context, policy policyv1alpha1.StackConfigPolicy, status policyv1alpha1.StackConfigPolicyStatus) (*reconciler.Results, policyv1alpha1.StackConfigPolicyStatus) {
	defer tracing.Span(&ctx)()
	log := ulog.FromContext(ctx)
	log.V(1).Info("Reconcile Kibana Resources")

	results := reconciler.NewResult(ctx)

	// prepare the selector to find Kibana resources to configure
	selector, err := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{
		MatchLabels:      policy.Spec.ResourceSelector.MatchLabels,
		MatchExpressions: policy.Spec.ResourceSelector.MatchExpressions,
	})
	if err != nil {
		return results.WithError(err), status
	}
	listOpts := client.ListOptions{LabelSelector: selector}

	// restrict the search to the policy namespace if it is different from the operator namespace
	if policy.Namespace != r.params.OperatorNamespace {
		listOpts.Namespace = policy.Namespace
	}

	// find the list of Kibana to configure
	var kibanaList kibanav1.KibanaList
	if err := r.Client.List(ctx, &kibanaList, &listOpts); err != nil {
		return results.WithError(err), status
	}

	configuredResources := kbMap{}
	for _, kibana := range kibanaList.Items {
		log.V(1).Info("Reconcile StackConfigPolicy", "kibana_namespace", kibana.Namespace, "kibana_name", kibana.Name)
		kibana := kibana

		// keep the list of Kibana to be configured
		kibanaNsn := k8s.ExtractNamespacedName(&kibana)

		// check that there is no other policy that already owns the kibana config secret
		currentOwner, ok, err := canBeOwned(ctx, r.Client, policy, kibana)
		if err != nil {
			return results.WithError(err), status
		}

		// record error if already owned by another stack config policy
		if !ok {
			err := fmt.Errorf("conflict: resource Kibana %s/%s already configured by StackConfigpolicy %s/%s", kibana.Namespace, kibana.Name, currentOwner.Namespace, currentOwner.Name)
			r.recorder.Eventf(&policy, corev1.EventTypeWarning, events.EventReasonUnexpected, err.Error())
			results.WithError(err)
			if err := status.AddPolicyErrorFor(kibanaNsn, policyv1alpha1.ConflictPhase, err.Error(), policyv1alpha1.KibanaResourceType); err != nil {
				return results.WithError(err), status
			}
			continue
		}

		// Create the Secret that holds the Kibana configuration.
		if policy.Spec.Kibana.Config != nil {
			// Only add to configured resources if Kibana config is set.
			// This will help clean up the config secret if config gets removed from the stack config policy.
			configuredResources[kibanaNsn] = kibana
			expectedConfigSecret, err := newKibanaConfigSecret(policy, kibana)
			if err != nil {
				return results.WithError(err), status
			}

			if err = filesettings.ReconcileSecret(ctx, r.Client, expectedConfigSecret, &kibana); err != nil {
				return results.WithError(err), status
			}
		}

		// Check if required Kibana configs are applied.
		configApplied, err := kibanaConfigApplied(r.Client, policy, kibana)
		if err != nil {
			return results.WithError(err), status
		}

		// update the Kibana resource status for this Kibana
		err = status.UpdateResourceStatusPhase(kibanaNsn, policyv1alpha1.ResourcePolicyStatus{}, configApplied, policyv1alpha1.KibanaResourceType)
		if err != nil {
			return results.WithError(err), status
		}
	}

	// delete Settings secrets for resources no longer selected by this policy
	results.WithError(deleteOrphanSoftOwnedSecrets(ctx, r.Client, k8s.ExtractNamespacedName(&policy), nil, configuredResources, policyv1alpha1.KibanaResourceType))

	return results, status
}