...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s/errorclient.go

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

     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 k8s
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"k8s.io/apimachinery/pkg/api/meta"
    22  
    23  	"k8s.io/apimachinery/pkg/runtime"
    24  	"sigs.k8s.io/controller-runtime/pkg/client"
    25  )
    26  
    27  type errorClient struct {
    28  }
    29  
    30  // Some packages, like 'gcpclient' make use of krmtotf which is tightly coupled with the controller-runtime client.
    31  // However, krmtotf does not actually need the client if all the resources passed in have all references resolved, etc.
    32  // To enable usage of the library but also to avoid panics, this erroring client can be passed to krmtotf methods.
    33  func NewErroringClient() client.Client {
    34  	return &errorClient{}
    35  }
    36  
    37  func (e *errorClient) Get(_ context.Context, key client.ObjectKey, _ client.Object, _ ...client.GetOption) error {
    38  	return fmt.Errorf("unexpected call to client.Get(...) for %v", key)
    39  }
    40  
    41  func (e *errorClient) List(_ context.Context, _ client.ObjectList, _ ...client.ListOption) error {
    42  	return fmt.Errorf("unexpected call to client.List(...)")
    43  }
    44  
    45  func (e *errorClient) Create(_ context.Context, obj client.Object, _ ...client.CreateOption) error {
    46  	return fmt.Errorf("unexpected call to client.Create(...) for object with kind %v", obj.GetObjectKind())
    47  }
    48  
    49  func (e *errorClient) Delete(_ context.Context, obj client.Object, _ ...client.DeleteOption) error {
    50  	return fmt.Errorf("unexpected call to client.Delete(...) for object with kind %v", obj.GetObjectKind())
    51  }
    52  
    53  func (e *errorClient) Update(_ context.Context, obj client.Object, _ ...client.UpdateOption) error {
    54  	return fmt.Errorf("unexpected call to client.Update(...) for object with kind %v", obj.GetObjectKind())
    55  }
    56  
    57  func (e *errorClient) Patch(_ context.Context, obj client.Object, _ client.Patch, _ ...client.PatchOption) error {
    58  	return fmt.Errorf("unexpected call to client.Patch(...) for object with kind %v", obj.GetObjectKind())
    59  }
    60  
    61  func (e *errorClient) DeleteAllOf(_ context.Context, obj client.Object, _ ...client.DeleteAllOfOption) error {
    62  	return fmt.Errorf("unexpected call to client.DeleteAllOf(...) for object with kind %v", obj.GetObjectKind())
    63  }
    64  
    65  func (e *errorClient) Scheme() *runtime.Scheme {
    66  	panic("unexpected call to client.Scheme(...)")
    67  }
    68  
    69  func (e *errorClient) RESTMapper() meta.RESTMapper {
    70  	panic("unexpected call to client.RESTMapper(...)")
    71  }
    72  
    73  func (e *errorClient) Status() client.StatusWriter {
    74  	return e
    75  }
    76  

View as plain text