|
| 1 | +/* |
| 2 | +
|
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package controllers |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + |
| 24 | + kmmv1beta1 "github.com/kubernetes-sigs/kernel-module-management/api/v1beta1" |
| 25 | + "github.com/kubernetes-sigs/kernel-module-management/api/v1beta2" |
| 26 | + "github.com/kubernetes-sigs/kernel-module-management/internal/api" |
| 27 | + "github.com/kubernetes-sigs/kernel-module-management/internal/filter" |
| 28 | + "github.com/kubernetes-sigs/kernel-module-management/internal/metrics" |
| 29 | + "github.com/kubernetes-sigs/kernel-module-management/internal/mic" |
| 30 | + "github.com/kubernetes-sigs/kernel-module-management/internal/module" |
| 31 | + "github.com/kubernetes-sigs/kernel-module-management/internal/preflight" |
| 32 | + "k8s.io/apimachinery/pkg/types" |
| 33 | + ctrl "sigs.k8s.io/controller-runtime" |
| 34 | + "sigs.k8s.io/controller-runtime/pkg/builder" |
| 35 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 36 | + "sigs.k8s.io/controller-runtime/pkg/controller" |
| 37 | + "sigs.k8s.io/controller-runtime/pkg/handler" |
| 38 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 39 | +) |
| 40 | + |
| 41 | +const ( |
| 42 | + PreflightValidationReconcilerName = "PreflightValidation" |
| 43 | +) |
| 44 | + |
| 45 | +// PreflightReconciler reconciles a PreflightValidation object |
| 46 | +type preflightValidationReconciler struct { |
| 47 | + client client.Client |
| 48 | + filterAPI *filter.Filter |
| 49 | + metricsAPI metrics.Metrics |
| 50 | + helper preflightReconcilerHelper |
| 51 | + preflightAPI preflight.PreflightAPI |
| 52 | +} |
| 53 | + |
| 54 | +func NewPreflightValidationReconciler( |
| 55 | + client client.Client, |
| 56 | + filterAPI *filter.Filter, |
| 57 | + metricsAPI metrics.Metrics, |
| 58 | + micAPI mic.MIC, |
| 59 | + kernelAPI module.KernelMapper, |
| 60 | + preflightAPI preflight.PreflightAPI, |
| 61 | +) *preflightValidationReconciler { |
| 62 | + helper := newPreflightReconcilerHelper(client, micAPI, metricsAPI, kernelAPI, preflightAPI) |
| 63 | + return &preflightValidationReconciler{ |
| 64 | + client: client, |
| 65 | + filterAPI: filterAPI, |
| 66 | + metricsAPI: metricsAPI, |
| 67 | + helper: helper, |
| 68 | + preflightAPI: preflightAPI, |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func (r *preflightValidationReconciler) SetupWithManager(mgr ctrl.Manager) error { |
| 73 | + return ctrl.NewControllerManagedBy(mgr). |
| 74 | + Named(PreflightValidationReconcilerName). |
| 75 | + For(&v1beta2.PreflightValidation{}, builder.WithPredicates(filter.PreflightReconcilerUpdatePredicate())). |
| 76 | + Owns(&kmmv1beta1.ModuleImagesConfig{}). |
| 77 | + Watches( |
| 78 | + &kmmv1beta1.Module{}, |
| 79 | + handler.EnqueueRequestsFromMapFunc(r.filterAPI.EnqueueAllPreflightValidations), |
| 80 | + builder.WithPredicates(filter.PreflightReconcilerUpdatePredicate()), |
| 81 | + ). |
| 82 | + WithOptions(controller.Options{ |
| 83 | + MaxConcurrentReconciles: 1, |
| 84 | + }). |
| 85 | + Complete( |
| 86 | + reconcile.AsReconciler[*v1beta2.PreflightValidation](r.client, r), |
| 87 | + ) |
| 88 | +} |
| 89 | + |
| 90 | +// Reconcile Reconiliation entry point |
| 91 | +func (r *preflightValidationReconciler) Reconcile(ctx context.Context, pv *v1beta2.PreflightValidation) (ctrl.Result, error) { |
| 92 | + log := ctrl.LoggerFrom(ctx).WithValues("preflight name", pv.Name) |
| 93 | + log.Info("Start PreflightValidation Reconciliation") |
| 94 | + |
| 95 | + // we want the metric just to signal that this customer uses preflight |
| 96 | + r.metricsAPI.SetKMMPreflightsNum(1) |
| 97 | + |
| 98 | + modsWithMapping, modsWithoutMapping, err := r.helper.getModulesData(ctx, pv) |
| 99 | + if err != nil { |
| 100 | + return ctrl.Result{}, fmt.Errorf("failed to get modules' data: %v", err) |
| 101 | + } |
| 102 | + |
| 103 | + err = r.helper.updateStatus(ctx, modsWithMapping, modsWithoutMapping, pv) |
| 104 | + if err != nil { |
| 105 | + return ctrl.Result{}, fmt.Errorf("failed to update PreflightValidation's status: %v", err) |
| 106 | + } |
| 107 | + |
| 108 | + err = r.helper.processPreflightValidation(ctx, modsWithMapping, pv) |
| 109 | + if err != nil { |
| 110 | + return ctrl.Result{}, fmt.Errorf("failed to process the preflight validation: %v", err) |
| 111 | + } |
| 112 | + |
| 113 | + if r.preflightAPI.AllModulesVerified(pv) { |
| 114 | + log.Info("PreflightValidation reconciliation success") |
| 115 | + return ctrl.Result{}, nil |
| 116 | + } |
| 117 | + |
| 118 | + return ctrl.Result{}, nil |
| 119 | +} |
| 120 | + |
| 121 | +//go:generate mockgen -source=preflightvalidation_reconciler.go -package=controllers -destination=mock_preflightvalidation_reconciler.go preflightReconcilerHelper |
| 122 | +type preflightReconcilerHelper interface { |
| 123 | + updateStatus(ctx context.Context, modsWithMapping []*api.ModuleLoaderData, modsWithoutMapping []types.NamespacedName, pv *v1beta2.PreflightValidation) error |
| 124 | + getModulesData(ctx context.Context, pv *v1beta2.PreflightValidation) ([]*api.ModuleLoaderData, []types.NamespacedName, error) |
| 125 | + processPreflightValidation(ctx context.Context, modsWithMapping []*api.ModuleLoaderData, pv *v1beta2.PreflightValidation) error |
| 126 | +} |
| 127 | + |
| 128 | +type preflightReconcilerHelperImpl struct { |
| 129 | + client client.Client |
| 130 | + micAPI mic.MIC |
| 131 | + metricsAPI metrics.Metrics |
| 132 | + kernelAPI module.KernelMapper |
| 133 | + preflightAPI preflight.PreflightAPI |
| 134 | +} |
| 135 | + |
| 136 | +func newPreflightReconcilerHelper(client client.Client, |
| 137 | + micAPI mic.MIC, |
| 138 | + metricsAPI metrics.Metrics, |
| 139 | + kernelAPI module.KernelMapper, |
| 140 | + preflightAPI preflight.PreflightAPI) preflightReconcilerHelper { |
| 141 | + |
| 142 | + return &preflightReconcilerHelperImpl{ |
| 143 | + client: client, |
| 144 | + micAPI: micAPI, |
| 145 | + metricsAPI: metricsAPI, |
| 146 | + kernelAPI: kernelAPI, |
| 147 | + preflightAPI: preflightAPI, |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +func (p *preflightReconcilerHelperImpl) updateStatus( |
| 152 | + ctx context.Context, |
| 153 | + modsWithMapping []*api.ModuleLoaderData, |
| 154 | + modsWithoutMapping []types.NamespacedName, |
| 155 | + pv *v1beta2.PreflightValidation) error { |
| 156 | + |
| 157 | + unmodifiedPV := pv.DeepCopy() |
| 158 | + |
| 159 | + // setting the status for modules without mapping |
| 160 | + for _, mod := range modsWithoutMapping { |
| 161 | + p.preflightAPI.SetModuleStatus(pv, mod.Namespace, mod.Name, v1beta2.VerificationFailure, "mapping not found") |
| 162 | + } |
| 163 | + |
| 164 | + // setting status for modules with mapping |
| 165 | + for _, mod := range modsWithMapping { |
| 166 | + modStatus := v1beta2.VerificationInProgress |
| 167 | + modReason := "verification is not finished yet" |
| 168 | + foundMIC, err := p.micAPI.Get(ctx, mod.Name+"-preflight", mod.Namespace) |
| 169 | + if err == nil { |
| 170 | + imageStatus := p.micAPI.GetImageState(foundMIC, mod.ContainerImage) |
| 171 | + switch imageStatus { |
| 172 | + case kmmv1beta1.ImageExists: |
| 173 | + modStatus = v1beta2.VerificationSuccess |
| 174 | + modReason = "verified image exists" |
| 175 | + case kmmv1beta1.ImageDoesNotExist: |
| 176 | + modStatus = v1beta2.VerificationFailure |
| 177 | + modReason = "verified image does not exist" |
| 178 | + if mod.Build != nil || mod.Sign != nil { |
| 179 | + modReason += " and build/sign failed" |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + p.preflightAPI.SetModuleStatus(pv, mod.Namespace, mod.Name, modStatus, modReason) |
| 184 | + } |
| 185 | + |
| 186 | + return p.client.Status().Patch(ctx, pv, client.MergeFrom(unmodifiedPV)) |
| 187 | +} |
| 188 | + |
| 189 | +func (p *preflightReconcilerHelperImpl) getModulesData(ctx context.Context, pv *v1beta2.PreflightValidation) ([]*api.ModuleLoaderData, []types.NamespacedName, error) { |
| 190 | + modulesList := kmmv1beta1.ModuleList{} |
| 191 | + err := p.client.List(ctx, &modulesList) |
| 192 | + if err != nil { |
| 193 | + return nil, nil, fmt.Errorf("failed to get list of all Modules: %v", err) |
| 194 | + } |
| 195 | + |
| 196 | + mldsWithoutMapping := make([]types.NamespacedName, 0, len(modulesList.Items)) |
| 197 | + mldsWithMapping := make([]*api.ModuleLoaderData, 0, len(modulesList.Items)) |
| 198 | + for _, mod := range modulesList.Items { |
| 199 | + // ignore modules being deleted |
| 200 | + if mod.GetDeletionTimestamp() != nil { |
| 201 | + continue |
| 202 | + } |
| 203 | + mld, err := p.kernelAPI.GetModuleLoaderDataForKernel(&mod, pv.Spec.KernelVersion) |
| 204 | + if err != nil { |
| 205 | + if !errors.Is(err, module.ErrNoMatchingKernelMapping) { |
| 206 | + return nil, nil, fmt.Errorf("failed to get MLD for module %s/%s: %v", mod.Namespace, mod.Name, err) |
| 207 | + } |
| 208 | + mldsWithoutMapping = append(mldsWithoutMapping, types.NamespacedName{Name: mod.Name, Namespace: mod.Namespace}) |
| 209 | + } else { |
| 210 | + mldsWithMapping = append(mldsWithMapping, mld) |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + return mldsWithMapping, mldsWithoutMapping, nil |
| 215 | +} |
| 216 | + |
| 217 | +func (p *preflightReconcilerHelperImpl) processPreflightValidation(ctx context.Context, modsWithMapping []*api.ModuleLoaderData, pv *v1beta2.PreflightValidation) error { |
| 218 | + errs := []error{} |
| 219 | + for _, mod := range modsWithMapping { |
| 220 | + status := p.preflightAPI.GetModuleStatus(pv, mod.Namespace, mod.Name) |
| 221 | + if status == v1beta2.VerificationSuccess || status == v1beta2.VerificationFailure { |
| 222 | + continue |
| 223 | + } |
| 224 | + |
| 225 | + micObjSpec := kmmv1beta1.ModuleImageSpec{ |
| 226 | + Image: mod.ContainerImage, |
| 227 | + KernelVersion: mod.KernelVersion, |
| 228 | + Build: mod.Build, |
| 229 | + Sign: mod.Sign, |
| 230 | + RegistryTLS: mod.RegistryTLS, |
| 231 | + } |
| 232 | + micName := mod.Name + "-preflight" |
| 233 | + err := p.micAPI.CreateOrPatch(ctx, micName, mod.Namespace, []kmmv1beta1.ModuleImageSpec{micObjSpec}, mod.ImageRepoSecret, pv) |
| 234 | + if err != nil { |
| 235 | + errs = append(errs, fmt.Errorf("failed to apply %s/%s MIC: %v", mod.Namespace, mod.Name, err)) |
| 236 | + } |
| 237 | + } |
| 238 | + return errors.Join(errs...) |
| 239 | +} |
0 commit comments