Skip to content

Commit

Permalink
Simplify and refactor k8s labels and annnotations
Browse files Browse the repository at this point in the history
The conduit.io/* k8s labels and annotations we're redundant in some
cases, and not flexible enough in others.

This change modifies the labels in the following ways:
`conduit.io/plane: control` => `conduit.io/controller-component: web`
`conduit.io/controller: conduit` => `conduit.io/controller-ns: conduit`
`conduit.io/plane: data` => (remove, redundant with `conduit.io/controller-ns`)
It also centralizes all k8s labels and annotations into
pkg/k8s/labels.go, and adds tests for the install command.

Part of #201

Signed-off-by: Andrew Seigner <siggy@buoyant.io>
  • Loading branch information
siggy committed Feb 1, 2018
1 parent 9ff439e commit a00b051
Show file tree
Hide file tree
Showing 16 changed files with 568 additions and 89 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ jobs:
- bin/docker-push $CONDUIT_TAG
- bin/docker-retag-all $CONDUIT_TAG master && bin/docker-push master
- target/cli/linux/conduit install --version=$CONDUIT_TAG |tee conduit.yml
- kubectl -n conduit apply -f conduit.yml --prune --selector='conduit.io/plane=control'
- kubectl -n conduit apply -f conduit.yml --prune --selector='conduit.io/control-plane-component'

notifications:
email:
Expand Down
8 changes: 7 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion bin/update-go-deps-shas
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ set -eu
sha=$(. bin/_tag.sh ; go_deps_sha)

for f in $( grep -lR --include=Dockerfile\* go-deps: . ) ; do
sed -E -i'' -e "s|runconduit/go-deps:[^ ]+|runconduit/go-deps:${sha}|" "$f"
sed -E -i.bak -e "s|runconduit/go-deps:[^ ]+|runconduit/go-deps:${sha}|" "$f"
rm "$f".bak
done
3 changes: 2 additions & 1 deletion bin/update-proxy-deps-shas
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ set -eu
sha=$(. bin/_tag.sh ; proxy_deps_sha)

for f in $( grep -lR --include=Dockerfile\* proxy-deps: . ) ; do
sed -E -i'' -e "s|runconduit/proxy-deps:[^ ]+|runconduit/proxy-deps:${sha}|" "$f"
sed -E -i.bak -e "s|runconduit/proxy-deps:[^ ]+|runconduit/proxy-deps:${sha}|" "$f"
rm "$f".bak
done
4 changes: 2 additions & 2 deletions cli/Dockerfile-bin
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## compile binaries
FROM gcr.io/runconduit/go-deps:dac3fae6 as golang
FROM gcr.io/runconduit/go-deps:7bceac7a as golang
ARG CONDUIT_VERSION
WORKDIR /go/src/github.com/runconduit/conduit
COPY cli cli
Expand All @@ -10,7 +10,7 @@ RUN CGO_ENABLED=0 GOOS=darwin go build -a -installsuffix cgo -o /out/conduit-dar
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o /out/conduit-linux -ldflags "-X github.com/runconduit/conduit/pkg/version.Version=$CONDUIT_VERSION" ./cli
RUN CGO_ENABLED=0 GOOS=windows go build -a -installsuffix cgo -o /out/conduit-windows -ldflags "-X github.com/runconduit/conduit/pkg/version.Version=$CONDUIT_VERSION" ./cli

## export without sources & depdenndencies
## export without sources & dependencies
FROM gcr.io/runconduit/base:2017-10-30.01
COPY --from=golang /out /out
WORKDIR /out
32 changes: 14 additions & 18 deletions cli/cmd/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"github.com/ghodss/yaml"
"github.com/runconduit/conduit/pkg/k8s"
"github.com/runconduit/conduit/pkg/version"
"github.com/spf13/cobra"
batchV1 "k8s.io/api/batch/v1"
Expand All @@ -19,20 +20,16 @@ import (
)

var (
initImage string
proxyImage string
proxyUID int64
inboundPort uint
outboundPort uint
ignoreInboundPorts []uint
ignoreOutboundPorts []uint
proxyControlPort uint
proxyAPIPort uint
proxyLogLevel string
conduitCreatedByAnnotation = "conduit.io/created-by"
conduitProxyVersionAnnotation = "conduit.io/proxy-version"
conduitControlLabel = "conduit.io/controller"
conduitPlaneLabel = "conduit.io/plane"
initImage string
proxyImage string
proxyUID int64
inboundPort uint
outboundPort uint
ignoreInboundPorts []uint
ignoreOutboundPorts []uint
proxyControlPort uint
proxyAPIPort uint
proxyLogLevel string
)

var injectCmd = &cobra.Command{
Expand Down Expand Up @@ -290,14 +287,13 @@ func injectPodTemplateSpec(t *v1.PodTemplateSpec) enhancedPodTemplateSpec {
if t.Annotations == nil {
t.Annotations = make(map[string]string)
}
t.Annotations[conduitCreatedByAnnotation] = fmt.Sprintf("conduit/cli %s", version.Version)
t.Annotations[conduitProxyVersionAnnotation] = conduitVersion
t.Annotations[k8s.CreatedByAnnotation] = k8s.CreatedByAnnotationValue()
t.Annotations[k8s.ProxyVersionAnnotation] = conduitVersion

if t.Labels == nil {
t.Labels = make(map[string]string)
}
t.Labels[conduitControlLabel] = controlPlaneNamespace
t.Labels[conduitPlaneLabel] = "data"
t.Labels[k8s.ControllerNSLabel] = controlPlaneNamespace
t.Spec.Containers = append(t.Spec.Containers, sidecar)
return enhancedPodTemplateSpec{
t,
Expand Down
122 changes: 66 additions & 56 deletions cli/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package cmd

import (
"fmt"
"io"
"os"
"regexp"
"text/template"

"github.com/runconduit/conduit/pkg/k8s"
"github.com/runconduit/conduit/pkg/version"
uuid "github.com/satori/go.uuid"
log "github.com/sirupsen/logrus"
Expand All @@ -24,7 +26,7 @@ kind: ServiceAccount
apiVersion: v1
metadata:
name: conduit-controller
namespace: conduit
namespace: {{.Namespace}}
### RBAC ###
---
Expand All @@ -45,23 +47,23 @@ kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: conduit-controller
namespace: conduit
namespace: {{.Namespace}}
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: conduit-controller
subjects:
- kind: ServiceAccount
name: conduit-controller
namespace: conduit
namespace: {{.Namespace}}
### Service Account Prometheus ###
---
kind: ServiceAccount
apiVersion: v1
metadata:
name: conduit-prometheus
namespace: conduit
namespace: {{.Namespace}}
### RBAC ###
---
Expand All @@ -79,15 +81,15 @@ kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: conduit-prometheus
namespace: conduit
namespace: {{.Namespace}}
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: conduit-prometheus
subjects:
- kind: ServiceAccount
name: conduit-prometheus
namespace: conduit
namespace: {{.Namespace}}
### Controller ###
---
Expand All @@ -98,9 +100,9 @@ metadata:
namespace: {{.Namespace}}
labels:
app: controller
conduit.io/plane: control
{{.ControllerComponentLabel}}: controller
annotations:
conduit.io/created-by: "{{.CliVersion}}"
{{.CreatedByAnnotation}}: {{.CliVersion}}
spec:
type: ClusterIP
selector:
Expand All @@ -118,9 +120,9 @@ metadata:
namespace: {{.Namespace}}
labels:
app: controller
conduit.io/plane: control
{{.ControllerComponentLabel}}: controller
annotations:
conduit.io/created-by: "{{.CliVersion}}"
{{.CreatedByAnnotation}}: {{.CliVersion}}
spec:
type: ClusterIP
selector:
Expand All @@ -138,18 +140,18 @@ metadata:
namespace: {{.Namespace}}
labels:
app: controller
conduit.io/plane: control
{{.ControllerComponentLabel}}: controller
annotations:
conduit.io/created-by: "{{.CliVersion}}"
{{.CreatedByAnnotation}}: {{.CliVersion}}
spec:
replicas: {{.ControllerReplicas}}
template:
metadata:
labels:
app: controller
conduit.io/plane: control
{{.ControllerComponentLabel}}: controller
annotations:
conduit.io/created-by: "{{.CliVersion}}"
{{.CreatedByAnnotation}}: {{.CliVersion}}
spec:
serviceAccount: conduit-controller
containers:
Expand Down Expand Up @@ -234,9 +236,9 @@ metadata:
namespace: {{.Namespace}}
labels:
app: web
conduit.io/plane: control
{{.ControllerComponentLabel}}: web
annotations:
conduit.io/created-by: "{{.CliVersion}}"
{{.CreatedByAnnotation}}: {{.CliVersion}}
spec:
type: ClusterIP
selector:
Expand All @@ -257,18 +259,18 @@ metadata:
namespace: {{.Namespace}}
labels:
app: web
conduit.io/plane: control
{{.ControllerComponentLabel}}: web
annotations:
conduit.io/created-by: "{{.CliVersion}}"
{{.CreatedByAnnotation}}: {{.CliVersion}}
spec:
replicas: {{.WebReplicas}}
template:
metadata:
labels:
app: web
conduit.io/plane: control
{{.ControllerComponentLabel}}: web
annotations:
conduit.io/created-by: "{{.CliVersion}}"
{{.CreatedByAnnotation}}: {{.CliVersion}}
spec:
containers:
- name: web
Expand Down Expand Up @@ -297,9 +299,9 @@ metadata:
namespace: {{.Namespace}}
labels:
app: prometheus
conduit.io/plane: control
{{.ControllerComponentLabel}}: prometheus
annotations:
conduit.io/created-by: "{{.CliVersion}}"
{{.CreatedByAnnotation}}: {{.CliVersion}}
spec:
type: ClusterIP
selector:
Expand All @@ -317,18 +319,18 @@ metadata:
namespace: {{.Namespace}}
labels:
app: prometheus
conduit.io/plane: control
{{.ControllerComponentLabel}}: prometheus
annotations:
conduit.io/created-by: "{{.CliVersion}}"
{{.CreatedByAnnotation}}: {{.CliVersion}}
spec:
replicas: {{.PrometheusReplicas}}
template:
metadata:
labels:
app: prometheus
conduit.io/plane: control
{{.ControllerComponentLabel}}: prometheus
annotations:
conduit.io/created-by: "{{.CliVersion}}"
{{.CreatedByAnnotation}}: {{.CliVersion}}
spec:
serviceAccount: conduit-prometheus
volumes:
Expand Down Expand Up @@ -364,9 +366,9 @@ metadata:
namespace: {{.Namespace}}
labels:
app: prometheus
conduit.io/plane: control
{{.ControllerComponentLabel}}: prometheus
annotations:
conduit.io/created-by: "{{.CliVersion}}"
{{.CreatedByAnnotation}}: {{.CliVersion}}
data:
prometheus.yml: |-
global:
Expand All @@ -393,17 +395,19 @@ data:
`

type installConfig struct {
Namespace string
ControllerImage string
WebImage string
PrometheusImage string
ControllerReplicas uint
WebReplicas uint
PrometheusReplicas uint
ImagePullPolicy string
UUID string
CliVersion string
ControllerLogLevel string
Namespace string
ControllerImage string
WebImage string
PrometheusImage string
ControllerReplicas uint
WebReplicas uint
PrometheusReplicas uint
ImagePullPolicy string
UUID string
CliVersion string
ControllerLogLevel string
ControllerComponentLabel string
CreatedByAnnotation string
}

var (
Expand All @@ -424,27 +428,33 @@ var installCmd = &cobra.Command{
if err := validate(); err != nil {
return err
}
template, err := template.New("conduit").Parse(conduitTemplate)
if err != nil {
return err
config := installConfig{
Namespace: controlPlaneNamespace,
ControllerImage: fmt.Sprintf("%s/controller:%s", dockerRegistry, conduitVersion),
WebImage: fmt.Sprintf("%s/web:%s", dockerRegistry, conduitVersion),
PrometheusImage: "prom/prometheus:v1.8.1",
ControllerReplicas: controllerReplicas,
WebReplicas: webReplicas,
PrometheusReplicas: prometheusReplicas,
ImagePullPolicy: imagePullPolicy,
UUID: uuid.NewV4().String(),
CliVersion: k8s.CreatedByAnnotationValue(),
ControllerLogLevel: controllerLogLevel,
ControllerComponentLabel: k8s.ControllerComponentLabel,
CreatedByAnnotation: k8s.CreatedByAnnotation,
}
template.Execute(os.Stdout, installConfig{
Namespace: controlPlaneNamespace,
ControllerImage: fmt.Sprintf("%s/controller:%s", dockerRegistry, conduitVersion),
WebImage: fmt.Sprintf("%s/web:%s", dockerRegistry, conduitVersion),
PrometheusImage: "prom/prometheus:v1.8.1",
ControllerReplicas: controllerReplicas,
WebReplicas: webReplicas,
PrometheusReplicas: prometheusReplicas,
ImagePullPolicy: imagePullPolicy,
UUID: uuid.NewV4().String(),
CliVersion: fmt.Sprintf("conduit/cli %s", version.Version),
ControllerLogLevel: controllerLogLevel,
})
return nil
return render(config, os.Stdout)
},
}

func render(config installConfig, w io.Writer) error {
template, err := template.New("conduit").Parse(conduitTemplate)
if err != nil {
return err
}
return template.Execute(w, config)
}

var alphaNumDash = regexp.MustCompile("^[a-zA-Z0-9-]+$")
var alphaNumDashDot = regexp.MustCompile("^[\\.a-zA-Z0-9-]+$")
var alphaNumDashDotSlash = regexp.MustCompile("^[\\./a-zA-Z0-9-]+$")
Expand Down
Loading

0 comments on commit a00b051

Please sign in to comment.