Skip to content

Commit

Permalink
add support to run fluentbit as daemonset
Browse files Browse the repository at this point in the history
Signed-off-by: karan k <karan.k@oracle.com>
  • Loading branch information
karan56625 committed Jul 24, 2023
1 parent 1630d7b commit 42023be
Show file tree
Hide file tree
Showing 16 changed files with 7,195 additions and 15 deletions.
25 changes: 25 additions & 0 deletions apis/fluentbit/v1alpha2/zz_generated.deepcopy.go

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

11 changes: 11 additions & 0 deletions apis/fluentd/v1alpha1/fluentd_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type FluentdSpec struct {
// By default will build the related service according to the globalinputs definition.
DisableService bool `json:"disableService,omitempty"`
// Numbers of the Fluentd instance
// Applicable when the mode is "collector", and will be ignored when the mode is "agent"
Replicas *int32 `json:"replicas,omitempty"`
// Numbers of the workers in Fluentd instance
Workers *int32 `json:"workers,omitempty"`
Expand Down Expand Up @@ -92,13 +93,23 @@ type FluentdSpec struct {
// claims in a way that maintains the identity of a pod. Every claim in
// this list must have at least one matching (by name) volumeMount in one
// container in the template.
// Applicable when the mode is "collector", and will be ignored when the mode is "agent"
VolumeClaimTemplates []corev1.PersistentVolumeClaim `json:"volumeClaimTemplates,omitempty"`
// Service represents configurations on the fluentd service.
Service FluentDService `json:"service,omitempty"`
// PodSecurityContext represents the security context for the fluentd pods.
SecurityContext *corev1.PodSecurityContext `json:"securityContext,omitempty"`
// SchedulerName represents the desired scheduler for fluentd pods.
SchedulerName string `json:"schedulerName,omitempty"`
// Mode to determine whether to run Fluentd as collector or agent.
// +kubebuilder:validation:Enum:=collector;agent
// +kubebuilder:default:=collector
Mode string `json:"mode,omitempty"`
// ContainerSecurityContext represents the security context for the fluentd container.
ContainerSecurityContext *corev1.SecurityContext `json:"containerSecurityContext,omitempty"`
// Storage for position db. You will use it if tail input is enabled.
// Applicable when the mode is "agent", and will be ignored when the mode is "collector"
PositionDB corev1.VolumeSource `json:"positionDB,omitempty"`
}

// FluentDService the service of the FluentD
Expand Down
6 changes: 6 additions & 0 deletions apis/fluentd/v1alpha1/zz_generated.deepcopy.go

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

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions charts/fluent-operator/templates/fluentd-fluentd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ spec:
port: {{ .Values.fluentd.port }}
replicas: {{ .Values.fluentd.replicas }}
image: {{ .Values.fluentd.image.repository }}:{{ .Values.fluentd.image.tag }}
mode: {{ .Values.fluentd.mode }}
resources:
{{- toYaml .Values.fluentd.resources | nindent 4 }}
fluentdCfgSelector:
Expand Down
6 changes: 6 additions & 0 deletions charts/fluent-operator/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,16 @@ fluentd:
crdsEnable: true
enable: false
name: fluentd
# Valid modes include "collector" and "agent".
# The "collector" mode will deploy Fluentd as a StatefulSet as before.
# The new "agent" mode will deploy Fluentd as a DaemonSet.
mode: "collector"
port: 24224
image:
repository: "kubesphere/fluentd"
tag: "v1.15.3"
# Numbers of the Fluentd instance
# Applicable when the mode is "collector", and will be ignored when the mode is "agent"
replicas: 1
forward:
port: 24224
Expand Down
1,716 changes: 1,715 additions & 1 deletion config/crd/bases/fluentd.fluent.io_fluentds.yaml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions controllers/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ var (
fluentdOwnerKey = ".fluentd.metadata.controller"
fluentbitApiGVStr = fluentbitv1alpha2.SchemeGroupVersion.String()
fluentdApiGVStr = fluentdv1alpha1.SchemeGroupVersion.String()
fluentdAgentMode = "agent"
)
22 changes: 21 additions & 1 deletion controllers/fluent_controller_finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ func (r *FluentdReconciler) delete(ctx context.Context, fd *fluentdv1alpha1.Flue
return err
}

ds := appsv1.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Name: fd.Name,
Namespace: fd.Namespace,
},
}
if err := r.Delete(ctx, &ds); err != nil && !errors.IsNotFound(err) {
return err
}

svc := corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: fd.Name,
Expand Down Expand Up @@ -129,8 +139,18 @@ func (r *FluentdReconciler) mutate(obj client.Object, fd *fluentdv1alpha1.Fluent
return nil
}
case *appsv1.StatefulSet:
expected := operator.MakeStatefulset(*fd)
expected := operator.MakeStatefulSet(*fd)

return func() error {
o.Labels = expected.Labels
o.Spec = expected.Spec
if err := ctrl.SetControllerReference(fd, o, r.Scheme); err != nil {
return err
}
return nil
}
case *appsv1.DaemonSet:
expected := operator.MakeFluentdDaemonSet(*fd)
return func() error {
o.Labels = expected.Labels
o.Spec = expected.Spec
Expand Down
58 changes: 51 additions & 7 deletions controllers/fluentd_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,44 @@ func (r *FluentdReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
return ctrl.Result{}, err
}

// Deploy Fluentd Statefulset
sts := operator.MakeStatefulset(fd)
if _, err := controllerutil.CreateOrPatch(ctx, r.Client, sts, r.mutate(sts, &fd)); err != nil {
return ctrl.Result{}, err
var err error
if fd.Spec.Mode == "agent" {
// Deploy Fluentd DaemonSet
ds := operator.MakeFluentdDaemonSet(fd)
_, err = controllerutil.CreateOrPatch(ctx, r.Client, ds, r.mutate(ds, &fd))
if err != nil {
return ctrl.Result{}, err
}
sts := appsv1.StatefulSet{

This comment has been minimized.

Copy link
@cw-Guo

cw-Guo Nov 2, 2023

Collaborator

there is a mismatch. appsv1.StatefulSet vs MakeFluentdDaemonSet

ObjectMeta: metav1.ObjectMeta{
Name: fd.Name,
Namespace: fd.Namespace,
},
}
if err = r.Delete(ctx, &sts); err != nil && !errors.IsNotFound(err) {
return ctrl.Result{}, err
}
} else {
// Deploy Fluentd StatefulSet
sts := operator.MakeStatefulSet(fd)
_, err = controllerutil.CreateOrPatch(ctx, r.Client, sts, r.mutate(sts, &fd))
ds := appsv1.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Name: fd.Name,
Namespace: fd.Namespace,
},
}
if err = r.Delete(ctx, &ds); err != nil && !errors.IsNotFound(err) {
return ctrl.Result{}, err
}
}

// Deploy Fluentd Service
if !fd.Spec.DisableService {
svc := operator.MakeFluentdService(fd)
if _, err := controllerutil.CreateOrPatch(ctx, r.Client, svc, r.mutate(svc, &fd)); err != nil {
return ctrl.Result{}, err
if len(svc.Spec.Ports) > 0 {
if _, err = controllerutil.CreateOrPatch(ctx, r.Client, svc, r.mutate(svc, &fd)); err != nil {
return ctrl.Result{}, err
}
}
}

Expand All @@ -139,6 +166,22 @@ func (r *FluentdReconciler) SetupWithManager(mgr ctrl.Manager) error {
return err
}

if err := mgr.GetFieldIndexer().IndexField(context.Background(), &appsv1.DaemonSet{}, fluentdOwnerKey, func(rawObj client.Object) []string {
// grab the job object, extract the owner.
ds := rawObj.(*appsv1.DaemonSet)
owner := metav1.GetControllerOf(ds)
if owner == nil {
return nil
}

if owner.APIVersion != fluentdApiGVStr || owner.Kind != "Fluentd" {
return nil
}
return []string{owner.Name}
}); err != nil {
return err
}

if err := mgr.GetFieldIndexer().IndexField(context.Background(), &appsv1.StatefulSet{}, fluentdOwnerKey, func(rawObj client.Object) []string {
// grab the job object, extract the owner.
sts := rawObj.(*appsv1.StatefulSet)
Expand Down Expand Up @@ -174,6 +217,7 @@ func (r *FluentdReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&fluentdv1alpha1.Fluentd{}).
Owns(&corev1.ServiceAccount{}).
Owns(&appsv1.DaemonSet{}).
Owns(&appsv1.StatefulSet{}).
Owns(&corev1.Service{}).
Complete(r)
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ require (
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.7.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/term v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.9.1 // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
Expand All @@ -72,7 +74,9 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.26.1 // indirect
k8s.io/code-generator v0.26.1 // indirect
k8s.io/component-base v0.26.1 // indirect
k8s.io/gengo v0.0.0-20220902162205-c0856e24416d // indirect
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f // indirect
k8s.io/utils v0.0.0-20230209194617-a36077c30491 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
Expand Down
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA=
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/go-logr/logr v0.2.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU=
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
Expand Down Expand Up @@ -349,6 +350,8 @@ golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzB
golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk=
golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -503,6 +506,7 @@ golang.org/x/tools v0.0.0-20200304193943-95d2e580d8eb/go.mod h1:o4KQGtdN14AW+yjs
golang.org/x/tools v0.0.0-20200312045724-11d5b4c81c7d/go.mod h1:o4KQGtdN14AW+yjsvvwRTJJuXz8XRtIHtEnmAXLyFUw=
golang.org/x/tools v0.0.0-20200331025713-a30bf2db82d4/go.mod h1:Sl4aGygMT6LrqrWclx+PTx3U+LnKx/seiNR+3G19Ar8=
golang.org/x/tools v0.0.0-20200501065659-ab2804fb9c9d/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200505023115-26f46d2f7ef8/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200512131952-2bc93b1c0c88/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200515010526-7d3b6ebf133d/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200618134242-20370b0cb4b2/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
Expand All @@ -513,6 +517,7 @@ golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc
golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.9.1 h1:8WMNJAz3zrtPmnYC7ISf5dEn3MT0gY7jBJfw27yrrLo=
golang.org/x/tools v0.9.1/go.mod h1:owI94Op576fPu3cIGQeHs3joujW/2Oc6MtlxbF5dfNc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down Expand Up @@ -640,8 +645,13 @@ k8s.io/apimachinery v0.27.3 h1:Ubye8oBufD04l9QnNtW05idcOe9Z3GQN8+7PqmuVcUM=
k8s.io/apimachinery v0.27.3/go.mod h1:XNfZ6xklnMCOGGFNqXG7bUrQCoR04dh/E7FprV6pb+E=
k8s.io/client-go v0.26.3 h1:k1UY+KXfkxV2ScEL3gilKcF7761xkYsSD6BC9szIu8s=
k8s.io/client-go v0.26.3/go.mod h1:ZPNu9lm8/dbRIPAgteN30RSXea6vrCpFvq+MateTUuQ=
k8s.io/code-generator v0.26.1 h1:dusFDsnNSKlMFYhzIM0jAO1OlnTN5WYwQQ+Ai12IIlo=
k8s.io/code-generator v0.26.1/go.mod h1:OMoJ5Dqx1wgaQzKgc+ZWaZPfGjdRq/Y3WubFrZmeI3I=
k8s.io/component-base v0.26.1 h1:4ahudpeQXHZL5kko+iDHqLj/FSGAEUnSVO0EBbgDd+4=
k8s.io/component-base v0.26.1/go.mod h1:VHrLR0b58oC035w6YQiBSbtsf0ThuSwXP+p5dD/kAWU=
k8s.io/gengo v0.0.0-20220902162205-c0856e24416d h1:U9tB195lKdzwqicbJvyJeOXV7Klv+wNAWENRnXEGi08=
k8s.io/gengo v0.0.0-20220902162205-c0856e24416d/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E=
k8s.io/klog/v2 v2.2.0/go.mod h1:Od+F08eJP+W3HUb4pSrPpgp9DGU4GzlpG/TmITuYh/Y=
k8s.io/klog/v2 v2.100.1 h1:7WCHKK6K8fNhTqfBhISHQ97KrnJNFZMcQvKp7gP/tmg=
k8s.io/klog/v2 v2.100.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f h1:2kWPakN3i/k81b0gvD5C5FJ2kxm1WrQFanWchyKuqGg=
Expand All @@ -657,5 +667,6 @@ sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMm
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0=
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 h1:PRbqxJClWWYMNV1dhaG4NsibJbArud9kFxnAMREiWFE=
sigs.k8s.io/structured-merge-diff/v4 v4.2.3/go.mod h1:qjx8mGObPmV2aSZepjQjbmb2ihdVs8cGKBraizNC69E=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo=
sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8=
Loading

0 comments on commit 42023be

Please sign in to comment.