...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package preflight
16
17 import (
18 "context"
19 "fmt"
20
21 corev1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/apis/core/v1beta1"
22 ctrl "sigs.k8s.io/controller-runtime"
23 "sigs.k8s.io/controller-runtime/pkg/client"
24 "sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative"
25 )
26
27 var (
28 nlog = ctrl.Log.WithName("NameChecker")
29 )
30
31 type NameChecker struct {
32 allowedName string
33 client client.Client
34 }
35
36 func NewNameChecker(client client.Client, allowedName string) *NameChecker {
37 return &NameChecker{allowedName: allowedName, client: client}
38 }
39
40 func (n *NameChecker) Preflight(ctx context.Context, o declarative.DeclarativeObject) error {
41 nlog.Info("preflight check before reconciling the object", "kind", o.GetObjectKind().GroupVersionKind().Kind, "name", o.GetName(), "namespace", o.GetNamespace())
42 if o.GetName() != n.allowedName {
43 switch v := o.(type) {
44 case *corev1beta1.ConfigConnector:
45 return fmt.Errorf("the only allowed name for ConfigConnector object is '%v'. The name restriction is required to ensure that there is only one ConfigConnector instance in your cluster", n.allowedName)
46 case *corev1beta1.ConfigConnectorContext:
47 return fmt.Errorf("the only allowed name for ConfigConnectorContext object is '%v'. The name restriction is required to ensure that there is only one ConfigConnectorContext instance in your namespace", n.allowedName)
48 default:
49 return fmt.Errorf("unrecongized type %v", v)
50 }
51 }
52 return nil
53 }
54
View as plain text