Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 3107cbf

Browse files
committed
reworking kustomization support
1 parent 0e1ec5a commit 3107cbf

File tree

12 files changed

+393
-76
lines changed

12 files changed

+393
-76
lines changed

.github/workflows/gotest-cover.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: golangci-lint
1+
name: go-test-cover
22

33
on:
44
pull_request:

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ CAPI_KIND_CLUSTER_NAME ?= capi-test
231231
# It is set by Prow GIT_TAG, a git-based tag of the form vYYYYMMDD-hash, e.g., v20210120-v0.3.10-308-gc61521971
232232

233233
# Next release is: v1.0.0-preview
234-
TAG ?= v1.0.0-preview.16
234+
TAG ?= v1.0.0-preview.34
235235
ARCH ?= $(shell go env GOARCH)
236236
ALL_ARCH = amd64 arm arm64
237237

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ spec:
6868

6969
Examples of `Cdk8sAppProxy` usage can be found in the `/examples` directory in this repository.
7070

71-
- [`cdk8sappproxy_sample.yaml`](./examples/cdk8sappproxy_sample.yaml): This example demonstrates how to deploy a sample cdk8s application from a public Git repository to clusters matching a specific label selector. It shows the usage of the `gitRepository` field.
71+
- [`examples/cdk8sappproxy_sample-go.yaml`](./examples/cdk8sappproxy_sample-go.yaml): This example demonstrates how to deploy a sample cdk8s application from a public Git repository to clusters matching a specific label selector. It shows the usage of the `gitRepository` field.
72+
- [`examples/cdk8sappproxy_sample-typescript.yaml`](./examples/cdk8s_sample-typescript.yaml): This directory contains a sample cdk8s application written in Typescript, which also generates a kustomization file.
7273

7374
## 🤗 Community, discussion, contribution, and support
7475

config/default/manager_image_patch.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ spec:
77
template:
88
spec:
99
containers:
10-
- image: ghcr.io/patricklaabs/cluster-api-addon-provider-cdk8s/cluster-api-cdk8s-controller:v1.0.0-preview.16
10+
- image: ghcr.io/patricklaabs/cluster-api-addon-provider-cdk8s/cluster-api-cdk8s-controller:v1.0.0-preview.34
1111
name: manager

config/default/manager_image_patch.yaml-e

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ spec:
77
template:
88
spec:
99
containers:
10-
- image: ghcr.io/patricklaabs/cluster-api-addon-provider-cdk8s/cluster-api-cdk8s-controller:v1.0.0-preview.16
10+
- image: ghcr.io/patricklaabs/cluster-api-addon-provider-cdk8s/cluster-api-cdk8s-controller:v1.0.0-preview.34
1111
name: manager

controllers/cdk8sappproxy/cdk8sappproxy_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func getPluralFromKind(kind string) string {
144144
// SetupWithManager sets up the controller with the Manager.
145145
func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
146146
// Initialize the new simplified watch system
147-
r.WatchManager = NewResourceWatchManager(mgr.GetClient())
147+
r.WatchManager = NewResourceWatchManager(mgr.GetClient(), r.Reconcile)
148148

149149
return ctrl.NewControllerManagedBy(mgr).
150150
For(&addonsv1alpha1.Cdk8sAppProxy{}).
@@ -178,7 +178,7 @@ func (r *Reconciler) ClusterToCdk8sAppProxyMapper(ctx context.Context, o client.
178178
if err := r.List(ctx, proxies); err != nil { // staticcheck: QF1008
179179
logger.Error(err, "failed to list Cdk8sAppProxies")
180180

181-
return nil
181+
return requests
182182
}
183183
logger.Info("Checking Cdk8sAppProxies for matches", "count", len(proxies.Items))
184184

controllers/cdk8sappproxy/cdk8sappproxy_reconciler.go

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"sigs.k8s.io/cluster-api/util/conditions"
1919
ctrl "sigs.k8s.io/controller-runtime"
2020
"sigs.k8s.io/controller-runtime/pkg/client"
21-
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
2221
"sigs.k8s.io/controller-runtime/pkg/log"
2322
)
2423

@@ -36,7 +35,6 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
3635
gitImpl := &gitoperator.GitImplementer{}
3736
synthImpl := &synthesizer.Implementer{}
3837
cdk8sAppProxy := &addonsv1alpha1.Cdk8sAppProxy{}
39-
proxyNamespacedName := types.NamespacedName{Name: cdk8sAppProxy.Name, Namespace: cdk8sAppProxy.Namespace}
4038

4139
if err := r.Get(ctx, req.NamespacedName, cdk8sAppProxy); err != nil {
4240
if apierrors.IsNotFound(err) {
@@ -49,10 +47,12 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
4947
return ctrl.Result{}, errors.Wrapf(err, "failed to get Cdk8sAppProxy %s/%s", req.Namespace, req.Name)
5048
}
5149

52-
// Add finalizer if needed
53-
if shouldRequeue, err := r.ensureFinalizer(ctx, cdk8sAppProxy, logger); err != nil || shouldRequeue {
54-
return ctrl.Result{Requeue: shouldRequeue}, err
55-
}
50+
proxyNamespacedName := types.NamespacedName{Name: cdk8sAppProxy.Name, Namespace: cdk8sAppProxy.Namespace}
51+
52+
//// Add finalizer if needed
53+
//if shouldRequeue, err := r.ensureFinalizer(ctx, cdk8sAppProxy, logger); err != nil || shouldRequeue {
54+
// return ctrl.Result{Requeue: shouldRequeue}, err
55+
//}
5656

5757
if cdk8sAppProxy.Spec.GitRepository.URL == "" {
5858
logger.Info("GitRepository URL is not specified in Cdk8sAppProxy spec", "cdk8sAppProxy", cdk8sAppProxy.Name)
@@ -68,13 +68,13 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
6868
logger.Error(err, "failed to synthesize resources")
6969
}
7070

71-
if len(parsedResources) == 0 {
72-
if err := r.handleNoResources(ctx, cdk8sAppProxy, logger); err != nil {
73-
return ctrl.Result{}, err
74-
}
75-
76-
return ctrl.Result{}, nil
77-
}
71+
//if len(parsedResources) == 0 {
72+
// if err := r.handleNoResources(ctx, cdk8sAppProxy, logger); err != nil {
73+
// return ctrl.Result{}, err
74+
// }
75+
//
76+
// return ctrl.Result{}, nil
77+
//}
7878

7979
// Determine if apply is needed
8080
clusterList, err := r.applyNeeded(ctx, cdk8sAppProxy, parsedResources, logger)
@@ -120,20 +120,20 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
120120
logger.Error(err, "failed to clone git repository")
121121
}
122122

123-
parsedResources, err := synthImpl.Synthesize(directory, logger)
123+
parsedResources, err = synthImpl.Synthesize(directory, logger)
124124
if err != nil {
125125
logger.Error(err, "failed to synthesize resources")
126126
}
127127

128-
if len(parsedResources) == 0 {
129-
if err := r.handleNoResources(ctx, cdk8sAppProxy, logger); err != nil {
130-
logger.Error(err, "Failed to handle no resources case")
131-
132-
return
133-
}
134-
135-
logger.Info("No valid Kubernetes resources parsed from manifest files, skipping apply.")
136-
}
128+
//if len(parsedResources) == 0 {
129+
// if err := r.handleNoResources(ctx, cdk8sAppProxy, logger); err != nil {
130+
// logger.Error(err, "Failed to handle no resources case")
131+
//
132+
// return
133+
// }
134+
//
135+
// logger.Info("No valid Kubernetes resources parsed from manifest files, skipping apply.")
136+
//}
137137

138138
// Determine if apply is needed
139139
clusterList, err = r.applyNeeded(ctx, cdk8sAppProxy, parsedResources, logger)
@@ -159,34 +159,34 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
159159
return ctrl.Result{}, nil
160160
}
161161

162-
func (r *Reconciler) ensureFinalizer(ctx context.Context, cdk8sAppProxy *addonsv1alpha1.Cdk8sAppProxy, logger logr.Logger) (bool, error) {
163-
if !controllerutil.ContainsFinalizer(cdk8sAppProxy, Finalizer) {
164-
logger.Info("Adding finalizer", "finalizer", Finalizer)
165-
controllerutil.AddFinalizer(cdk8sAppProxy, Finalizer)
166-
if err := r.Update(ctx, cdk8sAppProxy); err != nil {
167-
logger.Error(err, "Failed to add finalizer")
168-
169-
return false, err
170-
}
171-
logger.Info("Successfully added finalizer")
172-
173-
return true, nil
174-
}
175-
176-
return false, nil
177-
}
178-
179-
func (r *Reconciler) handleNoResources(ctx context.Context, cdk8sAppProxy *addonsv1alpha1.Cdk8sAppProxy, logger logr.Logger) error {
180-
logger.Info("No valid Kubernetes resources parsed from manifest files")
181-
conditions.MarkFalse(cdk8sAppProxy, addonsv1alpha1.DeploymentProgressingCondition, addonsv1alpha1.NoResourcesParsedReason, clusterv1.ConditionSeverityWarning, "No valid Kubernetes resources found in manifests")
182-
if err := r.Status().Update(ctx, cdk8sAppProxy); err != nil {
183-
logger.Error(err, "Failed to update status after no resources parsed")
184-
185-
return err
186-
}
187-
188-
return nil
189-
}
162+
//func (r *Reconciler) ensureFinalizer(ctx context.Context, cdk8sAppProxy *addonsv1alpha1.Cdk8sAppProxy, logger logr.Logger) (bool, error) {
163+
// if !controllerutil.ContainsFinalizer(cdk8sAppProxy, Finalizer) {
164+
// logger.Info("Adding finalizer", "finalizer", Finalizer)
165+
// controllerutil.AddFinalizer(cdk8sAppProxy, Finalizer)
166+
// if err := r.Update(ctx, cdk8sAppProxy); err != nil {
167+
// logger.Error(err, "Failed to add finalizer")
168+
//
169+
// return false, err
170+
// }
171+
// logger.Info("Successfully added finalizer")
172+
//
173+
// return true, nil
174+
// }
175+
//
176+
// return false, nil
177+
//}
178+
179+
//func (r *Reconciler) handleNoResources(ctx context.Context, cdk8sAppProxy *addonsv1alpha1.Cdk8sAppProxy, logger logr.Logger) error {
180+
// logger.Info("No valid Kubernetes resources parsed from manifest files")
181+
// conditions.MarkFalse(cdk8sAppProxy, addonsv1alpha1.DeploymentProgressingCondition, addonsv1alpha1.NoResourcesParsedReason, clusterv1.ConditionSeverityWarning, "No valid Kubernetes resources found in manifests")
182+
// if err := r.Status().Update(ctx, cdk8sAppProxy); err != nil {
183+
// logger.Error(err, "Failed to update status after no resources parsed")
184+
//
185+
// return err
186+
// }
187+
//
188+
// return nil
189+
//}
190190

191191
func (r *Reconciler) applyNeeded(ctx context.Context, cdk8sAppProxy *addonsv1alpha1.Cdk8sAppProxy, parsedResources []*unstructured.Unstructured, logger logr.Logger) (clusterv1.ClusterList, error) {
192192
var clusterList clusterv1.ClusterList

controllers/cdk8sappproxy/cdk8sappproxy_resourcewatcher.go

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import (
55
"sync"
66
"time"
77

8-
addonsv1alpha1 "github.com/PatrickLaabs/cluster-api-addon-provider-cdk8s/api/v1alpha1"
98
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
109
"k8s.io/apimachinery/pkg/runtime/schema"
1110
"k8s.io/apimachinery/pkg/types"
1211
"k8s.io/apimachinery/pkg/watch"
1312
"k8s.io/client-go/dynamic"
13+
ctrl "sigs.k8s.io/controller-runtime"
1414
"sigs.k8s.io/controller-runtime/pkg/client"
1515
"sigs.k8s.io/controller-runtime/pkg/log"
1616
)
@@ -20,13 +20,24 @@ type ResourceWatchManager struct {
2020
mu sync.RWMutex
2121
watches map[types.NamespacedName]map[string]context.CancelFunc
2222
client client.Client
23+
queue workqueue.RateLimitingInterface
24+
}
25+
26+
// WatchInfo holds information about an active watch
27+
type WatchInfo struct {
28+
cancel context.CancelFunc
29+
targetClient dynamic.Interface
30+
gvk schema.GroupVersionKind
31+
namespace string
32+
name string
2333
}
2434

2535
// NewResourceWatchManager creates a new resource watch manager.
26-
func NewResourceWatchManager(client client.Client) *ResourceWatchManager {
36+
func NewResourceWatchManager(client client.Client, reconcileFunc func(context.Context, ctrl.Request) (ctrl.Result, error)) *ResourceWatchManager {
2737
return &ResourceWatchManager{
2838
watches: make(map[types.NamespacedName]map[string]context.CancelFunc),
2939
client: client,
40+
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "resource-deletion"),
3041
}
3142
}
3243

@@ -129,7 +140,7 @@ func (m *ResourceWatchManager) watchResource(ctx context.Context, targetClient d
129140
return nil
130141
}
131142

132-
// For other events (Added, Modified), just continue watching
143+
// For other events (Added, Modified), continue watching
133144
logger.V(1).Info("Received non-deletion event", "type", event.Type)
134145

135146
case <-ctx.Done():
@@ -140,20 +151,28 @@ func (m *ResourceWatchManager) watchResource(ctx context.Context, targetClient d
140151
}
141152
}
142153

143-
// triggerReconciliation triggers a reconciliation by updating the parent proxy.
154+
// triggerReconciliation simply logs the deletion - controller-runtime will handle the rest
144155
func (m *ResourceWatchManager) triggerReconciliation(ctx context.Context, parentProxy types.NamespacedName) error {
145-
proxy := &addonsv1alpha1.Cdk8sAppProxy{}
146-
if err := m.client.Get(ctx, parentProxy, proxy); err != nil {
147-
return err
148-
}
149-
150-
if proxy.Annotations == nil {
151-
proxy.Annotations = make(map[string]string)
156+
logger := log.FromContext(ctx).WithValues("parentProxy", parentProxy.String())
157+
158+
if m.reconcileFunc != nil {
159+
req := ctrl.Request{NamespacedName: parentProxy}
160+
go func() {
161+
reconcileCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
162+
defer cancel()
163+
164+
_, err := m.reconcileFunc(reconcileCtx, req)
165+
if err != nil {
166+
logger.Error(err, "Failed to reconcile after resource deletion")
167+
} else {
168+
logger.Info("Successfully triggered reconciliation after resource deletion")
169+
}
170+
}()
152171
}
153172

154-
proxy.Annotations["cdk8s.addons.cluster.x-k8s.io/reconcile-trigger"] = metav1.Now().Format(time.RFC3339Nano)
173+
logger.Info("Resource deletion detected, reconciliation triggered")
155174

156-
return m.client.Update(ctx, proxy)
175+
return nil
157176
}
158177

159178
func (m *ResourceWatchManager) isActive(parentProxy types.NamespacedName, watchKey string) bool {

0 commit comments

Comments
 (0)