|
| 1 | +package crds |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io/fs" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 10 | + "sigs.k8s.io/yaml" |
| 11 | + |
| 12 | + "github.com/openmcp-project/controller-utils/pkg/clusters" |
| 13 | + "github.com/openmcp-project/controller-utils/pkg/controller" |
| 14 | + "github.com/openmcp-project/controller-utils/pkg/errors" |
| 15 | + "github.com/openmcp-project/controller-utils/pkg/logging" |
| 16 | + "github.com/openmcp-project/controller-utils/pkg/resources" |
| 17 | +) |
| 18 | + |
| 19 | +type ( |
| 20 | + CRDLabelToClusterMappings map[string]*clusters.Cluster |
| 21 | + CRDList func() ([]*apiextv1.CustomResourceDefinition, error) |
| 22 | +) |
| 23 | + |
| 24 | +type CRDManager struct { |
| 25 | + mappingLabelName string |
| 26 | + crdLabelsToClusterMappings CRDLabelToClusterMappings |
| 27 | + crdList CRDList |
| 28 | +} |
| 29 | + |
| 30 | +func NewCRDManager(mappingLabelName string, crdList CRDList) *CRDManager { |
| 31 | + return &CRDManager{ |
| 32 | + mappingLabelName: mappingLabelName, |
| 33 | + crdLabelsToClusterMappings: make(CRDLabelToClusterMappings), |
| 34 | + crdList: crdList, |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func (m *CRDManager) AddCRDLabelToClusterMapping(labelValue string, cluster *clusters.Cluster) { |
| 39 | + m.crdLabelsToClusterMappings[labelValue] = cluster |
| 40 | +} |
| 41 | + |
| 42 | +func (m *CRDManager) CreateOrUpdateCRDs(ctx context.Context, log *logging.Logger) error { |
| 43 | + crds, err := m.crdList() |
| 44 | + if err != nil { |
| 45 | + return fmt.Errorf("error getting CRDs: %w", err) |
| 46 | + } |
| 47 | + |
| 48 | + var errs error |
| 49 | + |
| 50 | + for _, crd := range crds { |
| 51 | + c, err := m.getClusterForCRD(crd) |
| 52 | + if err != nil { |
| 53 | + errs = errors.Join(errs, err) |
| 54 | + continue |
| 55 | + } |
| 56 | + |
| 57 | + if log != nil { |
| 58 | + log.Info("creating/updating CRD", "name", crd.Name, "cluster", c.ID()) |
| 59 | + } |
| 60 | + err = resources.CreateOrUpdateResource(ctx, c.Client(), resources.NewCRDMutator(crd, crd.Labels, crd.Annotations)) |
| 61 | + errs = errors.Join(errs, err) |
| 62 | + } |
| 63 | + |
| 64 | + if errs != nil { |
| 65 | + return fmt.Errorf("error creating/updating CRDs: %w", errs) |
| 66 | + } |
| 67 | + return nil |
| 68 | +} |
| 69 | + |
| 70 | +func (m *CRDManager) getClusterForCRD(crd *apiextv1.CustomResourceDefinition) (*clusters.Cluster, error) { |
| 71 | + labelValue, ok := controller.GetLabel(crd, m.mappingLabelName) |
| 72 | + if !ok { |
| 73 | + return nil, fmt.Errorf("missing label '%s' for CRD '%s'", m.mappingLabelName, crd.Name) |
| 74 | + } |
| 75 | + |
| 76 | + cluster, ok := m.crdLabelsToClusterMappings[labelValue] |
| 77 | + if !ok { |
| 78 | + return nil, fmt.Errorf("no cluster mapping found for label value '%s' in CRD '%s'", labelValue, crd.Name) |
| 79 | + } |
| 80 | + |
| 81 | + return cluster, nil |
| 82 | +} |
| 83 | + |
| 84 | +// CRDsFromFileSystem reads CRDs from the specified filesystem path. |
| 85 | +func CRDsFromFileSystem(fsys fs.FS, path string) ([]*apiextv1.CustomResourceDefinition, error) { |
| 86 | + var crds []*apiextv1.CustomResourceDefinition |
| 87 | + |
| 88 | + entries, err := fs.ReadDir(fsys, path) |
| 89 | + if err != nil { |
| 90 | + return nil, fmt.Errorf("failed to read directory %s: %w", path, err) |
| 91 | + } |
| 92 | + |
| 93 | + for _, entry := range entries { |
| 94 | + if entry.IsDir() { |
| 95 | + continue |
| 96 | + } |
| 97 | + |
| 98 | + filePath := filepath.Join(path, entry.Name()) |
| 99 | + data, err := fs.ReadFile(fsys, filePath) |
| 100 | + if err != nil { |
| 101 | + return nil, fmt.Errorf("failed to read file %s: %w", filePath, err) |
| 102 | + } |
| 103 | + |
| 104 | + var crd apiextv1.CustomResourceDefinition |
| 105 | + if err := yaml.Unmarshal(data, &crd); err != nil { |
| 106 | + return nil, fmt.Errorf("failed to unmarshal CRD from file %s: %w", filePath, err) |
| 107 | + } |
| 108 | + |
| 109 | + crds = append(crds, &crd) |
| 110 | + } |
| 111 | + |
| 112 | + return crds, nil |
| 113 | +} |
0 commit comments