github.com/smartcontractkit/chainlink-testing-framework/libs@v0.0.0-20240227141906-ec710b4eb1a3/k8s/pkg/helm/mock-adapter/mock-adapter.go (about)

     1  package mock_adapter
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strconv"
     7  
     8  	"github.com/rs/zerolog/log"
     9  
    10  	"github.com/smartcontractkit/chainlink-testing-framework/libs/k8s/client"
    11  	"github.com/smartcontractkit/chainlink-testing-framework/libs/k8s/config"
    12  	"github.com/smartcontractkit/chainlink-testing-framework/libs/k8s/environment"
    13  	"github.com/smartcontractkit/chainlink-testing-framework/libs/utils/projectpath"
    14  )
    15  
    16  const (
    17  	LocalURLsKey    = "qa_mock_adapter_local"    // #nosec G101
    18  	InternalURLsKey = "qa_mock_adapter_internal" // #nosec G101
    19  )
    20  
    21  type Props struct {
    22  }
    23  
    24  type Chart struct {
    25  	Name    string
    26  	Path    string
    27  	Version string
    28  	Props   *Props
    29  	Values  *map[string]interface{}
    30  }
    31  
    32  func (m Chart) IsDeploymentNeeded() bool {
    33  	return true
    34  }
    35  
    36  func (m Chart) GetName() string {
    37  	return m.Name
    38  }
    39  
    40  func (m Chart) GetPath() string {
    41  	return m.Path
    42  }
    43  
    44  func (m Chart) GetVersion() string {
    45  	return m.Version
    46  }
    47  
    48  func (m Chart) GetProps() interface{} {
    49  	return m.Props
    50  }
    51  
    52  func (m Chart) GetValues() *map[string]interface{} {
    53  	return m.Values
    54  }
    55  
    56  func (m Chart) ExportData(e *environment.Environment) error {
    57  	mockLocal, err := e.Fwd.FindPort("qa-mock-adapter:0", "qa-mock-adapter", "serviceport").As(client.LocalConnection, client.HTTP)
    58  	if err != nil {
    59  		return err
    60  	}
    61  	services, err := e.Client.ListServices(e.Cfg.Namespace, fmt.Sprintf("app=%s", m.Name))
    62  	if err != nil {
    63  		return err
    64  	}
    65  	var mockInternal string
    66  	if services != nil && len(services.Items) != 0 {
    67  		mockInternal = fmt.Sprintf("http://%s:6060", services.Items[0].Name)
    68  	} else {
    69  		mockInternal, err = e.Fwd.FindPort("qa-mock-adapter:0", "qa-mock-adapter", "serviceport").As(client.RemoteConnection, client.HTTP)
    70  		if err != nil {
    71  			return err
    72  		}
    73  	}
    74  	if e.Cfg.InsideK8s {
    75  		mockLocal = mockInternal
    76  	}
    77  
    78  	e.URLs[LocalURLsKey] = []string{mockLocal}
    79  	e.URLs[InternalURLsKey] = []string{mockInternal}
    80  	log.Info().Str("Local Connection", mockLocal).Str("Internal Connection", mockInternal).Msg("QA Mock Adapter")
    81  	return nil
    82  }
    83  
    84  func defaultProps() map[string]interface{} {
    85  	internalRepo := os.Getenv(config.EnvVarInternalDockerRepo)
    86  	qaMockAdapterRepo := "qa-mock-adapter"
    87  	if internalRepo != "" {
    88  		qaMockAdapterRepo = internalRepo
    89  	}
    90  
    91  	return map[string]interface{}{
    92  		"replicaCount": "1",
    93  		"service": map[string]interface{}{
    94  			"type": "NodePort",
    95  			"port": "6060",
    96  		},
    97  		"app": map[string]interface{}{
    98  			"serverPort": "6060",
    99  			"resources": map[string]interface{}{
   100  				"requests": map[string]interface{}{
   101  					"cpu":    "200m",
   102  					"memory": "256Mi",
   103  				},
   104  				"limits": map[string]interface{}{
   105  					"cpu":    "200m",
   106  					"memory": "256Mi",
   107  				},
   108  			},
   109  		},
   110  		"image": map[string]interface{}{
   111  			"repository": qaMockAdapterRepo,
   112  			"snapshot":   false,
   113  			"pullPolicy": "IfNotPresent",
   114  		},
   115  	}
   116  }
   117  
   118  func New(props map[string]interface{}) environment.ConnectedChart {
   119  	return NewVersioned("", props)
   120  }
   121  
   122  // NewVersioned enables choosing a specific helm chart version
   123  func NewVersioned(helmVersion string, props map[string]interface{}) environment.ConnectedChart {
   124  	dp := defaultProps()
   125  	config.MustMerge(&dp, props)
   126  	chartPath := "chainlink-qa/qa-mock-adapter"
   127  	if b, err := strconv.ParseBool(os.Getenv(config.EnvVarLocalCharts)); err == nil && b {
   128  		chartPath = fmt.Sprintf("%s/qa-mock-adapter", projectpath.ChartsRoot)
   129  	}
   130  	return Chart{
   131  		Name:    "qa-mock-adapter",
   132  		Path:    chartPath,
   133  		Values:  &dp,
   134  		Version: helmVersion,
   135  	}
   136  }