github.com/argoproj/argo-cd/v3@v3.2.1/util/notification/settings/settings.go (about)

     1  package settings
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/argoproj/notifications-engine/pkg/api"
     7  	"github.com/argoproj/notifications-engine/pkg/services"
     8  	corev1 "k8s.io/api/core/v1"
     9  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    10  	"sigs.k8s.io/yaml"
    11  
    12  	"github.com/argoproj/argo-cd/v3/util/notification/expression"
    13  
    14  	service "github.com/argoproj/argo-cd/v3/util/notification/argocd"
    15  )
    16  
    17  func GetFactorySettings(argocdService service.Service, secretName, configMapName string, selfServiceNotificationEnabled bool) api.Settings {
    18  	return api.Settings{
    19  		SecretName:    secretName,
    20  		ConfigMapName: configMapName,
    21  		InitGetVars: func(cfg *api.Config, configMap *corev1.ConfigMap, secret *corev1.Secret) (api.GetVars, error) {
    22  			if selfServiceNotificationEnabled {
    23  				return initGetVarsWithoutSecret(argocdService, cfg, configMap, secret)
    24  			}
    25  			return initGetVars(argocdService, cfg, configMap, secret)
    26  		},
    27  	}
    28  }
    29  
    30  // GetFactorySettingsForCLI allows the initialization of argocdService to be deferred until it is used, when InitGetVars is called.
    31  func GetFactorySettingsForCLI(serviceGetter func() service.Service, secretName, configMapName string, selfServiceNotificationEnabled bool) api.Settings {
    32  	return api.Settings{
    33  		SecretName:    secretName,
    34  		ConfigMapName: configMapName,
    35  		InitGetVars: func(cfg *api.Config, configMap *corev1.ConfigMap, secret *corev1.Secret) (api.GetVars, error) {
    36  			argocdService := serviceGetter()
    37  			if argocdService == nil {
    38  				return nil, errors.New("argocdService is not initialized")
    39  			}
    40  
    41  			if selfServiceNotificationEnabled {
    42  				return initGetVarsWithoutSecret(argocdService, cfg, configMap, secret)
    43  			}
    44  			return initGetVars(argocdService, cfg, configMap, secret)
    45  		},
    46  	}
    47  }
    48  
    49  func getContext(cfg *api.Config, configMap *corev1.ConfigMap, secret *corev1.Secret) (map[string]string, error) {
    50  	context := map[string]string{}
    51  	if contextYaml, ok := configMap.Data["context"]; ok {
    52  		if err := yaml.Unmarshal([]byte(contextYaml), &context); err != nil {
    53  			return nil, err
    54  		}
    55  	}
    56  	if err := ApplyLegacyConfig(cfg, context, configMap, secret); err != nil {
    57  		return nil, err
    58  	}
    59  	return context, nil
    60  }
    61  
    62  func initGetVarsWithoutSecret(argocdService service.Service, cfg *api.Config, configMap *corev1.ConfigMap, secret *corev1.Secret) (api.GetVars, error) {
    63  	context, err := getContext(cfg, configMap, secret)
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  
    68  	return func(obj map[string]any, dest services.Destination) map[string]any {
    69  		return expression.Spawn(&unstructured.Unstructured{Object: obj}, argocdService, map[string]any{
    70  			"app":     obj,
    71  			"context": injectLegacyVar(context, dest.Service),
    72  		})
    73  	}, nil
    74  }
    75  
    76  func initGetVars(argocdService service.Service, cfg *api.Config, configMap *corev1.ConfigMap, secret *corev1.Secret) (api.GetVars, error) {
    77  	context, err := getContext(cfg, configMap, secret)
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  
    82  	return func(obj map[string]any, dest services.Destination) map[string]any {
    83  		return expression.Spawn(&unstructured.Unstructured{Object: obj}, argocdService, map[string]any{
    84  			"app":     obj,
    85  			"context": injectLegacyVar(context, dest.Service),
    86  			"secrets": secret.Data,
    87  		})
    88  	}, nil
    89  }