...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/preflight/configconnectorcontextchecker.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  	"github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/k8s"
    23  
    24  	ctrl "sigs.k8s.io/controller-runtime"
    25  	"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative"
    26  )
    27  
    28  var (
    29  	clog = ctrl.Log.WithName("ConfigConnectorContextChecker")
    30  )
    31  
    32  type ConfigConnectorContextChecker struct {
    33  }
    34  
    35  func NewConfigConnectorContextChecker() *ConfigConnectorContextChecker {
    36  	return &ConfigConnectorContextChecker{}
    37  }
    38  
    39  func (c *ConfigConnectorContextChecker) Preflight(ctx context.Context, o declarative.DeclarativeObject) error {
    40  	clog.Info("preflight check before reconciling the object", "kind", o.GetObjectKind().GroupVersionKind().Kind, "name", o.GetName(), "namespace", o.GetNamespace())
    41  
    42  	ccc, ok := o.(*corev1beta1.ConfigConnectorContext)
    43  	if !ok {
    44  		return fmt.Errorf("expected the resource to be a ConfigConnectorContext, but it was not. Object: %v", o)
    45  	}
    46  
    47  	if ccc.GetRequestProjectPolicy() != k8s.BillingProjectPolicy && ccc.Spec.BillingProject != "" {
    48  		return fmt.Errorf("spec.billingProject cannot be set if spec.requestProjectPolicy is not set to %v", k8s.BillingProjectPolicy)
    49  	}
    50  
    51  	if ccc.GetRequestProjectPolicy() == k8s.BillingProjectPolicy && ccc.Spec.BillingProject == "" {
    52  		return fmt.Errorf("spec.billingProject must be set if spec.requestProjectPolicy is set to %v", k8s.BillingProjectPolicy)
    53  	}
    54  
    55  	return nil
    56  }
    57  

View as plain text