...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/preflight/namecheck.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/preflight

     1  // Copyright 2022 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    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