...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s/errors.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  	"errors"
    19  	"fmt"
    20  
    21  	"k8s.io/apimachinery/pkg/runtime/schema"
    22  	"k8s.io/apimachinery/pkg/types"
    23  )
    24  
    25  type ErrorWithReason struct {
    26  	Message string
    27  	Reason  string
    28  }
    29  
    30  func (e ErrorWithReason) Error() string {
    31  	return fmt.Sprintf("%v: %v", e.Reason, e.Message)
    32  }
    33  
    34  type ReferenceNotReadyError struct {
    35  	RefResourceGVK schema.GroupVersionKind
    36  	RefResource    types.NamespacedName
    37  }
    38  
    39  func (e *ReferenceNotReadyError) Error() string {
    40  	return fmt.Sprintf("reference %v %v is not ready", e.RefResourceGVK.Kind, e.RefResource)
    41  }
    42  
    43  func NewReferenceNotReadyErrorForResource(r *Resource) *ReferenceNotReadyError {
    44  	return &ReferenceNotReadyError{
    45  		r.GroupVersionKind(),
    46  		types.NamespacedName{Namespace: r.GetNamespace(), Name: r.GetName()},
    47  	}
    48  }
    49  
    50  func AsReferenceNotReadyError(err error) (unwrappedErr *ReferenceNotReadyError, ok bool) {
    51  	ok = errors.As(err, &unwrappedErr)
    52  	return unwrappedErr, ok
    53  }
    54  
    55  type ReferenceNotFoundError struct {
    56  	RefResourceGVK schema.GroupVersionKind
    57  	RefResource    types.NamespacedName
    58  }
    59  
    60  func (e *ReferenceNotFoundError) Error() string {
    61  	return fmt.Sprintf("reference %v %v is not found", e.RefResourceGVK.Kind, e.RefResource)
    62  }
    63  
    64  func NewReferenceNotFoundError(refResourceGVK schema.GroupVersionKind, refResource types.NamespacedName) *ReferenceNotFoundError {
    65  	return &ReferenceNotFoundError{refResourceGVK, refResource}
    66  }
    67  
    68  func NewReferenceNotFoundErrorForResource(r *Resource) *ReferenceNotFoundError {
    69  	return &ReferenceNotFoundError{
    70  		r.GroupVersionKind(),
    71  		types.NamespacedName{Namespace: r.GetNamespace(), Name: r.GetName()},
    72  	}
    73  }
    74  
    75  func IsReferenceNotFoundError(err error) bool {
    76  	_, ok := AsReferenceNotFoundError(err)
    77  	return ok
    78  }
    79  
    80  func AsReferenceNotFoundError(err error) (unwrappedErr *ReferenceNotFoundError, ok bool) {
    81  	ok = errors.As(err, &unwrappedErr)
    82  	return unwrappedErr, ok
    83  }
    84  
    85  type SecretNotFoundError struct {
    86  	Secret types.NamespacedName
    87  }
    88  
    89  func (e *SecretNotFoundError) Error() string {
    90  	return fmt.Sprintf("Secret %v was not found", e.Secret)
    91  }
    92  
    93  func NewSecretNotFoundError(secret types.NamespacedName) *SecretNotFoundError {
    94  	return &SecretNotFoundError{secret}
    95  }
    96  
    97  func AsSecretNotFoundError(err error) (unwrappedErr *SecretNotFoundError, ok bool) {
    98  	ok = errors.As(err, &unwrappedErr)
    99  	return unwrappedErr, ok
   100  }
   101  
   102  type KeyInSecretNotFoundError struct {
   103  	key    string
   104  	secret types.NamespacedName
   105  }
   106  
   107  func (e *KeyInSecretNotFoundError) Error() string {
   108  	return fmt.Sprintf("key '%v' was not found in Secret %v", e.key, e.secret)
   109  }
   110  
   111  func NewKeyInSecretNotFoundError(key string, secret types.NamespacedName) *KeyInSecretNotFoundError {
   112  	return &KeyInSecretNotFoundError{key, secret}
   113  }
   114  
   115  func AsKeyInSecretNotFoundError(err error) (unwrappedErr *KeyInSecretNotFoundError, ok bool) {
   116  	ok = errors.As(err, &unwrappedErr)
   117  	return unwrappedErr, ok
   118  }
   119  
   120  type TransitiveDependencyNotFoundError struct {
   121  	ResourceGVK schema.GroupVersionKind
   122  	Resource    types.NamespacedName
   123  }
   124  
   125  func (e *TransitiveDependencyNotFoundError) Error() string {
   126  	return fmt.Sprintf("transitive dependency %v %v is not found", e.ResourceGVK.Kind, e.Resource)
   127  }
   128  
   129  func NewTransitiveDependencyNotFoundError(resourceGVK schema.GroupVersionKind, resource types.NamespacedName) *TransitiveDependencyNotFoundError {
   130  	return &TransitiveDependencyNotFoundError{resourceGVK, resource}
   131  }
   132  
   133  func AsTransitiveDependencyNotFoundError(err error) (unwrappedErr *TransitiveDependencyNotFoundError, ok bool) {
   134  	ok = errors.As(err, &unwrappedErr)
   135  	return unwrappedErr, ok
   136  }
   137  
   138  type TransitiveDependencyNotReadyError struct {
   139  	ResourceGVK schema.GroupVersionKind
   140  	Resource    types.NamespacedName
   141  }
   142  
   143  func (e *TransitiveDependencyNotReadyError) Error() string {
   144  	return fmt.Sprintf("transitive dependency %v %v is not ready", e.ResourceGVK.Kind, e.Resource)
   145  }
   146  
   147  func NewTransitiveDependencyNotReadyError(resourceGVK schema.GroupVersionKind, resource types.NamespacedName) *TransitiveDependencyNotReadyError {
   148  	return &TransitiveDependencyNotReadyError{resourceGVK, resource}
   149  }
   150  
   151  func AsTransitiveDependencyNotReadyError(err error) (unwrappedErr *TransitiveDependencyNotReadyError, ok bool) {
   152  	ok = errors.As(err, &unwrappedErr)
   153  	return unwrappedErr, ok
   154  }
   155  
   156  type ServerGeneratedIDNotFoundError struct {
   157  	resourceGVK schema.GroupVersionKind
   158  	resource    types.NamespacedName
   159  }
   160  
   161  func (e *ServerGeneratedIDNotFoundError) Error() string {
   162  	return fmt.Sprintf("the resource %v %v has a server-generated ID that has not yet been set",
   163  		e.resourceGVK.Kind, e.resource)
   164  }
   165  
   166  func NewServerGeneratedIDNotFoundError(resourceGVK schema.GroupVersionKind, resource types.NamespacedName) *ServerGeneratedIDNotFoundError {
   167  	return &ServerGeneratedIDNotFoundError{resourceGVK, resource}
   168  }
   169  
   170  func AsServerGeneratedIDNotFoundError(err error) (unwrappedErr *ServerGeneratedIDNotFoundError, ok bool) {
   171  	ok = errors.As(err, &unwrappedErr)
   172  	return unwrappedErr, ok
   173  }
   174  
   175  type ResourceIDNotFoundError struct {
   176  	resourceGVK schema.GroupVersionKind
   177  	resource    types.NamespacedName
   178  }
   179  
   180  func (e *ResourceIDNotFoundError) Error() string {
   181  	return fmt.Sprintf("'%s' is not set in %v %v", ResourceIDFieldPath,
   182  		e.resourceGVK.Kind, e.resource)
   183  }
   184  
   185  func NewResourceIDNotFoundError(resourceGVK schema.GroupVersionKind, resource types.NamespacedName) *ResourceIDNotFoundError {
   186  	return &ResourceIDNotFoundError{resourceGVK, resource}
   187  }
   188  
   189  func AsResourceIDNotFoundError(err error) (unwrappedErr *ResourceIDNotFoundError, ok bool) {
   190  	ok = errors.As(err, &unwrappedErr)
   191  	return unwrappedErr, ok
   192  }
   193  
   194  type ImmutableFieldsMutationError struct {
   195  	immutableFields []string
   196  }
   197  
   198  func (e *ImmutableFieldsMutationError) Error() string {
   199  	return fmt.Sprintf("cannot make changes to immutable field(s): %v; please refer to our troubleshooting doc: https://cloud.google.com/config-connector/docs/troubleshooting", e.immutableFields)
   200  }
   201  
   202  func NewImmutableFieldsMutationError(immutableFields []string) *ImmutableFieldsMutationError {
   203  	return &ImmutableFieldsMutationError{immutableFields}
   204  }
   205  

View as plain text