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 Jan 29, 2018
1 parent 5b53123 commit a928a15
Show file tree
Hide file tree
Showing 14 changed files with 563 additions and 84 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/controller-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
30 changes: 13 additions & 17 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,19 +20,15 @@ import (
)

var (
initImage string
proxyImage string
proxyUID int64
inboundPort uint
outboundPort uint
ignoreInboundPorts []uint
ignoreOutboundPorts []uint
proxyControlPort uint
proxyAPIPort uint
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
)

var injectCmd = &cobra.Command{
Expand Down Expand Up @@ -289,14 +286,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
118 changes: 64 additions & 54 deletions cli/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package cmd

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

"github.com/runconduit/conduit/pkg/k8s"
"github.com/runconduit/conduit/pkg/version"
uuid "github.com/satori/go.uuid"
"github.com/spf13/cobra"
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 @@ -229,9 +231,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 @@ -252,18 +254,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 @@ -291,9 +293,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 @@ -311,18 +313,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 @@ -358,9 +360,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 @@ -387,16 +389,18 @@ data:
`

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

var (
Expand All @@ -416,26 +420,32 @@ var installCmd = &cobra.Command{
if err := validate(); err != nil {
log.Fatal(err.Error())
}
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(),
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),
})
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 a928a15

Please sign in to comment.