Skip to content

Commit

Permalink
Create basic Reconcile methods for WatcherAPI
Browse files Browse the repository at this point in the history
This patch is basic recond methods to the watcherapi
controller, as well as some initial input validation.

The change adds the required CR spec and status, and creates the initial
structure for initialization and deletion. In addition it some some
input validation by accessing the osp secret and the database that
should be created by the watcher controller.

Finally, it is also adding some initial structure test for functional envtest
testing in WatcherAPI.

Related: OSPRH-11483
  • Loading branch information
cescgina committed Nov 27, 2024
1 parent 7ec0729 commit d4a7511
Show file tree
Hide file tree
Showing 9 changed files with 603 additions and 22 deletions.
81 changes: 78 additions & 3 deletions api/bases/watcher.openstack.org_watcherapis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,88 @@ spec:
spec:
description: WatcherAPISpec defines the desired state of WatcherAPI
properties:
foo:
description: Foo is an example field of WatcherAPI. Edit watcherapi_types.go
to remove/update
databaseAccount:
default: watcher
description: DatabaseAccount - MariaDBAccount CR name used for watcher
DB, defaults to watcher
type: string
databaseInstance:
description: MariaDB instance name Required to use the mariadb-operator
instance to create the DB and user
type: string
passwordSelectors:
default:
service: WatcherPassword
description: PasswordSelectors - Selectors to identify the ServiceUser
password from the Secret
properties:
service:
default: WatcherPassword
description: Service - Selector to get the watcher service user
password from the Secret
type: string
type: object
secret:
default: osp-secret
description: Secret containing all passwords / keys needed
type: string
required:
- databaseInstance
type: object
status:
description: WatcherAPIStatus defines the observed state of WatcherAPI
properties:
conditions:
description: Conditions
items:
description: Condition defines an observation of a API resource
operational state.
properties:
lastTransitionTime:
description: 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: A human readable message indicating details about
the transition.
type: string
reason:
description: The reason for the condition's last transition
in CamelCase.
type: string
severity:
description: Severity provides a classification of Reason code,
so the current situation is immediately understandable and
could act accordingly. It is meant for situations where Status=False
and it should be indicated if it is just informational, warning
(next reconciliation might fix it) or an error (e.g. DB create
issue and no actions to automatically resolve the issue can/should
be done). For conditions where Status=Unknown or Status=True
the Severity should be SeverityNone.
type: string
status:
description: Status of the condition, one of True, False, Unknown.
type: string
type:
description: Type of condition in CamelCase.
type: string
required:
- lastTransitionTime
- status
- type
type: object
type: array
observedGeneration:
description: ObservedGeneration - the most recent generation observed
for this service. If the observed generation is less than the spec
generation, then the controller has not processed the latest changes
injected by the openstack-operator in the top-level CR (e.g. the
ContainerImage)
format: int64
type: integer
type: object
type: object
served: true
Expand Down
17 changes: 10 additions & 7 deletions api/v1beta1/watcherapi_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,28 @@ limitations under the License.
package v1beta1

import (
"github.com/openstack-k8s-operators/lib-common/modules/common/condition"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// WatcherAPISpec defines the desired state of WatcherAPI
type WatcherAPISpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file

// Foo is an example field of WatcherAPI. Edit watcherapi_types.go to remove/update
Foo string `json:"foo,omitempty"`
WatcherTemplate `json:",inline"`
}

// WatcherAPIStatus defines the observed state of WatcherAPI
type WatcherAPIStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
// Conditions
Conditions condition.Conditions `json:"conditions,omitempty" optional:"true"`

// ObservedGeneration - the most recent generation observed for this
// service. If the observed generation is less than the spec generation,
// then the controller has not processed the latest changes injected by
// the openstack-operator in the top-level CR (e.g. the ContainerImage)
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down
10 changes: 9 additions & 1 deletion api/v1beta1/zz_generated.deepcopy.go

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

81 changes: 78 additions & 3 deletions config/crd/bases/watcher.openstack.org_watcherapis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,88 @@ spec:
spec:
description: WatcherAPISpec defines the desired state of WatcherAPI
properties:
foo:
description: Foo is an example field of WatcherAPI. Edit watcherapi_types.go
to remove/update
databaseAccount:
default: watcher
description: DatabaseAccount - MariaDBAccount CR name used for watcher
DB, defaults to watcher
type: string
databaseInstance:
description: MariaDB instance name Required to use the mariadb-operator
instance to create the DB and user
type: string
passwordSelectors:
default:
service: WatcherPassword
description: PasswordSelectors - Selectors to identify the ServiceUser
password from the Secret
properties:
service:
default: WatcherPassword
description: Service - Selector to get the watcher service user
password from the Secret
type: string
type: object
secret:
default: osp-secret
description: Secret containing all passwords / keys needed
type: string
required:
- databaseInstance
type: object
status:
description: WatcherAPIStatus defines the observed state of WatcherAPI
properties:
conditions:
description: Conditions
items:
description: Condition defines an observation of a API resource
operational state.
properties:
lastTransitionTime:
description: 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: A human readable message indicating details about
the transition.
type: string
reason:
description: The reason for the condition's last transition
in CamelCase.
type: string
severity:
description: Severity provides a classification of Reason code,
so the current situation is immediately understandable and
could act accordingly. It is meant for situations where Status=False
and it should be indicated if it is just informational, warning
(next reconciliation might fix it) or an error (e.g. DB create
issue and no actions to automatically resolve the issue can/should
be done). For conditions where Status=Unknown or Status=True
the Severity should be SeverityNone.
type: string
status:
description: Status of the condition, one of True, False, Unknown.
type: string
type:
description: Type of condition in CamelCase.
type: string
required:
- lastTransitionTime
- status
- type
type: object
type: array
observedGeneration:
description: ObservedGeneration - the most recent generation observed
for this service. If the observed generation is less than the spec
generation, then the controller has not processed the latest changes
injected by the openstack-operator in the top-level CR (e.g. the
ContainerImage)
format: int64
type: integer
type: object
type: object
served: true
Expand Down
77 changes: 77 additions & 0 deletions controllers/watcher_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@ package controllers

import (
"context"
"fmt"
"time"

"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

"github.com/openstack-k8s-operators/lib-common/modules/common/condition"
"github.com/openstack-k8s-operators/lib-common/modules/common/util"
)

// GetLogger returns a logger object with a prefix of "controller.name" and additional controller context fields
Expand Down Expand Up @@ -96,3 +103,73 @@ func (r *Reconcilers) OverrideRequeueTimeout(timeout time.Duration) {
reconciler.SetRequeueTimeout(timeout)
}
}

type conditionUpdater interface {
Set(c *condition.Condition)
MarkTrue(t condition.Type, messageFormat string, messageArgs ...interface{})
}

// ensureSecret - ensures that the Secret object exists and the expected fields
// are in the Secret. It returns a hash of the values of the expected fields.
func ensureSecret(
ctx context.Context,
secretName types.NamespacedName,
expectedFields []string,
reader client.Reader,
conditionUpdater conditionUpdater,
requeueTimeout time.Duration,
) (string, ctrl.Result, corev1.Secret, error) {
secret := &corev1.Secret{}
err := reader.Get(ctx, secretName, secret)
if err != nil {
if k8s_errors.IsNotFound(err) {
log.FromContext(ctx).Info(fmt.Sprintf("secret %s not found", secretName))
conditionUpdater.Set(condition.FalseCondition(
condition.InputReadyCondition,
condition.RequestedReason,
condition.SeverityInfo,
condition.InputReadyWaitingMessage))
return "",
ctrl.Result{RequeueAfter: requeueTimeout},
*secret,
nil
}
conditionUpdater.Set(condition.FalseCondition(
condition.InputReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.InputReadyErrorMessage,
err.Error()))
return "", ctrl.Result{}, *secret, err
}

// collect the secret values the caller expects to exist
values := [][]byte{}
for _, field := range expectedFields {
val, ok := secret.Data[field]
if !ok {
err := fmt.Errorf("field '%s' not found in secret/%s", field, secretName.Name)
conditionUpdater.Set(condition.FalseCondition(
condition.InputReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.InputReadyErrorMessage,
err.Error()))
return "", ctrl.Result{}, *secret, err
}
values = append(values, val)
}

hash, err := util.ObjectHash(values)
if err != nil {
conditionUpdater.Set(condition.FalseCondition(
condition.InputReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.InputReadyErrorMessage,
err.Error()))
return "", ctrl.Result{}, *secret, err
}

return hash, ctrl.Result{}, *secret, nil
}
Loading

0 comments on commit d4a7511

Please sign in to comment.