|
| 1 | +package tenant |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "golang.org/x/sync/errgroup" |
| 9 | + corev1 "k8s.io/api/core/v1" |
| 10 | + "k8s.io/apimachinery/pkg/fields" |
| 11 | + "k8s.io/apimachinery/pkg/types" |
| 12 | + "k8s.io/client-go/util/retry" |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 14 | + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" |
| 15 | + |
| 16 | + capsulev1beta1 "github.com/clastix/capsule/api/v1beta1" |
| 17 | +) |
| 18 | + |
| 19 | +// Ensuring all annotations are applied to each Namespace handled by the Tenant. |
| 20 | +func (r *Manager) syncNamespaces(tenant *capsulev1beta1.Tenant) (err error) { |
| 21 | + group := new(errgroup.Group) |
| 22 | + |
| 23 | + for _, item := range tenant.Status.Namespaces { |
| 24 | + namespace := item |
| 25 | + |
| 26 | + group.Go(func() error { |
| 27 | + return r.syncNamespaceMetadata(namespace, tenant) |
| 28 | + }) |
| 29 | + } |
| 30 | + |
| 31 | + if err = group.Wait(); err != nil { |
| 32 | + r.Log.Error(err, "Cannot sync Namespaces") |
| 33 | + |
| 34 | + err = fmt.Errorf("cannot sync Namespaces: %s", err.Error()) |
| 35 | + } |
| 36 | + return |
| 37 | +} |
| 38 | + |
| 39 | +func (r *Manager) syncNamespaceMetadata(namespace string, tnt *capsulev1beta1.Tenant) (err error) { |
| 40 | + var res controllerutil.OperationResult |
| 41 | + |
| 42 | + err = retry.RetryOnConflict(retry.DefaultBackoff, func() (conflictErr error) { |
| 43 | + ns := &corev1.Namespace{} |
| 44 | + if conflictErr = r.Client.Get(context.TODO(), types.NamespacedName{Name: namespace}, ns); err != nil { |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + capsuleLabel, _ := capsulev1beta1.GetTypeLabel(&capsulev1beta1.Tenant{}) |
| 49 | + |
| 50 | + res, conflictErr = controllerutil.CreateOrUpdate(context.TODO(), r.Client, ns, func() error { |
| 51 | + annotations := make(map[string]string) |
| 52 | + |
| 53 | + if tnt.Spec.NamespacesMetadata != nil { |
| 54 | + for k, v := range tnt.Spec.NamespacesMetadata.AdditionalAnnotations { |
| 55 | + annotations[k] = v |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + if tnt.Spec.NodeSelector != nil { |
| 60 | + var selector []string |
| 61 | + for k, v := range tnt.Spec.NodeSelector { |
| 62 | + selector = append(selector, fmt.Sprintf("%s=%s", k, v)) |
| 63 | + } |
| 64 | + annotations["scheduler.alpha.kubernetes.io/node-selector"] = strings.Join(selector, ",") |
| 65 | + } |
| 66 | + |
| 67 | + if tnt.Spec.IngressClasses != nil { |
| 68 | + if len(tnt.Spec.IngressClasses.Exact) > 0 { |
| 69 | + annotations[capsulev1beta1.AvailableIngressClassesAnnotation] = strings.Join(tnt.Spec.IngressClasses.Exact, ",") |
| 70 | + } |
| 71 | + if len(tnt.Spec.IngressClasses.Regex) > 0 { |
| 72 | + annotations[capsulev1beta1.AvailableIngressClassesRegexpAnnotation] = tnt.Spec.IngressClasses.Regex |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + if tnt.Spec.StorageClasses != nil { |
| 77 | + if len(tnt.Spec.StorageClasses.Exact) > 0 { |
| 78 | + annotations[capsulev1beta1.AvailableStorageClassesAnnotation] = strings.Join(tnt.Spec.StorageClasses.Exact, ",") |
| 79 | + } |
| 80 | + if len(tnt.Spec.StorageClasses.Regex) > 0 { |
| 81 | + annotations[capsulev1beta1.AvailableStorageClassesRegexpAnnotation] = tnt.Spec.StorageClasses.Regex |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if tnt.Spec.ContainerRegistries != nil { |
| 86 | + if len(tnt.Spec.ContainerRegistries.Exact) > 0 { |
| 87 | + annotations[capsulev1beta1.AllowedRegistriesAnnotation] = strings.Join(tnt.Spec.ContainerRegistries.Exact, ",") |
| 88 | + } |
| 89 | + if len(tnt.Spec.ContainerRegistries.Regex) > 0 { |
| 90 | + annotations[capsulev1beta1.AllowedRegistriesRegexpAnnotation] = tnt.Spec.ContainerRegistries.Regex |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + ns.SetAnnotations(annotations) |
| 95 | + |
| 96 | + newLabels := map[string]string{ |
| 97 | + "name": namespace, |
| 98 | + capsuleLabel: tnt.GetName(), |
| 99 | + } |
| 100 | + |
| 101 | + if tnt.Spec.NamespacesMetadata != nil { |
| 102 | + for k, v := range tnt.Spec.NamespacesMetadata.AdditionalLabels { |
| 103 | + newLabels[k] = v |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + ns.SetLabels(newLabels) |
| 108 | + |
| 109 | + return nil |
| 110 | + }) |
| 111 | + |
| 112 | + return |
| 113 | + }) |
| 114 | + |
| 115 | + r.emitEvent(tnt, namespace, res, "Ensuring Namespace metadata", err) |
| 116 | + |
| 117 | + return |
| 118 | +} |
| 119 | + |
| 120 | +func (r *Manager) ensureNamespaceCount(tenant *capsulev1beta1.Tenant) error { |
| 121 | + return retry.RetryOnConflict(retry.DefaultBackoff, func() error { |
| 122 | + tenant.Status.Size = uint(len(tenant.Status.Namespaces)) |
| 123 | + |
| 124 | + found := &capsulev1beta1.Tenant{} |
| 125 | + if err := r.Client.Get(context.TODO(), types.NamespacedName{Name: tenant.GetName()}, found); err != nil { |
| 126 | + return err |
| 127 | + } |
| 128 | + |
| 129 | + found.Status.Size = tenant.Status.Size |
| 130 | + |
| 131 | + return r.Client.Status().Update(context.TODO(), found, &client.UpdateOptions{}) |
| 132 | + }) |
| 133 | +} |
| 134 | + |
| 135 | +func (r *Manager) collectNamespaces(tenant *capsulev1beta1.Tenant) error { |
| 136 | + return retry.RetryOnConflict(retry.DefaultBackoff, func() (err error) { |
| 137 | + list := &corev1.NamespaceList{} |
| 138 | + err = r.Client.List(context.TODO(), list, client.MatchingFieldsSelector{ |
| 139 | + Selector: fields.OneTermEqualSelector(".metadata.ownerReferences[*].capsule", tenant.GetName()), |
| 140 | + }) |
| 141 | + |
| 142 | + if err != nil { |
| 143 | + return |
| 144 | + } |
| 145 | + |
| 146 | + _, err = controllerutil.CreateOrUpdate(context.TODO(), r.Client, tenant.DeepCopy(), func() error { |
| 147 | + tenant.AssignNamespaces(list.Items) |
| 148 | + |
| 149 | + return r.Client.Status().Update(context.TODO(), tenant, &client.UpdateOptions{}) |
| 150 | + }) |
| 151 | + return |
| 152 | + }) |
| 153 | +} |
0 commit comments