Skip to content
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
22 changes: 16 additions & 6 deletions pkg/crds/crds.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ func (m *CRDManager) AddCRDLabelToClusterMapping(labelValue string, cluster *clu
m.crdLabelsToClusterMappings[labelValue] = cluster
}

func (m *CRDManager) SkipCRDsWithClusterLabel(labelValue string) {
m.crdLabelsToClusterMappings[labelValue] = nil
}

func (m *CRDManager) CreateOrUpdateCRDs(ctx context.Context, log *logging.Logger) error {
crds, err := m.crdList()
if err != nil {
Expand All @@ -48,14 +52,20 @@ func (m *CRDManager) CreateOrUpdateCRDs(ctx context.Context, log *logging.Logger
var errs error

for _, crd := range crds {
c, err := m.getClusterForCRD(crd)
c, cLabel, err := m.getClusterForCRD(crd)
if err != nil {
errs = errors.Join(errs, err)
continue
}

if c == nil {
if log != nil {
log.Info("Skipping CRD because the assigned cluster is nil", "name", crd.Name, "clusterLabel", cLabel)
}
continue
}
if log != nil {
log.Info("creating/updating CRD", "name", crd.Name, "cluster", c.ID())
log.Info("Creating/updating CRD", "name", crd.Name, "cluster", c.ID())
}
m := resources.NewCRDMutator(crd)
m.MetadataMutator().WithLabels(crd.Labels).WithAnnotations(crd.Annotations)
Expand All @@ -69,18 +79,18 @@ func (m *CRDManager) CreateOrUpdateCRDs(ctx context.Context, log *logging.Logger
return nil
}

func (m *CRDManager) getClusterForCRD(crd *apiextv1.CustomResourceDefinition) (*clusters.Cluster, error) {
func (m *CRDManager) getClusterForCRD(crd *apiextv1.CustomResourceDefinition) (*clusters.Cluster, string, error) {
labelValue, ok := controller.GetLabel(crd, m.mappingLabelName)
if !ok {
return nil, fmt.Errorf("missing label '%s' for CRD '%s'", m.mappingLabelName, crd.Name)
return nil, "", fmt.Errorf("missing label '%s' for CRD '%s'", m.mappingLabelName, crd.Name)
}

cluster, ok := m.crdLabelsToClusterMappings[labelValue]
if !ok {
return nil, fmt.Errorf("no cluster mapping found for label value '%s' in CRD '%s'", labelValue, crd.Name)
return nil, labelValue, fmt.Errorf("no cluster mapping found for label value '%s' in CRD '%s'", labelValue, crd.Name)
}

return cluster, nil
return cluster, labelValue, nil
}

// CRDsFromFileSystem reads CRDs from the specified filesystem path.
Expand Down
7 changes: 6 additions & 1 deletion pkg/crds/crds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var _ = Describe("CRDsFromFileSystem", func() {
crdPath := "testdata"
crdsList, err := crds.CRDsFromFileSystem(testFS, crdPath)
Expect(err).NotTo(HaveOccurred())
Expect(crdsList).To(HaveLen(2))
Expect(crdsList).To(HaveLen(3))

// Validate the first CRD
Expect(crdsList[0].Name).To(Equal("testresources.example.com"))
Expand Down Expand Up @@ -69,6 +69,11 @@ var _ = Describe("CRDManager", func() {

ctx := context.Background()

// CRD creation should fail due to unknown cluster label
Expect(crdManager.CreateOrUpdateCRDs(ctx, nil)).To(HaveOccurred())

crdManager.SkipCRDsWithClusterLabel("cluster_c")

err = crdManager.CreateOrUpdateCRDs(ctx, nil)
Expect(err).NotTo(HaveOccurred())

Expand Down
30 changes: 30 additions & 0 deletions pkg/crds/testdata/crd_c.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: unused.example.com
labels:
openmcp.cloud/cluster: "cluster_c"
spec:
group: example.com
names:
kind: TestResource
listKind: TestResourceList
plural: testresources
singular: testresource
scope: Namespaced
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
name:
type: string
replicas:
type: integer
minimum: 1