Skip to content

Commit

Permalink
Merge qos policies (#786)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanyatukade committed Apr 25, 2021
1 parent 6d2034b commit 6f5011f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
41 changes: 27 additions & 14 deletions pkg/hostagent/group_assign.go
Expand Up @@ -42,25 +42,36 @@ func addGroup(gset map[metadata.OpflexGroup]bool, g []metadata.OpflexGroup,
return g
}

func (agent *HostAgent) assignqosPolicies(pod *v1.Pod) ([]metadata.OpflexGroup, error) {
//var policies []metadata.OpflexGroup
func addPolicy(gset map[metadata.OpflexGroup]bool, g metadata.OpflexGroup,
tenant string, name string) metadata.OpflexGroup {
newg := metadata.OpflexGroup{
PolicySpace: tenant,
Name: name,
}
if _, ok := gset[newg]; !ok {
gset[newg] = true
g = newg
}
return g
}

logger := podLogger(agent.log, pod)
func (agent *HostAgent) mergeQosPolicy(podkey string, qpval metadata.OpflexGroup) metadata.OpflexGroup {

var g []metadata.OpflexGroup
podkey, err := cache.MetaNamespaceKeyFunc(pod)
if err != nil {
logger.Error("Could not create pod key: ", err)
return g, err
}
gset := make(map[metadata.OpflexGroup]bool)
var g metadata.OpflexGroup

// Add qos policies that directly select this pod
// Add qos policy that directly select this pod
for _, qpkey := range agent.qosPolPods.GetObjForPod(podkey) {
g = addGroup(gset, g, agent.config.DefaultEg.PolicySpace,
g = addPolicy(gset, g, agent.config.DefaultEg.PolicySpace,
util.AciNameForKey(agent.config.AciPrefix, "qp", qpkey))
}
return g, nil

// When the pod is not selected by any qos policy, return the
// existing value from the user annotation
if len(gset) == 0 {
return qpval
}
return g
}

func (agent *HostAgent) mergeNetPolSg(podkey string, pod *v1.Pod,
Expand Down Expand Up @@ -130,7 +141,7 @@ func decodeAnnotation(annStr string, into interface{}, logger *logrus.Entry, com
}
}

// Gets eg, sg annotations on associated deployment or rc
// Gets eg, sg and qos policy annotations on associated deployment or rc
func (agent *HostAgent) getParentAnn(podKey string) (string, string, string, bool) {
set := []struct {
indexer *index.PodSelectorIndex
Expand Down Expand Up @@ -170,7 +181,7 @@ func (agent *HostAgent) getParentAnn(podKey string) (string, string, string, boo
return "", "", "", false
}

// assignGroups assigns epg and security groups based on annotations on the
// assignGroups assigns epg, security groups and qos policy based on annotations on the
// namespace, deployment and pod.
func (agent *HostAgent) assignGroups(pod *v1.Pod) (metadata.OpflexGroup, []metadata.OpflexGroup, metadata.OpflexGroup, error) {
var egval metadata.OpflexGroup
Expand Down Expand Up @@ -257,5 +268,7 @@ func (agent *HostAgent) assignGroups(pod *v1.Pod) (metadata.OpflexGroup, []metad
"security groups:", err)
}

qpval = agent.mergeQosPolicy(podkey, qpval)

return egval, sgval, qpval, nil
}
7 changes: 2 additions & 5 deletions pkg/hostagent/pods.go
Expand Up @@ -54,7 +54,6 @@ type opflexEndpoint struct {
EndpointGroup string `json:"endpoint-group-name,omitempty"`
SecurityGroup []metadata.OpflexGroup `json:"security-group,omitempty"`
QosPolicy metadata.OpflexGroup `json:"qos-policy,omitempty"`
QoSPolicies []metadata.OpflexGroup `json:"qos-policies,omitempty"`

IpAddress []string `json:"ip,omitempty"`
MacAddress string `json:"mac,omitempty"`
Expand Down Expand Up @@ -488,15 +487,14 @@ func (agent *HostAgent) podChangedLocked(podobj interface{}) {
if epAttributes == nil {
epAttributes = make(map[string]string)
}
qosPolicies, _ := agent.assignqosPolicies(pod)
epAttributes["vm-name"] = pod.ObjectMeta.Name
epAttributes["namespace"] = pod.ObjectMeta.Namespace

agent.epChanged(&epUuid, &epMetaKey, &epGroup, secGroup, qpGroup, qosPolicies, epAttributes, logger)
agent.epChanged(&epUuid, &epMetaKey, &epGroup, secGroup, qpGroup, epAttributes, logger)
}

func (agent *HostAgent) epChanged(epUuid *string, epMetaKey *string, epGroup *metadata.OpflexGroup,
epSecGroups []metadata.OpflexGroup, epQosPolicy metadata.OpflexGroup, epQoSPolicies []metadata.OpflexGroup, epAttributes map[string]string,
epSecGroups []metadata.OpflexGroup, epQosPolicy metadata.OpflexGroup, epAttributes map[string]string,
logger *logrus.Entry) {
if logger == nil {
logger = agent.log.WithFields(logrus.Fields{})
Expand Down Expand Up @@ -556,7 +554,6 @@ func (agent *HostAgent) epChanged(epUuid *string, epMetaKey *string, epGroup *me
}
ep.SecurityGroup = epSecGroups
ep.QosPolicy = epQosPolicy
ep.QoSPolicies = epQoSPolicies

neweps = append(neweps, ep)
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/hostagent/pods_test.go
Expand Up @@ -158,8 +158,10 @@ func (agent *testHostAgent) doTestPod(t *testing.T, tempdir string,

eg := &metadata.OpflexGroup{}
sg := make([]metadata.OpflexGroup, 0)
qp := metadata.OpflexGroup{}
json.Unmarshal([]byte(pt.eg), eg)
json.Unmarshal([]byte(pt.sg), &sg)
json.Unmarshal([]byte(pt.qp), &qp)

epidstr := pt.uuid + "_" + pt.cont + "_" + pt.veth
assert.Equal(t, epidstr, ep.Uuid, desc, pt.name, "uuid")
Expand All @@ -168,6 +170,8 @@ func (agent *testHostAgent) doTestPod(t *testing.T, tempdir string,
assert.Equal(t, eg.AppProfile+"|"+eg.Name, ep.EndpointGroup,
desc, pt.name, "eg")
assert.Equal(t, sg, ep.SecurityGroup, desc, pt.name, "secgroup")
assert.Equal(t, qp, ep.QosPolicy, desc, pt.name, "qos")

}

func TestPodSync(t *testing.T) {
Expand Down

0 comments on commit 6f5011f

Please sign in to comment.