Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor state loader functions get all node deployments at once #1298

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 63 additions & 54 deletions grid-client/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -506,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
Expand All @@ -571,3 +566,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
}
6 changes: 3 additions & 3 deletions grid-client/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Loading