From 6f5011fe6b901c02e300de7d89074ba75a1af81d Mon Sep 17 00:00:00 2001 From: Tanya Tukade <31555290+tanyatukade@users.noreply.github.com> Date: Sat, 24 Apr 2021 21:04:31 -0500 Subject: [PATCH] Merge qos policies (#786) --- pkg/hostagent/group_assign.go | 41 +++++++++++++++++++++++------------ pkg/hostagent/pods.go | 7 ++---- pkg/hostagent/pods_test.go | 4 ++++ 3 files changed, 33 insertions(+), 19 deletions(-) diff --git a/pkg/hostagent/group_assign.go b/pkg/hostagent/group_assign.go index 04738b5ba1..d4a83a98cf 100644 --- a/pkg/hostagent/group_assign.go +++ b/pkg/hostagent/group_assign.go @@ -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, @@ -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 @@ -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 @@ -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 } diff --git a/pkg/hostagent/pods.go b/pkg/hostagent/pods.go index 71e8dc92e3..6f970c6736 100644 --- a/pkg/hostagent/pods.go +++ b/pkg/hostagent/pods.go @@ -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"` @@ -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{}) @@ -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) } diff --git a/pkg/hostagent/pods_test.go b/pkg/hostagent/pods_test.go index 2b248a9fe8..50367f33ae 100644 --- a/pkg/hostagent/pods_test.go +++ b/pkg/hostagent/pods_test.go @@ -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") @@ -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) {