Skip to content

Commit 36a56b2

Browse files
committed
Inspect Support for policy and Tenant
1 parent 40b58cb commit 36a56b2

File tree

14 files changed

+735
-9
lines changed

14 files changed

+735
-9
lines changed

Godeps/Godeps.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

netctl/commands.go

+14
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,13 @@ var Commands = []cli.Command{
249249
ArgsUsage: "[tenant]",
250250
Action: createTenant,
251251
},
252+
{
253+
Name: "inspect",
254+
Usage: "Inspect a tenant",
255+
ArgsUsage: "[tenant]",
256+
Flags: []cli.Flag{jsonFlag},
257+
Action: inspectTenant,
258+
},
252259
},
253260
},
254261
{
@@ -278,6 +285,13 @@ var Commands = []cli.Command{
278285
Flags: []cli.Flag{tenantFlag, allFlag, jsonFlag, quietFlag},
279286
Action: listPolicies,
280287
},
288+
{
289+
Name: "inspect",
290+
Usage: "Inspect a policy",
291+
ArgsUsage: "[policy]",
292+
Flags: []cli.Flag{tenantFlag},
293+
Action: inspectPolicy,
294+
},
281295
{
282296
Name: "rule-ls",
283297
Usage: "List rules for a given tenant,policy",

netctl/netctl.go

+38
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,27 @@ func deletePolicy(ctx *cli.Context) {
5757
errCheck(ctx, getClient(ctx).PolicyDelete(tenant, policy))
5858
}
5959

60+
func inspectPolicy(ctx *cli.Context) {
61+
if len(ctx.Args()) != 1 {
62+
errExit(ctx, exitHelp, "Policy name required", true)
63+
}
64+
65+
tenant := ctx.String("tenant")
66+
policy := ctx.Args()[0]
67+
68+
fmt.Printf("Inspeting policy: %s tenant: %s\n", policy, tenant)
69+
70+
pol, err := getClient(ctx).PolicyInspect(tenant, policy)
71+
errCheck(ctx, err)
72+
73+
content, err := json.MarshalIndent(pol, "", " ")
74+
if err != nil {
75+
errExit(ctx, exitIO, err.Error(), false)
76+
}
77+
os.Stdout.Write(content)
78+
os.Stdout.WriteString("\n")
79+
}
80+
6081
func listPolicies(ctx *cli.Context) {
6182
if len(ctx.Args()) != 0 {
6283
errExit(ctx, exitHelp, "More arguments than required", true)
@@ -542,6 +563,23 @@ func deleteTenant(ctx *cli.Context) {
542563
errCheck(ctx, getClient(ctx).TenantDelete(tenant))
543564
}
544565

566+
func inspectTenant(ctx *cli.Context) {
567+
if len(ctx.Args()) != 1 {
568+
errExit(ctx, exitHelp, "Tenant name required", true)
569+
}
570+
571+
tenant := ctx.Args()[0]
572+
573+
fmt.Printf("Inspecting tenant: %s ", tenant)
574+
575+
ten, err := getClient(ctx).TenantInspect(tenant)
576+
errCheck(ctx, err)
577+
578+
content, err := json.MarshalIndent(ten, "", " ")
579+
os.Stdout.Write(content)
580+
os.Stdout.WriteString("\n")
581+
}
582+
545583
func listTenants(ctx *cli.Context) {
546584
if len(ctx.Args()) != 0 {
547585
errExit(ctx, exitHelp, "More arguments than required", true)

netmaster/mastercfg/endpointGroupState.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type EndpointGroupState struct {
3434
PktTagType string `json:"pktTagType"`
3535
PktTag int `json:"pktTag"`
3636
ExtPktTag int `json:"extPktTag"`
37-
EpCount int `json:"epCount"`
37+
EpCount int `json:"epCount"` // To store endpoint Count
3838
DSCP int `json:"DSCP"`
3939
Bandwidth string `json:"Bandwidth"`
4040
Burst int `json:"Burst"`

netmaster/objApi/apiController.go

+228
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,84 @@ func (ac *APIController) PolicyCreate(policy *contivModel.Policy) error {
11771177
return nil
11781178
}
11791179

1180+
// PolicyGetOper inspects policy
1181+
func (ac *APIController) PolicyGetOper(policy *contivModel.PolicyInspect) error {
1182+
log.Infof("Received PolicyInspect: %+v", policy)
1183+
1184+
// Get the state driver
1185+
stateDriver, err := utils.GetStateDriver()
1186+
if err != nil {
1187+
return err
1188+
}
1189+
1190+
// To hold total number of Endpoint count
1191+
var policyEPCount int
1192+
1193+
policyCfg := &mastercfg.EpgPolicy{}
1194+
policyCfg.StateDriver = stateDriver
1195+
1196+
// Policy is attached to EPG. So we need to fetch EPGs as well.
1197+
epgCfg := &mastercfg.EndpointGroupState{}
1198+
epgCfg.StateDriver = stateDriver
1199+
1200+
// Get all the Endpoints
1201+
readEp := &mastercfg.CfgEndpointState{}
1202+
readEp.StateDriver = stateDriver
1203+
epCfgs, epErr := readEp.ReadAll()
1204+
1205+
// Get all the EPGs on which this policy is applied
1206+
epgs := policy.Config.LinkSets.EndpointGroups
1207+
1208+
// Scan all the EPGs which are under this policy
1209+
for _, epg := range epgs {
1210+
log.Infof("EPG Object : %+v", epg)
1211+
1212+
// Reversing key from TenantName:EPGName to EPGName:TenantName
1213+
sList := strings.Split(epg.ObjKey, ":")
1214+
if sList == nil {
1215+
log.Errorf("EPG key %+v is not in valid format", epg.ObjKey)
1216+
return err
1217+
}
1218+
epgID := sList[1] + ":" + sList[0]
1219+
1220+
log.Infof("EPG ID : %s", epgID)
1221+
1222+
if err := epgCfg.Read(epgID); err != nil {
1223+
log.Errorf("Error fetching endpointGroup from mastercfg: %s", epgID)
1224+
return err
1225+
}
1226+
1227+
policyEPCount = policyEPCount + epgCfg.EpCount
1228+
1229+
if epErr == nil {
1230+
for _, epCfg := range epCfgs {
1231+
ep := epCfg.(*mastercfg.CfgEndpointState)
1232+
if ep.EndpointGroupKey == epgID {
1233+
epOper := contivModel.EndpointOper{}
1234+
epOper.Network = ep.NetID
1235+
epOper.EndpointID = ep.EndpointID
1236+
epOper.ServiceName = ep.ServiceName
1237+
epOper.EndpointGroupID = ep.EndpointGroupID
1238+
epOper.EndpointGroupKey = ep.EndpointGroupKey
1239+
epOper.IpAddress = []string{ep.IPAddress, ep.IPv6Address}
1240+
epOper.MacAddress = ep.MacAddress
1241+
epOper.HomingHost = ep.HomingHost
1242+
epOper.IntfName = ep.IntfName
1243+
epOper.VtepIP = ep.VtepIP
1244+
epOper.Labels = fmt.Sprintf("%s", ep.Labels)
1245+
epOper.ContainerID = ep.ContainerID
1246+
epOper.ContainerName = ep.ContainerName
1247+
policy.Oper.Endpoints = append(policy.Oper.Endpoints, epOper)
1248+
}
1249+
}
1250+
}
1251+
} // End of main for loop
1252+
1253+
policy.Oper.NumEndpoints = policyEPCount
1254+
1255+
return nil
1256+
}
1257+
11801258
// PolicyUpdate updates policy
11811259
func (ac *APIController) PolicyUpdate(policy, params *contivModel.Policy) error {
11821260
log.Infof("Received PolicyUpdate: %+v, params: %+v", policy, params)
@@ -1412,6 +1490,156 @@ func (ac *APIController) TenantCreate(tenant *contivModel.Tenant) error {
14121490
return nil
14131491
}
14141492

1493+
// Get all the networks inside tenant
1494+
func getTenantNetworks(tenant *contivModel.TenantInspect) error {
1495+
1496+
// Get the state driver
1497+
stateDriver, err := utils.GetStateDriver()
1498+
if err != nil {
1499+
return err
1500+
}
1501+
1502+
tenantID := tenant.Config.TenantName
1503+
numEPs := 0
1504+
s := []string{}
1505+
networkID := ""
1506+
for _, net := range tenant.Config.LinkSets.Networks {
1507+
networkID = net.ObjKey
1508+
log.Infof("network has ID %s", networkID)
1509+
s = strings.Split(networkID, ":")
1510+
if s[0] == tenantID {
1511+
networkID = s[1] + "." + s[0]
1512+
nwCfg := &mastercfg.CfgNetworkState{}
1513+
nwCfg.StateDriver = stateDriver
1514+
if err := nwCfg.Read(networkID); err != nil {
1515+
log.Errorf("Error fetching network from mastercfg: %s", networkID)
1516+
return err
1517+
}
1518+
numEPs = numEPs + nwCfg.EpCount
1519+
netOper := contivModel.NetworkOper{}
1520+
netOper.AllocatedAddressesCount = nwCfg.EpAddrCount
1521+
netOper.AvailableIPAddresses = master.ListAvailableIPs(nwCfg)
1522+
netOper.AllocatedIPAddresses = master.ListAllocatedIPs(nwCfg)
1523+
netOper.DnsServerIP = nwCfg.DNSServer
1524+
netOper.ExternalPktTag = nwCfg.ExtPktTag
1525+
netOper.PktTag = nwCfg.PktTag
1526+
netOper.NumEndpoints = nwCfg.EpCount
1527+
readEp := &mastercfg.CfgEndpointState{}
1528+
readEp.StateDriver = stateDriver
1529+
epCfgs, err := readEp.ReadAll()
1530+
if err == nil {
1531+
for _, epCfg := range epCfgs {
1532+
ep := epCfg.(*mastercfg.CfgEndpointState)
1533+
if ep.NetID == networkID {
1534+
epOper := contivModel.EndpointOper{}
1535+
epOper.Network = ep.NetID
1536+
epOper.EndpointID = ep.EndpointID
1537+
epOper.ServiceName = ep.ServiceName
1538+
epOper.EndpointGroupID = ep.EndpointGroupID
1539+
epOper.EndpointGroupKey = ep.EndpointGroupKey
1540+
epOper.IpAddress = []string{ep.IPAddress, ep.IPv6Address}
1541+
epOper.MacAddress = ep.MacAddress
1542+
epOper.HomingHost = ep.HomingHost
1543+
epOper.IntfName = ep.IntfName
1544+
epOper.VtepIP = ep.VtepIP
1545+
epOper.Labels = fmt.Sprintf("%s", ep.Labels)
1546+
epOper.ContainerID = ep.ContainerID
1547+
epOper.ContainerName = ep.ContainerName
1548+
netOper.Endpoints = append(netOper.Endpoints, epOper)
1549+
}
1550+
}
1551+
}
1552+
tenant.Oper.Networks = append(tenant.Oper.Networks, netOper)
1553+
}
1554+
}
1555+
1556+
tenant.Oper.TotalEndpoints = numEPs
1557+
return nil
1558+
}
1559+
1560+
// Get all the EPGs inside tenant
1561+
func getTenantEPGs(tenant *contivModel.TenantInspect) error {
1562+
1563+
// Get the state driver
1564+
stateDriver, err := utils.GetStateDriver()
1565+
if err != nil {
1566+
return err
1567+
}
1568+
1569+
tenantID := tenant.Config.TenantName
1570+
s := []string{}
1571+
epgID := ""
1572+
for _, epg := range tenant.Config.LinkSets.EndpointGroups {
1573+
epgID = epg.ObjKey
1574+
log.Infof("EPG ID is %s", epgID)
1575+
s = strings.Split(epgID, ":")
1576+
if s[0] == tenantID {
1577+
epgID = s[1] + ":" + s[0]
1578+
epgCfg := &mastercfg.EndpointGroupState{}
1579+
epgCfg.StateDriver = stateDriver
1580+
if err := epgCfg.Read(epgID); err != nil {
1581+
log.Errorf("Error fetching epg from mastercfg: %s", epgID)
1582+
return err
1583+
}
1584+
epgOper := contivModel.EndpointGroupOper{}
1585+
epgOper.ExternalPktTag = epgCfg.ExtPktTag
1586+
epgOper.PktTag = epgCfg.PktTag
1587+
epgOper.NumEndpoints = epgCfg.EpCount
1588+
1589+
readEp := &mastercfg.CfgEndpointState{}
1590+
readEp.StateDriver = stateDriver
1591+
epCfgs, err := readEp.ReadAll()
1592+
if err == nil {
1593+
for _, epCfg := range epCfgs {
1594+
ep := epCfg.(*mastercfg.CfgEndpointState)
1595+
log.Infof("EndpointGroupKey is %s", ep.EndpointGroupKey)
1596+
if ep.EndpointGroupKey == epgID {
1597+
epOper := contivModel.EndpointOper{}
1598+
epOper.Network = ep.NetID
1599+
epOper.EndpointID = ep.EndpointID
1600+
epOper.ServiceName = ep.ServiceName
1601+
epOper.EndpointGroupID = ep.EndpointGroupID
1602+
epOper.EndpointGroupKey = ep.EndpointGroupKey
1603+
epOper.IpAddress = []string{ep.IPAddress, ep.IPv6Address}
1604+
epOper.MacAddress = ep.MacAddress
1605+
epOper.HomingHost = ep.HomingHost
1606+
epOper.IntfName = ep.IntfName
1607+
epOper.VtepIP = ep.VtepIP
1608+
epOper.Labels = fmt.Sprintf("%s", ep.Labels)
1609+
epOper.ContainerID = ep.ContainerID
1610+
epOper.ContainerName = ep.ContainerName
1611+
epgOper.Endpoints = append(epgOper.Endpoints, epOper)
1612+
}
1613+
}
1614+
}
1615+
tenant.Oper.EndpointGroups = append(tenant.Oper.EndpointGroups, epgOper)
1616+
}
1617+
}
1618+
1619+
return nil
1620+
}
1621+
1622+
// TenantGetOper inspects tenant
1623+
func (ac *APIController) TenantGetOper(tenant *contivModel.TenantInspect) error {
1624+
log.Infof("Received TenantInspect: %+v", tenant)
1625+
1626+
tenant.Oper.TotalNetworks = len(tenant.Config.LinkSets.Networks)
1627+
tenant.Oper.TotalEPGs = len(tenant.Config.LinkSets.EndpointGroups)
1628+
tenant.Oper.TotalNetprofiles = len(tenant.Config.LinkSets.NetProfiles)
1629+
tenant.Oper.TotalPolicies = len(tenant.Config.LinkSets.Policies)
1630+
tenant.Oper.TotalAppProfiles = len(tenant.Config.LinkSets.AppProfiles)
1631+
tenant.Oper.TotalServicelbs = len(tenant.Config.LinkSets.Servicelbs)
1632+
1633+
//Get all the networks config and oper parmeters under this tenant
1634+
getTenantNetworks(tenant)
1635+
1636+
//Get all the EPGs config and oper parmeters under this tenant
1637+
getTenantEPGs(tenant)
1638+
1639+
return nil
1640+
1641+
}
1642+
14151643
// TenantUpdate updates a tenant
14161644
func (ac *APIController) TenantUpdate(tenant, params *contivModel.Tenant) error {
14171645
log.Infof("Received TenantUpdate: %+v, params: %+v", tenant, params)

0 commit comments

Comments
 (0)