github.com/spotahome/redis-operator@v1.2.4/service/k8s/util.go (about)

     1  package k8s
     2  
     3  import (
     4  	"fmt"
     5  
     6  	redisfailoverv1 "github.com/spotahome/redis-operator/api/redisfailover/v1"
     7  	"github.com/spotahome/redis-operator/metrics"
     8  	"k8s.io/apimachinery/pkg/api/errors"
     9  )
    10  
    11  // GetRedisPassword retreives password from kubernetes secret or, if
    12  // unspecified, returns a blank string
    13  func GetRedisPassword(s Services, rf *redisfailoverv1.RedisFailover) (string, error) {
    14  
    15  	if rf.Spec.Auth.SecretPath == "" {
    16  		// no auth settings specified, return blank password
    17  		return "", nil
    18  	}
    19  
    20  	secret, err := s.GetSecret(rf.ObjectMeta.Namespace, rf.Spec.Auth.SecretPath)
    21  	if err != nil {
    22  		return "", err
    23  	}
    24  
    25  	if password, ok := secret.Data["password"]; ok {
    26  		return string(password), nil
    27  	}
    28  
    29  	return "", fmt.Errorf("secret \"%s\" does not have a password field", rf.Spec.Auth.SecretPath)
    30  }
    31  
    32  func recordMetrics(namespace string, kind string, object string, operation string, err error, metricsRecorder metrics.Recorder) {
    33  	if nil == err {
    34  		metricsRecorder.RecordK8sOperation(namespace, kind, object, operation, metrics.SUCCESS, metrics.NOT_APPLICABLE)
    35  	} else if errors.IsForbidden(err) {
    36  		metricsRecorder.RecordK8sOperation(namespace, kind, object, operation, metrics.FAIL, metrics.K8S_FORBIDDEN_ERR)
    37  	} else if errors.IsUnauthorized(err) {
    38  		metricsRecorder.RecordK8sOperation(namespace, kind, object, operation, metrics.FAIL, metrics.K8S_UNAUTH)
    39  	} else if errors.IsNotFound(err) {
    40  		metricsRecorder.RecordK8sOperation(namespace, kind, object, operation, metrics.FAIL, metrics.K8S_NOT_FOUND)
    41  	} else {
    42  		metricsRecorder.RecordK8sOperation(namespace, kind, object, operation, metrics.FAIL, metrics.K8S_MISC)
    43  	}
    44  }