-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle k8s service events and trigger SE generation #238
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -693,12 +693,11 @@ func TestGetServiceForRolloutCanary(t *testing.T) { | |
selectorMap["app"] = "test" | ||
|
||
service := &coreV1.Service{ | ||
ObjectMeta: v12.ObjectMeta{Name: SERVICENAME, Namespace: NAMESPACE, CreationTimestamp: v12.NewTime(time.Now())}, | ||
Spec: coreV1.ServiceSpec{ | ||
Selector: selectorMap, | ||
}, | ||
} | ||
service.Name = SERVICENAME | ||
service.Namespace = NAMESPACE | ||
port1 := coreV1.ServicePort{ | ||
Port: 8080, | ||
} | ||
|
@@ -711,15 +710,15 @@ func TestGetServiceForRolloutCanary(t *testing.T) { | |
service.Spec.Ports = ports | ||
|
||
stableService := &coreV1.Service{ | ||
ObjectMeta: v12.ObjectMeta{Name: STABLESERVICENAME, Namespace: NAMESPACE}, | ||
ObjectMeta: v12.ObjectMeta{Name: STABLESERVICENAME, Namespace: NAMESPACE, CreationTimestamp: v12.NewTime(time.Now().Add(time.Duration(-15)))}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick: StableServiceName |
||
Spec: coreV1.ServiceSpec{ | ||
Selector: selectorMap, | ||
Ports: ports, | ||
}, | ||
} | ||
|
||
canaryService := &coreV1.Service{ | ||
ObjectMeta: v12.ObjectMeta{Name: CANARYSERVICENAME, Namespace: NAMESPACE}, | ||
ObjectMeta: v12.ObjectMeta{Name: CANARYSERVICENAME, Namespace: NAMESPACE, CreationTimestamp: v12.NewTime(time.Now().Add(time.Duration(-15)))}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: CanaryServiceName |
||
Spec: coreV1.ServiceSpec{ | ||
Selector: selectorMap, | ||
Ports: ports, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import ( | |
k8sAppsV1 "k8s.io/api/apps/v1" | ||
k8sAppsinformers "k8s.io/client-go/informers/apps/v1" | ||
"k8s.io/client-go/rest" | ||
"reflect" | ||
"time" | ||
|
||
log "github.com/sirupsen/logrus" | ||
|
@@ -78,45 +79,6 @@ func (p *deploymentCache) DeleteFromDeploymentClusterCache(key string, deploymen | |
} | ||
} | ||
|
||
func (d *DeploymentController) GetDeployments() ([]*k8sAppsV1.Deployment, error) { | ||
|
||
ns := d.K8sClient.CoreV1().Namespaces() | ||
|
||
namespaceSidecarInjectionLabelFilter := d.labelSet.NamespaceSidecarInjectionLabel + "=" + d.labelSet.NamespaceSidecarInjectionLabelValue | ||
istioEnabledNs, err := ns.List(meta_v1.ListOptions{LabelSelector: namespaceSidecarInjectionLabelFilter}) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("error getting istio labled namespaces: %v", err) | ||
} | ||
|
||
var res []*k8sAppsV1.Deployment | ||
|
||
for _, v := range istioEnabledNs.Items { | ||
|
||
deployments := d.K8sClient.AppsV1().Deployments(v.Name) | ||
deploymentsList, err := deployments.List(meta_v1.ListOptions{}) | ||
if err != nil { | ||
return nil, fmt.Errorf("error listing deployments: %v", err) | ||
} | ||
var admiralDeployments []k8sAppsV1.Deployment | ||
for _, deployment := range deploymentsList.Items { | ||
if !d.shouldIgnoreBasedOnLabels(&deployment) { | ||
admiralDeployments = append(admiralDeployments, deployment) | ||
} | ||
} | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("error getting istio labled namespaces: %v", err) | ||
} | ||
|
||
for _, pi := range admiralDeployments { | ||
res = append(res, &pi) | ||
} | ||
} | ||
|
||
return res, nil | ||
} | ||
|
||
func NewDeploymentController(clusterID string, stopCh <-chan struct{}, handler DeploymentHandler, config *rest.Config, resyncPeriod time.Duration) (*DeploymentController, error) { | ||
|
||
deploymentController := DeploymentController{} | ||
|
@@ -211,10 +173,10 @@ func (d *DeploymentController) shouldIgnoreBasedOnLabels(deployment *k8sAppsV1.D | |
return false //labels are fine, we should not ignore | ||
} | ||
|
||
func (d *DeploymentController) GetDeploymentByLabel(labelValue string, namespace string) []k8sAppsV1.Deployment { | ||
matchLabel := common.GetGlobalTrafficDeploymentLabel() | ||
func (d *DeploymentController) GetDeploymentBySelectorInNamespace(serviceSelector map[string]string, namespace string) []k8sAppsV1.Deployment { | ||
|
||
labelOptions := meta_v1.ListOptions{} | ||
labelOptions.LabelSelector = fmt.Sprintf("%s=%s", matchLabel, labelValue) | ||
|
||
matchedDeployments, err := d.K8sClient.AppsV1().Deployments(namespace).List(labelOptions) | ||
nirvanagit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if err != nil { | ||
|
@@ -226,5 +188,16 @@ func (d *DeploymentController) GetDeploymentByLabel(labelValue string, namespace | |
return []k8sAppsV1.Deployment{} | ||
} | ||
|
||
return matchedDeployments.Items | ||
var filteredDeployments = make([]k8sAppsV1.Deployment, 0) | ||
|
||
for _, deployment := range matchedDeployments.Items { | ||
if deployment.Spec.Selector == nil || deployment.Spec.Selector.MatchLabels == nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could check for nil values for |
||
continue | ||
} | ||
if reflect.DeepEqual(deployment.Spec.Selector.MatchLabels, serviceSelector) { | ||
nirvanagit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
filteredDeployments = append(filteredDeployments, deployment) | ||
} | ||
} | ||
|
||
return filteredDeployments | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: ServiceName, Namespace