|
| 1 | +package parrot |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "github.com/smartcontractkit/chainlink-testing-framework/lib/k8s/client" |
| 9 | + "github.com/smartcontractkit/chainlink-testing-framework/lib/k8s/config" |
| 10 | + "github.com/smartcontractkit/chainlink-testing-framework/lib/k8s/environment" |
| 11 | + "github.com/smartcontractkit/chainlink-testing-framework/lib/utils/projectpath" |
| 12 | + |
| 13 | + "github.com/rs/zerolog/log" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + LocalURLsKey = "parrot_local" |
| 18 | + InternalURLsKey = "parrot_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) GetLabels() map[string]string { |
| 57 | + return map[string]string{ |
| 58 | + "chain.link/component": "parrot", |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func (m Chart) ExportData(e *environment.Environment) error { |
| 63 | + parrotLocal, err := e.Fwd.FindPort("parrot:0", "parrot", "http").As(client.LocalConnection, client.HTTP) |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + services, err := e.Client.ListServices(e.Cfg.Namespace, fmt.Sprintf("app=%s", m.Name)) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + var parrotInternal string |
| 72 | + if services != nil && len(services.Items) != 0 { |
| 73 | + parrotInternal = fmt.Sprintf("http://%s:80", services.Items[0].Name) |
| 74 | + } else { |
| 75 | + parrotInternal, err = e.Fwd.FindPort("parrot:0", "parrot", "http").As(client.RemoteConnection, client.HTTP) |
| 76 | + if err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + } |
| 80 | + if e.Cfg.InsideK8s { |
| 81 | + parrotLocal = parrotInternal |
| 82 | + } |
| 83 | + |
| 84 | + e.URLs[LocalURLsKey] = []string{parrotLocal} |
| 85 | + e.URLs[InternalURLsKey] = []string{parrotInternal} |
| 86 | + log.Info().Str("Local Connection", parrotLocal).Str("Internal Connection", parrotInternal).Msg("Parrot") |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +func defaultProps() map[string]interface{} { |
| 91 | + internalRepo := os.Getenv(config.EnvVarInternalDockerRepo) |
| 92 | + mockserverRepo := "parrot" |
| 93 | + if internalRepo != "" { |
| 94 | + mockserverRepo = fmt.Sprintf("%s/parrot", internalRepo) |
| 95 | + } |
| 96 | + |
| 97 | + return map[string]interface{}{ |
| 98 | + "replicaCount": "1", |
| 99 | + "service": map[string]interface{}{ |
| 100 | + "type": "NodePort", |
| 101 | + "port": "1080", |
| 102 | + }, |
| 103 | + "app": map[string]interface{}{ |
| 104 | + "logLevel": "INFO", |
| 105 | + "serverPort": "1080", |
| 106 | + "mountedConfigMapName": "mockserver-config", |
| 107 | + "propertiesFileName": "mockserver.properties", |
| 108 | + "readOnlyRootFilesystem": "false", |
| 109 | + "resources": map[string]interface{}{ |
| 110 | + "requests": map[string]interface{}{ |
| 111 | + "cpu": "200m", |
| 112 | + "memory": "256Mi", |
| 113 | + }, |
| 114 | + "limits": map[string]interface{}{ |
| 115 | + "cpu": "200m", |
| 116 | + "memory": "256Mi", |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + "image": map[string]interface{}{ |
| 121 | + "repository": mockserverRepo, |
| 122 | + "version": "5.15.0", |
| 123 | + "snapshot": false, |
| 124 | + "pullPolicy": "IfNotPresent", |
| 125 | + }, |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func New(props map[string]interface{}) environment.ConnectedChart { |
| 130 | + return NewVersioned("", props) |
| 131 | +} |
| 132 | + |
| 133 | +// NewVersioned enables choosing a specific helm chart version |
| 134 | +func NewVersioned(helmVersion string, props map[string]interface{}) environment.ConnectedChart { |
| 135 | + dp := defaultProps() |
| 136 | + config.MustMerge(&dp, props) |
| 137 | + chartPath := "chainlink-qa/parrot" |
| 138 | + if b, err := strconv.ParseBool(os.Getenv(config.EnvVarLocalCharts)); err == nil && b { |
| 139 | + chartPath = fmt.Sprintf("%s/parrot", projectpath.ChartsRoot) |
| 140 | + } |
| 141 | + return Chart{ |
| 142 | + Name: "parrot", |
| 143 | + Path: chartPath, |
| 144 | + Values: &dp, |
| 145 | + Version: helmVersion, |
| 146 | + } |
| 147 | +} |
0 commit comments