github.com/freiheit-com/kuberpult@v1.24.2-0.20240328135542-315d5630abe6/services/rollout-service/pkg/notifier/notifier.go (about)

     1  /*This file is part of kuberpult.
     2  
     3  Kuberpult is free software: you can redistribute it and/or modify
     4  it under the terms of the Expat(MIT) License as published by
     5  the Free Software Foundation.
     6  
     7  Kuberpult is distributed in the hope that it will be useful,
     8  but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    10  MIT License for more details.
    11  
    12  You should have received a copy of the MIT License
    13  along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>.
    14  
    15  Copyright 2023 freiheit.com*/
    16  
    17  package notifier
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"time"
    23  
    24  	argoapplication "github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
    25  	argoappv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
    26  	"github.com/freiheit-com/kuberpult/pkg/logger"
    27  	"github.com/freiheit-com/kuberpult/pkg/ptr"
    28  	"go.uber.org/zap"
    29  	"golang.org/x/sync/errgroup"
    30  	"google.golang.org/grpc"
    31  	"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
    32  )
    33  
    34  type SimplifiedApplicationInterface interface {
    35  	Get(ctx context.Context, in *argoapplication.ApplicationQuery, opts ...grpc.CallOption) (*argoappv1.Application, error)
    36  }
    37  
    38  type Notifier interface {
    39  	NotifyArgoCd(ctx context.Context, environment, application string)
    40  }
    41  
    42  func New(client SimplifiedApplicationInterface, concurrencyLimit int) Notifier {
    43  	n := &notifier{client, errgroup.Group{}}
    44  	n.errGroup.SetLimit(concurrencyLimit)
    45  	return n
    46  }
    47  
    48  type notifier struct {
    49  	client   SimplifiedApplicationInterface
    50  	errGroup errgroup.Group
    51  }
    52  
    53  func (n *notifier) NotifyArgoCd(ctx context.Context, environment, application string) {
    54  	n.errGroup.Go(func() error {
    55  		var err error
    56  		span, ctx := tracer.StartSpanFromContext(ctx, "argocd.refresh")
    57  		span.SetTag("environment", environment)
    58  		span.SetTag("application", application)
    59  		ctx, cancel := context.WithTimeout(ctx, 60*time.Second)
    60  		defer cancel()
    61  		l := logger.FromContext(ctx).With(zap.String("environment", environment), zap.String("application", application))
    62  		//exhaustruct:ignore
    63  		_, err = n.client.Get(ctx, &argoapplication.ApplicationQuery{
    64  			Name:    ptr.FromString(fmt.Sprintf("%s-%s", environment, application)),
    65  			Refresh: ptr.FromString(string(argoappv1.RefreshTypeNormal)),
    66  		})
    67  		if err != nil {
    68  			l.Error("argocd.refresh", zap.Error(err))
    69  		}
    70  		span.Finish(tracer.WithError(err))
    71  		return nil
    72  	})
    73  }