github.com/smartcontractkit/chainlink-testing-framework/libs@v0.0.0-20240227141906-ec710b4eb1a3/k8s/pkg/helm/mockserver/mockserver.go (about) 1 package mockserver 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 = "mockserver_local" 18 InternalURLsKey = "mockserver_internal" 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("mockserver:0", "mockserver", "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:1080", services.Items[0].Name) 68 } else { 69 mockInternal, err = e.Fwd.FindPort("mockserver:0", "mockserver", "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("Mockserver") 81 return nil 82 } 83 84 func defaultProps() map[string]interface{} { 85 internalRepo := os.Getenv(config.EnvVarInternalDockerRepo) 86 mockserverRepo := "mockserver" 87 if internalRepo != "" { 88 mockserverRepo = fmt.Sprintf("%s/mockserver", internalRepo) 89 } 90 91 return map[string]interface{}{ 92 "replicaCount": "1", 93 "service": map[string]interface{}{ 94 "type": "NodePort", 95 "port": "1080", 96 }, 97 "app": map[string]interface{}{ 98 "logLevel": "INFO", 99 "serverPort": "1080", 100 "mountedConfigMapName": "mockserver-config", 101 "propertiesFileName": "mockserver.properties", 102 "readOnlyRootFilesystem": "false", 103 "resources": map[string]interface{}{ 104 "requests": map[string]interface{}{ 105 "cpu": "200m", 106 "memory": "256Mi", 107 }, 108 "limits": map[string]interface{}{ 109 "cpu": "200m", 110 "memory": "256Mi", 111 }, 112 }, 113 }, 114 "image": map[string]interface{}{ 115 "repository": mockserverRepo, 116 "snapshot": false, 117 "pullPolicy": "IfNotPresent", 118 }, 119 } 120 } 121 122 func New(props map[string]interface{}) environment.ConnectedChart { 123 return NewVersioned("", props) 124 } 125 126 // NewVersioned enables choosing a specific helm chart version 127 func NewVersioned(helmVersion string, props map[string]interface{}) environment.ConnectedChart { 128 dp := defaultProps() 129 config.MustMerge(&dp, props) 130 chartPath := "chainlink-qa/mockserver" 131 if b, err := strconv.ParseBool(os.Getenv(config.EnvVarLocalCharts)); err == nil && b { 132 chartPath = fmt.Sprintf("%s/mockserver", projectpath.ChartsRoot) 133 } 134 return Chart{ 135 Name: "mockserver", 136 Path: chartPath, 137 Values: &dp, 138 Version: helmVersion, 139 } 140 }