func()

in pkg/service/platform/k8s/k8s.go [236:279]


func (s *K8SService) getKeycloakRealm(instance *gerritApi.Gerrit) (*keycloakApi.KeycloakRealm, error) {
	ctx := context.Background()

	if instance.Spec.KeycloakSpec.Realm != "" {
		realmList := keycloakApi.KeycloakRealmList{}
		listOpts := k8sClient.ListOptions{Namespace: instance.Namespace}

		k8sClient.MatchingLabels(map[string]string{
			"targetRealm": instance.Spec.KeycloakSpec.Realm,
		}).ApplyToList(&listOpts)

		if err := s.client.List(ctx, &realmList, &listOpts); err != nil {
			return nil, errors.Wrap(err, "unable to get reams by label")
		}

		if len(realmList.Items) > 0 {
			return &realmList.Items[0], nil
		}

		if err := s.client.List(ctx, &realmList,
			&k8sClient.ListOptions{Namespace: instance.Namespace}); err != nil {
			return nil, errors.Wrap(err, "unable to get all reams")
		}

		for i := 0; i < len(realmList.Items); i++ {
			if realmList.Items[i].Spec.RealmName == instance.Spec.KeycloakSpec.Realm {
				return &realmList.Items[i], nil
			}
		}
	}

	name := "main"
	realm := &keycloakApi.KeycloakRealm{}

	err := s.client.Get(ctx, types.NamespacedName{
		Name:      name,
		Namespace: instance.Namespace,
	}, realm)
	if err != nil {
		return nil, fmt.Errorf("failed to GET KeycloakRealm resorce %q: %w", name, err)
	}

	return realm, nil
}