Skip to content

Commit

Permalink
Merge pull request #333 from weaveworks/default-labels
Browse files Browse the repository at this point in the history
Add the app/name label to services and primary deployment
  • Loading branch information
stefanprodan authored Oct 9, 2019
2 parents 316de42 + a8ee477 commit d4250f3
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 29 deletions.
1 change: 1 addition & 0 deletions charts/flagger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Parameter | Description | Default
`image.pullPolicy` | image pull policy | `IfNotPresent`
`prometheus.install` | if `true`, installs Prometheus configured to scrape all pods in the custer including the App Mesh sidecar | `false`
`metricsServer` | Prometheus URL, used when `prometheus.install` is `false` | `http://prometheus.istio-system:9090`
`selectorLabels` | list of labels that Flagger uses to create pod selectors | `app,name,app.kubernetes.io/name`
`slack.url` | Slack incoming webhook | None
`slack.channel` | Slack channel | None
`slack.user` | Slack username | `flagger`
Expand Down
3 changes: 3 additions & 0 deletions charts/flagger/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ spec:
{{- else }}
- -metrics-server={{ .Values.metricsServer }}
{{- end }}
{{- if .Values.selectorLabels }}
- -selector-labels={{ .Values.selectorLabels }}
{{- end }}
{{- if .Values.namespace }}
- -namespace={{ .Values.namespace }}
{{- end }}
Expand Down
6 changes: 5 additions & 1 deletion charts/flagger/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ podAnnotations:

metricsServer: "http://prometheus:9090"

# accepted values are istio, appmesh, nginx or supergloo:mesh.namespace (defaults to istio)
# accepted values are kubernetes, istio, linkerd, appmesh, nginx, gloo or supergloo:mesh.namespace (defaults to istio)
meshProvider: ""

# single namespace restriction
namespace: ""

# list of pod labels that Flagger uses to create pod selectors
# defaults to: app,name,app.kubernetes.io/name
selectorLabels: ""

slack:
user: flagger
channel:
Expand Down
11 changes: 7 additions & 4 deletions pkg/canary/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Deployer struct {

// Initialize creates the primary deployment, hpa,
// scales to zero the canary deployment and returns the pod selector label and container ports
func (c *Deployer) Initialize(cd *flaggerv1.Canary, skipLivenessChecks bool) (label string, ports *map[string]int32, err error) {
func (c *Deployer) Initialize(cd *flaggerv1.Canary, skipLivenessChecks bool) (label string, ports map[string]int32, err error) {
primaryName := fmt.Sprintf("%s-primary", cd.Spec.TargetRef.Name)
label, ports, err = c.createPrimaryDeployment(cd)
if err != nil {
Expand Down Expand Up @@ -185,7 +185,7 @@ func (c *Deployer) Scale(cd *flaggerv1.Canary, replicas int32) error {
return nil
}

func (c *Deployer) createPrimaryDeployment(cd *flaggerv1.Canary) (string, *map[string]int32, error) {
func (c *Deployer) createPrimaryDeployment(cd *flaggerv1.Canary) (string, map[string]int32, error) {
targetName := cd.Spec.TargetRef.Name
primaryName := fmt.Sprintf("%s-primary", cd.Spec.TargetRef.Name)

Expand All @@ -203,13 +203,13 @@ func (c *Deployer) createPrimaryDeployment(cd *flaggerv1.Canary) (string, *map[s
targetName, cd.Namespace, targetName)
}

var ports *map[string]int32
var ports map[string]int32
if cd.Spec.Service.PortDiscovery {
p, err := c.getPorts(cd, canaryDep)
if err != nil {
return "", nil, fmt.Errorf("port discovery failed with error: %v", err)
}
ports = &p
ports = p
}

primaryDep, err := c.KubeClient.AppsV1().Deployments(cd.Namespace).Get(primaryName, metav1.GetOptions{})
Expand Down Expand Up @@ -237,6 +237,9 @@ func (c *Deployer) createPrimaryDeployment(cd *flaggerv1.Canary) (string, *map[s
ObjectMeta: metav1.ObjectMeta{
Name: primaryName,
Namespace: cd.Namespace,
Labels: map[string]string{
label: primaryName,
},
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(cd, schema.GroupVersionKind{
Group: flaggerv1.SchemeGroupVersion.Group,
Expand Down
8 changes: 4 additions & 4 deletions pkg/controller/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (c *Controller) advanceCanary(name string, namespace string, skipLivenessCh
if skipLivenessChecks || strings.Contains(provider, "istio") || strings.Contains(provider, "appmesh") {
skipPrimaryCheck = true
}
label, ports, err := c.deployer.Initialize(cd, skipPrimaryCheck)
labelSelector, ports, err := c.deployer.Initialize(cd, skipPrimaryCheck)
if err != nil {
c.recordEventWarningf(cd, "%v", err)
return
Expand All @@ -112,7 +112,7 @@ func (c *Controller) advanceCanary(name string, namespace string, skipLivenessCh
meshRouter := c.routerFactory.MeshRouter(provider)

// create or update ClusterIP services
if err := c.routerFactory.KubernetesRouter(label, ports).Reconcile(cd); err != nil {
if err := c.routerFactory.KubernetesRouter(labelSelector, map[string]string{}, ports).Reconcile(cd); err != nil {
c.recordEventWarningf(cd, "%v", err)
return
}
Expand All @@ -123,6 +123,7 @@ func (c *Controller) advanceCanary(name string, namespace string, skipLivenessCh
return
}

// check for deployment spec or configs changes
shouldAdvance, err := c.shouldAdvance(cd)
if err != nil {
c.recordEventWarningf(cd, "%v", err)
Expand Down Expand Up @@ -153,8 +154,7 @@ func (c *Controller) advanceCanary(name string, namespace string, skipLivenessCh
}
}

// check if virtual service exists
// and if it contains weighted destination routes to the primary and canary services
// get the routing settings
primaryWeight, canaryWeight, mirrored, err := meshRouter.GetRoutes(cd)
if err != nil {
c.recordEventWarningf(cd, "%v", err)
Expand Down
5 changes: 3 additions & 2 deletions pkg/router/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ func NewFactory(kubeConfig *restclient.Config, kubeClient kubernetes.Interface,
}

// KubernetesRouter returns a ClusterIP service router
func (factory *Factory) KubernetesRouter(label string, ports *map[string]int32) *KubernetesRouter {
func (factory *Factory) KubernetesRouter(labelSelector string, annotations map[string]string, ports map[string]int32) *KubernetesRouter {
return &KubernetesRouter{
logger: factory.logger,
flaggerClient: factory.flaggerClient,
kubeClient: factory.kubeClient,
label: label,
labelSelector: labelSelector,
annotations: annotations,
ports: ports,
}
}
Expand Down
37 changes: 19 additions & 18 deletions pkg/router/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ type KubernetesRouter struct {
kubeClient kubernetes.Interface
flaggerClient clientset.Interface
logger *zap.SugaredLogger
label string
ports *map[string]int32
labelSelector string
annotations map[string]string
ports map[string]int32
}

// Reconcile creates or updates the primary and canary services
Expand Down Expand Up @@ -78,7 +79,7 @@ func (c *KubernetesRouter) reconcileService(canary *flaggerv1.Canary, name strin

svcSpec := corev1.ServiceSpec{
Type: corev1.ServiceTypeClusterIP,
Selector: map[string]string{c.label: target},
Selector: map[string]string{c.labelSelector: target},
Ports: []corev1.ServicePort{
{
Name: portName,
Expand All @@ -89,28 +90,28 @@ func (c *KubernetesRouter) reconcileService(canary *flaggerv1.Canary, name strin
},
}

if c.ports != nil {
for n, p := range *c.ports {
cp := corev1.ServicePort{
Name: n,
Protocol: corev1.ProtocolTCP,
Port: p,
TargetPort: intstr.IntOrString{
Type: intstr.Int,
IntVal: p,
},
}

svcSpec.Ports = append(svcSpec.Ports, cp)
for n, p := range c.ports {
cp := corev1.ServicePort{
Name: n,
Protocol: corev1.ProtocolTCP,
Port: p,
TargetPort: intstr.IntOrString{
Type: intstr.Int,
IntVal: p,
},
}

svcSpec.Ports = append(svcSpec.Ports, cp)
}

svc, err := c.kubeClient.CoreV1().Services(canary.Namespace).Get(name, metav1.GetOptions{})
if errors.IsNotFound(err) {
svc = &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: canary.Namespace,
Name: name,
Namespace: canary.Namespace,
Labels: map[string]string{c.labelSelector: name},
Annotations: c.annotations,
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(canary, schema.GroupVersionKind{
Group: flaggerv1.SchemeGroupVersion.Group,
Expand Down

0 comments on commit d4250f3

Please sign in to comment.