forked from open-telemetry/opentelemetry-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopampbridge_webhook.go
144 lines (124 loc) · 4.96 KB
/
opampbridge_webhook.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package v1alpha1
import (
"context"
"fmt"
"strings"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"github.com/open-telemetry/opentelemetry-operator/internal/config"
)
var (
_ admission.CustomValidator = &OpAMPBridgeWebhook{}
_ admission.CustomDefaulter = &OpAMPBridgeWebhook{}
)
//+kubebuilder:webhook:path=/mutate-opentelemetry-io-v1alpha1-opampbridge,mutating=true,failurePolicy=fail,sideEffects=None,groups=opentelemetry.io,resources=opampbridges,verbs=create;update,versions=v1alpha1,name=mopampbridge.kb.io,admissionReviewVersions=v1
//+kubebuilder:webhook:path=/validate-opentelemetry-io-v1alpha1-opampbridge,mutating=false,failurePolicy=fail,sideEffects=None,groups=opentelemetry.io,resources=opampbridges,verbs=create;update,versions=v1alpha1,name=vopampbridgecreateupdate.kb.io,admissionReviewVersions=v1
//+kubebuilder:webhook:path=/validate-opentelemetry-io-v1alpha1-opampbridge,mutating=false,failurePolicy=ignore,sideEffects=None,groups=opentelemetry.io,resources=opampbridges,verbs=delete,versions=v1alpha1,name=vopampbridgedelete.kb.io,admissionReviewVersions=v1
//+kubebuilder:object:generate=false
type OpAMPBridgeWebhook struct {
logger logr.Logger
cfg config.Config
scheme *runtime.Scheme
}
func (o *OpAMPBridgeWebhook) Default(ctx context.Context, obj runtime.Object) error {
opampBridge, ok := obj.(*OpAMPBridge)
if !ok {
return fmt.Errorf("expected an OpAMPBridge, received %T", obj)
}
return o.defaulter(opampBridge)
}
func (o *OpAMPBridgeWebhook) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
opampBridge, ok := obj.(*OpAMPBridge)
if !ok {
return nil, fmt.Errorf("expected an OpAMPBridge, received %T", obj)
}
return o.validate(opampBridge)
}
func (o *OpAMPBridgeWebhook) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) {
opampBridge, ok := newObj.(*OpAMPBridge)
if !ok {
return nil, fmt.Errorf("expected an OpAMPBridge, received %T", newObj)
}
return o.validate(opampBridge)
}
func (o *OpAMPBridgeWebhook) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
opampBridge, ok := obj.(*OpAMPBridge)
if !ok || opampBridge == nil {
return nil, fmt.Errorf("expected an OpAMPBridge, received %T", obj)
}
return o.validate(opampBridge)
}
func (o *OpAMPBridgeWebhook) defaulter(r *OpAMPBridge) error {
if len(r.Spec.UpgradeStrategy) == 0 {
r.Spec.UpgradeStrategy = UpgradeStrategyAutomatic
}
if r.Labels == nil {
r.Labels = map[string]string{}
}
one := int32(1)
if r.Spec.Replicas == nil {
r.Spec.Replicas = &one
}
// ReportsStatus Capability must be set
if r.Spec.Capabilities == nil {
r.Spec.Capabilities = make(map[OpAMPBridgeCapability]bool)
}
enabled, found := r.Spec.Capabilities[OpAMPBridgeCapabilityReportsStatus]
if !enabled || !found {
r.Spec.Capabilities[OpAMPBridgeCapabilityReportsStatus] = true
}
return nil
}
func (o *OpAMPBridgeWebhook) validate(r *OpAMPBridge) (admission.Warnings, error) {
warnings := admission.Warnings{}
// validate OpAMP server endpoint
if len(strings.TrimSpace(r.Spec.Endpoint)) == 0 {
return warnings, fmt.Errorf("the OpAMP server endpoint is not specified")
}
// validate OpAMPBridge capabilities
if len(r.Spec.Capabilities) == 0 {
return warnings, fmt.Errorf("the capabilities supported by OpAMP Bridge are not specified")
}
// validate port config
for _, p := range r.Spec.Ports {
nameErrs := validation.IsValidPortName(p.Name)
numErrs := validation.IsValidPortNum(int(p.Port))
if len(nameErrs) > 0 || len(numErrs) > 0 {
return warnings, fmt.Errorf("the OpAMPBridge Spec Ports configuration is incorrect, port name '%s' errors: %s, num '%d' errors: %s",
p.Name, nameErrs, p.Port, numErrs)
}
}
// check for maximum replica count
if r.Spec.Replicas != nil && *r.Spec.Replicas > 1 {
return warnings, fmt.Errorf("replica count must not be greater than 1")
}
return warnings, nil
}
func SetupOpAMPBridgeWebhook(mgr ctrl.Manager, cfg config.Config) error {
webhook := &OpAMPBridgeWebhook{
logger: mgr.GetLogger().WithValues("handler", "OpAMPBridgeWebhook"),
scheme: mgr.GetScheme(),
cfg: cfg,
}
return ctrl.NewWebhookManagedBy(mgr).
For(&OpAMPBridge{}).
WithValidator(webhook).
WithDefaulter(webhook).
Complete()
}