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

[feature]add serviceType #376

Merged
merged 4 commits into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions api/v1beta1/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type KubernetesConfig struct {
ExistingPasswordSecret *ExistingPasswordSecret `json:"redisSecret,omitempty"`
ImagePullSecrets *[]corev1.LocalObjectReference `json:"imagePullSecrets,omitempty"`
UpdateStrategy appsv1.StatefulSetUpdateStrategy `json:"updateStrategy,omitempty"`
// +kubebuilder:validation:Enum=LoadBalancer;NodePort;ClusterIP
ServiceType string `json:"serviceType,omitempty"`
iamabhishek-dubey marked this conversation as resolved.
Show resolved Hide resolved
ServiceAnnotations map[string]string `json:"annotations,omitempty"`
}

// RedisConfig defines the external configuration of Redis
Expand Down
7 changes: 7 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

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

10 changes: 10 additions & 0 deletions config/crd/bases/redis.redis.opstreelabs.in_redis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,10 @@ spec:
description: KubernetesConfig will be the JSON struct for Basic Redis
Config
properties:
annotations:
additionalProperties:
type: string
type: object
image:
type: string
imagePullPolicy:
Expand Down Expand Up @@ -992,6 +996,12 @@ spec:
to an implementation-defined value. More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/'
type: object
type: object
serviceType:
enum:
- LoadBalancer
- NodePort
- ClusterIP
type: string
updateStrategy:
description: StatefulSetUpdateStrategy indicates the strategy
that the StatefulSet controller will use to perform updates.
Expand Down
10 changes: 10 additions & 0 deletions config/crd/bases/redis.redis.opstreelabs.in_redisclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ spec:
description: KubernetesConfig will be the JSON struct for Basic Redis
Config
properties:
annotations:
additionalProperties:
type: string
type: object
image:
type: string
imagePullPolicy:
Expand Down Expand Up @@ -190,6 +194,12 @@ spec:
to an implementation-defined value. More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/'
type: object
type: object
serviceType:
enum:
- LoadBalancer
- NodePort
- ClusterIP
type: string
updateStrategy:
description: StatefulSetUpdateStrategy indicates the strategy
that the StatefulSet controller will use to perform updates.
Expand Down
6 changes: 5 additions & 1 deletion k8sutils/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func filterAnnotations(anots map[string]string) map[string]string {
}

// generateServiceAnots generates and returns service annotations
func generateServiceAnots(stsMeta metav1.ObjectMeta) map[string]string {
func generateServiceAnots(stsMeta metav1.ObjectMeta, serviceAnots map[string]string) map[string]string {
anots := map[string]string{
"redis.opstreelabs.in": "true",
"redis.opstreelabs.instance": stsMeta.GetName(),
Expand All @@ -84,6 +84,10 @@ func generateServiceAnots(stsMeta metav1.ObjectMeta) map[string]string {
for k, v := range stsMeta.GetAnnotations() {
anots[k] = v
}
for k := range serviceAnots {
anots[k] = serviceAnots[k]
}

return filterAnnotations(anots)
}

Expand Down
6 changes: 3 additions & 3 deletions k8sutils/redis-cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,18 @@ func (service RedisClusterService) CreateRedisClusterService(cr *redisv1beta1.Re
serviceName := cr.ObjectMeta.Name + "-" + service.RedisServiceRole
logger := serviceLogger(cr.Namespace, serviceName)
labels := getRedisLabels(serviceName, "cluster", service.RedisServiceRole, cr.ObjectMeta.Labels)
annotations := generateServiceAnots(cr.ObjectMeta)
annotations := generateServiceAnots(cr.ObjectMeta, cr.Spec.KubernetesConfig.ServiceAnnotations)
if cr.Spec.RedisExporter != nil && cr.Spec.RedisExporter.Enabled {
enableMetrics = true
}
objectMetaInfo := generateObjectMetaInformation(serviceName, cr.Namespace, labels, annotations)
headlessObjectMetaInfo := generateObjectMetaInformation(serviceName+"-headless", cr.Namespace, labels, annotations)
err := CreateOrUpdateService(cr.Namespace, headlessObjectMetaInfo, redisClusterAsOwner(cr), false, true)
err := CreateOrUpdateService(cr.Namespace, headlessObjectMetaInfo, redisClusterAsOwner(cr), false, true, cr.Spec.KubernetesConfig.ServiceType)
if err != nil {
logger.Error(err, "Cannot create headless service for Redis", "Setup.Type", service.RedisServiceRole)
return err
}
err = CreateOrUpdateService(cr.Namespace, objectMetaInfo, redisClusterAsOwner(cr), enableMetrics, false)
err = CreateOrUpdateService(cr.Namespace, objectMetaInfo, redisClusterAsOwner(cr), enableMetrics, false, cr.Spec.KubernetesConfig.ServiceType)
if err != nil {
logger.Error(err, "Cannot create service for Redis", "Setup.Type", service.RedisServiceRole)
return err
Expand Down
6 changes: 3 additions & 3 deletions k8sutils/redis-standalone.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ var (
func CreateStandaloneService(cr *redisv1beta1.Redis) error {
logger := serviceLogger(cr.Namespace, cr.ObjectMeta.Name)
labels := getRedisLabels(cr.ObjectMeta.Name, "standalone", "standalone", cr.ObjectMeta.Labels)
annotations := generateServiceAnots(cr.ObjectMeta)
annotations := generateServiceAnots(cr.ObjectMeta, cr.Spec.KubernetesConfig.ServiceAnnotations)
if cr.Spec.RedisExporter != nil && cr.Spec.RedisExporter.Enabled {
enableMetrics = true
}
objectMetaInfo := generateObjectMetaInformation(cr.ObjectMeta.Name, cr.Namespace, labels, annotations)
headlessObjectMetaInfo := generateObjectMetaInformation(cr.ObjectMeta.Name+"-headless", cr.Namespace, labels, annotations)
err := CreateOrUpdateService(cr.Namespace, headlessObjectMetaInfo, redisAsOwner(cr), false, true)
err := CreateOrUpdateService(cr.Namespace, headlessObjectMetaInfo, redisAsOwner(cr), false, true, cr.Spec.KubernetesConfig.ServiceType)
if err != nil {
logger.Error(err, "Cannot create standalone headless service for Redis")
return err
}
err = CreateOrUpdateService(cr.Namespace, objectMetaInfo, redisAsOwner(cr), enableMetrics, false)
err = CreateOrUpdateService(cr.Namespace, objectMetaInfo, redisAsOwner(cr), enableMetrics, false, cr.Spec.KubernetesConfig.ServiceType)
if err != nil {
logger.Error(err, "Cannot create standalone service for Redis")
return err
Expand Down
8 changes: 4 additions & 4 deletions k8sutils/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ var (
)

// generateServiceDef generates service definition for Redis
func generateServiceDef(serviceMeta metav1.ObjectMeta, enableMetrics bool, ownerDef metav1.OwnerReference, headless bool) *corev1.Service {
func generateServiceDef(serviceMeta metav1.ObjectMeta, enableMetrics bool, ownerDef metav1.OwnerReference, headless bool, serviceType string) *corev1.Service {
service := &corev1.Service{
TypeMeta: generateMetaInformation("Service", "v1"),
ObjectMeta: serviceMeta,
Spec: corev1.ServiceSpec{
Type: generateServiceType("ClusterIP"),
Type: generateServiceType(serviceType),
ClusterIP: "",
Selector: serviceMeta.GetLabels(),
Ports: []corev1.ServicePort{
Expand Down Expand Up @@ -120,9 +120,9 @@ func serviceLogger(namespace string, name string) logr.Logger {
}

// CreateOrUpdateService method will create or update Redis service
func CreateOrUpdateService(namespace string, serviceMeta metav1.ObjectMeta, ownerDef metav1.OwnerReference, enableMetrics, headless bool) error {
func CreateOrUpdateService(namespace string, serviceMeta metav1.ObjectMeta, ownerDef metav1.OwnerReference, enableMetrics, headless bool, serviceType string) error {
logger := serviceLogger(namespace, serviceMeta.Name)
serviceDef := generateServiceDef(serviceMeta, enableMetrics, ownerDef, headless)
serviceDef := generateServiceDef(serviceMeta, enableMetrics, ownerDef, headless, serviceType)
storedService, err := getService(namespace, serviceMeta.Name)
if err != nil {
if errors.IsNotFound(err) {
Expand Down