Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow clowder controlled statefulsets #1103

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apis/cloud.redhat.com/v1alpha1/clowdapp_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ type Deployment struct {
// deployment has no public service enabled
DeploymentStrategy *DeploymentStrategy `json:"deploymentStrategy,omitempty"`

// If set to true, creates an StatefulSet resource rather than
// a standard OpenShift Deployment resource
UseStatefulSet bool `json:"useStatefulSet,omitempty"`

Metadata DeploymentMetadata `json:"metadata,omitempty"`
}

Expand Down
2 changes: 2 additions & 0 deletions controllers/cloud.redhat.com/clowdapp_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import (
_ "github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/providers/serviceaccount"
_ "github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/providers/servicemesh"
_ "github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/providers/sidecar"
_ "github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/providers/statefulset"
_ "github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/providers/web"

crd "github.com/RedHatInsights/clowder/apis/cloud.redhat.com/v1alpha1"
Expand Down Expand Up @@ -202,6 +203,7 @@ func (r *ClowdAppReconciler) SetupWithManager(mgr ctrl.Manager) error {

watchers := []Watcher{}
watchers = append(watchers, Watcher{obj: &apps.Deployment{}, filter: deploymentFilter})
watchers = append(watchers, Watcher{obj: &apps.StatefulSet{}, filter: statefulSetFilter})
watchers = append(watchers, Watcher{obj: &core.Service{}, filter: generationOnlyFilter})
watchers = append(watchers, Watcher{obj: &core.ConfigMap{}, filter: generationOnlyFilter})
watchers = append(watchers, Watcher{obj: &core.Secret{}, filter: alwaysFilter})
Expand Down
2 changes: 2 additions & 0 deletions controllers/cloud.redhat.com/clowdenvironment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import (
_ "github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/providers/serviceaccount"
_ "github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/providers/servicemesh"
_ "github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/providers/sidecar"
_ "github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/providers/statefulset"
_ "github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/providers/web"

crd "github.com/RedHatInsights/clowder/apis/cloud.redhat.com/v1alpha1"
Expand Down Expand Up @@ -232,6 +233,7 @@ func (r *ClowdEnvironmentReconciler) SetupWithManager(mgr ctrl.Manager) error {

watchers := []Watcher{}
watchers = append(watchers, Watcher{obj: &apps.Deployment{}, filter: deploymentFilter})
watchers = append(watchers, Watcher{obj: &apps.StatefulSet{}, filter: statefulSetFilter})
watchers = append(watchers, Watcher{obj: &core.Service{}, filter: alwaysFilter})
watchers = append(watchers, Watcher{obj: &core.Secret{}, filter: alwaysFilter})

Expand Down
18 changes: 18 additions & 0 deletions controllers/cloud.redhat.com/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ func deploymentUpdateFunc(e event.UpdateEvent) bool {
return false
}

func statefulSetUpdateFunc(e event.UpdateEvent) bool {
objOld := e.ObjectOld.(*apps.StatefulSet)
objNew := e.ObjectNew.(*apps.StatefulSet)
if objNew.GetGeneration() != objOld.GetGeneration() {
return true
}
if (objOld.Status.AvailableReplicas != objNew.Status.AvailableReplicas) && (objNew.Status.AvailableReplicas == objNew.Status.ReadyReplicas) {
return true
}
if (objOld.Status.AvailableReplicas == objOld.Status.ReadyReplicas) && (objNew.Status.AvailableReplicas != objNew.Status.ReadyReplicas) {
return true
}
return false
}
func kafkaUpdateFunc(e event.UpdateEvent) bool {
objOld := e.ObjectOld.(*strimzi.Kafka)
objNew := e.ObjectNew.(*strimzi.Kafka)
Expand Down Expand Up @@ -114,6 +128,10 @@ func deploymentFilter(logr logr.Logger, ctrlName string) HandlerFuncs {
return genFilterFunc(deploymentUpdateFunc, logr, ctrlName)
}

func statefulSetFilter(logr logr.Logger, ctrlName string) HandlerFuncs {
return genFilterFunc(statefulSetUpdateFunc, logr, ctrlName)
}

func kafkaFilter(logr logr.Logger, ctrlName string) HandlerFuncs {
return genFilterFunc(kafkaUpdateFunc, logr, ctrlName)
}
Expand Down
8 changes: 4 additions & 4 deletions controllers/cloud.redhat.com/providers/deployment/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ func (dp *deploymentProvider) EnvProvide() error {
}

func (dp *deploymentProvider) Provide(app *crd.ClowdApp) error {

for _, deployment := range app.Spec.Deployments {

if err := dp.makeDeployment(deployment, app); err != nil {
return err
if !deployment.UseStatefulSet {
if err := dp.makeDeployment(deployment, app); err != nil {
return err
}
}
}
return nil
Expand Down
40 changes: 31 additions & 9 deletions controllers/cloud.redhat.com/providers/metrics/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
crd "github.com/RedHatInsights/clowder/apis/cloud.redhat.com/v1alpha1"
"github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/config"
deployProvider "github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/providers/deployment"
statefulsetProvider "github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/providers/statefulset"
webProvider "github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/providers/web"

prom "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
Expand All @@ -28,9 +29,16 @@ func makeMetrics(cache *rc.ObjectCache, deployment *crd.Deployment, app *crd.Clo
}

d := &apps.Deployment{}
sts := &apps.StatefulSet{}

if err := cache.Get(deployProvider.CoreDeployment, d, app.GetDeploymentNamespacedName(deployment)); err != nil {
return err
if deployment.UseStatefulSet {
if err := cache.Get(statefulsetProvider.CoreStatefulSet, sts, app.GetDeploymentNamespacedName(deployment)); err != nil {
return err
}
} else {
if err := cache.Get(deployProvider.CoreDeployment, d, app.GetDeploymentNamespacedName(deployment)); err != nil {
return err
}
}

appProtocol := "http"
Expand All @@ -44,18 +52,32 @@ func makeMetrics(cache *rc.ObjectCache, deployment *crd.Deployment, app *crd.Clo

s.Spec.Ports = append(s.Spec.Ports, metricsPort)

d.Spec.Template.Spec.Containers[0].Ports = append(d.Spec.Template.Spec.Containers[0].Ports,
core.ContainerPort{
Name: "metrics",
ContainerPort: port,
Protocol: core.ProtocolTCP,
},
)
if deployment.UseStatefulSet {
sts.Spec.Template.Spec.Containers[0].Ports = append(sts.Spec.Template.Spec.Containers[0].Ports,
core.ContainerPort{
Name: "metrics",
ContainerPort: port,
Protocol: core.ProtocolTCP,
},
)
} else {
d.Spec.Template.Spec.Containers[0].Ports = append(d.Spec.Template.Spec.Containers[0].Ports,
core.ContainerPort{
Name: "metrics",
ContainerPort: port,
Protocol: core.ProtocolTCP,
},
)
}

if err := cache.Update(webProvider.CoreService, s); err != nil {
return err
}

if deployment.UseStatefulSet {
return cache.Update(statefulsetProvider.CoreStatefulSet, sts)
}

return cache.Update(deployProvider.CoreDeployment, d)
}

Expand Down
27 changes: 21 additions & 6 deletions controllers/cloud.redhat.com/providers/serviceaccount/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/providers/featureflags"
"github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/providers/inmemorydb"
"github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/providers/objectstore"
"github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/providers/statefulset"
apps "k8s.io/api/apps/v1"
core "k8s.io/api/core/v1"
rbac "k8s.io/api/rbac/v1"
Expand Down Expand Up @@ -115,11 +116,18 @@ func (sa *serviceaccountProvider) Provide(app *crd.ClowdApp) error {

for _, dep := range app.Spec.Deployments {
d := &apps.Deployment{}
sts := &apps.StatefulSet{}
innerDeployment := dep
nn := app.GetDeploymentNamespacedName(&innerDeployment)

if err := sa.Cache.Get(deployment.CoreDeployment, d, nn); err != nil {
return err
if innerDeployment.UseStatefulSet {
if err := sa.Cache.Get(statefulset.CoreStatefulSet, sts, nn); err != nil {
return err
}
} else {
if err := sa.Cache.Get(deployment.CoreDeployment, d, nn); err != nil {
return err
}
}

labeler := utils.GetCustomLabeler(nil, nn, app)
Expand All @@ -128,11 +136,18 @@ func (sa *serviceaccountProvider) Provide(app *crd.ClowdApp) error {
return err
}

d.Spec.Template.Spec.ServiceAccountName = nn.Name
if err := sa.Cache.Update(deployment.CoreDeployment, d); err != nil {
return err
}
if innerDeployment.UseStatefulSet {
sts.Spec.Template.Spec.ServiceAccountName = nn.Name
if err := sa.Cache.Update(statefulset.CoreStatefulSet, sts); err != nil {
return err
}
} else {

d.Spec.Template.Spec.ServiceAccountName = nn.Name
if err := sa.Cache.Update(deployment.CoreDeployment, d); err != nil {
return err
}
}
if err := CreateRoleBinding(sa.Cache, CoreDeploymentRoleBinding, nn, labeler, innerDeployment.K8sAccessLevel); err != nil {
return err
}
Expand Down
37 changes: 30 additions & 7 deletions controllers/cloud.redhat.com/providers/sidecar/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/providers"
cronjobProvider "github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/providers/cronjob"
deployProvider "github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/providers/deployment"
statefulsetProvider "github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/providers/statefulset"
provutils "github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/providers/utils"
"github.com/RedHatInsights/rhc-osdk-utils/utils"

Expand All @@ -32,9 +33,17 @@ func (sc *sidecarProvider) Provide(app *crd.ClowdApp) error {
for _, deployment := range app.Spec.Deployments {
innerDeployment := deployment
d := &apps.Deployment{}
sts := &apps.StatefulSet{}

if err := sc.Cache.Get(deployProvider.CoreDeployment, d, app.GetDeploymentNamespacedName(&innerDeployment)); err != nil {
return err
if innerDeployment.UseStatefulSet {
if err := sc.Cache.Get(statefulsetProvider.CoreStatefulSet, sts, app.GetDeploymentNamespacedName(&innerDeployment)); err != nil {
return err
}
} else {

if err := sc.Cache.Get(deployProvider.CoreDeployment, d, app.GetDeploymentNamespacedName(&innerDeployment)); err != nil {
return err
}
}

for _, sidecar := range innerDeployment.PodSpec.Sidecars {
Expand All @@ -43,15 +52,23 @@ func (sc *sidecarProvider) Provide(app *crd.ClowdApp) error {
if sidecar.Enabled && sc.Env.Spec.Providers.Sidecars.TokenRefresher.Enabled {
cont := getTokenRefresher(app.Name)
if cont != nil {
d.Spec.Template.Spec.Containers = append(d.Spec.Template.Spec.Containers, *cont)
if innerDeployment.UseStatefulSet {
sts.Spec.Template.Spec.Containers = append(sts.Spec.Template.Spec.Containers, *cont)
} else {
d.Spec.Template.Spec.Containers = append(d.Spec.Template.Spec.Containers, *cont)
}
}
}
case "otel-collector":
if sidecar.Enabled && sc.Env.Spec.Providers.Sidecars.OtelCollector.Enabled {
cont := getOtelCollector(app.Name)
if cont != nil {
d.Spec.Template.Spec.Containers = append(d.Spec.Template.Spec.Containers, *cont)
d.Spec.Template.Spec.Volumes = append(d.Spec.Template.Spec.Volumes, core.Volume{
if innerDeployment.UseStatefulSet {
sts.Spec.Template.Spec.Containers = append(sts.Spec.Template.Spec.Containers, *cont)
} else {
d.Spec.Template.Spec.Containers = append(d.Spec.Template.Spec.Containers, *cont)
}
innerDeployment.PodSpec.Volumes = append(innerDeployment.PodSpec.Volumes, core.Volume{
Name: fmt.Sprintf("%s-otel-config", app.Name),
VolumeSource: core.VolumeSource{
ConfigMap: &core.ConfigMapVolumeSource{
Expand All @@ -69,8 +86,14 @@ func (sc *sidecarProvider) Provide(app *crd.ClowdApp) error {
}
}

if err := sc.Cache.Update(deployProvider.CoreDeployment, d); err != nil {
return err
if innerDeployment.UseStatefulSet {
if err := sc.Cache.Update(statefulsetProvider.CoreStatefulSet, sts); err != nil {
return err
}
} else {
if err := sc.Cache.Update(deployProvider.CoreDeployment, d); err != nil {
return err
}
}
}

Expand Down
36 changes: 36 additions & 0 deletions controllers/cloud.redhat.com/providers/statefulset/default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package statefulset

import (
crd "github.com/RedHatInsights/clowder/apis/cloud.redhat.com/v1alpha1"
"github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/providers"
rc "github.com/RedHatInsights/rhc-osdk-utils/resourceCache"
apps "k8s.io/api/apps/v1"
)

type statefulSetProvider struct {
providers.Provider
}

// CoreStatefulSet is the statefulset object for the apps deployments.
var CoreStatefulSet = rc.NewMultiResourceIdent(ProvName, "core_statefulset", &apps.StatefulSet{})

func NewStatefulSetProvider(p *providers.Provider) (providers.ClowderProvider, error) {
p.Cache.AddPossibleGVKFromIdent(CoreStatefulSet)
return &statefulSetProvider{Provider: *p}, nil
}

func (dp *statefulSetProvider) EnvProvide() error {
return nil
}

func (dp *statefulSetProvider) Provide(app *crd.ClowdApp) error {

for _, deployment := range app.Spec.Deployments {
if deployment.UseStatefulSet {
if err := dp.makeStatefulSet(deployment, app); err != nil {
return err
}
}
}
return nil
}
Loading
Loading