Skip to content

Commit

Permalink
chore: added status information to reflect the number of AZs a cluste…
Browse files Browse the repository at this point in the history
…r is spread across (#359)

* chore: add information about high availability of NATS servers

* updated

* Update 02-configuration.md

* add status information to reflect the number of AZs a cluster is spread across

* revert doc change

* added roles

* added metrics

* moved pkg

* renamed metrics

* added unit tests

* added unit tests

* updated unit test

* added integration tests

* replaced cache

* added test case for AZ>3
  • Loading branch information
mfaizanse authored Apr 29, 2024
1 parent 27517b5 commit 6f59f58
Show file tree
Hide file tree
Showing 34 changed files with 2,025 additions and 146 deletions.
2 changes: 2 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ linters-settings:
alias: nmctrlcache
- pkg: github.com/kyma-project/nats-manager/pkg/labels
alias: nmlabels
- pkg: github.com/prometheus/client_golang/prometheus/testutil
alias: ptestutil

ireturn:
allow:
Expand Down
17 changes: 17 additions & 0 deletions api/v1alpha1/nats_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ func (ns *NATSStatus) UpdateConditionAvailable(
meta.SetStatusCondition(&ns.Conditions, condition)
}

func (ns *NATSStatus) UpdateConditionAvailabilityZones(
status kmetav1.ConditionStatus,
reason ConditionReason,
message string,
) {
condition := kmetav1.Condition{
Type: string(ConditionAvailabilityZones),
Status: status,
LastTransitionTime: kmetav1.Now(),
Reason: string(reason),
Message: message,
}
meta.SetStatusCondition(&ns.Conditions, condition)
}

func (ns *NATSStatus) SetStateReady() {
ns.State = StateReady
ns.UpdateConditionStatefulSet(kmetav1.ConditionTrue,
Expand All @@ -78,6 +93,8 @@ func (ns *NATSStatus) SetWaitingStateForStatefulSet() {
ns.UpdateConditionStatefulSet(kmetav1.ConditionFalse,
ConditionReasonStatefulSetPending, "")
ns.UpdateConditionAvailable(kmetav1.ConditionFalse, ConditionReasonDeploying, "")
ns.UpdateConditionAvailabilityZones(kmetav1.ConditionFalse,
ConditionReasonStatefulSetPending, "")
}

func (ns *NATSStatus) SetStateError() {
Expand Down
49 changes: 49 additions & 0 deletions api/v1alpha1/nats_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,41 @@ func Test_UpdateConditionAvailable(t *testing.T) {
})
}

func Test_UpdateConditionAvailabilityZones(t *testing.T) {
t.Parallel()

t.Run("should update the AvailabilityZones condition", func(t *testing.T) {
t.Parallel()

// given
natsStatus1 := &NATSStatus{
Conditions: []kmetav1.Condition{
{
Type: string(ConditionAvailabilityZones),
Status: kmetav1.ConditionFalse,
Reason: "",
Message: "",
},
},
State: StateReady,
}

givenStatus := kmetav1.ConditionTrue
givenReason := ConditionReasonProcessing
givenMessage := "testxyz"

// when
natsStatus1.UpdateConditionAvailabilityZones(givenStatus, givenReason, givenMessage)

// then
gotCondition := natsStatus1.Conditions[0]
require.Equal(t, string(ConditionAvailabilityZones), gotCondition.Type)
require.Equal(t, givenStatus, gotCondition.Status)
require.Equal(t, string(givenReason), gotCondition.Reason)
require.Equal(t, givenMessage, gotCondition.Message)
})
}

func Test_SetStateReady(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -307,6 +342,14 @@ func Test_SetWaitingStateForStatefulSet(t *testing.T) {
LastTransitionTime: currentTime,
}

expectedAvailabilityZonesCondition := &kmetav1.Condition{
Type: string(ConditionAvailabilityZones),
Status: kmetav1.ConditionFalse,
Reason: string(ConditionReasonStatefulSetPending),
Message: "",
LastTransitionTime: currentTime,
}

// when
natsStatus1.SetWaitingStateForStatefulSet()

Expand All @@ -323,6 +366,12 @@ func Test_SetWaitingStateForStatefulSet(t *testing.T) {
require.NotNil(t, availableCondition)
availableCondition.LastTransitionTime = currentTime
require.Equal(t, expectedAvailableCondition, availableCondition)

// compare ConditionAvailabilityZones
availableZonesCondition := natsStatus1.FindCondition(ConditionAvailabilityZones)
require.NotNil(t, availableZonesCondition)
availableZonesCondition.LastTransitionTime = currentTime
require.Equal(t, expectedAvailabilityZonesCondition, availableZonesCondition)
})
}

Expand Down
17 changes: 11 additions & 6 deletions api/v1alpha1/nats_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ const (
StateDeleting string = "Deleting"
StateWarning string = "Warning"

ConditionAvailable ConditionType = "Available"
ConditionStatefulSet ConditionType = "StatefulSet"
ConditionDeleted ConditionType = "Deleted"
ConditionAvailable ConditionType = "Available"
ConditionStatefulSet ConditionType = "StatefulSet"
ConditionDeleted ConditionType = "Deleted"
ConditionAvailabilityZones ConditionType = "AvailabilityZones"

ConditionReasonProcessing ConditionReason = "Processing"
ConditionReasonDeploying ConditionReason = "Deploying"
Expand All @@ -51,6 +52,8 @@ const (
ConditionReasonSyncFailError ConditionReason = "FailedToSyncResources"
ConditionReasonManifestError ConditionReason = "InvalidManifests"
ConditionReasonDeletionError ConditionReason = "DeletionError"
ConditionReasonNotConfigured ConditionReason = "NotConfigured"
ConditionReasonUnknown ConditionReason = "Unknown"
)

/*
Expand All @@ -75,6 +78,7 @@ undefined they will be interpreted as "" and result in 0 instead of being replac

// NATS is the Schema for the NATS API.
// +kubebuilder:object:root=true
// +kubebuilder:resource:path=nats
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="State",type="string",JSONPath=".status.state",description="State of NATS deployment"
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description="Age of the resource"
Expand All @@ -89,9 +93,10 @@ type NATS struct {

// NATSStatus defines the observed state of NATS.
type NATSStatus struct {
State string `json:"state"`
URL string `json:"url,omitempty"`
Conditions []kmetav1.Condition `json:"conditions,omitempty"`
State string `json:"state"`
URL string `json:"url,omitempty"`
AvailabilityZonesUsed int `json:"availabilityZonesUsed,omitempty"`
Conditions []kmetav1.Condition `json:"conditions,omitempty"`
}

// NATSSpec defines the desired state of NATS.
Expand Down
3 changes: 1 addition & 2 deletions api/v1alpha1/zz_generated.deepcopy.go

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

5 changes: 5 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/kyma-project/nats-manager/pkg/k8s"
"github.com/kyma-project/nats-manager/pkg/k8s/chart"
nmmgr "github.com/kyma-project/nats-manager/pkg/manager"
"github.com/kyma-project/nats-manager/pkg/metrics"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
kapiextclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
Expand Down Expand Up @@ -150,6 +151,9 @@ func main() { //nolint:funlen // main function needs to initialize many objects

natsManager := nmmgr.NewNATSManger(kubeClient, helmRenderer, sugaredLogger)

collector := metrics.NewPrometheusCollector()
collector.RegisterMetrics()

// create NATS reconciler instance
natsReconciler := nmctrl.NewReconciler(
mgr.GetClient(),
Expand All @@ -165,6 +169,7 @@ func main() { //nolint:funlen // main function needs to initialize many objects
Namespace: envConfigs.NATSCRNamespace,
},
},
collector,
)

if err = (natsReconciler).SetupWithManager(mgr); err != nil {
Expand Down
118 changes: 66 additions & 52 deletions config/crd/bases/operator.kyma-project.io_nats.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.11.1
creationTimestamp: null
controller-gen.kubebuilder.io/version: v0.14.0
name: nats.operator.kyma-project.io
spec:
group: operator.kyma-project.io
Expand All @@ -30,14 +29,19 @@ spec:
description: NATS is the Schema for the NATS API.
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
description: |-
APIVersion defines the versioned schema of this representation of an object.
Servers should convert recognized schemas to the latest internal value, and
may reject unrecognized values.
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
description: |-
Kind is a string value representing the REST resource this object represents.
Servers may infer this from the endpoint the client submits requests to.
Cannot be updated.
In CamelCase.
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
type: string
metadata:
type: object
Expand Down Expand Up @@ -186,18 +190,24 @@ spec:
description: Resources defines resources for NATS.
properties:
claims:
description: "Claims lists the names of resources, defined in
spec.resourceClaims, that are used by this container. \n This
is an alpha field and requires enabling the DynamicResourceAllocation
feature gate. \n This field is immutable. It can only be set
for containers."
description: |-
Claims lists the names of resources, defined in spec.resourceClaims,
that are used by this container.
This is an alpha field and requires enabling the
DynamicResourceAllocation feature gate.
This field is immutable. It can only be set for containers.
items:
description: ResourceClaim references one entry in PodSpec.ResourceClaims.
properties:
name:
description: Name must match the name of one entry in pod.spec.resourceClaims
of the Pod where this field is used. It makes that resource
available inside a container.
description: |-
Name must match the name of one entry in pod.spec.resourceClaims of
the Pod where this field is used. It makes that resource available
inside a container.
type: string
required:
- name
Expand All @@ -213,8 +223,9 @@ spec:
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
description: 'Limits describes the maximum amount of compute resources
allowed. More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/'
description: |-
Limits describes the maximum amount of compute resources allowed.
More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
type: object
requests:
additionalProperties:
Expand All @@ -223,56 +234,58 @@ spec:
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
description: 'Requests describes the minimum amount of compute
resources required. If Requests is omitted for a container,
it defaults to Limits if that is explicitly specified, otherwise
to an implementation-defined value. Requests cannot exceed Limits.
More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/'
description: |-
Requests describes the minimum amount of compute resources required.
If Requests is omitted for a container, it defaults to Limits if that is explicitly specified,
otherwise to an implementation-defined value. Requests cannot exceed Limits.
More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
type: object
type: object
type: object
status:
description: NATSStatus defines the observed state of NATS.
properties:
availabilityZonesUsed:
type: integer
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 []kmetav1.Condition
`json:\"conditions,omitempty\" patchStrategy:\"merge\" patchMergeKey:\"type\"
protobuf:\"bytes,1,rep,name=conditions\"` \n // other fields }"
state of this API Resource.\n---\nThis struct is intended for
direct use as an array at the field path .status.conditions. For
example,\n\n\n\ttype FooStatus struct{\n\t // Represents the
observations of a foo's current state.\n\t // Known .status.conditions.type
are: \"Available\", \"Progressing\", and \"Degraded\"\n\t //
+patchMergeKey=type\n\t // +patchStrategy=merge\n\t // +listType=map\n\t
\ // +listMapKey=type\n\t Conditions []metav1.Condition `json:\"conditions,omitempty\"
patchStrategy:\"merge\" patchMergeKey:\"type\" protobuf:\"bytes,1,rep,name=conditions\"`\n\n\n\t
\ // other fields\n\t}"
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.
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.
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.
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.
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
Expand All @@ -286,11 +299,12 @@ spec:
- 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)
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
Expand Down
Loading

0 comments on commit 6f59f58

Please sign in to comment.