Commit 39870211 authored by Edmund Ochieng's avatar Edmund Ochieng

update logic to update hpa

parent 6aabfbc7
......@@ -17,6 +17,10 @@ func getHPALabels(labels map[string]string) map[string]string {
func HorizontalAutoscaler(deployment *appsv1.Deployment, cr *gitlabv1beta1.GitLab) *autoscalingv1.HorizontalPodAutoscaler {
labels := getHPALabels(deployment.ObjectMeta.Labels)
if cr.Spec.AutoScaling == nil {
return nil
}
return &autoscalingv1.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: deployment.Name,
......
......@@ -632,27 +632,54 @@ func (r *GitLabReconciler) setupAutoscaling(ctx context.Context, cr *gitlabv1bet
}
for _, deploy := range deployments.Items {
if !strings.Contains(deploy.Name, "gitlab-exporter") {
hpa := gitlabctl.HorizontalAutoscaler(&deploy, cr)
if err := r.reconcileHPA(ctx, &deploy, cr); err != nil {
return err
}
}
return nil
}
func (r *GitLabReconciler) reconcileHPA(ctx context.Context, deployment *appsv1.Deployment, cr *gitlabv1beta1.GitLab) error {
if strings.Contains(deployment.Name, "gitlab-exporter") {
return nil
}
hpa := gitlabctl.HorizontalAutoscaler(deployment, cr)
found := &autoscalingv1.HorizontalPodAutoscaler{}
err := r.Get(ctx, types.NamespacedName{Name: deployment.Name, Namespace: cr.Namespace}, found)
if err != nil {
if errors.IsNotFound(err) {
if err := controllerutil.SetControllerReference(cr, hpa, r.Scheme); err != nil {
return err
}
found := &autoscalingv1.HorizontalPodAutoscaler{}
err := r.Get(ctx, types.NamespacedName{Name: hpa.Name, Namespace: cr.Namespace}, found)
if err != nil {
if errors.IsNotFound(err) {
return r.Create(ctx, hpa)
}
return r.Create(ctx, hpa)
}
return err
}
return err
}
if !reflect.DeepEqual(hpa.Spec, found.Spec) {
return r.Patch(ctx, found, client.MergeFrom(hpa))
}
if cr.Spec.AutoScaling == nil {
return r.Delete(ctx, found)
}
if !reflect.DeepEqual(hpa.Spec, found.Spec) {
if *found.Spec.MinReplicas != *hpa.Spec.MinReplicas {
found.Spec.MinReplicas = hpa.Spec.MinReplicas
}
if found.Spec.MaxReplicas != hpa.Spec.MaxReplicas {
found.Spec.MaxReplicas = hpa.Spec.MaxReplicas
}
if found.Spec.TargetCPUUtilizationPercentage != hpa.Spec.TargetCPUUtilizationPercentage {
found.Spec.TargetCPUUtilizationPercentage = hpa.Spec.TargetCPUUtilizationPercentage
}
return r.Update(ctx, found)
}
return nil
......
......@@ -23,7 +23,8 @@ var EndpointMembers []string
func (r *GitLabReconciler) reconcileGitlabStatus(cr *gitlabv1beta1.GitLab) error {
// get current Gitlab resource
gitlab, err := r.retrieveUpdatedGitlabResource(cr)
gitlab := &gitlabv1beta1.GitLab{}
err := r.Get(context.TODO(), types.NamespacedName{Namespace: cr.Namespace, Name: cr.Name}, gitlab)
if err != nil {
return err
}
......@@ -140,12 +141,6 @@ func (r *GitLabReconciler) isWebserviceDeployed(cr *gitlabv1beta1.GitLab) bool {
return !reflect.DeepEqual(*webservice, appsv1.Deployment{}) || !errors.IsNotFound(err)
}
func (r *GitLabReconciler) retrieveUpdatedGitlabResource(cr *gitlabv1beta1.GitLab) (*gitlabv1beta1.GitLab, error) {
gitlab := &gitlabv1beta1.GitLab{}
err := r.Get(context.TODO(), types.NamespacedName{Namespace: cr.Namespace, Name: cr.Name}, gitlab)
return gitlab, err
}
// setGitlabStatus sets status of custom resource
func (r *GitLabReconciler) setGitlabStatus(object runtime.Object) error {
return r.Status().Update(context.TODO(), object)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment