// CreateEndpoint creates an endpoint func CreateEndpoint(stateDriver core.StateDriver, nwCfg *mastercfg.CfgNetworkState, ep *intent.ConfigEP) (*mastercfg.CfgEndpointState, error) { epCfg := &mastercfg.CfgEndpointState{} epCfg.StateDriver = stateDriver epCfg.ID = getEpName(nwCfg.ID, ep) err := epCfg.Read(epCfg.ID) if err == nil { // TODO: check for diffs and possible updates return epCfg, nil } epCfg.NetID = nwCfg.ID epCfg.ContName = ep.Container epCfg.AttachUUID = ep.AttachUUID epCfg.HomingHost = ep.Host epCfg.ServiceName = ep.ServiceName // Allocate addresses err = allocSetEpAddress(ep, epCfg, nwCfg) if err != nil { log.Errorf("error allocating and/or reserving IP. Error: %s", err) return nil, err } // Set endpoint group // Skip for infra nw if nwCfg.NwType != "infra" { epCfg.EndpointGroupKey = mastercfg.GetEndpointGroupKey(ep.ServiceName, nwCfg.Tenant) epCfg.EndpointGroupID, err = mastercfg.GetEndpointGroupID(stateDriver, ep.ServiceName, nwCfg.Tenant) if err != nil { log.Errorf("Error getting endpoint group for %s.%s. Err: %v", ep.ServiceName, nwCfg.ID, err) return nil, err } } err = nwCfg.IncrEpCount() if err != nil { log.Errorf("Error incrementing ep count. Err: %v", err) return nil, err } err = epCfg.Write() if err != nil { log.Errorf("error writing ep config. Error: %s", err) return nil, err } return epCfg, nil }
// PolicyAttach attaches a policy to an endpoint and adds associated rules to policyDB func PolicyAttach(epg *contivModel.EndpointGroup, policy *contivModel.Policy) error { // Dont install policies in ACI mode if !isPolicyEnabled() { return nil } epgpKey := epg.Key + ":" + policy.Key // See if it already exists gp := mastercfg.FindEpgPolicy(epgpKey) if gp != nil { log.Errorf("EPG policy %s already exists", epgpKey) return EpgPolicyExists } stateDriver, err := utils.GetStateDriver() if err != nil { log.Errorf("Could not get StateDriver while attaching policy %+v", policy) return err } epgID, err := mastercfg.GetEndpointGroupID(stateDriver, epg.GroupName, epg.TenantName) if err != nil { log.Errorf("Error getting epgID for %s. Err: %v", epgpKey, err) return err } // Create the epg policy gp, err = mastercfg.NewEpgPolicy(epgpKey, epgID, policy) if err != nil { log.Errorf("Error creating EPG policy. Err: %v", err) return err } return nil }
// CreateEndpoint creates an endpoint func CreateEndpoint(stateDriver core.StateDriver, nwCfg *mastercfg.CfgNetworkState, epReq *CreateEndpointRequest) (*mastercfg.CfgEndpointState, error) { ep := &epReq.ConfigEP epCfg := &mastercfg.CfgEndpointState{} epCfg.StateDriver = stateDriver epCfg.ID = getEpName(nwCfg.ID, ep) err := epCfg.Read(epCfg.ID) if err == nil { // TODO: check for diffs and possible updates return epCfg, nil } epCfg.NetID = nwCfg.ID epCfg.EndpointID = ep.Container epCfg.HomingHost = ep.Host epCfg.ServiceName = ep.ServiceName epCfg.EPCommonName = epReq.EPCommonName // Allocate addresses err = allocSetEpAddress(ep, epCfg, nwCfg) if err != nil { log.Errorf("error allocating and/or reserving IP. Error: %s", err) return nil, err } // cleanup relies on var err being used for all error checking defer freeAddrOnErr(nwCfg, epCfg.IPAddress, &err) // Set endpoint group // Skip for infra nw if nwCfg.NwType != "infra" { epCfg.EndpointGroupKey = mastercfg.GetEndpointGroupKey(ep.ServiceName, nwCfg.Tenant) epCfg.EndpointGroupID, err = mastercfg.GetEndpointGroupID(stateDriver, ep.ServiceName, nwCfg.Tenant) if err != nil { log.Errorf("Error getting endpoint group ID for %s.%s. Err: %v", ep.ServiceName, nwCfg.ID, err) return nil, err } if epCfg.EndpointGroupKey != "" { epgCfg := &mastercfg.EndpointGroupState{} epgCfg.StateDriver = stateDriver err = epgCfg.Read(epCfg.EndpointGroupKey) if err != nil { log.Errorf("Error reading Epg info for EP: %+v. Error: %v", ep, err) return nil, err } epgCfg.EpCount++ err = epgCfg.Write() if err != nil { log.Errorf("Error saving epg state: %+v", epgCfg) return nil, err } } } err = nwCfg.IncrEpCount() if err != nil { log.Errorf("Error incrementing ep count. Err: %v", err) return nil, err } err = epCfg.Write() if err != nil { log.Errorf("error writing ep config. Error: %s", err) return nil, err } return epCfg, nil }