Skip to content

Commit c3ec48d

Browse files
authored
fix: Fix panic when applying CNI CRS via hook (#13)
1 parent 8084bb7 commit c3ec48d

File tree

8 files changed

+29
-24
lines changed

8 files changed

+29
-24
lines changed

.github/workflows/checks.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ on:
1212
- opened
1313
- synchronize
1414
- reopened
15-
branches:
16-
- main
1715

1816
permissions:
1917
contents: read

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ metadata:
99
name: {{ include "chart.name" . }}
1010
rules:
1111
- apiGroups: [""]
12-
resources: ["secrets"]
13-
verbs: ["watch", "list", "get"]
12+
resources: ["configmaps"]
13+
verbs: ["watch", "list", "get", "create", "patch", "update", "delete"]
1414
- apiGroups:
1515
- addons.cluster.x-k8s.io
1616
- bootstrap.cluster.x-k8s.io
@@ -20,4 +20,4 @@ rules:
2020
- ipam.cluster.x-k8s.io
2121
- runtime.cluster.x-k8s.io
2222
resources: ["*"]
23-
verbs: ["watch", "list", "get"]
23+
verbs: ["watch", "list", "get", "create", "patch", "update", "delete"]

make/clusterctl.mk

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
.PHONY: clusterctl.init
55
clusterctl.init: install-tool.clusterctl
6-
env CLUSTER_TOPOLOGY=true EXP_RUNTIME_SDK=true clusterctl init \
6+
env CLUSTER_TOPOLOGY=true \
7+
EXP_RUNTIME_SDK=true \
8+
EXP_CLUSTER_RESOURCE_SET=true \
9+
clusterctl init \
710
--kubeconfig=$(KIND_KUBECONFIG) \
811
--infrastructure docker \
912
--wait-providers

pkg/addons/crs.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88

99
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
10+
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
1011
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/repository"
1112
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/yamlprocessor"
1213
)
@@ -25,9 +26,10 @@ func crsObjsFromTemplates(ns string, templates ...[]byte) ([]unstructured.Unstru
2526

2627
func objsFromTemplate(template []byte, ns string) ([]unstructured.Unstructured, error) {
2728
ti := repository.TemplateInput{
28-
RawArtifact: template,
29-
TargetNamespace: ns,
30-
Processor: yamlprocessor.NewSimpleProcessor(),
29+
RawArtifact: template,
30+
TargetNamespace: ns,
31+
Processor: yamlprocessor.NewSimpleProcessor(),
32+
ConfigVariablesClient: config.NewMemoryReader(),
3133
}
3234

3335
t, err := repository.NewTemplate(ti)

pkg/addons/templates/cni/calico-cni-installation-crs.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ spec:
1414
name: tigera-operator
1515
- kind: ConfigMap
1616
name: calico-cni-installation
17-
strategy: ApplyAlways
17+
strategy: ApplyOnce

pkg/addons/templates/cni/docker-calico-cni-installation-configmap.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ data:
1818
# Note: The ipPools section cannot be modified post-install.
1919
ipPools:
2020
- blockSize: 26
21-
cidr: ${POD_SUBNET}
21+
cidr: 192.168.0.0/16
2222
encapsulation: VXLANCrossSubnet
2323
natOutgoing: Enabled
2424
nodeSelector: all()

pkg/handlers/lifecycle/handlers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (m *ExtensionHandlers) DoAfterControlPlaneInitialized(
5252
response.Message = err.Error()
5353
return
5454
}
55-
err = genericResourcesClient.Create(ctx, objs)
55+
err = genericResourcesClient.Apply(ctx, objs)
5656
if err != nil {
5757
response.Status = runtimehooksv1.ResponseStatusFailure
5858
response.Message = err.Error()

pkg/k8s/client/client.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@ import (
88
"fmt"
99

1010
"github.com/go-logr/logr"
11-
"k8s.io/apimachinery/pkg/api/errors"
1211
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1312
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
1413
)
1514

16-
type k8sResourcesCreateError struct {
15+
type k8sResourcesApplyError struct {
1716
err error
1817
}
1918

20-
func (e k8sResourcesCreateError) Error() string {
21-
return fmt.Sprintf("unable to create kubernetes resource: %v", e.err)
19+
func (e k8sResourcesApplyError) Error() string {
20+
return fmt.Sprintf("unable to apply Kubernetes resource: %v", e.err)
2221
}
2322

2423
type GenericResourcesClient struct {
@@ -33,18 +32,21 @@ func NewGenericResourcesClient(client ctrlclient.Client, log logr.Logger) *Gener
3332
}
3433
}
3534

36-
// Create will create objects, ignoring individual already exists errors.
37-
func (c *GenericResourcesClient) Create(
35+
// Apply will apply objects via server-side apply. This will overwrite any changes that have been manually applied.
36+
func (c *GenericResourcesClient) Apply(
3837
ctx context.Context,
3938
objects []unstructured.Unstructured,
4039
) error {
41-
opts := &ctrlclient.CreateOptions{}
42-
43-
// try to create, continue if it is just an alreadyExists error, fail otherwise
4440
for i := range objects {
45-
err := c.client.Create(ctx, &objects[i], opts)
46-
if err != nil && !errors.IsAlreadyExists(err) {
47-
return k8sResourcesCreateError{err: err}
41+
err := c.client.Patch(
42+
ctx,
43+
&objects[i],
44+
ctrlclient.Apply,
45+
ctrlclient.ForceOwnership,
46+
ctrlclient.FieldOwner("capi-runtime-extensions"),
47+
)
48+
if err != nil {
49+
return k8sResourcesApplyError{err: err}
4850
}
4951
}
5052

0 commit comments

Comments
 (0)