|
| 1 | +// Copyright 2024 |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package providers |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "os" |
| 20 | + "path/filepath" |
| 21 | + "slices" |
| 22 | + |
| 23 | + "gopkg.in/yaml.v3" |
| 24 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 25 | +) |
| 26 | + |
| 27 | +// GVK represents the GroupVersionKind structure in YAML. |
| 28 | +type GVK struct { |
| 29 | + Group string `yaml:"group"` |
| 30 | + Version string `yaml:"version"` |
| 31 | + Kind string `yaml:"kind"` |
| 32 | +} |
| 33 | + |
| 34 | +// YAMLProviderDefinition represents a YAML-based provider configuration. |
| 35 | +type YAMLProviderDefinition struct { |
| 36 | + Name string `yaml:"name"` |
| 37 | + ClusterGVK GVK `yaml:"clusterGVK"` |
| 38 | + ClusterIdentityKinds []string `yaml:"clusterIdentityKinds"` |
| 39 | +} |
| 40 | + |
| 41 | +var _ ProviderModule = (*YAMLProviderDefinition)(nil) |
| 42 | + |
| 43 | +func (p *YAMLProviderDefinition) GetName() string { |
| 44 | + return p.Name |
| 45 | +} |
| 46 | + |
| 47 | +func (p *YAMLProviderDefinition) GetClusterGVK() schema.GroupVersionKind { |
| 48 | + return schema.GroupVersionKind{ |
| 49 | + Group: p.ClusterGVK.Group, |
| 50 | + Version: p.ClusterGVK.Version, |
| 51 | + Kind: p.ClusterGVK.Kind, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func (p *YAMLProviderDefinition) GetClusterIdentityKinds() []string { |
| 56 | + return slices.Clone(p.ClusterIdentityKinds) |
| 57 | +} |
| 58 | + |
| 59 | +// RegisterFromYAML registers a provider from a YAML file. |
| 60 | +func RegisterFromYAML(yamlFile string) error { |
| 61 | + data, err := os.ReadFile(yamlFile) |
| 62 | + if err != nil { |
| 63 | + return fmt.Errorf("failed to read YAML file: %w", err) |
| 64 | + } |
| 65 | + |
| 66 | + var ypd YAMLProviderDefinition |
| 67 | + |
| 68 | + if err := yaml.Unmarshal(data, &ypd); err != nil { |
| 69 | + return fmt.Errorf("failed to unmarshal YAML: %w", err) |
| 70 | + } |
| 71 | + |
| 72 | + Register(&ypd) |
| 73 | + |
| 74 | + return nil |
| 75 | +} |
| 76 | + |
| 77 | +// RegisterProvidersFromGlob loads and registers provider YAML files matching the glob pattern. |
| 78 | +func RegisterProvidersFromGlob(pattern string) error { |
| 79 | + matches, err := filepath.Glob(pattern) |
| 80 | + if err != nil { |
| 81 | + return fmt.Errorf("failed to glob pattern %q: %w", pattern, err) |
| 82 | + } |
| 83 | + |
| 84 | + for _, file := range matches { |
| 85 | + if err := RegisterFromYAML(file); err != nil { |
| 86 | + return fmt.Errorf("provider %s: %w", filepath.Base(file), err) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return nil |
| 91 | +} |
0 commit comments