Skip to content

Commit

Permalink
Use deployments rather than pods
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryNguyen5 committed Oct 12, 2024
1 parent 136bcad commit 3a10b2a
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 75 deletions.
30 changes: 15 additions & 15 deletions core/scripts/keystone/src/03_deploy_streams_trigger_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func setupMercuryV03(env helpers.Environment, nodeListPath string, ocrConfigFile
fmt.Printf("Signers: %v\n", ocrConfig.Signers)
fmt.Printf("Transmitters: %v\n", ocrConfig.Transmitters)
fmt.Printf("F: %d\n", ocrConfig.F)

tx, err := verifier.SetConfig(
env.Owner,
feed.id,
Expand Down Expand Up @@ -278,7 +278,7 @@ func deployOCR2JobSpecsForFeed(nca []NodeKeys, nodes []*node, verifier *verifier
// Prepare data for Mercury V3 Job
mercuryData := MercuryV3JobSpecData{
FeedName: fmt.Sprintf("feed-%s", feed.name),
BootstrapHost: fmt.Sprintf("%s@%s:%s", nca[0].P2PPeerID, "app-node1", "6690"),
BootstrapHost: fmt.Sprintf("%s@%s:%s", nca[0].P2PPeerID, nodes[0].serviceName, "6690"),
VerifierAddress: verifier.Address().Hex(),
Bridge: feed.bridgeName,
NodeCSAKey: n.CSAPublicKey,
Expand All @@ -298,28 +298,28 @@ func deployOCR2JobSpecsForFeed(nca []NodeKeys, nodes []*node, verifier *verifier
}

func upsertJob(api *nodeAPI, jobSpecName string, jobSpecStr string, upsert bool) {
jobsResp := api.mustExec(api.methods.ListJobs)
jobs := mustJSON[[]JobSpec](jobsResp)
for _, job := range *jobs {
if job.Name == jobSpecName {
jobsResp := api.mustExec(api.methods.ListJobs)
jobs := mustJSON[[]JobSpec](jobsResp)
for _, job := range *jobs {
if job.Name == jobSpecName {
if !upsert {
fmt.Printf("Job already exists: %s, skipping..\n", jobSpecName)
return
}

fmt.Printf("Job already exists: %s, replacing..\n", jobSpecName)
api.withArg(job.Id).mustExec(api.methods.DeleteJob)
fmt.Printf("Deleted job: %s\n", jobSpecName)
fmt.Printf("Job already exists: %s, replacing..\n", jobSpecName)
api.withArg(job.Id).mustExec(api.methods.DeleteJob)
fmt.Printf("Deleted job: %s\n", jobSpecName)
break
}
}
}

fmt.Printf("Deploying jobspec: %s\n... \n", jobSpecStr)
_, err := api.withArg(jobSpecStr).exec(api.methods.CreateJob)
if err != nil {
panic(fmt.Sprintf("Failed to deploy job spec: %s Error: %s", jobSpecStr, err))
}
fmt.Printf("Deploying jobspec: %s\n... \n", jobSpecStr)
_, err := api.withArg(jobSpecStr).exec(api.methods.CreateJob)
if err != nil {
panic(fmt.Sprintf("Failed to deploy job spec: %s Error: %s", jobSpecStr, err))
}
}

type WorkflowJobSpecConfig struct {
JobSpecName string
Expand Down
2 changes: 1 addition & 1 deletion core/scripts/keystone/src/88_gen_jobspecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func genSpecs(

bootstrapSpecLines, err := readLines(filepath.Join(templatesDir, bootstrapSpecTemplate))
helpers.PanicErr(err)
bootHost := nodes[0].remoteURL.Hostname()
bootHost := nodes[0].serviceName
bootstrapSpecLines = replacePlaceholders(
bootstrapSpecLines,
chainID, p2pPort,
Expand Down
33 changes: 17 additions & 16 deletions core/scripts/keystone/src/99_crib_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ type CribClient struct {
}

type CLNodeCredentials struct {
URL *url.URL
PodName string
Username string
Password string
NodePassword string
URL *url.URL
DeploymentName string
ServiceName string
Username string
Password string
NodePassword string
}

func NewCribClient() *CribClient {
Expand All @@ -28,30 +29,30 @@ func NewCribClient() *CribClient {
}

func (m *CribClient) GetCLNodeCredentials() ([]CLNodeCredentials, error) {
fmt.Println("Getting CL node pods with config maps...")
pods, err := m.k8sClient.GetPodsWithConfigMap()
fmt.Println("Getting CL node deployments with config maps...")
deployments, err := m.k8sClient.GetDeploymentsWithConfigMap()
if err != nil {
return nil, err
}
clNodeCredentials := []CLNodeCredentials{}

for _, pod := range pods {
apiCredentials := pod.ConfigMap.Data["apicredentials"]
for _, deployment := range deployments {
apiCredentials := deployment.ConfigMap.Data["apicredentials"]
splitCreds := strings.Split(strings.TrimSpace(apiCredentials), "\n")
username := splitCreds[0]
password := splitCreds[1]
nodePassword := pod.ConfigMap.Data["node-password"]
url, err := url.Parse("https://" + pod.Host)
nodePassword := deployment.ConfigMap.Data["node-password"]
url, err := url.Parse("https://" + deployment.Host)
if err != nil {
return nil, err
}

clNodeCredential := CLNodeCredentials{
URL: url,
PodName: pod.Name,
Username: username,
Password: password,
NodePassword: nodePassword,
URL: url,
DeploymentName: deployment.Name,
Username: username,
Password: password,
NodePassword: nodePassword,
}

clNodeCredentials = append(clNodeCredentials, clNodeCredential)
Expand Down
9 changes: 6 additions & 3 deletions core/scripts/keystone/src/99_fetch_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,12 @@ func clNodesWithCredsToNodes(clNodesWithCreds []CLNodeCredentials) []*node {
nodes := []*node{}
for _, cl := range clNodesWithCreds {
n := node{
url: cl.URL,
password: cl.Password,
login: cl.Username,
url: cl.URL,
remoteURL: cl.URL,
serviceName: cl.ServiceName,
deploymentName: cl.DeploymentName,
password: cl.Password,
login: cl.Username,
}
nodes = append(nodes, &n)
}
Expand Down
69 changes: 33 additions & 36 deletions core/scripts/keystone/src/99_k8s_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sort"
"strings"

apps "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
networkingV1 "k8s.io/api/networking/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -63,86 +64,82 @@ func MustNewK8sClient() *K8sClient {
}
}

type PodWithConfigMap struct {
v1.Pod
type DeploymentWithConfigMap struct {
apps.Deployment
ServiceName string
ConfigMap v1.ConfigMap
Host string
}

func (m *K8sClient) GetPodsWithConfigMap() ([]PodWithConfigMap, error) {
pods, err := m.ListPods("app=app")
func (m *K8sClient) GetDeploymentsWithConfigMap() ([]DeploymentWithConfigMap, error) {
deployments, err := m.ListDeployments("app=app")
if err != nil {
return nil, err
}
if len(pods.Items) == 0 {
return nil, fmt.Errorf("no chainlink node crib pods found, is your crib cluster deployed?")
if len(deployments.Items) == 0 {
return nil, fmt.Errorf("no deployments found, is your cluster deployed?")
}

podsWithConfigMaps := []PodWithConfigMap{}
deploymentsWithConfigMaps := []DeploymentWithConfigMap{}
ingressList, err := m.ListIngresses()
if err != nil {
return nil, err
}
if len(ingressList.Items) == 0 {
return nil, fmt.Errorf("no ingress found, is your crib cluster deployed?")
return nil, fmt.Errorf("no ingress found, is your cluster deployed?")
}

for _, pod := range pods.Items {
for _, v := range pod.Spec.Volumes {
for _, deployment := range deployments.Items {
for _, v := range deployment.Spec.Template.Spec.Volumes {
if v.ConfigMap == nil {
continue
}
cm, err := m.GetConfigMap(v.ConfigMap.Name)
if err != nil {
return nil, err
}
// - host: crib-henry-keystone-node2.main.stage.cldev.sh
// http:
// paths:
// - backend:
// service:
// name: app-node-2
// port:
// number: 6688
// path: /*
// pathType: ImplementationSpecific
instance := pod.Labels["instance"]
instance := deployment.Labels["instance"]
var host string
var serviceName string
for _, ingress := range ingressList.Items {
for _, rule := range ingress.Spec.Rules {
for _, path := range rule.HTTP.Paths {
if strings.Contains(path.Backend.Service.Name, instance) {
host = rule.Host
serviceName = path.Backend.Service.Name
}
}
}
}

if host == "" {
return nil, fmt.Errorf("could not find host for pod %s", pod.Name)
return nil, fmt.Errorf("could not find host for deployment %s", deployment.Name)
}

podWithConfigMap := PodWithConfigMap{
Host: host,
Pod: pod,
ConfigMap: *cm,
deploymentWithConfigMap := DeploymentWithConfigMap{
Host: host,
ServiceName: serviceName,
Deployment: deployment,
ConfigMap: *cm,
}
podsWithConfigMaps = append(podsWithConfigMaps, podWithConfigMap)
deploymentsWithConfigMaps = append(deploymentsWithConfigMaps, deploymentWithConfigMap)
}
}

fmt.Printf("Found %d chainlink node crib pods\n", len(podsWithConfigMaps))
return podsWithConfigMaps, nil
fmt.Printf("Found %d deployments with config maps\n", len(deploymentsWithConfigMaps))
return deploymentsWithConfigMaps, nil
}

// ListPods lists pods for a namespace and selector
func (m *K8sClient) ListPods(selector string) (*v1.PodList, error) {
pods, err := m.ClientSet.CoreV1().Pods(m.namespace).List(context.Background(), metaV1.ListOptions{LabelSelector: selector})
sort.Slice(pods.Items, func(i, j int) bool {
return pods.Items[i].CreationTimestamp.Before(pods.Items[j].CreationTimestamp.DeepCopy())
// ListDeployments lists deployments for a namespace
func (m *K8sClient) ListDeployments(selector string) (*apps.DeploymentList, error) {
deployments, err := m.ClientSet.AppsV1().Deployments(m.namespace).List(context.Background(), metaV1.ListOptions{LabelSelector: selector})
if err != nil {
return nil, err
}
sort.Slice(deployments.Items, func(i, j int) bool {
return deployments.Items[i].CreationTimestamp.Before(deployments.Items[j].CreationTimestamp.DeepCopy())
})

return pods.DeepCopy(), err
return deployments.DeepCopy(), nil
}

// Get a config map
Expand Down
10 changes: 6 additions & 4 deletions core/scripts/keystone/src/99_nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import (
)

type node struct {
url *url.URL
remoteURL *url.URL
login string
password string
url *url.URL
remoteURL *url.URL
serviceName string
deploymentName string
login string
password string
}

func (n node) IsTerminal() bool {
Expand Down

0 comments on commit 3a10b2a

Please sign in to comment.