/
cloudprofile.go
49 lines (40 loc) · 1.54 KB
/
cloudprofile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package validator
import (
"context"
"fmt"
extensionswebhook "github.com/gardener/gardener/extensions/pkg/webhook"
"github.com/gardener/gardener/pkg/apis/core"
metalvalidation "github.com/metal-stack/gardener-extension-provider-metal/pkg/apis/metal/validation"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/util/validation/field"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// NewCloudProfileValidator returns a new instance of a cloud profile validator.
func NewCloudProfileValidator() extensionswebhook.Validator {
return &cloudProfile{}
}
type cloudProfile struct {
decoder runtime.Decoder
}
// InjectScheme injects the given scheme into the validator.
func (cp *cloudProfile) InjectScheme(scheme *runtime.Scheme) error {
cp.decoder = serializer.NewCodecFactory(scheme).UniversalDecoder()
return nil
}
// Validate validates the given cloud profile objects.
func (cp *cloudProfile) Validate(_ context.Context, new, _ client.Object) error {
cloudProfile, ok := new.(*core.CloudProfile)
if !ok {
return fmt.Errorf("wrong object type %T", new)
}
providerConfigPath := field.NewPath("spec").Child("providerConfig")
if cloudProfile.Spec.ProviderConfig == nil {
return field.Required(providerConfigPath, "providerConfig must be set for metal cloud profiles")
}
cpConfig, err := decodeCloudProfileConfig(cp.decoder, cloudProfile.Spec.ProviderConfig)
if err != nil {
return err
}
return metalvalidation.ValidateCloudProfileConfig(cpConfig, cloudProfile, providerConfigPath).ToAggregate()
}