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 network state grid client #866

Merged
Merged
Show file tree
Hide file tree
Changes from 13 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
467 changes: 296 additions & 171 deletions grid-client/deployer/deployment_deployer.go

Large diffs are not rendered by default.

202 changes: 113 additions & 89 deletions grid-client/deployer/deployment_deployer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,133 +280,139 @@ func musUnmarshal(t *testing.T, bs json.RawMessage, v interface{}) {
assert.NoError(t, err)
}

func TestDeploymentDeployer(t *testing.T) {
func TestDeploymentDeployerValidate(t *testing.T) {
tfPluginClient, err := setup()
assert.NoError(t, err)

t.Run("test validate", func(t *testing.T) {
d, _, sub, _, _ := constructTestDeployer(t, tfPluginClient, false)

t.Run("test validate flist checksum ", func(t *testing.T) {
dl := constructTestDeployment()
d, _, _, _, _ := constructTestDeployer(t, tfPluginClient, false)

network := dl.NetworkName
checksum := dl.Vms[0].FlistChecksum
dl.NetworkName = network

dl.Vms[0].FlistChecksum += " "
assert.Error(t, d.Validate(context.Background(), &dl))
assert.Error(t, d.Validate(context.Background(), []*workloads.Deployment{&dl}))

dl.Vms[0].FlistChecksum = checksum
assert.NoError(t, d.Validate(context.Background(), &dl))
assert.NoError(t, d.Validate(context.Background(), []*workloads.Deployment{&dl}))
})

t.Run("Validation failed", func(t *testing.T) {
dl := constructTestDeployment()

sub.EXPECT().
GetBalance(d.tfPluginClient.Identity).
Return(substrate.Balance{
Free: types.U128{
Int: big.NewInt(10),
},
}, nil)

assert.Error(t, d.Deploy(context.Background(), &dl))

// nothing should change
assert.Empty(t, dl.NodeDeploymentID)
assert.Empty(t, dl.ContractID)
assert.Equal(t, d.tfPluginClient.State.CurrentNodeDeployments, map[uint32]state.ContractIDs{})
})
}

func TestDeploymentDeployerDeploy(t *testing.T) {
tfPluginClient, err := setup()
assert.NoError(t, err)

d, cl, sub, ncPool, deployer := constructTestDeployer(t, tfPluginClient, true)
dl := constructTestDeployment()
net := constructTestNetwork()

t.Run("test generate deployment", func(t *testing.T) {
gridDl, err := dl.ZosDeployment(twinID)
assert.NoError(t, err)

net := constructTestNetwork()
workload := net.ZosWorkload(net.NodesIPRange[nodeID], "", uint16(0), []zos.Peer{}, "", nil)
networkDl := workloads.NewGridDeployment(twinID, []gridtypes.Workload{workload})

d.tfPluginClient.State.CurrentNodeDeployments[nodeID] = append(d.tfPluginClient.State.CurrentNodeDeployments[nodeID], netContractID)
d.tfPluginClient.State.Networks = state.NetworkState{net.Name: state.Network{
Subnets: map[uint32]string{nodeID: net.IPRange.String()},
NodeDeploymentHostIDs: map[uint32]state.DeploymentHostIDs{nodeID: map[uint64][]byte{contractID: {}}},
Subnets: map[uint32]string{nodeID: net.IPRange.String()},
}}

ncPool.EXPECT().
GetNodeClient(sub, nodeID).
Return(client.NewNodeClient(twinID, cl, d.tfPluginClient.RMBTimeout), nil)
Return(client.NewNodeClient(twinID, cl, d.tfPluginClient.RMBTimeout), nil).AnyTimes()

cl.EXPECT().
Call(gomock.Any(), twinID, "zos.deployment.get", gomock.Any(), gomock.Any()).
Call(gomock.Any(), twinID, "zos.network.list_private_ips", gomock.Any(), gomock.Any()).
DoAndReturn(func(ctx context.Context, twin uint32, fn string, data, result interface{}) error {
var res *gridtypes.Deployment = result.(*gridtypes.Deployment)
*res = networkDl
var res *[]string = result.(*[]string)
*res = []string{}
return nil
}).AnyTimes()

ips := make([]byte, 0)
dls, _, err := d.GenerateVersionlessDeployments(context.Background(), &dl, ips)
dls, err := d.GenerateVersionlessDeployments(context.Background(), []*workloads.Deployment{&dl})
assert.NoError(t, err)

assert.Equal(t, len(gridDl.Workloads), len(dls[dl.NodeID].Workloads))
assert.Equal(t, gridDl.Workloads, dls[dl.NodeID].Workloads)
})
assert.Equal(t, len(gridDl.Workloads), len(dls[dl.NodeID][0].Workloads))
assert.Equal(t, gridDl.Workloads, dls[dl.NodeID][0].Workloads)

t.Run("test sync", func(t *testing.T) {
ips := make([]byte, 0)
dls, _, err := d.GenerateVersionlessDeployments(context.Background(), &dl, ips)
assert.NoError(t, err)

assert.Equal(t, dls[nodeID].Metadata, "{\"version\":3,\"type\":\"vm\",\"name\":\"test\",\"projectName\":\"vm/test\"}")

t.Run("Validation failed", func(t *testing.T) {
sub.EXPECT().
GetBalance(d.tfPluginClient.Identity).
Return(substrate.Balance{
Free: types.U128{
Int: big.NewInt(10),
},
}, nil)

assert.Error(t, d.Deploy(context.Background(), &dl))
assert.Equal(t, dls[nodeID][0].Metadata, "{\"version\":3,\"type\":\"vm\",\"name\":\"test\",\"projectName\":\"vm/test\"}")
})

// nothing should change
assert.Empty(t, dl.NodeDeploymentID)
assert.Empty(t, dl.ContractID)
assert.Equal(t, d.tfPluginClient.State.CurrentNodeDeployments, map[uint32]state.ContractIDs{nodeID: {netContractID}})
})
t.Run("Deploying failed", func(t *testing.T) {
sub.EXPECT().
GetBalance(d.tfPluginClient.Identity).
Return(substrate.Balance{
Free: types.U128{
Int: big.NewInt(20000000),
},
}, nil)

t.Run("Deploying failed", func(t *testing.T) {
sub.EXPECT().
GetBalance(d.tfPluginClient.Identity).
Return(substrate.Balance{
Free: types.U128{
Int: big.NewInt(20000000),
},
}, nil)
deployer.EXPECT().Deploy(
gomock.Any(),
dl.NodeDeploymentID,
gomock.Any(),
gomock.Any(),
).Return(map[uint32]uint64{}, errors.New("error"))

deployer.EXPECT().Deploy(
gomock.Any(),
dl.NodeDeploymentID,
dls,
gomock.Any(),
).Return(map[uint32]uint64{}, errors.New("error"))
assert.Error(t, d.Deploy(context.Background(), &dl))

assert.Error(t, d.Deploy(context.Background(), &dl))
// nothing should change
assert.Empty(t, dl.NodeDeploymentID)
assert.Empty(t, dl.ContractID)
assert.Equal(t, d.tfPluginClient.State.CurrentNodeDeployments, map[uint32]state.ContractIDs{nodeID: {netContractID}})
})

// nothing should change
assert.Empty(t, dl.NodeDeploymentID)
assert.Empty(t, dl.ContractID)
assert.Equal(t, d.tfPluginClient.State.CurrentNodeDeployments, map[uint32]state.ContractIDs{nodeID: {netContractID}})
})
t.Run("Deploying succeeded", func(t *testing.T) {
dl.NodeDeploymentID = map[uint32]uint64{}
sub.EXPECT().
GetBalance(d.tfPluginClient.Identity).
Return(substrate.Balance{
Free: types.U128{
Int: big.NewInt(20000000),
},
}, nil)
t.Run("Deploying succeeded", func(t *testing.T) {
dl.NodeDeploymentID = map[uint32]uint64{}
sub.EXPECT().
GetBalance(d.tfPluginClient.Identity).
Return(substrate.Balance{
Free: types.U128{
Int: big.NewInt(20000000),
},
}, nil)

deployer.EXPECT().Deploy(
gomock.Any(),
dl.NodeDeploymentID,
gomock.Any(),
gomock.Any(),
).Return(map[uint32]uint64{nodeID: contractID}, nil)
assert.NoError(t, d.Deploy(context.Background(), &dl))

// should reflect on deployment and state
assert.Equal(t, dl.NodeDeploymentID, map[uint32]uint64{nodeID: contractID})
assert.Equal(t, dl.ContractID, contractID)
assert.Equal(t, d.tfPluginClient.State.CurrentNodeDeployments, map[uint32]state.ContractIDs{nodeID: {netContractID, contractID}})
})
}

deployer.EXPECT().Deploy(
gomock.Any(),
dl.NodeDeploymentID,
dls,
gomock.Any(),
).Return(map[uint32]uint64{nodeID: contractID}, nil)
assert.NoError(t, d.Deploy(context.Background(), &dl))
func TestDeploymentDeployerDelete(t *testing.T) {
tfPluginClient, err := setup()
assert.NoError(t, err)

// should reflect on deployment and state
assert.Equal(t, dl.NodeDeploymentID, map[uint32]uint64{nodeID: contractID})
assert.Equal(t, dl.ContractID, contractID)
assert.Equal(t, d.tfPluginClient.State.CurrentNodeDeployments, map[uint32]state.ContractIDs{nodeID: {netContractID, contractID}})
})
})
d, _, sub, _, deployer := constructTestDeployer(t, tfPluginClient, true)
dl := constructTestDeployment()

t.Run("test delete", func(t *testing.T) {
dl.ContractID = contractID
Expand Down Expand Up @@ -470,6 +476,14 @@ func TestDeploymentDeployer(t *testing.T) {
assert.Empty(t, dl.NodeDeploymentID)
})
})
}

func TestDeploymentDeployerSync(t *testing.T) {
tfPluginClient, err := setup()
assert.NoError(t, err)

d, cl, sub, ncPool, deployer := constructTestDeployer(t, tfPluginClient, true)
dl := constructTestDeployment()

t.Run("test sync", func(t *testing.T) {
dl.ContractID = contractID
Expand All @@ -485,13 +499,12 @@ func TestDeploymentDeployer(t *testing.T) {
Subnets: map[uint32]string{
nodeID: net.IPRange.String(),
},
NodeDeploymentHostIDs: make(state.NodeDeploymentHostIDs),
},
}

ncPool.EXPECT().
GetNodeClient(sub, nodeID).
Return(client.NewNodeClient(twinID, cl, d.tfPluginClient.RMBTimeout), nil)
Return(client.NewNodeClient(twinID, cl, d.tfPluginClient.RMBTimeout), nil).AnyTimes()

cl.EXPECT().
Call(gomock.Any(), twinID, "zos.deployment.get", gomock.Any(), gomock.Any()).
Expand All @@ -501,11 +514,22 @@ func TestDeploymentDeployer(t *testing.T) {
return nil
}).AnyTimes()

ips := make([]byte, 0)
dls, _, err := d.GenerateVersionlessDeployments(context.Background(), &dl, ips)
ncPool.EXPECT().
GetNodeClient(sub, nodeID).
Return(client.NewNodeClient(twinID, cl, d.tfPluginClient.RMBTimeout), nil)

cl.EXPECT().
Call(gomock.Any(), twinID, "zos.network.list_private_ips", gomock.Any(), gomock.Any()).
DoAndReturn(func(ctx context.Context, twin uint32, fn string, data, result interface{}) error {
var res *[]string = result.(*[]string)
*res = []string{}
return nil
})

dls, err := d.GenerateVersionlessDeployments(context.Background(), []*workloads.Deployment{&dl})
assert.NoError(t, err)

gridDl := dls[dl.NodeID]
gridDl := dls[dl.NodeID][0]
err = json.NewEncoder(log.Writer()).Encode(gridDl.Workloads)
assert.NoError(t, err)

Expand Down
6 changes: 2 additions & 4 deletions grid-client/deployer/k8s_deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,25 +422,23 @@ func (d *K8sDeployer) removeDeletedContracts(ctx context.Context, k8sCluster *wo
return nil
}

// TODO: integrate new list private ips function
func (d *K8sDeployer) getK8sUsedIPs(k8s *workloads.K8sCluster) map[uint32][]byte {
usedIPs := make(map[uint32][]byte)
network := d.tfPluginClient.State.Networks.GetNetwork(k8s.NetworkName)

if k8s.Master.IP != "" {
ip := net.ParseIP(k8s.Master.IP).To4()
if ip != nil {
usedIPs[k8s.Master.Node] = append(usedIPs[k8s.Master.Node], ip[3])
}

}
usedIPs[k8s.Master.Node] = append(usedIPs[k8s.Master.Node], network.GetUsedNetworkHostIDs(k8s.Master.Node)...)

for _, w := range k8s.Workers {
if w.IP != "" {
ip := net.ParseIP(k8s.Master.IP).To4()
if ip != nil {
usedIPs[w.Node] = append(usedIPs[w.Node], ip[3])
}
usedIPs[w.Node] = append(usedIPs[w.Node], network.GetUsedNetworkHostIDs(w.Node)...)
}
}

Expand Down
3 changes: 1 addition & 2 deletions grid-client/deployer/k8s_deployer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ func constructTestK8s(t *testing.T, mock bool) (
}
net := constructTestNetwork()
tfPluginClient.State.Networks = state.NetworkState{net.Name: state.Network{
Subnets: map[uint32]string{nodeID: net.IPRange.String()},
NodeDeploymentHostIDs: map[uint32]state.DeploymentHostIDs{nodeID: map[uint64][]byte{contractID: {}}},
Subnets: map[uint32]string{nodeID: net.IPRange.String()},
}}

return tfPluginClient.K8sDeployer, cl, sub, ncPool, deployer, gridProxyCl
Expand Down
Loading
Loading