From 92d1aa03e5d11776d901d4ceb72cd9a6687306ee Mon Sep 17 00:00:00 2001 From: Eslam-Nawara Date: Tue, 17 Dec 2024 15:45:44 +0200 Subject: [PATCH 1/3] refactor load network from grid to get all node deployments at once --- grid-client/state/state.go | 52 ++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/grid-client/state/state.go b/grid-client/state/state.go index f264e012..e3bb44b5 100644 --- a/grid-client/state/state.go +++ b/grid-client/state/state.go @@ -316,20 +316,17 @@ func (st *State) LoadNetworkFromGrid(ctx context.Context, name string) (znet wor return znet, errors.Wrapf(err, "could not get node client: %d", nodeID) } - for _, contractID := range st.CurrentNodeDeployments[nodeID] { - dl, err := nodeClient.DeploymentGet(ctx, contractID) - if err != nil { - return znet, errors.Wrapf(err, "could not get network deployment %d from node %d", contractID, nodeID) - } + dls, err := nodeClient.DeploymentList(ctx) + if err != nil { + return znet, errors.Wrapf(err, "could not get all deployments from node %d", nodeID) + } + + for _, dl := range dls { if len(strings.TrimSpace(dl.Metadata)) == 0 { - contract, err := sub.GetContract(contractID) + dl.Metadata, err = getContractMetadata(sub, dl.ContractID, nodeID) if err != nil { - return znet, errors.Wrapf(err, "could not get contract %d from node %d", contractID, nodeID) - } - dl.Metadata = contract.ContractType.NodeContract.DeploymentData - if len(strings.TrimSpace(dl.Metadata)) == 0 { - return znet, errors.Wrapf(err, "contract %d doesn't have metadata", contractID) + return znet, err } } @@ -410,25 +407,22 @@ func (st *State) LoadNetworkLightFromGrid(ctx context.Context, name string) (zne sub := st.Substrate for nodeID := range st.CurrentNodeDeployments { + nodeClient, err := st.NcPool.GetNodeClient(sub, nodeID) if err != nil { return znet, errors.Wrapf(err, "could not get node client: %d", nodeID) } - for _, contractID := range st.CurrentNodeDeployments[nodeID] { - dl, err := nodeClient.DeploymentGet(ctx, contractID) - if err != nil { - return znet, errors.Wrapf(err, "could not get network deployment %d from node %d", contractID, nodeID) - } + dls, err := nodeClient.DeploymentList(ctx) + if err != nil { + return znet, errors.Wrapf(err, "could not get all deployments from node %d", nodeID) + } + for _, dl := range dls { if len(strings.TrimSpace(dl.Metadata)) == 0 { - contract, err := sub.GetContract(contractID) + dl.Metadata, err = getContractMetadata(sub, dl.ContractID, nodeID) if err != nil { - return znet, errors.Wrapf(err, "could not get contract %d from node %d", contractID, nodeID) - } - dl.Metadata = contract.ContractType.NodeContract.DeploymentData - if len(strings.TrimSpace(dl.Metadata)) == 0 { - return znet, errors.Wrapf(err, "contract %d doesn't have metadata", contractID) + return znet, err } } @@ -571,3 +565,17 @@ func (st *State) AssignNodesIPRange(k *workloads.K8sCluster) (err error) { return nil } + +func getContractMetadata(sub subi.SubstrateExt, contractID uint64, nodeID uint32) (string, error) { + contract, err := sub.GetContract(contractID) + if err != nil { + return "", errors.Wrapf(err, "could not get contract %d from node %d", contractID, nodeID) + } + + metadata := contract.ContractType.NodeContract.DeploymentData + if len(strings.TrimSpace(metadata)) == 0 { + return "", errors.Wrapf(err, "contract %d doesn't have metadata", contractID) + } + + return metadata, nil +} From a4a322da10f3788970f007f28bc1c27e0203dc7c Mon Sep 17 00:00:00 2001 From: Eslam-Nawara Date: Thu, 26 Dec 2024 16:46:20 +0200 Subject: [PATCH 2/3] update GetWorkloadInDeployment to use list deployments --- grid-client/state/state.go | 65 +++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/grid-client/state/state.go b/grid-client/state/state.go index e3bb44b5..ae6f52da 100644 --- a/grid-client/state/state.go +++ b/grid-client/state/state.go @@ -500,51 +500,52 @@ func (st *State) LoadDeploymentFromGrid(ctx context.Context, nodeID uint32, name // if name is empty it returns a deployment with name equal to deploymentName and empty workload func (st *State) GetWorkloadInDeployment(ctx context.Context, nodeID uint32, name string, deploymentName string) (zosTypes.Workload, zosTypes.Deployment, error) { sub := st.Substrate - if contractIDs, ok := st.CurrentNodeDeployments[nodeID]; ok { - nodeClient, err := st.NcPool.GetNodeClient(sub, nodeID) - if err != nil { - return zosTypes.Workload{}, zosTypes.Deployment{}, errors.Wrapf(err, "could not get node client: %d", nodeID) - } + nodeClient, err := st.NcPool.GetNodeClient(sub, nodeID) + if err != nil { + return zosTypes.Workload{}, zosTypes.Deployment{}, errors.Wrapf(err, "could not get node client: %d", nodeID) + } + + if _, ok := st.CurrentNodeDeployments[nodeID]; !ok { + return zosTypes.Workload{}, zosTypes.Deployment{}, errors.Wrapf(ErrNotFound, "failed to find deployment %s on node %d", name, nodeID) + } + + dls, err := nodeClient.DeploymentList(ctx) + if err != nil { + return zosTypes.Workload{}, zosTypes.Deployment{}, errors.Wrapf(err, "could not get deployments from node %d", nodeID) + } - for _, contractID := range contractIDs { - dl, err := nodeClient.DeploymentGet(ctx, contractID) + for _, dl := range dls { + if len(strings.TrimSpace(dl.Metadata)) == 0 { if err != nil { - return zosTypes.Workload{}, zosTypes.Deployment{}, errors.Wrapf(err, "could not get deployment %d from node %d", contractID, nodeID) + return zosTypes.Workload{}, zosTypes.Deployment{}, errors.Wrapf(err, "could not get contract %d from node %d", dl.ContractID, nodeID) } + dl.Metadata, err = getContractMetadata(sub, dl.ContractID, nodeID) if len(strings.TrimSpace(dl.Metadata)) == 0 { - contract, err := sub.GetContract(contractID) - if err != nil { - return zosTypes.Workload{}, zosTypes.Deployment{}, errors.Wrapf(err, "could not get contract %d from node %d", contractID, nodeID) - } - dl.Metadata = contract.ContractType.NodeContract.DeploymentData - if len(strings.TrimSpace(dl.Metadata)) == 0 { - return zosTypes.Workload{}, zosTypes.Deployment{}, errors.Wrapf(err, "contract %d doesn't have metadata", contractID) - } + return zosTypes.Workload{}, zosTypes.Deployment{}, errors.Wrapf(err, "contract %d doesn't have metadata", dl.ContractID) } + } - dlData, err := workloads.ParseDeploymentData(dl.Metadata) - if err != nil { - return zosTypes.Workload{}, zosTypes.Deployment{}, errors.Wrapf(err, "could not get deployment %d data", contractID) - } + dlData, err := workloads.ParseDeploymentData(dl.Metadata) + if err != nil { + return zosTypes.Workload{}, zosTypes.Deployment{}, errors.Wrapf(err, "could not get deployment %d data", dl.ContractID) + } - if dlData.Name != deploymentName { - continue - } + if dlData.Name != deploymentName { + continue + } - if name == "" { - return zosTypes.Workload{}, dl, nil - } + if name == "" { + return zosTypes.Workload{}, dl, nil + } - for _, workload := range dl.Workloads { - if workload.Name == name { - return workload, dl, nil - } + for _, wl := range dl.Workloads { + if wl.Name == name { + return wl, dl, nil } } - return zosTypes.Workload{}, zosTypes.Deployment{}, errors.Wrapf(ErrNotFound, "failed to find workload '%s'", name) } - return zosTypes.Workload{}, zosTypes.Deployment{}, errors.Wrapf(ErrNotFound, "failed to find deployment %s on node %d", name, nodeID) + return zosTypes.Workload{}, zosTypes.Deployment{}, errors.Wrapf(ErrNotFound, "failed to find workload '%s'", name) } // AssignNodesIPRange to assign ip range of k8s cluster nodes From 200b200748d7a26d2beb603fdee35487ec01b3c5 Mon Sep 17 00:00:00 2001 From: Eslam-Nawara Date: Thu, 26 Dec 2024 16:46:32 +0200 Subject: [PATCH 3/3] fix client tests --- grid-client/state/state_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/grid-client/state/state_test.go b/grid-client/state/state_test.go index f195a6d4..942aa76e 100644 --- a/grid-client/state/state_test.go +++ b/grid-client/state/state_test.go @@ -43,11 +43,11 @@ func SetupLoaderTests(t *testing.T, wls []zosTypes.Workload) *State { Return(client.NewNodeClient(13, cl, 10), nil).AnyTimes() cl.EXPECT(). - Call(gomock.Any(), uint32(13), "zos.deployment.get", gomock.Any(), gomock.Any()). + Call(gomock.Any(), uint32(13), "zos.deployment.list", gomock.Any(), gomock.Any()). DoAndReturn(func(ctx context.Context, twin uint32, fn string, data, result interface{}) error { - var res *zosTypes.Deployment = result.(*zosTypes.Deployment) + var res *[]zosTypes.Deployment = result.(*[]zosTypes.Deployment) dl1.Metadata = "{\"type\":\"\",\"name\":\"testName\",\"projectName\":\"\"}" - *res = dl1 + *res = []zosTypes.Deployment{dl1} return nil }).AnyTimes()