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 21, 2024
1 parent b974705 commit a15db2d
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions controllers/hyperconverged/hyperconverged_controller.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package hyperconverged

import (
"bufio"
"cmp"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"os"
"reflect"
"regexp"
"slices"

"github.com/blang/semver/v4"
Expand Down Expand Up @@ -336,6 +340,11 @@ 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 +1339,71 @@ func (r *ReconcileHyperConverged) deleteObj(req *common.HcoRequest, obj client.O
return removed, nil
}

func (r *ReconcileHyperConverged) evaluateUpgradeEligibility(req *common.HcoRequest) error {

rhel8Regex := regexp.MustCompile(`.*rhel8.*`)

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
}

scanner := bufio.NewScanner(resp.Body)
defer resp.Body.Close()

for scanner.Scan() {
line := scanner.Text()
if rhel8Regex.MatchString(line) {
req.Logger.Info("Detected outdated machine type in metrics", "pod", pod.Name, "matched", rhel8Regex.FindString(line))
req.Upgradeable = false
return nil
}
}

if err := scanner.Err(); err != nil {
req.Logger.Info("Failed to scan metrics response from pod", "pod", pod.Name, "error", err)
continue
}
}

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 a15db2d

Please sign in to comment.