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 ed2b6ed
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 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,12 @@ func (r *ReconcileHyperConverged) Reconcile(ctx context.Context, request reconci
return result, err
}

err = r.EvaluateUpgradeEligibility(hcoRequest)
if err != nil {
hcoRequest.Logger.Error(err, "Failed to evaluate upgrade eligibility", "err", err)
return reconcile.Result{}, err
}

if err = r.setOperatorUpgradeableStatus(hcoRequest); err != nil {
return reconcile.Result{}, err
}
Expand Down Expand Up @@ -1330,6 +1340,66 @@ 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
}

virtMetricsData := string(body)
rhel8Regex := regexp.MustCompile(`.*rhel8.*`)
if rhel8Regex.MatchString(virtMetricsData) {
req.Logger.Info("Detected outdated machine type in metrics", "pod", pod.Name, "matched", rhel8Regex.FindString(virtMetricsData))
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 ed2b6ed

Please sign in to comment.