Skip to content

Commit

Permalink
refactor: DaemonSet generator placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
rg0now committed Dec 17, 2024
1 parent 8795d04 commit 1b45166
Show file tree
Hide file tree
Showing 15 changed files with 462 additions and 361 deletions.
38 changes: 28 additions & 10 deletions api/v1/dataplane_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ func init() {
SchemeBuilder.Register(&Dataplane{}, &DataplaneList{})
}

type DataplaneResourceType string

const (
DataplaneResourceDeployment DataplaneResourceType = "Deployment"
DataplaneResourceDaemonSet DataplaneResourceType = "DaemonSet"
)

// +genclient
// +genclient:nonNamespaced
// +kubebuilder:object:root=true
Expand All @@ -46,7 +53,8 @@ type Dataplane struct {
Spec DataplaneSpec `json:"spec,omitempty"`
}

// this must be kept in sync with Renderer.createDeployment and Updater.upsertDeployment
// this must be kept in sync with Renderer.createDeployment and generateDaemonSet, as well as
// Updater.upsertDeployment and Updater.upsertDaemonSet

// DataplaneSpec describes the prefixes reachable via a Dataplane.
type DataplaneSpec struct {
Expand All @@ -69,18 +77,28 @@ type DataplaneSpec struct {
// +optional
ImagePullSecrets []corev1.LocalObjectReference `json:"imagePullSecrets,omitempty"`

// DataplaneResource defines the Kubernetes resource kind to use to deploy the dataplane,
// can be either Deployment (default) or DaemonSet (not supported in the free tier).
//
// +optional
// +kubebuilder:default=Deployment
// +kubebuilder:validation:Enum="Deployment";"DaemonSet"
DataplaneResource *DataplaneResourceType `json:"dataplaneResource,omitempty"`

// Custom labels to add to dataplane pods. Note that this does not affect the labels added
// to the Deployment (those come from the Gateway), just the pods. Note also that mandatory
// pod labels override whatever you set here on conflict. The only way to set pod labels is
// here: whatever you set manually on the dataplane pod will be reset by the opetator.
// to the dataplane resource (Deployment or DaemonSet) as those are copied from the
// Gateway, just the pods. Note also that mandatory pod labels override whatever you set
// here on conflict. The only way to set pod labels is here: whatever you set manually on
// the dataplane pod will be reset by the opetator.
//
// +optional
Labels map[string]string `json:"labels,omitempty"`

// Custom annotations to add to dataplane pods. Note that this does not affect the
// annotations added to the Deployment (this come from the correspnding Gateway), just the
// pods. Note also that mandatory pod annotations override whatever you set here on
// conflict, and the annotations set here override annotations manually added to the pods.
// annotations added to the dataplane resource (Deployment or DaemonSet) as those are
// copied from the correspnding Gateway, just the pods. Note also that mandatory pod
// annotations override whatever you set here on conflict, and the annotations set here
// override annotations manually added to the pods.
//
// +optional
Annotations map[string]string `json:"annotations,omitempty"`
Expand Down Expand Up @@ -110,9 +128,9 @@ type DataplaneSpec struct {
// +optional
ContainerSecurityContext *corev1.SecurityContext `json:"containerSecurityContext,omitempty"`

// Number of desired pods. If empty or set to 1, use whatever is in the target Deployment.
// Otherwise, enforce this setting, overwiting whatever is set in the Deployment (this may
// block autoscaling the dataplane though). Defaults to 1.
// Number of desired pods. If empty or set to 1, use whatever is in the target Deployment,
// otherwise overwite whatever is in the Deployment (this may block autoscaling the
// dataplane though). Ignored if the dataplane is deployed into a DaemonSet. Defaults to 1.
//
// +optional
Replicas *int32 `json:"replicas,omitempty"`
Expand Down
5 changes: 5 additions & 0 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 20 additions & 9 deletions config/crd/bases/stunner.l7mp.io_dataplanes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -971,9 +971,10 @@ spec:
type: string
description: |-
Custom annotations to add to dataplane pods. Note that this does not affect the
annotations added to the Deployment (this come from the correspnding Gateway), just the
pods. Note also that mandatory pod annotations override whatever you set here on
conflict, and the annotations set here override annotations manually added to the pods.
annotations added to the dataplane resource (Deployment or DaemonSet) as those are
copied from the correspnding Gateway, just the pods. Note also that mandatory pod
annotations override whatever you set here on conflict, and the annotations set here
override annotations manually added to the pods.
type: object
args:
description: Arguments to the entrypoint.
Expand Down Expand Up @@ -1177,6 +1178,15 @@ spec:
type: string
type: object
type: object
dataplaneResource:
default: Deployment
description: |-
DataplaneResource defines the Kubernetes resource kind to use to deploy the dataplane,
can be either Deployment (default) or DaemonSet (not supported in the free tier).
enum:
- Deployment
- DaemonSet
type: string
disableHealthCheck:
description: |-
Disable health-checking. Default is to enable HTTP health-checks on port 8086: a
Expand Down Expand Up @@ -1394,15 +1404,16 @@ spec:
type: string
description: |-
Custom labels to add to dataplane pods. Note that this does not affect the labels added
to the Deployment (those come from the Gateway), just the pods. Note also that mandatory
pod labels override whatever you set here on conflict. The only way to set pod labels is
here: whatever you set manually on the dataplane pod will be reset by the opetator.
to the dataplane resource (Deployment or DaemonSet) as those are copied from the
Gateway, just the pods. Note also that mandatory pod labels override whatever you set
here on conflict. The only way to set pod labels is here: whatever you set manually on
the dataplane pod will be reset by the opetator.
type: object
replicas:
description: |-
Number of desired pods. If empty or set to 1, use whatever is in the target Deployment.
Otherwise, enforce this setting, overwiting whatever is set in the Deployment (this may
block autoscaling the dataplane though). Defaults to 1.
Number of desired pods. If empty or set to 1, use whatever is in the target Deployment,
otherwise overwite whatever is in the Deployment (this may block autoscaling the
dataplane though). Ignored if the dataplane is deployed into a DaemonSet. Defaults to 1.
format: int32
type: integer
resources:
Expand Down
3 changes: 3 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ rules:
- apiGroups:
- apps
resources:
- daemonsets
- deployments
verbs:
- create
Expand All @@ -52,6 +53,8 @@ rules:
- apiGroups:
- apps
resources:
- daemonsets/finalizers
- daemonsets/status
- deployments/finalizers
- deployments/status
verbs:
Expand Down
50 changes: 38 additions & 12 deletions internal/controllers/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,17 @@ func NewGatewayController(mgr manager.Manager, ch chan event.Event, log logr.Log
); err != nil {
return nil, err
}
r.log.Info("Watching Deployment objects")
r.log.Info("Watching dataplane Deployment objects")

// watch Deployment objects referenced by one of our Gateways
if err := c.Watch(
source.Kind(mgr.GetCache(), &appv1.DaemonSet{},
&handler.TypedEnqueueRequestForObject[*appv1.DaemonSet]{},
predicate.NewTypedPredicateFuncs[*appv1.DaemonSet](r.validateDaemonSetForReconcile)),
); err != nil {
return nil, err
}
r.log.Info("Watching dataplane DaemonSet objects")
}

// NOTE: LoadBalancer Service resources are watched by the UDPRoute controller (together
Expand All @@ -145,6 +155,7 @@ func (r *gatewayReconciler) Reconcile(ctx context.Context, req reconcile.Request
gatewayList := []client.Object{}
secretList := []client.Object{}
deploymentList := []client.Object{}
daemonSetList := []client.Object{}

// find Gateways managed by this controller
gwClasses := &gwapiv1.GatewayClassList{}
Expand Down Expand Up @@ -233,13 +244,17 @@ func (r *gatewayReconciler) Reconcile(ctx context.Context, req reconcile.Request
}

if config.DataplaneMode == config.DataplaneModeManaged {
deploymentName := store.GetNamespacedName(&gw)
resourceName := store.GetNamespacedName(&gw)

// does the gateway actually exist?
dp := &appv1.Deployment{}
if err := r.Get(context.Background(), deploymentName, dp); err == nil {
if err := r.Get(context.Background(), resourceName, dp); err == nil {
deploymentList = append(deploymentList, dp)
}

ds := &appv1.DaemonSet{}
if err := r.Get(context.Background(), resourceName, ds); err == nil {
daemonSetList = append(daemonSetList, dp)
}
}
}
}
Expand All @@ -257,6 +272,9 @@ func (r *gatewayReconciler) Reconcile(ctx context.Context, req reconcile.Request
store.Deployments.Reset(deploymentList)
r.log.V(2).Info("reset Deployment store", "deployments", store.Deployments.String())

store.DaemonSets.Reset(daemonSetList)
r.log.V(2).Info("reset DaemonSet store", "daemonSets", store.DaemonSets.String())

r.eventCh <- event.NewEventReconcile()

return reconcile.Result{}, nil
Expand Down Expand Up @@ -311,25 +329,33 @@ func (r *gatewayReconciler) validateSecretForReconcile(secret *corev1.Secret) bo
return false
}

// validateDeploymentForReconcile checks whether there is a Gateway with the same name as the
// Deployment and the Deployment is owned by us.
func (r *gatewayReconciler) validateDeploymentForReconcile(deployment *appv1.Deployment) bool {
// we don't watch Deployments in legacy mode
return r.validateDataplaneResourceForReconcile(deployment)
}

func (r *gatewayReconciler) validateDaemonSetForReconcile(daemonSet *appv1.DaemonSet) bool {
return r.validateDataplaneResourceForReconcile(daemonSet)
}

// validateDataplaneResourceForReconcile checks whether there is a Gateway with the same name as
// the dataplane resource (Deployment or DaemonSet) and the resource is owned by us.
func (r *gatewayReconciler) validateDataplaneResourceForReconcile(obj client.Object) bool {
// we don't watch dataplane resources in legacy mode
if config.DataplaneMode != config.DataplaneModeManaged {
return false
}

// is deployment owned by us?
val, ok := deployment.GetLabels()[opdefault.OwnedByLabelKey]
// is the dataplane resource owned by us?
val, ok := obj.GetLabels()[opdefault.OwnedByLabelKey]
if !ok || val != opdefault.OwnedByLabelValue {
return false
}

// gateway has the same name as the deployment
gatewayName := store.GetNamespacedName(deployment)
// gateway has the same name as the dataplane resource
gatewayName := store.GetNamespacedName(obj)

// is the related-gateway annotation set?
val, ok = deployment.GetAnnotations()[opdefault.RelatedGatewayKey]
val, ok = obj.GetAnnotations()[opdefault.RelatedGatewayKey]
if !ok || val != gatewayName.String() {
return false
}
Expand Down
2 changes: 2 additions & 0 deletions internal/controllers/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ package controllers
// apps
// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=apps,resources=deployments/status;deployments/finalizers,verbs=get;list;watch
// +kubebuilder:rbac:groups=apps,resources=daemonsets,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=apps,resources=daemonsets/status;daemonsets/finalizers,verbs=get;list;watch

// discovery.k8s.io
// +kubebuilder:rbac:groups=discovery.k8s.io,resources=endpointslices,verbs=get;list;watch
Expand Down
13 changes: 9 additions & 4 deletions internal/event/event_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type UpdateConf struct {
Services *store.ServiceStore
ConfigMaps *store.ConfigMapStore
Deployments *store.DeploymentStore
DaemonSets *store.DaemonSetStore
}

type EventUpdate struct {
Expand All @@ -41,6 +42,7 @@ func NewEventUpdate(generation int) *EventUpdate {
Services: store.NewServiceStore(),
ConfigMaps: store.NewConfigMapStore(),
Deployments: store.NewDeploymentStore(),
DaemonSets: store.NewDaemonSetStore(),
},
DeleteQueue: UpdateConf{
GatewayClasses: store.NewGatewayClassStore(),
Expand All @@ -50,6 +52,7 @@ func NewEventUpdate(generation int) *EventUpdate {
Services: store.NewServiceStore(),
ConfigMaps: store.NewConfigMapStore(),
Deployments: store.NewDeploymentStore(),
DaemonSets: store.NewDaemonSetStore(),
},
ConfigQueue: []*stnrv1.StunnerConfig{},
Generation: generation,
Expand All @@ -63,18 +66,18 @@ func (e *EventUpdate) GetType() EventType {

func (e *EventUpdate) String() string {
return fmt.Sprintf("%s (gen: %d, ack: %t): upsert-queue: gway-cls: %d, gway: %d, "+
"route: %d, routeV1A2: %d, svc: %d, confmap: %d, dp: %d / "+
"route: %d, routeV1A2: %d, svc: %d, confmap: %d, dp: %d, ds: %d / "+
"delete-queue: gway-cls: %d, gway: %d, route: %d, routeV1A2: %d, "+
"svc: %d, confmap: %d, dp: %d / config-queue: %d",
"svc: %d, confmap: %d, dp: %d, ds: %d / config-queue: %d",
e.Type.String(), e.Generation, e.RequestAck,
e.UpsertQueue.GatewayClasses.Len(), e.UpsertQueue.Gateways.Len(),
e.UpsertQueue.UDPRoutes.Len(), e.UpsertQueue.UDPRoutesV1A2.Len(),
e.UpsertQueue.Services.Len(), e.UpsertQueue.ConfigMaps.Len(),
e.UpsertQueue.Deployments.Len(),
e.UpsertQueue.Deployments.Len(), e.UpsertQueue.DaemonSets.Len(),
e.DeleteQueue.GatewayClasses.Len(), e.DeleteQueue.Gateways.Len(),
e.DeleteQueue.UDPRoutes.Len(), e.DeleteQueue.UDPRoutesV1A2.Len(),
e.DeleteQueue.Services.Len(), e.DeleteQueue.ConfigMaps.Len(),
e.DeleteQueue.Deployments.Len(),
e.DeleteQueue.Deployments.Len(), e.DeleteQueue.DaemonSets.Len(),
len(e.ConfigQueue))
}

Expand All @@ -91,6 +94,7 @@ func (e *EventUpdate) DeepCopy() *EventUpdate {
u.UpsertQueue.Services = q.Services.DeepCopy()
u.UpsertQueue.ConfigMaps = q.ConfigMaps.DeepCopy()
u.UpsertQueue.Deployments = q.Deployments.DeepCopy()
u.UpsertQueue.DaemonSets = q.DaemonSets.DeepCopy()

q = e.DeleteQueue
u.DeleteQueue.GatewayClasses = q.GatewayClasses.DeepCopy()
Expand All @@ -100,6 +104,7 @@ func (e *EventUpdate) DeepCopy() *EventUpdate {
u.DeleteQueue.Services = q.Services.DeepCopy()
u.DeleteQueue.ConfigMaps = q.ConfigMaps.DeepCopy()
u.DeleteQueue.Deployments = q.Deployments.DeepCopy()
u.DeleteQueue.DaemonSets = q.DaemonSets.DeepCopy()

u.ConfigQueue = make([]*stnrv1.StunnerConfig, len(e.ConfigQueue))
copy(u.ConfigQueue, e.ConfigQueue)
Expand Down
Loading

0 comments on commit 1b45166

Please sign in to comment.