Skip to content

Commit

Permalink
Fix and refactor service to deploy/rollout matching (istio-ecosystem#239
Browse files Browse the repository at this point in the history
) (istio-ecosystem#137)

Co-authored-by: aattuluri <44482891+aattuluri@users.noreply.github.com>
  • Loading branch information
2 people authored and GitHub Enterprise committed Jul 5, 2022
1 parent d11568d commit 5bb5e5b
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 45 deletions.
23 changes: 2 additions & 21 deletions admiral/pkg/clusters/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -779,14 +779,7 @@ func getServiceForDeployment(rc *RemoteController, deployment *k8sAppsV1.Deploym
}
var matchedService *k8sV1.Service
for _, service := range cachedServices {
var match = true
for lkey, lvalue := range service.Spec.Selector {
value, ok := deployment.Spec.Selector.MatchLabels[lkey]
if !ok || value != lvalue {
match = false
break
}
}
var match = common.IsServiceMatch(service.Spec.Selector, deployment.Spec.Selector)
//make sure the service matches the deployment Selector and also has a mesh port in the port spec
if match {
ports := GetMeshPorts(rc.ClusterID, service, deployment)
Expand Down Expand Up @@ -925,25 +918,13 @@ func getServiceForRollout(rc *RemoteController, rollout *argo.Rollout) map[strin
var matchedServices = make(map[string]*WeightedService)

for _, service := range cachedServices {
var match = true
//skip services that are not referenced in the rollout
if len(blueGreenActiveService) > 0 && service.ObjectMeta.Name != blueGreenActiveService && service.ObjectMeta.Name != blueGreenPreviewService {
log.Infof("Skipping service=%s for rollout=%s in namespace=%s and cluster=%s", service.Name, rollout.Name, rollout.Namespace, rc.ClusterID)
continue
}

for lkey, lvalue := range service.Spec.Selector {
// Rollouts controller adds a dynamic label with name rollouts-pod-template-hash to both active and passive replicasets.
// This dynamic label is not available on the rollout template. Hence ignoring the label with name rollouts-pod-template-hash
if lkey == common.RolloutPodHashLabel {
continue
}
value, ok := rollout.Spec.Selector.MatchLabels[lkey]
if !ok || value != lvalue {
match = false
break
}
}
match := common.IsServiceMatch(service.Spec.Selector, rollout.Spec.Selector)
//make sure the service matches the rollout Selector and also has a mesh port in the port spec
if match {
ports := GetMeshPortsForRollout(rc.ClusterID, service, rollout)
Expand Down
17 changes: 10 additions & 7 deletions admiral/pkg/controller/admiral/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/istio-ecosystem/admiral/admiral/pkg/controller/util"
"github.com/sirupsen/logrus"
k8sAppsV1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/labels"
k8sAppsinformers "k8s.io/client-go/informers/apps/v1"
"k8s.io/client-go/rest"
"time"
Expand Down Expand Up @@ -176,11 +175,7 @@ func (d *DeploymentController) shouldIgnoreBasedOnLabels(deployment *k8sAppsV1.D

func (d *DeploymentController) GetDeploymentBySelectorInNamespace(serviceSelector map[string]string, namespace string) []k8sAppsV1.Deployment {

labelOptions := meta_v1.ListOptions{
LabelSelector: labels.SelectorFromSet(serviceSelector).String(),
}

matchedDeployments, err := d.K8sClient.AppsV1().Deployments(namespace).List(labelOptions)
matchedDeployments, err := d.K8sClient.AppsV1().Deployments(namespace).List(meta_v1.ListOptions{})

if err != nil {
logrus.Errorf("Failed to list deployments in cluster, error: %v", err)
Expand All @@ -191,5 +186,13 @@ func (d *DeploymentController) GetDeploymentBySelectorInNamespace(serviceSelecto
return []k8sAppsV1.Deployment{}
}

return matchedDeployments.Items
filteredDeployments := make ([]k8sAppsV1.Deployment, 0)

for _, deployment := range matchedDeployments.Items {
if common.IsServiceMatch(serviceSelector, deployment.Spec.Selector) {
filteredDeployments = append(filteredDeployments, deployment)
}
}

return filteredDeployments
}
4 changes: 0 additions & 4 deletions admiral/pkg/controller/admiral/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ func TestDeploymentController_GetDeploymentBySelectorInNamespace(t *testing.T) {
},
},
}
deployment.Labels = map[string]string{"identity": "app1"}

deployment2 := k8sAppsV1.Deployment{}
deployment2.Namespace = "namespace"
Expand All @@ -222,7 +221,6 @@ func TestDeploymentController_GetDeploymentBySelectorInNamespace(t *testing.T) {
},
},
}
deployment2.Labels = map[string]string{"identity": "app1"}

deployment3 := k8sAppsV1.Deployment{}
deployment3.Namespace = "namespace"
Expand All @@ -236,7 +234,6 @@ func TestDeploymentController_GetDeploymentBySelectorInNamespace(t *testing.T) {
},
},
}
deployment3.Labels = map[string]string{"identity": "app1"}

deployment4 := k8sAppsV1.Deployment{}
deployment4.Namespace = "namespace"
Expand All @@ -250,7 +247,6 @@ func TestDeploymentController_GetDeploymentBySelectorInNamespace(t *testing.T) {
},
},
}
deployment4.Labels = map[string]string{"identity": "app2"}

oneDeploymentClient := fake.NewSimpleClientset(&deployment)

Expand Down
19 changes: 10 additions & 9 deletions admiral/pkg/controller/admiral/rollouts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package admiral

import (
"fmt"
"k8s.io/apimachinery/pkg/labels"
"sync"
"time"

Expand Down Expand Up @@ -204,13 +203,7 @@ func (roc *RolloutController) Deleted(ojb interface{}) {

func (d *RolloutController) GetRolloutBySelectorInNamespace(serviceSelector map[string]string, namespace string) []argo.Rollout {

//Remove pod template hash from the service selector as that's not on the deployment
delete(serviceSelector, common.RolloutPodHashLabel)

labelOptions := meta_v1.ListOptions{
LabelSelector: labels.SelectorFromSet(serviceSelector).String(),
}
matchedRollouts, err := d.RolloutClient.Rollouts(namespace).List(labelOptions)
matchedRollouts, err := d.RolloutClient.Rollouts(namespace).List(meta_v1.ListOptions{})

if err != nil {
logrus.Errorf("Failed to list rollouts in cluster, error: %v", err)
Expand All @@ -221,5 +214,13 @@ func (d *RolloutController) GetRolloutBySelectorInNamespace(serviceSelector map[
return make([]argo.Rollout,0)
}

return matchedRollouts.Items
filteredRollouts := make ([]argo.Rollout, 0)

for _, rollout := range matchedRollouts.Items {
if common.IsServiceMatch(serviceSelector, rollout.Spec.Selector) {
filteredRollouts = append(filteredRollouts, rollout)
}
}

return filteredRollouts
}
4 changes: 0 additions & 4 deletions admiral/pkg/controller/admiral/rollouts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ func TestRolloutController_GetRolloutBySelectorInNamespace(t *testing.T) {
},
},
}
rollout.Labels = map[string]string{"identity": "app1"}

rollout2 := argo.Rollout{}
rollout2.Namespace = "namespace"
Expand All @@ -220,7 +219,6 @@ func TestRolloutController_GetRolloutBySelectorInNamespace(t *testing.T) {
},
},
}
rollout2.Labels = map[string]string{"identity": "app1"}

rollout3 := argo.Rollout{}
rollout3.Namespace = "namespace"
Expand All @@ -234,7 +232,6 @@ func TestRolloutController_GetRolloutBySelectorInNamespace(t *testing.T) {
},
},
}
rollout3.Labels = map[string]string{"identity": "app1"}

rollout4 := argo.Rollout{}
rollout4.Namespace = "namespace"
Expand All @@ -248,7 +245,6 @@ func TestRolloutController_GetRolloutBySelectorInNamespace(t *testing.T) {
},
},
}
rollout4.Labels = map[string]string{"identity": "app2"}

oneRolloutClient := argofake.NewSimpleClientset(&rollout).ArgoprojV1alpha1()

Expand Down
20 changes: 20 additions & 0 deletions admiral/pkg/controller/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,23 @@ func ConstructGtpKey(env, identity string) string {
func ShouldIgnoreResource(metadata v12.ObjectMeta) bool {
return metadata.Annotations[AdmiralIgnoreAnnotation] == "true" || metadata.Labels[AdmiralIgnoreAnnotation] == "true"
}

func IsServiceMatch(serviceSelector map[string]string, selector *v12.LabelSelector) bool {
if selector == nil || len(selector.MatchLabels) == 0 || len(serviceSelector) == 0 {
return false
}
var match = true
for lkey, lvalue := range serviceSelector {
// Rollouts controller adds a dynamic label with name rollouts-pod-template-hash to both active and passive replicasets.
// This dynamic label is not available on the rollout template. Hence ignoring the label with name rollouts-pod-template-hash
if lkey == RolloutPodHashLabel {
continue
}
value, ok := selector.MatchLabels[lkey]
if !ok || value != lvalue {
match = false
break
}
}
return match
}

0 comments on commit 5bb5e5b

Please sign in to comment.