Skip to content

Commit

Permalink
feat(controller): reconcileV2 function skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
clamoriniere committed May 12, 2022
1 parent 7a62cc6 commit 4cddea4
Show file tree
Hide file tree
Showing 24 changed files with 667 additions and 4,958 deletions.
6 changes: 5 additions & 1 deletion apis/datadoghq/common/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const (
DefaultMetricsProviderPort int32 = 8443
// DefaultKubeStateMetricsCoreConf default ksm core ConfigMap name
DefaultKubeStateMetricsCoreConf string = "kube-state-metrics-core-config"
// DefaultSysprobeSocketPath default system probe socket path
// DefaultSysprobeSocketPath default system probe socket path
DefaultSysprobeSocketPath = "/var/run/sysprobe/sysprobe.sock"

// Liveness probe default config
Expand All @@ -67,6 +67,10 @@ const (
DefaultReadinessProbeSuccessThreshold int32 = 1
DefaultReadinessProbeFailureThreshold int32 = 6
DefaultReadinessProbeHTTPPath = "/ready"

// Default Image name
DefaultAgentImageName string = "agent"
DefaultClusterAgentImageName string = "cluster-agent"
)

// Datadog volume names and mount paths
Expand Down
5 changes: 3 additions & 2 deletions apis/datadoghq/v1alpha1/datadogagent_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
func (src *DatadogAgent) ConvertTo(dst conversion.Hub) error {
ddaV2 := dst.(*v2alpha1.DatadogAgent)

if err := convertTo(src, ddaV2); err != nil {
if err := ConvertTo(src, ddaV2); err != nil {
return fmt.Errorf("unable to convert DatadogAgent %s/%s to version: %v, err: %w", src.Namespace, src.Name, dst.GetObjectKind().GroupVersionKind().Version, err)
}

Expand All @@ -33,7 +33,8 @@ func (dst *DatadogAgent) ConvertFrom(src conversion.Hub) error { //nolint
return fmt.Errorf("convert from v2alpha1 to %s is not implemented", src.GetObjectKind().GroupVersionKind().Version)
}

func convertTo(src *DatadogAgent, dst *v2alpha1.DatadogAgent) error {
// ConvertTo use to convert v1alpha1.DatadogAgent to v2alpha1.DatadogAgent
func ConvertTo(src *DatadogAgent, dst *v2alpha1.DatadogAgent) error {
// Copying ObjectMeta as a whole
dst.ObjectMeta = src.ObjectMeta

Expand Down
2 changes: 1 addition & 1 deletion apis/datadoghq/v1alpha1/datadogagent_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestDatadogAgentConversion(t *testing.T) {
assert.NoError(t, err)

agentV2 := &v2alpha1.DatadogAgent{}
assert.NoError(t, convertTo(agentV1, agentV2))
assert.NoError(t, ConvertTo(agentV1, agentV2))

if tc.expectedFilename != "" {
expectedV2 := &v2alpha1.DatadogAgent{}
Expand Down
76 changes: 76 additions & 0 deletions apis/datadoghq/v2alpha1/condition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package v2alpha1

import (
"fmt"

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

// UpdateDatadogAgentStatusConditionsFailure used to update the failure StatusConditions
func UpdateDatadogAgentStatusConditionsFailure(status *DatadogAgentStatus, now metav1.Time, conditionType string, err error) {
if err != nil {
UpdateDatadogAgentStatusConditions(status, now, conditionType, metav1.ConditionTrue, fmt.Sprintf("%v", err), false)
} else {
UpdateDatadogAgentStatusConditions(status, now, conditionType, metav1.ConditionFalse, "", false)
}
}

// UpdateDatadogAgentStatusConditions used to update a specific string in conditions
func UpdateDatadogAgentStatusConditions(status *DatadogAgentStatus, now metav1.Time, t string, conditionStatus metav1.ConditionStatus, desc string, writeFalseIfNotExist bool) {
idConditionComplete := getIndexForConditionType(status, t)
if idConditionComplete >= 0 {
UpdateDatadogAgentStatusCondition(status.Conditions[idConditionComplete], now, t, conditionStatus, desc)
} else if conditionStatus == metav1.ConditionTrue || writeFalseIfNotExist {
// Only add if the condition is True
status.Conditions = append(status.Conditions, NewDatadogAgentStatusCondition(t, conditionStatus, now, "", desc))
}
}

// UpdateDatadogAgentStatusCondition used to update a specific string
func UpdateDatadogAgentStatusCondition(condition metav1.Condition, now metav1.Time, t string, conditionStatus metav1.ConditionStatus, desc string) metav1.Condition {
if condition.Status != conditionStatus {
condition.LastTransitionTime = now
condition.Status = conditionStatus
}
condition.LastTransitionTime = now
condition.Message = desc

return condition
}

// SetDatadogAgentStatusCondition use to set a condition
func SetDatadogAgentStatusCondition(status *DatadogAgentStatus, condition *metav1.Condition) {
idConditionComplete := getIndexForConditionType(status, condition.Type)
if idConditionComplete >= 0 {
status.Conditions[idConditionComplete] = *condition
} else {
status.Conditions = append(status.Conditions, *condition)
}
}

// NewDatadogAgentStatusCondition returns new DatadogAgentCondition instance
func NewDatadogAgentStatusCondition(conditionType string, conditionStatus metav1.ConditionStatus, now metav1.Time, reason, message string) metav1.Condition {
return metav1.Condition{
Type: conditionType,
Status: conditionStatus,
LastTransitionTime: now,
Reason: reason,
Message: message,
}
}

func getIndexForConditionType(status *DatadogAgentStatus, t string) int {
idCondition := -1
if status == nil {
return idCondition
}

for i, condition := range status.Conditions {
if condition.Type == t {
idCondition = i
break
}
}

return idCondition
}
13 changes: 13 additions & 0 deletions apis/datadoghq/v2alpha1/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package v2alpha1

const (

// ClusterAgentReconcileConditionType ReconcileConditionType for Cluster Agent component
ClusterAgentReconcileConditionType = "ClusterAgentReconcile"
// AgentReconcileConditionType ReconcileConditionType for Agent component
AgentReconcileConditionType = "AgentReconcile"
// ClusterCheckRunnerReconcileConditionType ReconcileConditionType for Cluster Check Runner component
ClusterCheckRunnerReconcileConditionType = "ClusterCheckRunnerReconcile"
// DatadogAgentReconcileErrorConditionType ReconcileConditionType for DatadogAgent reconcile error
DatadogAgentReconcileErrorConditionType = "DatadogAgentReconcileError"
)
7 changes: 4 additions & 3 deletions apis/datadoghq/v2alpha1/datadogagent_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -835,9 +835,10 @@ type DatadogAgentGenericContainer struct {
// DatadogAgentStatus defines the observed state of DatadogAgent.
// +k8s:openapi-gen=true
type DatadogAgentStatus struct {
// DefaultOverride contains attributes that were not configured that the runtime defaulted.
// +optional
DefaultOverride *DatadogAgentSpec `json:"defaultOverride,omitempty"`
// Conditions Represents the latest available observations of a DatadogAgent's current state.
// +listType=map
// +listMapKey=type
Conditions []metav1.Condition `json:"conditions"`
}

// DatadogAgent Deployment with the Datadog Operator.
Expand Down
10 changes: 6 additions & 4 deletions apis/datadoghq/v2alpha1/zz_generated.deepcopy.go

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

25 changes: 21 additions & 4 deletions apis/datadoghq/v2alpha1/zz_generated.openapi.go

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

80 changes: 76 additions & 4 deletions config/crd/bases/v1/datadoghq.com_datadogagents.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17052,10 +17052,82 @@ spec:
status:
description: DatadogAgentStatus defines the observed state of DatadogAgent.
properties:
defaultOverride:
description: DefaultOverride contains attributes that were not configured
that the runtime defaulted.
type: object
conditions:
description: Conditions Represents the latest available observations
of a DatadogAgent's current state.
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,
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
x-kubernetes-list-map-keys:
- type
x-kubernetes-list-type: map
required:
- conditions
type: object
type: object
served: false
Expand Down
Loading

0 comments on commit 4cddea4

Please sign in to comment.