Skip to content

Commit

Permalink
Merge pull request #136 from jlarriba/central_svc
Browse files Browse the repository at this point in the history
Added a service for CeilometerCentral and renamed everything in centr…
  • Loading branch information
jlarriba authored Jul 3, 2023
2 parents 5bfb130 + e8ae578 commit 3695de8
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 8 deletions.
12 changes: 12 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ rules:
- patch
- update
- watch
- apiGroups:
- ""
resources:
- services
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- keystone.openstack.org
resources:
Expand Down
15 changes: 11 additions & 4 deletions controllers/ceilometercentral_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type CeilometerCentralReconciler struct {
// +kubebuilder:rbac:groups=core,resources=configmaps,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete;
// +kubebuilder:rbac:groups=core,resources=secrets,verbs=get;list;watch;create;update;patch;delete;
// +kubebuilder:rbac:groups=core,resources=services,verbs=get;list;watch;create;update;patch;delete;
// +kubebuilder:rbac:groups=keystone.openstack.org,resources=keystoneservices,verbs=get;list;watch;create;update;patch;delete;
// +kubebuilder:rbac:groups=rabbitmq.openstack.org,resources=transporturls,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=keystone.openstack.org,resources=keystoneapis,verbs=get;list;watch;
Expand All @@ -78,7 +79,7 @@ type CeilometerCentralReconciler struct {

// Reconcile reconciles a CeilometerCentral
func (r *CeilometerCentralReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) {
_ = r.Log.WithValues("ceilometer-central", req.NamespacedName)
_ = r.Log.WithValues(ceilometercentral.ServiceName, req.NamespacedName)

// Fetch the Ceilometer instance
instance := &telemetryv1.CeilometerCentral{}
Expand Down Expand Up @@ -187,7 +188,7 @@ func (r *CeilometerCentralReconciler) reconcileDelete(ctx context.Context, insta

// Service is deleted so remove the finalizer.
controllerutil.RemoveFinalizer(instance, helper.GetFinalizer())
r.Log.Info(fmt.Sprintf("Reconciled Service '%s' delete successfully", instance.Name))
r.Log.Info(fmt.Sprintf("Reconciled Service '%s' delete successfully", ceilometercentral.ServiceName))

return ctrl.Result{}, nil
}
Expand Down Expand Up @@ -247,7 +248,7 @@ func (r *CeilometerCentralReconciler) reconcileInit(
}

func (r *CeilometerCentralReconciler) reconcileNormal(ctx context.Context, instance *telemetryv1.CeilometerCentral, helper *helper.Helper) (ctrl.Result, error) {
r.Log.Info(fmt.Sprintf("Reconciling Service '%s'", instance.Name))
r.Log.Info(fmt.Sprintf("Reconciling Service '%s'", ceilometercentral.ServiceName))

// Service account, role, binding
rbacRules := []rbacv1.PolicyRule{
Expand Down Expand Up @@ -415,6 +416,11 @@ func (r *CeilometerCentralReconciler) reconcileNormal(ctx context.Context, insta
}
instance.Status.Networks = instance.Spec.NetworkAttachmentDefinitions

_, _, err = ceilometercentral.Service(instance, helper, ceilometercentral.CeilometerPrometheusPort, serviceLabels)
if err != nil {
return ctrl.Result{}, err
}

r.Log.Info("Reconciled Service successfully")
return ctrl.Result{}, nil
}
Expand Down Expand Up @@ -524,7 +530,7 @@ func (r *CeilometerCentralReconciler) createHashOfInputHashes(
func (r *CeilometerCentralReconciler) transportURLCreateOrUpdate(instance *telemetryv1.CeilometerCentral) (*rabbitmqv1.TransportURL, controllerutil.OperationResult, error) {
transportURL := &rabbitmqv1.TransportURL{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-telemetry-transport", instance.Name),
Name: fmt.Sprintf("%s-transport", ceilometercentral.ServiceName),
Namespace: instance.Namespace,
},
}
Expand All @@ -545,6 +551,7 @@ func (r *CeilometerCentralReconciler) SetupWithManager(mgr ctrl.Manager) error {
Owns(&appsv1.Deployment{}).
Owns(&corev1.ConfigMap{}).
Owns(&corev1.Secret{}).
Owns(&corev1.Service{}).
Owns(&rabbitmqv1.TransportURL{}).
Complete(r)
}
2 changes: 1 addition & 1 deletion controllers/ceilometercompute_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ func (r *CeilometerComputeReconciler) generateServiceConfigMaps(
func (r *CeilometerComputeReconciler) transportURLCreateOrUpdate(instance *telemetryv1.CeilometerCompute) (*rabbitmqv1.TransportURL, controllerutil.OperationResult, error) {
transportURL := &rabbitmqv1.TransportURL{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-telemetry-transport", instance.Name),
Name: fmt.Sprintf("%s-transport", instance.Name),
Namespace: instance.Namespace,
},
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/ceilometercentral/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ package ceilometercentral

const (
// ServiceName -
ServiceName = "ceilometer-central"
ServiceName = "ceilometer"
// ServiceType -
ServiceType = "CeilometerCentral"
ServiceType = "Ceilometer"

// CeilometerPrometheusPort -
CeilometerPrometheusPort int32 = 3000
CeilometerPrometheusPort int = 3000

// KollaConfigCentral -
KollaConfigCentral = "/var/lib/config-data/merged/config-central.json"
Expand Down
60 changes: 60 additions & 0 deletions pkg/ceilometercentral/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package ceilometercentral

import (
"context"

helper "github.com/openstack-k8s-operators/lib-common/modules/common/helper"
telemetryv1 "github.com/openstack-k8s-operators/telemetry-operator/api/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

"k8s.io/apimachinery/pkg/util/intstr"

corev1 "k8s.io/api/core/v1"
)

// Service creates services in Kubernetes for the appropiate port in the passed node
func Service(
instance *telemetryv1.CeilometerCentral,
helper *helper.Helper,
port int,
labels map[string]string,
) (*corev1.Service, controllerutil.OperationResult, error) {
service := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: ServiceName,
Namespace: instance.Namespace,
},
}

op, err := controllerutil.CreateOrUpdate(context.TODO(), helper.GetClient(), service, func() error {
service.Labels = labels
service.Spec.Selector = labels
service.Spec.Ports = []corev1.ServicePort{{
Protocol: "TCP",
Port: int32(port),
TargetPort: intstr.FromInt(port),
}}

err := controllerutil.SetControllerReference(instance, service, helper.GetScheme())
if err != nil {
return err
}

return nil
})

return service, op, err
}

0 comments on commit 3695de8

Please sign in to comment.