|
| 1 | +package namespaces |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "reflect" |
| 7 | + "time" |
| 8 | + |
| 9 | + console "github.com/pluralsh/console-client-go" |
| 10 | + clienterrors "github.com/pluralsh/deployment-operator/internal/errors" |
| 11 | + "github.com/pluralsh/deployment-operator/pkg/client" |
| 12 | + "github.com/pluralsh/deployment-operator/pkg/controller" |
| 13 | + "github.com/pluralsh/deployment-operator/pkg/websocket" |
| 14 | + "github.com/pluralsh/polly/algorithms" |
| 15 | + v1 "k8s.io/api/core/v1" |
| 16 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 17 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 18 | + "k8s.io/client-go/util/workqueue" |
| 19 | + ctrlclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 20 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 21 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 22 | +) |
| 23 | + |
| 24 | +type NamespaceReconciler struct { |
| 25 | + ConsoleClient client.Client |
| 26 | + K8sClient ctrlclient.Client |
| 27 | + NamespaceQueue workqueue.RateLimitingInterface |
| 28 | + NamespaceCache *client.Cache[console.ManagedNamespaceFragment] |
| 29 | +} |
| 30 | + |
| 31 | +func NewNamespaceReconciler(consoleClient client.Client, k8sClient ctrlclient.Client, refresh time.Duration) *NamespaceReconciler { |
| 32 | + return &NamespaceReconciler{ |
| 33 | + ConsoleClient: consoleClient, |
| 34 | + K8sClient: k8sClient, |
| 35 | + NamespaceQueue: workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()), |
| 36 | + NamespaceCache: client.NewCache[console.ManagedNamespaceFragment](refresh, func(id string) (*console.ManagedNamespaceFragment, error) { |
| 37 | + return consoleClient.GetNamespace(id) |
| 38 | + }), |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func (n *NamespaceReconciler) GetPublisher() (string, websocket.Publisher) { |
| 43 | + return "namespace.event", &socketPublisher{ |
| 44 | + restoreQueue: n.NamespaceQueue, |
| 45 | + restoreCache: n.NamespaceCache, |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func (n *NamespaceReconciler) WipeCache() { |
| 50 | + n.NamespaceCache.Wipe() |
| 51 | +} |
| 52 | + |
| 53 | +func (n *NamespaceReconciler) ShutdownQueue() { |
| 54 | + n.NamespaceQueue.ShutDown() |
| 55 | +} |
| 56 | + |
| 57 | +func (n *NamespaceReconciler) ListNamespaces(ctx context.Context) *algorithms.Pager[*console.ManagedNamespaceEdgeFragment] { |
| 58 | + logger := log.FromContext(ctx) |
| 59 | + logger.Info("create namespace pager") |
| 60 | + fetch := func(page *string, size int64) ([]*console.ManagedNamespaceEdgeFragment, *algorithms.PageInfo, error) { |
| 61 | + resp, err := n.ConsoleClient.ListNamespaces(page, &size) |
| 62 | + if err != nil { |
| 63 | + logger.Error(err, "failed to fetch namespaces") |
| 64 | + return nil, nil, err |
| 65 | + } |
| 66 | + pageInfo := &algorithms.PageInfo{ |
| 67 | + HasNext: resp.PageInfo.HasNextPage, |
| 68 | + After: resp.PageInfo.EndCursor, |
| 69 | + PageSize: size, |
| 70 | + } |
| 71 | + return resp.Edges, pageInfo, nil |
| 72 | + } |
| 73 | + return algorithms.NewPager[*console.ManagedNamespaceEdgeFragment](controller.DefaultPageSize, fetch) |
| 74 | +} |
| 75 | + |
| 76 | +func (n *NamespaceReconciler) Poll(ctx context.Context) (done bool, err error) { |
| 77 | + logger := log.FromContext(ctx) |
| 78 | + logger.Info("fetching namespaces") |
| 79 | + pager := n.ListNamespaces(ctx) |
| 80 | + |
| 81 | + for pager.HasNext() { |
| 82 | + namespaces, err := pager.NextPage() |
| 83 | + if err != nil { |
| 84 | + logger.Error(err, "failed to fetch namespace list") |
| 85 | + return false, nil |
| 86 | + } |
| 87 | + for _, namespace := range namespaces { |
| 88 | + logger.Info("sending update for", "namespace", namespace.Node.ID) |
| 89 | + n.NamespaceQueue.Add(namespace.Node.ID) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return false, nil |
| 94 | +} |
| 95 | + |
| 96 | +func (n *NamespaceReconciler) Reconcile(ctx context.Context, id string) (reconcile.Result, error) { |
| 97 | + logger := log.FromContext(ctx) |
| 98 | + logger.Info("attempting to sync namespace", "id", id) |
| 99 | + namespace, err := n.NamespaceCache.Get(id) |
| 100 | + if err != nil { |
| 101 | + if clienterrors.IsNotFound(err) { |
| 102 | + logger.Info("namespace already deleted", "id", id) |
| 103 | + return reconcile.Result{}, nil |
| 104 | + } |
| 105 | + logger.Error(err, fmt.Sprintf("failed to fetch namespace: %s, ignoring for now", id)) |
| 106 | + return reconcile.Result{}, err |
| 107 | + } |
| 108 | + logger.Info("upsert namespace", "name", namespace.Name) |
| 109 | + if err = n.UpsertNamespace(ctx, namespace); err != nil { |
| 110 | + return reconcile.Result{}, err |
| 111 | + } |
| 112 | + |
| 113 | + return reconcile.Result{}, nil |
| 114 | +} |
| 115 | + |
| 116 | +func (n *NamespaceReconciler) UpsertNamespace(ctx context.Context, fragment *console.ManagedNamespaceFragment) error { |
| 117 | + var labels map[string]string |
| 118 | + var annotations map[string]string |
| 119 | + |
| 120 | + if fragment.Labels != nil { |
| 121 | + labels = convertMap(fragment.Labels) |
| 122 | + } |
| 123 | + if fragment.Annotations != nil { |
| 124 | + annotations = convertMap(fragment.Annotations) |
| 125 | + } |
| 126 | + existing := &v1.Namespace{} |
| 127 | + err := n.K8sClient.Get(ctx, ctrlclient.ObjectKey{Name: fragment.Name}, existing) |
| 128 | + if err != nil { |
| 129 | + if apierrors.IsNotFound(err) { |
| 130 | + if err := n.K8sClient.Create(ctx, &v1.Namespace{ |
| 131 | + ObjectMeta: metav1.ObjectMeta{ |
| 132 | + Name: fragment.Name, |
| 133 | + Labels: labels, |
| 134 | + Annotations: annotations, |
| 135 | + }, |
| 136 | + }); err != nil { |
| 137 | + return err |
| 138 | + } |
| 139 | + return nil |
| 140 | + } |
| 141 | + return err |
| 142 | + } |
| 143 | + |
| 144 | + if !reflect.DeepEqual(labels, existing.Labels) || !reflect.DeepEqual(annotations, existing.Annotations) { |
| 145 | + existing.Labels = labels |
| 146 | + existing.Annotations = annotations |
| 147 | + if err := n.K8sClient.Update(ctx, existing); err != nil { |
| 148 | + return err |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return nil |
| 153 | +} |
| 154 | + |
| 155 | +func convertMap(in map[string]interface{}) map[string]string { |
| 156 | + res := make(map[string]string) |
| 157 | + for k, v := range in { |
| 158 | + value, ok := v.(string) |
| 159 | + if ok { |
| 160 | + res[k] = value |
| 161 | + } |
| 162 | + } |
| 163 | + return res |
| 164 | +} |
0 commit comments