Skip to content

Commit a460895

Browse files
committed
feat: Deploy calico CNI via CRS
1 parent 414c0f7 commit a460895

File tree

22 files changed

+568
-114
lines changed

22 files changed

+568
-114
lines changed

.golangci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,6 @@ issues:
7878
- source: "flags.Parse|response.WriteError"
7979
linters:
8080
- errcheck
81+
- source: "^// \\+kubebuilder:"
82+
linters:
83+
- lll

.goreleaser.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ release:
2323
### Summary
2424
**Full Changelog**: https://github.com/d2iq-labs/{{.ProjectName}}/compare/{{ .PreviousTag }}...{{ .Tag }}
2525
26+
gomod:
27+
proxy: true
28+
2629
builds:
2730
- id: capi-runtime-extensions
2831
dir: ./cmd/capi-runtime-extensions

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ repos:
3131
stages: [commit]
3232
- id: check-added-large-files
3333
stages: [commit]
34+
exclude: ^pkg/handlers/cni/calico/manifests/tigera-operator-configmap.yaml$
3435
- id: check-case-conflict
3536
stages: [commit]
3637
- id: check-merge-conflict

README.md

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ example), run:
2323
make SKIP_BUILD=true dev.run-on-kind
2424
```
2525

26-
To create a cluster with [clusterctl](https://cluster-api.sigs.k8s.io/user/quick-start.html), run:
26+
To create a cluster with [clusterctl](https://cluster-api.sigs.k8s.io/user/quick-start.html), and label it for Calico
27+
CNI at the same time, run:
2728

2829
```shell
2930
env POD_SECURITY_STANDARD_ENABLED=false \
@@ -32,6 +33,7 @@ env POD_SECURITY_STANDARD_ENABLED=false \
3233
--kubernetes-version v1.27.2 \
3334
--control-plane-machine-count=1 \
3435
--worker-machine-count=1 | \
36+
kubectl label -f - --local --dry-run=client -oyaml capiext.labs.d2iq.io/cni=calico | \
3537
kubectl apply --server-side -f -
3638
```
3739

@@ -55,19 +57,6 @@ kubectl config set-cluster capi-quickstart \
5557
--server=https://$(docker port capi-quickstart-lb 6443/tcp)
5658
```
5759

58-
Deploy Calico to the workload cluster (TODO deploy via lifecycle hook):
59-
60-
```shell
61-
helm repo add --force-update projectcalico https://docs.tigera.io/calico/charts
62-
helm upgrade --install calico projectcalico/tigera-operator \
63-
--version v3.26.1 \
64-
--namespace tigera-operator \
65-
--create-namespace \
66-
--wait \
67-
--wait-for-jobs \
68-
--kubeconfig capd-kubeconfig
69-
```
70-
7160
Wait until all nodes are ready (this indicates that CNI has been deployed successfully):
7261

7362
```shell

charts/capi-runtime-extensions/templates/role.yaml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ rules:
1010
- ""
1111
resources:
1212
- configmaps
13-
- namespaces
14-
- secrets
1513
verbs:
1614
- create
1715
- delete
@@ -21,10 +19,14 @@ rules:
2119
- update
2220
- watch
2321
- apiGroups:
24-
- cluster.x-k8s.io
22+
- addons.cluster.x-k8s.io
2523
resources:
26-
- clusters
24+
- clusterresourcesets
2725
verbs:
26+
- create
27+
- delete
2828
- get
2929
- list
30+
- patch
31+
- update
3032
- watch
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright 2023 D2iQ, Inc. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
apiVersion: apps/v1
5+
kind: Deployment
6+
metadata:
7+
name: NA
8+
spec:
9+
template:
10+
spec:
11+
priorityClassName: system-cluster-critical
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2023 D2iQ, Inc. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
apiVersion: kustomize.config.k8s.io/v1beta1
5+
kind: Kustomization
6+
metadata:
7+
name: tigera-operator
8+
sortOptions:
9+
order: fifo
10+
patches:
11+
- path: ds-priorityClass.yaml
12+
target:
13+
kind: Deployment
14+
name: tigera-operator
15+
namespace: tigera-operator
16+
resources:
17+
- tigera-operator.yaml
Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env bash
2-
32
set -euo pipefail
43
IFS=$'\n\t'
54

@@ -9,7 +8,7 @@ readonly SCRIPT_DIR
98
# shellcheck source=hack/common.sh
109
source "${SCRIPT_DIR}/../common.sh"
1110

12-
if [ -z "${CALICO_VERSION-}" ]; then
11+
if [ -z "${CALICO_VERSION:-}" ]; then
1312
echo "Missing environment variable: CALICO_VERSION"
1413
exit 1
1514
fi
@@ -18,19 +17,32 @@ CALICO_CNI_ASSETS_DIR="$(mktemp -d -p "${TMPDIR:-/tmp}")"
1817
readonly CALICO_CNI_ASSETS_DIR
1918
trap 'rm -rf ${CALICO_CNI_ASSETS_DIR}' EXIT
2019

21-
# The operator manifest in YAML format is 1226666 bytes. It turns out that much of that is whitespace. Converting the
22-
# manifest to JSON without indentation allows us to remove most of the whitespace, reducing the size by more than half,
23-
# to 527614 bytes.
20+
curl -fsSL "https://raw.githubusercontent.com/projectcalico/calico/${CALICO_VERSION}/manifests/tigera-operator.yaml" \
21+
-o "${CALICO_CNI_ASSETS_DIR}/tigera-operator.yaml"
22+
23+
readonly KUSTOMIZATION_DIR=${SCRIPT_DIR}/kustomize/tigera-operator
24+
cp -r "${KUSTOMIZATION_DIR}"/* "${CALICO_CNI_ASSETS_DIR}"
25+
kustomize --load-restrictor=LoadRestrictionsNone build "${CALICO_CNI_ASSETS_DIR}" -o "${CALICO_CNI_ASSETS_DIR}/kustomized.yaml"
26+
27+
# The operator manifest in YAML format is pretty big. It turns out that much of that is whitespace. Converting the
28+
# manifest to JSON without indentation allows us to remove most of the whitespace, reducing the size by more than half.
2429
#
2530
# Some important notes:
2631
# 1. The YAML manifest includes many documents, and the documents must become elements in a JSON array in order for the ClusterResourceController to [parse them](https://github.com/mesosphere/cluster-api//blob/65586de0080a960d085031de87ec627b2d606a6b/exp/addons/internal/controllers/clusterresourceset_helpers.go#L59). We create a JSON array with the --slurp flag.
2732
# 2. The YAML manifest has some whitespace between YAML document markers (`---`), and these become `null` entries in the JSON array. This causes the ["SortForCreate" subroutine](https://github.com/mesosphere/cluster-api//blob/65586de0080a960d085031de87ec627b2d606a6b/exp/addons/internal/controllers/clusterresourceset_helpers.go#L84) of the ClusterResourceSet controller to misbehave. We remove these null entries using a filter expression.
2833
# 3. If we indent the JSON document, it is nearly as large as the YAML document, at 1099093 bytes. We remove indentation with the --indent=0 flag.
29-
curl -fsSL "https://docs.projectcalico.org/archive/${CALICO_VERSION}/manifests/tigera-operator.yaml" |
30-
gojq --yaml-input --slurp --indent=0 \
31-
'[ .[] | select( . != null ) | (select( .kind=="Namespace").metadata.labels += {"pod-security.kubernetes.io/enforce": "privileged", "pod-security.kubernetes.io/enforce-version": "latest"}) ]' \
32-
>"${CALICO_CNI_ASSETS_DIR}/tigera-operator.json"
34+
gojq --yaml-input \
35+
--slurp \
36+
--indent=0 \
37+
'[ .[] | select( . != null ) |
38+
(select( .kind=="Namespace").metadata.labels += {
39+
"pod-security.kubernetes.io/enforce": "privileged",
40+
"pod-security.kubernetes.io/enforce-version": "latest"
41+
})
42+
]' \
43+
<"${CALICO_CNI_ASSETS_DIR}/kustomized.yaml" \
44+
>"${CALICO_CNI_ASSETS_DIR}/tigera-operator.json"
3345

3446
kubectl create configmap tigera-operator --dry-run=client --output yaml \
3547
--from-file "${CALICO_CNI_ASSETS_DIR}/tigera-operator.json" \
36-
>"${GIT_REPO_ROOT}/pkg/addons/clusterresourcesets/templates/cni/tigera-operator-configmap.yaml"
48+
>"${GIT_REPO_ROOT}/pkg/handlers/cni/calico/manifests/tigera-operator-configmap.yaml"

internal/controllermanager/controllermanager.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ import (
1717
capiextv1alpha1 "github.com/d2iq-labs/capi-runtime-extensions/api/v1alpha1"
1818
)
1919

20-
//+kubebuilder:rbac:groups=cluster.x-k8s.io,resources=clusters,verbs=get;list;watch
21-
//+kubebuilder:rbac:groups="",resources=namespaces;configmaps;secrets,verbs=watch;list;get;create;patch;update;delete
22-
2320
type Manager struct {
2421
port uint16
2522
webhookCertDir string

internal/runtimehooks/webhooks/server.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@ import (
77
"context"
88

99
"github.com/spf13/pflag"
10+
"k8s.io/apimachinery/pkg/runtime"
11+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
12+
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
13+
capiv1 "sigs.k8s.io/cluster-api/api/v1beta1"
14+
crsv1 "sigs.k8s.io/cluster-api/exp/addons/api/v1beta1"
1015
runtimecatalog "sigs.k8s.io/cluster-api/exp/runtime/catalog"
1116
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
1217
"sigs.k8s.io/cluster-api/exp/runtime/server"
1318
ctrl "sigs.k8s.io/controller-runtime"
1419
ctrclient "sigs.k8s.io/controller-runtime/pkg/client"
1520

1621
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers"
22+
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/cni/calico"
1723
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/servicelbgc"
1824
)
1925

@@ -72,13 +78,21 @@ func (s *Server) Start(ctx context.Context) error {
7278
return err
7379
}
7480

75-
client, err := ctrclient.New(restConfig, ctrclient.Options{})
81+
scheme := runtime.NewScheme()
82+
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
83+
utilruntime.Must(crsv1.AddToScheme(scheme))
84+
utilruntime.Must(capiv1.AddToScheme(scheme))
85+
86+
client, err := ctrclient.New(restConfig, ctrclient.Options{Scheme: scheme})
7687
if err != nil {
7788
setupLog.Error(err, "error creating client to the cluster")
7889
return err
7990
}
8091

81-
allHandlers := []handlers.NamedHandler{servicelbgc.New(client)}
92+
allHandlers := []handlers.NamedHandler{
93+
servicelbgc.New(client),
94+
calico.New(client),
95+
}
8296

8397
for idx := range allHandlers {
8498
h := allHandlers[idx]

make/addons.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright 2023 D2iQ, Inc. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0
33

4-
export CALICO_VERSION := v3.25
4+
export CALICO_VERSION := v3.26.1
55

66
.PHONY: update-addon.calico
77
update-addon.calico: ; $(info $(M) updating calico manifests)

pkg/handlers/cni/calico/doc.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2023 D2iQ, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Package calico provides a handler for managing Calico deployments on clusters, configurable via
5+
// labels and annotations.
6+
//
7+
// To enable Calico deployment, a cluster must be labelled with `capiext.labs.d2iq.io/cni=calico`.
8+
// This will ensure the Tigera Configmap and associated ClusterResourceSet.
9+
//
10+
// +kubebuilder:rbac:groups=addons.cluster.x-k8s.io,resources=clusterresourcesets,verbs=watch;list;get;create;patch;update;delete
11+
// +kubebuilder:rbac:groups="",resources=configmaps,verbs=watch;list;get;create;patch;update;delete
12+
package calico

0 commit comments

Comments
 (0)