Skip to content

Commit

Permalink
POC: block operator upgrade when detecting outdated vms
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Sionov <[email protected]>
  • Loading branch information
dasionov committed Nov 20, 2024
1 parent b974705 commit 74747cc
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions controllers/hyperconverged/hyperconverged_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ package hyperconverged
import (
"cmp"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"reflect"
"regexp"
"slices"

"github.com/blang/semver/v4"
Expand Down Expand Up @@ -336,6 +340,8 @@ func (r *ReconcileHyperConverged) Reconcile(ctx context.Context, request reconci
return result, err
}

r.EvaluateUpgradeEligibility(hcoRequest)

Check failure on line 343 in controllers/hyperconverged/hyperconverged_controller.go

View workflow job for this annotation

GitHub Actions / linter Checks

Error return value of `r.EvaluateUpgradeEligibility` is not checked (errcheck)

if err = r.setOperatorUpgradeableStatus(hcoRequest); err != nil {
return reconcile.Result{}, err
}
Expand Down Expand Up @@ -1330,6 +1336,69 @@ func (r *ReconcileHyperConverged) deleteObj(req *common.HcoRequest, obj client.O
return removed, nil
}

func (r *ReconcileHyperConverged) EvaluateUpgradeEligibility(req *common.HcoRequest) error {
podList := &corev1.PodList{}
listOpts := []client.ListOption{
client.InNamespace(req.Namespace),
client.MatchingLabels{"kubevirt.io": "virt-controller"},
}

if err := r.client.List(req.Ctx, podList, listOpts...); err != nil {
req.Logger.Info("Failed to list virt-controller pods", "namespace", req.Namespace, "error", err)
return fmt.Errorf("failed to list virt-controller pods: %w", err)
}

if len(podList.Items) == 0 {
req.Logger.Info("No virt-controller pods found", "namespace", req.Namespace)
return nil
}

httpClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}

for _, pod := range podList.Items {
if pod.Status.PodIP == "" {
continue
}
metricsURL := fmt.Sprintf("https://%s:%d/metrics", pod.Status.PodIP, 8443)

resp, err := httpClient.Get(metricsURL)
if err != nil {
req.Logger.Info("Failed to query metrics from pod", "pod", pod.Name, "error", err)
continue
}

if resp.StatusCode != http.StatusOK {
req.Logger.Info("Metrics endpoint returned non-200 status", "pod", pod.Name, "status", resp.StatusCode)
resp.Body.Close()
continue
}

body, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
req.Logger.Info("Failed to read metrics response from pod", "pod", pod.Name, "error", err)
continue
}

virtMetrics := string(body)

req.Logger.Info("Metrics data from pod", "pod", pod.Name, "metrics", virtMetrics)

rhel8Regex := regexp.MustCompile(`.*rhel8.*`)
if rhel8Regex.MatchString(virtMetrics) {
req.Logger.Info("Detected outdated machine type in metrics", "pod", pod.Name, "matched", rhel8Regex.FindString(virtMetrics))
req.Upgradeable = false
return nil
}
}

return nil
}

func removeOldQuickStartGuides(req *common.HcoRequest, cl client.Client, requiredQSList []string) {
existingQSList := &consolev1.ConsoleQuickStartList{}
req.Logger.Info("reading quickstart guides")
Expand Down

0 comments on commit 74747cc

Please sign in to comment.