...

Source file src/k8s.io/kubernetes/pkg/volume/metrics_errors.go

Documentation: k8s.io/kubernetes/pkg/volume

     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 volume
    18  
    19  import (
    20  	"fmt"
    21  )
    22  
    23  const (
    24  	// ErrCodeNotSupported code for NotSupported Errors.
    25  	ErrCodeNotSupported int = iota + 1
    26  	ErrCodeNoPathDefined
    27  	ErrCodeFsInfoFailed
    28  )
    29  
    30  // NewNotSupportedError creates a new MetricsError with code NotSupported.
    31  func NewNotSupportedError() *MetricsError {
    32  	return &MetricsError{
    33  		Code: ErrCodeNotSupported,
    34  		Msg:  "metrics are not supported for MetricsNil Volumes",
    35  	}
    36  }
    37  
    38  // NewNotImplementedError creates a new MetricsError with code NotSupported.
    39  func NewNotImplementedError(reason string) *MetricsError {
    40  	return &MetricsError{
    41  		Code: ErrCodeNotSupported,
    42  		Msg:  fmt.Sprintf("metrics support is not implemented: %s", reason),
    43  	}
    44  }
    45  
    46  // NewNotSupportedErrorWithDriverName creates a new MetricsError with code NotSupported.
    47  // driver name is added to the error message.
    48  func NewNotSupportedErrorWithDriverName(name string) *MetricsError {
    49  	return &MetricsError{
    50  		Code: ErrCodeNotSupported,
    51  		Msg:  fmt.Sprintf("metrics are not supported for %s volumes", name),
    52  	}
    53  }
    54  
    55  // NewNoPathDefinedError creates a new MetricsError with code NoPathDefined.
    56  func NewNoPathDefinedError() *MetricsError {
    57  	return &MetricsError{
    58  		Code: ErrCodeNoPathDefined,
    59  		Msg:  "no path defined for disk usage metrics.",
    60  	}
    61  }
    62  
    63  // NewFsInfoFailedError creates a new MetricsError with code FsInfoFailed.
    64  func NewFsInfoFailedError(err error) *MetricsError {
    65  	return &MetricsError{
    66  		Code: ErrCodeFsInfoFailed,
    67  		Msg:  fmt.Sprintf("failed to get FsInfo due to error %v", err),
    68  	}
    69  }
    70  
    71  // MetricsError to distinguish different Metrics Errors.
    72  type MetricsError struct {
    73  	Code int
    74  	Msg  string
    75  }
    76  
    77  func (e *MetricsError) Error() string {
    78  	return e.Msg
    79  }
    80  
    81  // IsNotSupported returns true if and only if err is "key" not found error.
    82  func IsNotSupported(err error) bool {
    83  	return isErrCode(err, ErrCodeNotSupported)
    84  }
    85  
    86  func isErrCode(err error, code int) bool {
    87  	if err == nil {
    88  		return false
    89  	}
    90  	if e, ok := err.(*MetricsError); ok {
    91  		return e.Code == code
    92  	}
    93  	return false
    94  }
    95  

View as plain text