|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + _ "embed" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + |
| 9 | + "github.com/google/go-jsonnet" |
| 10 | + appsv1 "k8s.io/api/apps/v1" |
| 11 | + corev1 "k8s.io/api/core/v1" |
| 12 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + "k8s.io/apimachinery/pkg/runtime" |
| 15 | + ctrl "sigs.k8s.io/controller-runtime" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 17 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + imagesConfigMapName = "machine-api-operator-images" |
| 22 | + originalUpstreamDeploymentName = "machine-api-controllers" |
| 23 | + imageKey = "images.json" |
| 24 | + |
| 25 | + caBundleConfigMapName = "appuio-machine-api-ca-bundle" |
| 26 | +) |
| 27 | + |
| 28 | +//go:embed machine_api_controllers_deployment.jsonnet |
| 29 | +var deploymentTemplate string |
| 30 | + |
| 31 | +// MachineAPIControllersReconciler creates a appuio-machine-api-controllers deployment based on the images.json ConfigMap |
| 32 | +// if the upstream machine-api-controllers does not exist. |
| 33 | +type MachineAPIControllersReconciler struct { |
| 34 | + client.Client |
| 35 | + Scheme *runtime.Scheme |
| 36 | + |
| 37 | + Namespace string |
| 38 | +} |
| 39 | + |
| 40 | +func (r *MachineAPIControllersReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { |
| 41 | + if req.Name != imagesConfigMapName { |
| 42 | + return ctrl.Result{}, nil |
| 43 | + } |
| 44 | + |
| 45 | + l := log.FromContext(ctx).WithName("UpstreamDeploymentReconciler.Reconcile") |
| 46 | + l.Info("Reconciling") |
| 47 | + |
| 48 | + var imageCM corev1.ConfigMap |
| 49 | + if err := r.Get(ctx, req.NamespacedName, &imageCM); err != nil { |
| 50 | + return ctrl.Result{}, client.IgnoreNotFound(err) |
| 51 | + } |
| 52 | + |
| 53 | + ij, ok := imageCM.Data[imageKey] |
| 54 | + if !ok { |
| 55 | + return ctrl.Result{}, fmt.Errorf("images.json key not found in ConfigMap %q", imagesConfigMapName) |
| 56 | + } |
| 57 | + images := make(map[string]string) |
| 58 | + if err := json.Unmarshal([]byte(ij), &images); err != nil { |
| 59 | + return ctrl.Result{}, fmt.Errorf("failed to unmarshal %q from %q: %w", imageKey, imagesConfigMapName, err) |
| 60 | + } |
| 61 | + |
| 62 | + // Check that the original upstream deployment does not exist |
| 63 | + // If it does, we should not create the new deployment |
| 64 | + var upstreamDeployment appsv1.Deployment |
| 65 | + err := r.Get(ctx, client.ObjectKey{ |
| 66 | + Name: originalUpstreamDeploymentName, |
| 67 | + Namespace: r.Namespace, |
| 68 | + }, &upstreamDeployment) |
| 69 | + if err == nil { |
| 70 | + return ctrl.Result{}, fmt.Errorf("original upstream deployment %s already exists", originalUpstreamDeploymentName) |
| 71 | + } else if !apierrors.IsNotFound(err) { |
| 72 | + return ctrl.Result{}, fmt.Errorf("failed to check for original upstream deployment %s: %w", originalUpstreamDeploymentName, err) |
| 73 | + } |
| 74 | + |
| 75 | + caBundleConfigMap := corev1.ConfigMap{ |
| 76 | + TypeMeta: metav1.TypeMeta{ |
| 77 | + APIVersion: "v1", |
| 78 | + Kind: "ConfigMap", |
| 79 | + }, |
| 80 | + ObjectMeta: metav1.ObjectMeta{ |
| 81 | + Name: caBundleConfigMapName, |
| 82 | + Namespace: r.Namespace, |
| 83 | + Labels: map[string]string{ |
| 84 | + "config.openshift.io/inject-trusted-cabundle": "true", |
| 85 | + }, |
| 86 | + }, |
| 87 | + } |
| 88 | + if err := r.Client.Patch(ctx, &caBundleConfigMap, client.Apply, client.FieldOwner("upstream-deployment-controller")); err != nil { |
| 89 | + return ctrl.Result{}, fmt.Errorf("failed to apply ConfigMap %q: %w", caBundleConfigMapName, err) |
| 90 | + } |
| 91 | + |
| 92 | + vm, err := jsonnetVMWithContext(images, caBundleConfigMap) |
| 93 | + if err != nil { |
| 94 | + return ctrl.Result{}, fmt.Errorf("failed to create jsonnet VM: %w", err) |
| 95 | + } |
| 96 | + |
| 97 | + ud, err := vm.EvaluateAnonymousSnippet("controllers_deployment.jsonnet", deploymentTemplate) |
| 98 | + if err != nil { |
| 99 | + return ctrl.Result{}, fmt.Errorf("failed to evaluate jsonnet: %w", err) |
| 100 | + } |
| 101 | + |
| 102 | + // TODO(bastjan) this could be way more generic and support any kind of object. |
| 103 | + // We don't need any other object types right now, so we're keeping it simple. |
| 104 | + var toDeploy appsv1.Deployment |
| 105 | + if err := json.Unmarshal([]byte(ud), &toDeploy); err != nil { |
| 106 | + return ctrl.Result{}, fmt.Errorf("failed to unmarshal jsonnet output: %w", err) |
| 107 | + } |
| 108 | + if toDeploy.APIVersion != "apps/v1" || toDeploy.Kind != "Deployment" { |
| 109 | + return ctrl.Result{}, fmt.Errorf("expected Deployment, got %s/%s", toDeploy.APIVersion, toDeploy.Kind) |
| 110 | + } |
| 111 | + toDeploy.Namespace = r.Namespace |
| 112 | + |
| 113 | + if err := r.Client.Patch(ctx, &toDeploy, client.Apply, client.FieldOwner("upstream-deployment-controller")); err != nil { |
| 114 | + return ctrl.Result{}, fmt.Errorf("failed to apply Deployment %q: %w", toDeploy.GetName(), err) |
| 115 | + } |
| 116 | + |
| 117 | + return ctrl.Result{}, nil |
| 118 | +} |
| 119 | + |
| 120 | +// SetupWithManager sets up the controller with the Manager. |
| 121 | +func (r *MachineAPIControllersReconciler) SetupWithManager(mgr ctrl.Manager) error { |
| 122 | + return ctrl.NewControllerManagedBy(mgr). |
| 123 | + For(&corev1.ConfigMap{}). |
| 124 | + Owns(&appsv1.Deployment{}). |
| 125 | + Owns(&corev1.ConfigMap{}). |
| 126 | + Complete(r) |
| 127 | +} |
| 128 | + |
| 129 | +func jsonnetVMWithContext(images map[string]string, cabundle corev1.ConfigMap) (*jsonnet.VM, error) { |
| 130 | + jcr, err := json.Marshal(map[string]any{ |
| 131 | + "images": images, |
| 132 | + "cabundle": cabundle, |
| 133 | + }) |
| 134 | + if err != nil { |
| 135 | + return nil, fmt.Errorf("unable to marshal jsonnet context: %w", err) |
| 136 | + } |
| 137 | + jvm := jsonnet.MakeVM() |
| 138 | + jvm.ExtCode("context", string(jcr)) |
| 139 | + // Don't allow imports |
| 140 | + jvm.Importer(&jsonnet.MemoryImporter{}) |
| 141 | + return jvm, nil |
| 142 | +} |
0 commit comments