Skip to content

Commit

Permalink
Change scaler name references to openstackSwift
Browse files Browse the repository at this point in the history
Add objectPrefix to metricName

Signed-off-by: Pedro Felipe Dominguite <p.dominguite@sidi.org.br>
  • Loading branch information
pdominguite committed Jan 4, 2021
1 parent 3e2e504 commit 0313be5
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 82 deletions.
78 changes: 43 additions & 35 deletions pkg/scalers/openstack_swift_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const (
defaultHTTPClientTimeout = 30
)

type swiftMetadata struct {
type openstackSwiftMetadata struct {
swiftURL string
containerName string
objectCount int
Expand All @@ -41,7 +41,7 @@ type swiftMetadata struct {
onlyFiles bool
}

type swiftAuthenticationMetadata struct {
type openstackSwiftAuthenticationMetadata struct {
userID string
password string
projectID string
Expand All @@ -50,22 +50,22 @@ type swiftAuthenticationMetadata struct {
appCredentialSecret string
}

type swiftScaler struct {
metadata *swiftMetadata
type openstackSwiftScaler struct {
metadata *openstackSwiftMetadata
authMetadata *openstack.KeystoneAuthMetadata
}

var swiftLog = logf.Log.WithName("swift_scaler")
var openstackSwiftLog = logf.Log.WithName("openstack_swift_scaler")

func (s *swiftScaler) getSwiftContainerObjectCount() (int, error) {
func (s *openstackSwiftScaler) getOpenstackSwiftContainerObjectCount() (int, error) {
var token string
var swiftURL string = s.metadata.swiftURL
var containerName string = s.metadata.containerName

isValid, validationError := openstack.IsTokenValid(*s.authMetadata)

if validationError != nil {
swiftLog.Error(validationError, "scaler could not validate the token for authentication")
openstackSwiftLog.Error(validationError, "scaler could not validate the token for authentication")
return 0, validationError
}

Expand All @@ -74,7 +74,7 @@ func (s *swiftScaler) getSwiftContainerObjectCount() (int, error) {
token, tokenRequestError = s.authMetadata.GetToken()
s.authMetadata.AuthToken = token
if tokenRequestError != nil {
swiftLog.Error(tokenRequestError, "error requesting token for authentication")
openstackSwiftLog.Error(tokenRequestError, "error requesting token for authentication")
return 0, tokenRequestError
}
}
Expand All @@ -84,7 +84,7 @@ func (s *swiftScaler) getSwiftContainerObjectCount() (int, error) {
swiftContainerURL, err := url.Parse(swiftURL)

if err != nil {
swiftLog.Error(err, fmt.Sprintf("the swiftURL is invalid: %s. You might have forgotten to provide the either 'http' or 'https' in the URL. Check our documentation to see if you missed something", swiftURL))
openstackSwiftLog.Error(err, fmt.Sprintf("the swiftURL is invalid: %s. You might have forgotten to provide the either 'http' or 'https' in the URL. Check our documentation to see if you missed something", swiftURL))
return 0, fmt.Errorf("the swiftURL is invalid: %s", err.Error())
}

Expand All @@ -108,7 +108,7 @@ func (s *swiftScaler) getSwiftContainerObjectCount() (int, error) {
resp, requestError := s.authMetadata.HTTPClient.Do(swiftRequest)

if requestError != nil {
swiftLog.Error(requestError, fmt.Sprintf("error getting metrics for container '%s'. You probably specified the wrong swift URL or the URL is not reachable", containerName))
openstackSwiftLog.Error(requestError, fmt.Sprintf("error getting metrics for container '%s'. You probably specified the wrong swift URL or the URL is not reachable", containerName))
return 0, requestError
}

Expand All @@ -117,7 +117,7 @@ func (s *swiftScaler) getSwiftContainerObjectCount() (int, error) {
body, readError := ioutil.ReadAll(resp.Body)

if readError != nil {
swiftLog.Error(readError, "could not read response body from Swift API")
openstackSwiftLog.Error(readError, "could not read response body from Swift API")
return 0, readError
}
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
Expand All @@ -136,7 +136,7 @@ func (s *swiftScaler) getSwiftContainerObjectCount() (int, error) {
objectLimit, conversionError := strconv.Atoi(s.metadata.objectLimit)

if conversionError != nil {
swiftLog.Error(err, fmt.Sprintf("the objectLimit value provided is invalid: %v", s.metadata.objectLimit))
openstackSwiftLog.Error(err, fmt.Sprintf("the objectLimit value provided is invalid: %v", s.metadata.objectLimit))
return 0, conversionError
}

Expand All @@ -159,49 +159,49 @@ func (s *swiftScaler) getSwiftContainerObjectCount() (int, error) {
}

if resp.StatusCode == 401 {
swiftLog.Error(nil, "the retrieved token is not a valid token. Provide the correct auth credentials so the scaler can retrieve a valid access token (Unauthorized)")
openstackSwiftLog.Error(nil, "the retrieved token is not a valid token. Provide the correct auth credentials so the scaler can retrieve a valid access token (Unauthorized)")
return 0, fmt.Errorf("the retrieved token is not a valid token. Provide the correct auth credentials so the scaler can retrieve a valid access token (Unauthorized)")
}

if resp.StatusCode == 403 {
swiftLog.Error(nil, "the retrieved token is a valid token, but it does not have sufficient permission to retrieve Swift and/or container metadata (Forbidden)")
openstackSwiftLog.Error(nil, "the retrieved token is a valid token, but it does not have sufficient permission to retrieve Swift and/or container metadata (Forbidden)")
return 0, fmt.Errorf("the retrieved token is a valid token, but it does not have sufficient permission to retrieve Swift and/or container metadata (Forbidden)")
}

if resp.StatusCode == 404 {
swiftLog.Error(nil, fmt.Sprintf("the container '%s' does not exist (Not Found)", containerName))
openstackSwiftLog.Error(nil, fmt.Sprintf("the container '%s' does not exist (Not Found)", containerName))
return 0, fmt.Errorf("the container '%s' does not exist (Not Found)", containerName)
}

return 0, fmt.Errorf(string(body))
}

// NewSwiftScaler creates a new swift scaler
func NewSwiftScaler(config *ScalerConfig) (Scaler, error) {
// NewOpenstackSwiftScaler creates a new swift scaler
func NewOpenstackSwiftScaler(config *ScalerConfig) (Scaler, error) {
var keystoneAuth *openstack.KeystoneAuthMetadata

swiftMetadata, err := parseSwiftMetadata(config)
openstackSwiftMetadata, err := parseOpenstackSwiftMetadata(config)

if err != nil {
return nil, fmt.Errorf("error parsing swift metadata: %s", err)
}

authMetadata, err := parseSwiftAuthenticationMetadata(config)
authMetadata, err := parseOpenstackSwiftAuthenticationMetadata(config)

if err != nil {
return nil, fmt.Errorf("error parsing swift authentication metadata: %s", err)
}

// User chose the "application_credentials" authentication method
if authMetadata.appCredentialID != "" {
keystoneAuth, err = openstack.NewAppCredentialsAuth(authMetadata.authURL, authMetadata.appCredentialID, authMetadata.appCredentialSecret, swiftMetadata.httpClientTimeout)
keystoneAuth, err = openstack.NewAppCredentialsAuth(authMetadata.authURL, authMetadata.appCredentialID, authMetadata.appCredentialSecret, openstackSwiftMetadata.httpClientTimeout)
if err != nil {
return nil, fmt.Errorf("error getting openstack credentials for application credentials method: %s", err)
}
} else {
// User chose the "password" authentication method
if authMetadata.userID != "" {
keystoneAuth, err = openstack.NewPasswordAuth(authMetadata.authURL, authMetadata.userID, authMetadata.password, authMetadata.projectID, swiftMetadata.httpClientTimeout)
keystoneAuth, err = openstack.NewPasswordAuth(authMetadata.authURL, authMetadata.userID, authMetadata.password, authMetadata.projectID, openstackSwiftMetadata.httpClientTimeout)
if err != nil {
return nil, fmt.Errorf("error getting openstack credentials for password method: %s", err)
}
Expand All @@ -210,14 +210,14 @@ func NewSwiftScaler(config *ScalerConfig) (Scaler, error) {
}
}

return &swiftScaler{
metadata: swiftMetadata,
return &openstackSwiftScaler{
metadata: openstackSwiftMetadata,
authMetadata: keystoneAuth,
}, nil
}

func parseSwiftMetadata(config *ScalerConfig) (*swiftMetadata, error) {
meta := swiftMetadata{}
func parseOpenstackSwiftMetadata(config *ScalerConfig) (*openstackSwiftMetadata, error) {
meta := openstackSwiftMetadata{}

if val, ok := config.TriggerMetadata["swiftURL"]; ok {
meta.swiftURL = val
Expand Down Expand Up @@ -282,8 +282,8 @@ func parseSwiftMetadata(config *ScalerConfig) (*swiftMetadata, error) {
return &meta, nil
}

func parseSwiftAuthenticationMetadata(config *ScalerConfig) (*swiftAuthenticationMetadata, error) {
authMeta := swiftAuthenticationMetadata{}
func parseOpenstackSwiftAuthenticationMetadata(config *ScalerConfig) (*openstackSwiftAuthenticationMetadata, error) {
authMeta := openstackSwiftAuthenticationMetadata{}

if config.AuthParams["authURL"] != "" {
authMeta.authURL = config.AuthParams["authURL"]
Expand Down Expand Up @@ -322,8 +322,8 @@ func parseSwiftAuthenticationMetadata(config *ScalerConfig) (*swiftAuthenticatio
return &authMeta, nil
}

func (s *swiftScaler) IsActive(ctx context.Context) (bool, error) {
objectCount, err := s.getSwiftContainerObjectCount()
func (s *openstackSwiftScaler) IsActive(ctx context.Context) (bool, error) {
objectCount, err := s.getOpenstackSwiftContainerObjectCount()

if err != nil {
return false, err
Expand All @@ -332,15 +332,15 @@ func (s *swiftScaler) IsActive(ctx context.Context) (bool, error) {
return objectCount > 0, nil
}

func (s *swiftScaler) Close() error {
func (s *openstackSwiftScaler) Close() error {
return nil
}

func (s *swiftScaler) GetMetrics(ctx context.Context, metricName string, metricSelector labels.Selector) ([]external_metrics.ExternalMetricValue, error) {
objectCount, err := s.getSwiftContainerObjectCount()
func (s *openstackSwiftScaler) GetMetrics(ctx context.Context, metricName string, metricSelector labels.Selector) ([]external_metrics.ExternalMetricValue, error) {
objectCount, err := s.getOpenstackSwiftContainerObjectCount()

if err != nil {
swiftLog.Error(err, "error getting objectCount")
openstackSwiftLog.Error(err, "error getting objectCount")
return []external_metrics.ExternalMetricValue{}, err
}

Expand All @@ -353,12 +353,20 @@ func (s *swiftScaler) GetMetrics(ctx context.Context, metricName string, metricS
return append([]external_metrics.ExternalMetricValue{}, metric), nil
}

func (s *swiftScaler) GetMetricSpecForScaling() []v2beta2.MetricSpec {
func (s *openstackSwiftScaler) GetMetricSpecForScaling() []v2beta2.MetricSpec {
targetObjectCount := resource.NewQuantity(int64(s.metadata.objectCount), resource.DecimalSI)

var metricName string

if s.metadata.objectPrefix != "" {
metricName = fmt.Sprintf("%s-%s", s.metadata.containerName, s.metadata.objectPrefix)
} else {
metricName = s.metadata.containerName
}

externalMetric := &v2beta2.ExternalMetricSource{
Metric: v2beta2.MetricIdentifier{
Name: kedautil.NormalizeString(fmt.Sprintf("%s-%s", "swift", s.metadata.containerName)),
Name: kedautil.NormalizeString(fmt.Sprintf("%s-%s", "openstack-swift", metricName)),
},
Target: v2beta2.MetricTarget{
Type: v2beta2.AverageValueMetricType,
Expand Down
Loading

0 comments on commit 0313be5

Please sign in to comment.