Skip to content

fix: Fix panic when applying CNI CRS via hook #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ on:
- opened
- synchronize
- reopened
branches:
- main

permissions:
contents: read
Expand Down
6 changes: 3 additions & 3 deletions charts/capi-runtime-extensions/templates/clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ metadata:
name: {{ include "chart.name" . }}
rules:
- apiGroups: [""]
resources: ["secrets"]
verbs: ["watch", "list", "get"]
resources: ["configmaps"]
verbs: ["watch", "list", "get", "create", "patch", "update", "delete"]
- apiGroups:
- addons.cluster.x-k8s.io
- bootstrap.cluster.x-k8s.io
Expand All @@ -20,4 +20,4 @@ rules:
- ipam.cluster.x-k8s.io
- runtime.cluster.x-k8s.io
resources: ["*"]
verbs: ["watch", "list", "get"]
verbs: ["watch", "list", "get", "create", "patch", "update", "delete"]
5 changes: 4 additions & 1 deletion make/clusterctl.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

.PHONY: clusterctl.init
clusterctl.init: install-tool.clusterctl
env CLUSTER_TOPOLOGY=true EXP_RUNTIME_SDK=true clusterctl init \
env CLUSTER_TOPOLOGY=true \
EXP_RUNTIME_SDK=true \
EXP_CLUSTER_RESOURCE_SET=true \
clusterctl init \
--kubeconfig=$(KIND_KUBECONFIG) \
--infrastructure docker \
--wait-providers
Expand Down
8 changes: 5 additions & 3 deletions pkg/addons/crs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"

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

func objsFromTemplate(template []byte, ns string) ([]unstructured.Unstructured, error) {
ti := repository.TemplateInput{
RawArtifact: template,
TargetNamespace: ns,
Processor: yamlprocessor.NewSimpleProcessor(),
RawArtifact: template,
TargetNamespace: ns,
Processor: yamlprocessor.NewSimpleProcessor(),
ConfigVariablesClient: config.NewMemoryReader(),
}

t, err := repository.NewTemplate(ti)
Expand Down
2 changes: 1 addition & 1 deletion pkg/addons/templates/cni/calico-cni-installation-crs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ spec:
name: tigera-operator
- kind: ConfigMap
name: calico-cni-installation
strategy: ApplyAlways
strategy: ApplyOnce
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ data:
# Note: The ipPools section cannot be modified post-install.
ipPools:
- blockSize: 26
cidr: ${POD_SUBNET}
cidr: 192.168.0.0/16
encapsulation: VXLANCrossSubnet
natOutgoing: Enabled
nodeSelector: all()
Expand Down
2 changes: 1 addition & 1 deletion pkg/handlers/lifecycle/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (m *ExtensionHandlers) DoAfterControlPlaneInitialized(
response.Message = err.Error()
return
}
err = genericResourcesClient.Create(ctx, objs)
err = genericResourcesClient.Apply(ctx, objs)
if err != nil {
response.Status = runtimehooksv1.ResponseStatusFailure
response.Message = err.Error()
Expand Down
26 changes: 14 additions & 12 deletions pkg/k8s/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@ import (
"fmt"

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

type k8sResourcesCreateError struct {
type k8sResourcesApplyError struct {
err error
}

func (e k8sResourcesCreateError) Error() string {
return fmt.Sprintf("unable to create kubernetes resource: %v", e.err)
func (e k8sResourcesApplyError) Error() string {
return fmt.Sprintf("unable to apply Kubernetes resource: %v", e.err)
}

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

// Create will create objects, ignoring individual already exists errors.
func (c *GenericResourcesClient) Create(
// Apply will apply objects via server-side apply. This will overwrite any changes that have been manually applied.
func (c *GenericResourcesClient) Apply(
ctx context.Context,
objects []unstructured.Unstructured,
) error {
opts := &ctrlclient.CreateOptions{}

// try to create, continue if it is just an alreadyExists error, fail otherwise
for i := range objects {
err := c.client.Create(ctx, &objects[i], opts)
if err != nil && !errors.IsAlreadyExists(err) {
return k8sResourcesCreateError{err: err}
err := c.client.Patch(
ctx,
&objects[i],
ctrlclient.Apply,
ctrlclient.ForceOwnership,
ctrlclient.FieldOwner("capi-runtime-extensions"),
)
if err != nil {
return k8sResourcesApplyError{err: err}
}
}

Expand Down