...

Source file src/k8s.io/kubernetes/pkg/volume/util/types/types.go

Documentation: k8s.io/kubernetes/pkg/volume/util/types

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // Package types defines types used only by volume components
    18  package types
    19  
    20  import (
    21  	"errors"
    22  
    23  	"k8s.io/apimachinery/pkg/types"
    24  	"k8s.io/apimachinery/pkg/util/runtime"
    25  	"k8s.io/mount-utils"
    26  )
    27  
    28  // UniquePodName defines the type to key pods off of
    29  type UniquePodName types.UID
    30  
    31  // UniquePVCName defines the type to key pvc off
    32  type UniquePVCName types.UID
    33  
    34  // GeneratedOperations contains the operation that is created as well as
    35  // supporting functions required for the operation executor
    36  type GeneratedOperations struct {
    37  	// Name of operation - could be used for resetting shared exponential backoff
    38  	OperationName     string
    39  	OperationFunc     func() (context OperationContext)
    40  	EventRecorderFunc func(*error)
    41  	CompleteFunc      func(CompleteFuncParam)
    42  }
    43  
    44  type OperationContext struct {
    45  	EventErr    error
    46  	DetailedErr error
    47  	Migrated    bool
    48  }
    49  
    50  func NewOperationContext(eventErr, detailedErr error, migrated bool) OperationContext {
    51  	return OperationContext{
    52  		EventErr:    eventErr,
    53  		DetailedErr: detailedErr,
    54  		Migrated:    migrated,
    55  	}
    56  }
    57  
    58  type CompleteFuncParam struct {
    59  	Err      *error
    60  	Migrated *bool
    61  }
    62  
    63  // Run executes the operations and its supporting functions
    64  func (o *GeneratedOperations) Run() (eventErr, detailedErr error) {
    65  	var context OperationContext
    66  	if o.CompleteFunc != nil {
    67  		c := CompleteFuncParam{
    68  			Err:      &context.DetailedErr,
    69  			Migrated: &context.Migrated,
    70  		}
    71  		defer o.CompleteFunc(c)
    72  	}
    73  	if o.EventRecorderFunc != nil {
    74  		defer o.EventRecorderFunc(&eventErr)
    75  	}
    76  	// Handle panic, if any, from operationFunc()
    77  	defer runtime.RecoverFromPanic(&detailedErr)
    78  
    79  	context = o.OperationFunc()
    80  	return context.EventErr, context.DetailedErr
    81  }
    82  
    83  // FailedPrecondition error indicates CSI operation returned failed precondition
    84  // error
    85  type FailedPrecondition struct {
    86  	msg string
    87  }
    88  
    89  func (err *FailedPrecondition) Error() string {
    90  	return err.msg
    91  }
    92  
    93  // NewFailedPreconditionError returns a new FailedPrecondition error instance
    94  func NewFailedPreconditionError(msg string) *FailedPrecondition {
    95  	return &FailedPrecondition{msg: msg}
    96  }
    97  
    98  // IsFailedPreconditionError checks if given error is of type that indicates
    99  // operation failed with precondition
   100  func IsFailedPreconditionError(err error) bool {
   101  	var failedPreconditionError *FailedPrecondition
   102  	return errors.As(err, &failedPreconditionError)
   103  }
   104  
   105  type OperationNotSupported struct {
   106  	msg string
   107  }
   108  
   109  func (err *OperationNotSupported) Error() string {
   110  	return err.msg
   111  }
   112  
   113  func NewOperationNotSupportedError(msg string) *OperationNotSupported {
   114  	return &OperationNotSupported{msg: msg}
   115  }
   116  
   117  func IsOperationNotSupportedError(err error) bool {
   118  	var operationNotSupportedError *OperationNotSupported
   119  	return errors.As(err, &operationNotSupportedError)
   120  }
   121  
   122  // TransientOperationFailure indicates operation failed with a transient error
   123  // and may fix itself when retried.
   124  type TransientOperationFailure struct {
   125  	msg string
   126  }
   127  
   128  func (err *TransientOperationFailure) Error() string {
   129  	return err.msg
   130  }
   131  
   132  // NewTransientOperationFailure creates an instance of TransientOperationFailure error
   133  func NewTransientOperationFailure(msg string) *TransientOperationFailure {
   134  	return &TransientOperationFailure{msg: msg}
   135  }
   136  
   137  // UncertainProgressError indicates operation failed with a non-final error
   138  // and operation may be in-progress in background.
   139  type UncertainProgressError struct {
   140  	msg string
   141  }
   142  
   143  func (err *UncertainProgressError) Error() string {
   144  	return err.msg
   145  }
   146  
   147  // NewUncertainProgressError creates an instance of UncertainProgressError type
   148  func NewUncertainProgressError(msg string) *UncertainProgressError {
   149  	return &UncertainProgressError{msg: msg}
   150  }
   151  
   152  // IsOperationFinishedError checks if given error is of type that indicates
   153  // operation is finished with a FINAL error.
   154  func IsOperationFinishedError(err error) bool {
   155  	if _, ok := err.(*UncertainProgressError); ok {
   156  		return false
   157  	}
   158  	if _, ok := err.(*TransientOperationFailure); ok {
   159  		return false
   160  	}
   161  	return true
   162  }
   163  
   164  // IsFilesystemMismatchError checks if mount failed because requested filesystem
   165  // on PVC and actual filesystem on disk did not match
   166  func IsFilesystemMismatchError(err error) bool {
   167  	mountError := mount.MountError{}
   168  	return errors.As(err, &mountError) && mountError.Type == mount.FilesystemMismatch
   169  }
   170  
   171  // IsUncertainProgressError checks if given error is of type that indicates
   172  // operation might be in-progress in background.
   173  func IsUncertainProgressError(err error) bool {
   174  	if _, ok := err.(*UncertainProgressError); ok {
   175  		return true
   176  	}
   177  	return false
   178  }
   179  
   180  const (
   181  	// VolumeResizerKey is key that will be used to store resizer used
   182  	// for resizing PVC. The generated key/value pair will be added
   183  	// as a annotation to the PVC.
   184  	VolumeResizerKey = "volume.kubernetes.io/storage-resizer"
   185  )
   186  

View as plain text