From 3a10b2ae672ce4feaae482a09c9c4cee51000694 Mon Sep 17 00:00:00 2001 From: HenryNguyen5 <6404866+HenryNguyen5@users.noreply.github.com> Date: Fri, 11 Oct 2024 18:00:14 -0700 Subject: [PATCH] Use deployments rather than pods --- .../src/03_deploy_streams_trigger_cmd.go | 30 ++++---- core/scripts/keystone/src/88_gen_jobspecs.go | 2 +- core/scripts/keystone/src/99_crib_client.go | 33 ++++----- core/scripts/keystone/src/99_fetch_keys.go | 9 ++- core/scripts/keystone/src/99_k8s_client.go | 69 +++++++++---------- core/scripts/keystone/src/99_nodes.go | 10 +-- 6 files changed, 78 insertions(+), 75 deletions(-) diff --git a/core/scripts/keystone/src/03_deploy_streams_trigger_cmd.go b/core/scripts/keystone/src/03_deploy_streams_trigger_cmd.go index 266c11e90f8..d9080266aab 100644 --- a/core/scripts/keystone/src/03_deploy_streams_trigger_cmd.go +++ b/core/scripts/keystone/src/03_deploy_streams_trigger_cmd.go @@ -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, @@ -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, @@ -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 diff --git a/core/scripts/keystone/src/88_gen_jobspecs.go b/core/scripts/keystone/src/88_gen_jobspecs.go index 9f03aa4fb18..352c73304b1 100644 --- a/core/scripts/keystone/src/88_gen_jobspecs.go +++ b/core/scripts/keystone/src/88_gen_jobspecs.go @@ -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, diff --git a/core/scripts/keystone/src/99_crib_client.go b/core/scripts/keystone/src/99_crib_client.go index ebf9f9ee955..bf5e6a73bf8 100644 --- a/core/scripts/keystone/src/99_crib_client.go +++ b/core/scripts/keystone/src/99_crib_client.go @@ -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 { @@ -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) diff --git a/core/scripts/keystone/src/99_fetch_keys.go b/core/scripts/keystone/src/99_fetch_keys.go index 598d7527619..762b12716e8 100644 --- a/core/scripts/keystone/src/99_fetch_keys.go +++ b/core/scripts/keystone/src/99_fetch_keys.go @@ -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) } diff --git a/core/scripts/keystone/src/99_k8s_client.go b/core/scripts/keystone/src/99_k8s_client.go index 55a0ac82bcb..00eed2ef0b7 100644 --- a/core/scripts/keystone/src/99_k8s_client.go +++ b/core/scripts/keystone/src/99_k8s_client.go @@ -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" @@ -63,32 +64,33 @@ 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 } @@ -96,53 +98,48 @@ func (m *K8sClient) GetPodsWithConfigMap() ([]PodWithConfigMap, error) { 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 diff --git a/core/scripts/keystone/src/99_nodes.go b/core/scripts/keystone/src/99_nodes.go index 83c5b1e84f5..234562280f1 100644 --- a/core/scripts/keystone/src/99_nodes.go +++ b/core/scripts/keystone/src/99_nodes.go @@ -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 {