Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix scalers leaking #684

Merged
merged 2 commits into from
Mar 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions pkg/handler/scale_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,24 +186,30 @@ func (h *ScaleHandler) resolveConfigValue(configKeyRef *corev1.ConfigMapKeySelec
return string(configMap.Data[keyName]), nil
}

func closeScalers(scalers []scalers.Scaler) {
for _, scaler := range scalers {
defer scaler.Close()
}
}

// GetDeploymentScalers returns list of Scalers and Deployment for the specified ScaledObject
func (h *ScaleHandler) GetDeploymentScalers(scaledObject *kedav1alpha1.ScaledObject) ([]scalers.Scaler, *appsv1.Deployment, error) {
scalers := []scalers.Scaler{}
scalersRes := []scalers.Scaler{}

deploymentName := scaledObject.Spec.ScaleTargetRef.DeploymentName
if deploymentName == "" {
return scalers, nil, fmt.Errorf("notified about ScaledObject with missing deployment name: %s", scaledObject.GetName())
return scalersRes, nil, fmt.Errorf("notified about ScaledObject with missing deployment name: %s", scaledObject.GetName())
}

deployment := &appsv1.Deployment{}
err := h.client.Get(context.TODO(), types.NamespacedName{Name: deploymentName, Namespace: scaledObject.GetNamespace()}, deployment)
if err != nil {
return scalers, nil, fmt.Errorf("error getting deployment: %s", err)
return scalersRes, nil, fmt.Errorf("error getting deployment: %s", err)
}

resolvedEnv, err := h.resolveDeploymentEnv(deployment, scaledObject.Spec.ScaleTargetRef.ContainerName)
if err != nil {
return scalers, nil, fmt.Errorf("error resolving secrets for deployment: %s", err)
return scalersRes, nil, fmt.Errorf("error resolving secrets for deployment: %s", err)
}

for i, trigger := range scaledObject.Spec.Triggers {
Expand All @@ -214,7 +220,8 @@ func (h *ScaleHandler) GetDeploymentScalers(scaledObject *kedav1alpha1.ScaledObj
serviceAccount := &v1.ServiceAccount{}
err = h.client.Get(context.TODO(), types.NamespacedName{Name: serviceAccountName, Namespace: scaledObject.GetNamespace()}, serviceAccount)
if err != nil {
return scalers, nil, fmt.Errorf("error getting deployment: %s", err)
closeScalers(scalersRes)
return []scalers.Scaler{}, nil, fmt.Errorf("error getting service account: %s", err)
}
authParams["awsRoleArn"] = serviceAccount.Annotations[kedav1alpha1.PodIdentityAnnotationEKS]
} else if podIdentity == kedav1alpha1.PodIdentityProviderAwsKiam {
Expand All @@ -223,34 +230,36 @@ func (h *ScaleHandler) GetDeploymentScalers(scaledObject *kedav1alpha1.ScaledObj

scaler, err := h.getScaler(scaledObject.Name, scaledObject.Namespace, trigger.Type, resolvedEnv, trigger.Metadata, authParams, podIdentity)
if err != nil {
return scalers, nil, fmt.Errorf("error getting scaler for trigger #%d: %s", i, err)
closeScalers(scalersRes)
return []scalers.Scaler{}, nil, fmt.Errorf("error getting scaler for trigger #%d: %s", i, err)
}

scalers = append(scalers, scaler)
scalersRes = append(scalersRes, scaler)
}

return scalers, deployment, nil
return scalersRes, deployment, nil
}

func (h *ScaleHandler) getJobScalers(scaledObject *kedav1alpha1.ScaledObject) ([]scalers.Scaler, error) {
scalers := []scalers.Scaler{}
scalersRes := []scalers.Scaler{}

resolvedEnv, err := h.resolveJobEnv(scaledObject)
if err != nil {
return scalers, fmt.Errorf("error resolving secrets for job: %s", err)
return scalersRes, fmt.Errorf("error resolving secrets for job: %s", err)
}

for i, trigger := range scaledObject.Spec.Triggers {
authParams, podIdentity := h.parseJobAuthRef(trigger.AuthenticationRef, scaledObject)
scaler, err := h.getScaler(scaledObject.Name, scaledObject.Namespace, trigger.Type, resolvedEnv, trigger.Metadata, authParams, podIdentity)
if err != nil {
return scalers, fmt.Errorf("error getting scaler for trigger #%d: %s", i, err)
closeScalers(scalersRes)
return []scalers.Scaler{}, fmt.Errorf("error getting scaler for trigger #%d: %s", i, err)
}

scalers = append(scalers, scaler)
scalersRes = append(scalersRes, scaler)
}

return scalers, nil
return scalersRes, nil
}

func (h *ScaleHandler) resolveAuthSecret(name, namespace, key string) string {
Expand Down