-
Notifications
You must be signed in to change notification settings - Fork 11
/
cloudprofile.go
70 lines (58 loc) · 2.24 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package validator
import (
"context"
"fmt"
extensionswebhook "github.com/gardener/gardener/extensions/pkg/webhook"
"github.com/gardener/gardener/pkg/apis/core"
"github.com/metal-stack/gardener-extension-provider-metal/pkg/apis/metal"
"github.com/metal-stack/gardener-extension-provider-metal/pkg/apis/metal/helper"
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"
"sigs.k8s.io/controller-runtime/pkg/manager"
)
// NewCloudProfileValidator returns a new instance of a cloud profile validator.
func NewCloudProfileValidator(mgr manager.Manager) extensionswebhook.Validator {
return &cloudProfile{
decoder: serializer.NewCodecFactory(mgr.GetScheme(), serializer.EnableStrict).UniversalDecoder(),
}
}
type cloudProfile struct {
decoder runtime.Decoder
}
// Validate validates the given cloud profile objects.
func (cp *cloudProfile) Validate(_ context.Context, new, old 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 := &metal.CloudProfileConfig{}
err := helper.DecodeRawExtension(cloudProfile.Spec.ProviderConfig, cpConfig, cp.decoder)
if err != nil {
return err
}
errs := metalvalidation.ValidateCloudProfileConfig(cpConfig, cloudProfile, providerConfigPath)
if old == nil {
return errs.ToAggregate()
}
oldCloudProfile, ok := old.(*core.CloudProfile)
if !ok {
return fmt.Errorf("wrong old object type %T", new)
}
if oldCloudProfile.Spec.ProviderConfig == nil {
return errs.ToAggregate()
}
oldCpConfig := &metal.CloudProfileConfig{}
err = helper.DecodeRawExtension(oldCloudProfile.Spec.ProviderConfig, oldCpConfig, cp.decoder)
if err != nil {
return err
}
errs = append(errs, metalvalidation.ValidateImmutableCloudProfileConfig(cpConfig, oldCpConfig, providerConfigPath)...)
return errs.ToAggregate()
}