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

Added ManageIQ Status with conditions, versions and ingress endpoint info #973

Merged
merged 6 commits into from
Jul 6, 2023
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
31 changes: 28 additions & 3 deletions manageiq-operator/api/v1alpha1/manageiq_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ package v1alpha1
import (
"errors"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"strings"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
Expand Down Expand Up @@ -369,10 +370,34 @@ type ManageIQSpec struct {
ZookeeperVolumeCapacity string `json:"zookeeperVolumeCapacity,omitempty"`
}

// SecretSource is a reference to a secret containing a hidden value
type SecretSource struct {
// The name of the secret containing the value
SecretName string `json:"secretName,omitempty"`
// The key for the value in the secret
Key string `json:"key,omitempty"`
}

type Endpoint struct {
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Scope string `json:"scope,omitempty"`
URI string `json:"uri,omitempty"`
CASecret SecretSource `json:"caSecret,omitempty"`
}

type Version struct {
Name string `json:"name,omitempty"`
Version string `json:"version,omitempty"`
}

// ManageIQStatus defines the observed state of ManageIQ
type ManageIQStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
Versions []Version `json:"versions,omitempty"`
Endpoints []Endpoint `json:"endpoints,omitempty"`

// +operator-sdk:csv:customresourcedefinitions:type=status
Conditions []metav1.Condition `json:"conditions,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down
102 changes: 102 additions & 0 deletions manageiq-operator/config/crd/bases/manageiq.org_manageiqs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,108 @@ spec:
type: object
status:
description: ManageIQStatus defines the observed state of ManageIQ
properties:
conditions:
items:
description: "Condition contains details for one aspect of the current
state of this API Resource. --- This struct is intended for direct
use as an array at the field path .status.conditions. For example,
\n type FooStatus struct{ // Represents the observations of a
foo's current state. // Known .status.conditions.type are: \"Available\",
\"Progressing\", and \"Degraded\" // +patchMergeKey=type // +patchStrategy=merge
// +listType=map // +listMapKey=type Conditions []metav1.Condition
`json:\"conditions,omitempty\" patchStrategy:\"merge\" patchMergeKey:\"type\"
protobuf:\"bytes,1,rep,name=conditions\"` \n // other fields }"
properties:
lastTransitionTime:
description: lastTransitionTime is the last time the condition
transitioned from one status to another. This should be when
the underlying condition changed. If that is not known, then
using the time when the API field changed is acceptable.
format: date-time
type: string
message:
description: message is a human readable message indicating
details about the transition. This may be an empty string.
maxLength: 32768
type: string
observedGeneration:
description: observedGeneration represents the .metadata.generation
that the condition was set based upon. For instance, if .metadata.generation
is currently 12, but the .status.conditions[x].observedGeneration
is 9, the condition is out of date with respect to the current
state of the instance.
format: int64
minimum: 0
type: integer
reason:
description: reason contains a programmatic identifier indicating
the reason for the condition's last transition. Producers
of specific condition types may define expected values and
meanings for this field, and whether the values are considered
a guaranteed API. The value should be a CamelCase string.
This field may not be empty.
maxLength: 1024
minLength: 1
pattern: ^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$
type: string
status:
description: status of the condition, one of True, False, Unknown.
enum:
- "True"
- "False"
- Unknown
type: string
type:
description: type of condition in CamelCase or in foo.example.com/CamelCase.
--- Many .condition.type values are consistent across resources
like Available, but because arbitrary conditions can be useful
(see .node.status.conditions), the ability to deconflict is
important. The regex it matches is (dns1123SubdomainFmt/)?(qualifiedNameFmt)
maxLength: 316
pattern: ^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$
type: string
required:
- lastTransitionTime
- message
- reason
- status
- type
type: object
type: array
endpoints:
items:
properties:
caSecret:
description: SecretSource is a reference to a secret containing
a hidden value
properties:
key:
description: The key for the value in the secret
type: string
secretName:
description: The name of the secret containing the value
type: string
type: object
name:
type: string
scope:
type: string
type:
type: string
uri:
type: string
type: object
type: array
versions:
items:
properties:
name:
type: string
version:
type: string
type: object
type: array
type: object
type: object
served: true
Expand Down
184 changes: 183 additions & 1 deletion manageiq-operator/controllers/manageiq_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,28 @@ package controllers

import (
"context"
"fmt"

"os"
bdunne marked this conversation as resolved.
Show resolved Hide resolved

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"os"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/ManageIQ/manageiq-pods/manageiq-operator/api/v1alpha1"
bdunne marked this conversation as resolved.
Show resolved Hide resolved
miqv1alpha1 "github.com/ManageIQ/manageiq-pods/manageiq-operator/api/v1alpha1"
cr_migration "github.com/ManageIQ/manageiq-pods/manageiq-operator/api/v1alpha1/helpers/cr_migration"
miqtool "github.com/ManageIQ/manageiq-pods/manageiq-operator/api/v1alpha1/helpers/miq-components"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// ManageIQReconciler reconciles a ManageIQ object
Expand Down Expand Up @@ -148,6 +153,8 @@ func (r *ManageIQReconciler) Reconcile(ctx context.Context, request ctrl.Request
return reconcile.Result{}, e
}

r.updateManageIQStatus(miqInstance)

return reconcile.Result{}, nil
}

Expand All @@ -166,6 +173,181 @@ func (r *ManageIQReconciler) SetupWithManager(mgr ctrl.Manager) error {

var logger = log.Log.WithName("controller_manageiq")

func FindDeployment(cr *miqv1alpha1.ManageIQ, client client.Client, name string) *appsv1.Deployment {
Copy link
Member

@bdunne bdunne Jul 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a followup, do you think we should lower case these functions? I think they can be private.

namespacedName := types.NamespacedName{Namespace: cr.Namespace, Name: name}
object := &appsv1.Deployment{}
if err := client.Get(context.TODO(), namespacedName, object); err != nil {
if errors.IsNotFound(err) {
return nil
}
}

return object
}

func FindIngress(cr *miqv1alpha1.ManageIQ, client client.Client, name string) *networkingv1.Ingress {
namespacedName := types.NamespacedName{Namespace: cr.Namespace, Name: name}
object := &networkingv1.Ingress{}
if err := client.Get(context.TODO(), namespacedName, object); err != nil {
if errors.IsNotFound(err) {
return nil
}
}

return object
}

func FindSecret(cr *miqv1alpha1.ManageIQ, client client.Client, name string) *corev1.Secret {
namespacedName := types.NamespacedName{Namespace: cr.Namespace, Name: name}
object := &corev1.Secret{}
if err := client.Get(context.TODO(), namespacedName, object); err != nil {
if errors.IsNotFound(err) {
return nil
}
}

return object
}

func (r *ManageIQReconciler) updateManageIQStatus(cr *miqv1alpha1.ManageIQ) {
// update status with operator and operand version
namespacedName := types.NamespacedName{Namespace: cr.Namespace, Name: cr.Name}
r.updateOperatorVersions(namespacedName, cr.Spec.AppAnnotations["productVersion"], cr.APIVersion)

// update status condition
deployments := []string{"httpd", "memcached", "orchestrator", "postgresql"}
for _, deploymentName := range deployments {
if object := FindDeployment(cr, r.Client, deploymentName); object != nil {
deploymentStatusConditions := object.Status.Conditions

if typeAvailableCondition := FindDeploymentStatusCondition(deploymentStatusConditions, appsv1.DeploymentAvailable); typeAvailableCondition != nil {
conditionMessage := fmt.Sprintf("[%s] %s", typeAvailableCondition.Type, typeAvailableCondition.Message)
r.reportStatusCondition(namespacedName, conditionMessage, typeAvailableCondition.Reason, metav1.ConditionStatus(typeAvailableCondition.Status), deploymentName)
} else if typeProgressingCondition := FindDeploymentStatusCondition(deploymentStatusConditions, appsv1.DeploymentProgressing); typeProgressingCondition != nil {
conditionMessage := fmt.Sprintf("[%s] %s", typeProgressingCondition.Type, typeProgressingCondition.Message)
r.reportStatusCondition(namespacedName, conditionMessage, typeProgressingCondition.Reason, metav1.ConditionStatus(typeProgressingCondition.Status), deploymentName)
} else if typeReplicaFailure := FindDeploymentStatusCondition(deploymentStatusConditions, appsv1.DeploymentReplicaFailure); typeReplicaFailure != nil {
conditionMessage := fmt.Sprintf("[%s] %s", typeReplicaFailure.Type, typeReplicaFailure.Message)
r.reportStatusCondition(namespacedName, conditionMessage, typeReplicaFailure.Reason, metav1.ConditionStatus(typeReplicaFailure.Status), deploymentName)
}
}
}

// update ingress endpoint status
ingresses := []string{"httpd"}
for _, ingressName := range ingresses {
if object := FindIngress(cr, r.Client, ingressName); object != nil {
if ownerReferences := object.OwnerReferences; len(ownerReferences) != 0 {
if object.Spec.TLS != nil ||
object.Spec.Rules != nil {
endpointInfo := &miqv1alpha1.Endpoint{}
if len(object.Spec.TLS[0].SecretName) != 0 {
if objectSecret := FindSecret(cr, r.Client, object.Spec.TLS[0].SecretName); object != nil {
endpointInfo.Name = ingressName
endpointInfo.Type = "UI"
endpointInfo.Scope = "External"
for k := range objectSecret.Data {
if k == "cs.crt" {
endpointInfo.CASecret.Key = "cs.crt"
sudhir-kelkar marked this conversation as resolved.
Show resolved Hide resolved
break
}
}
}
if len(object.Spec.TLS[0].SecretName) != 0 {
endpointInfo.CASecret.SecretName = object.Spec.TLS[0].SecretName
}
if len(object.Spec.Rules[0].Host) != 0 {
endpointInfo.URI = object.Spec.Rules[0].Host
}
r.reportEndpointInfo(namespacedName, *endpointInfo)
}
}
}
}
}
}

func FindDeploymentStatusCondition(conditions []appsv1.DeploymentCondition, conditionType appsv1.DeploymentConditionType) *appsv1.DeploymentCondition {
for i := range conditions {
if conditions[i].Type == conditionType {
return &conditions[i]
}
}
return nil
}

func (r *ManageIQReconciler) updateOperatorVersions(namespacedName types.NamespacedName, operatorVersion string, operandVersion string) error {
miqInstance := &miqv1alpha1.ManageIQ{}
err := r.Client.Get(context.TODO(), namespacedName, miqInstance)
if err != nil {
logger.Error(err, "Error getting cluster cr")
return err
}
miqInstance.Status.Versions = []v1alpha1.Version{
bdunne marked this conversation as resolved.
Show resolved Hide resolved
{
Name: "operator", Version: operatorVersion,
},
{
Name: "operand", Version: operandVersion,
}}

if err := r.Client.Status().Update(context.TODO(), miqInstance); err != nil {
logger.Error(err, "Error updating status")
return err
}
return nil
}

func (r *ManageIQReconciler) reportStatusCondition(namespacedName types.NamespacedName, statusMessage string,
reason string, conditionStatus metav1.ConditionStatus, statusType string) error {

miqInstance := &miqv1alpha1.ManageIQ{}
err := r.Client.Get(context.TODO(), namespacedName, miqInstance)
if err != nil {
logger.Error(err, "Error getting cluster cr")
return err
}
apimeta.SetStatusCondition(&miqInstance.Status.Conditions, metav1.Condition{
Type: statusType,
Status: conditionStatus,
Reason: reason,
Message: statusMessage,
})

if err := r.Client.Update(context.TODO(), miqInstance); err != nil {
sudhir-kelkar marked this conversation as resolved.
Show resolved Hide resolved
logger.Error(err, "Error updating status")
return err
}
return nil
}

func (r *ManageIQReconciler) reportEndpointInfo(namespacedName types.NamespacedName, endPointDetails miqv1alpha1.Endpoint) error {
miqInstance := &miqv1alpha1.ManageIQ{}
err := r.Client.Get(context.TODO(), namespacedName, miqInstance)
if err != nil {
logger.Error(err, "Error getting cluster cr")
return err
}
endpoints := miqInstance.Status.Endpoints
endpointFound := false
for i := range endpoints {
if endpoints[i].Type == endPointDetails.Type && endpoints[i].Name == endPointDetails.Name {
endpoints[i] = endPointDetails
endpointFound = true
break
}
}

if !endpointFound {
miqInstance.Status.Endpoints = append(miqInstance.Status.Endpoints, endPointDetails)
}
if err := r.Client.Status().Update(context.TODO(), miqInstance); err != nil {
logger.Error(err, "Error updating status")
return err
}
return nil
}

func (r *ManageIQReconciler) generateDefaultServiceAccount(cr *miqv1alpha1.ManageIQ) error {
serviceAccount, mutateFunc := miqtool.DefaultServiceAccount(cr, r.Scheme)
if result, err := controllerutil.CreateOrUpdate(context.TODO(), r.Client, serviceAccount, mutateFunc); err != nil {
Expand Down