-
Notifications
You must be signed in to change notification settings - Fork 96
/
cloudprofile.go
51 lines (41 loc) · 1.61 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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package validator
import (
"context"
"fmt"
extensionswebhook "github.com/gardener/gardener/extensions/pkg/webhook"
"github.com/gardener/gardener/pkg/apis/core"
"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"
awsvalidation "github.com/gardener/gardener-extension-provider-aws/pkg/apis/aws/validation"
)
// 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, _ 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 AWS cloud profiles")
}
cpConfig, err := decodeCloudProfileConfig(cp.decoder, cloudProfile.Spec.ProviderConfig)
if err != nil {
return err
}
return awsvalidation.ValidateCloudProfileConfig(cpConfig, providerConfigPath).ToAggregate()
}