Skip to content

Commit

Permalink
Merge branch 'main' into levan-m/bump-depedency
Browse files Browse the repository at this point in the history
  • Loading branch information
levan-m authored Jan 8, 2025
2 parents cab0a6a + 24de754 commit 36017c4
Show file tree
Hide file tree
Showing 28 changed files with 2,347 additions and 31 deletions.
95 changes: 95 additions & 0 deletions api/datadoghq/v1alpha1/datadoggenericresource_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.

package v1alpha1

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

type SupportedResourcesType string

// When adding a new type, make sure to update the kubebuilder validation enum marker
const (
Notebook SupportedResourcesType = "notebook"
SyntheticsAPITest SupportedResourcesType = "synthetics_api_test"
SyntheticsBrowserTest SupportedResourcesType = "synthetics_browser_test"
)

// DatadogGenericResourceSpec defines the desired state of DatadogGenericResource
// +k8s:openapi-gen=true
type DatadogGenericResourceSpec struct {
// Type is the type of the API object
// +kubebuilder:validation:Enum=notebook;synthetics_api_test;synthetics_browser_test
Type SupportedResourcesType `json:"type"`
// JsonSpec is the specification of the API object
JsonSpec string `json:"jsonSpec"`
}

// DatadogGenericResourceStatus defines the observed state of DatadogGenericResource
// +k8s:openapi-gen=true
type DatadogGenericResourceStatus struct {
// Conditions represents the latest available observations of the state of a DatadogGenericResource.
// +listType=map
// +listMapKey=type
Conditions []metav1.Condition `json:"conditions,omitempty"`
// Id is the object unique identifier generated in Datadog.
Id string `json:"id,omitempty"`
// Creator is the identity of the creator.
Creator string `json:"creator,omitempty"`
// Created is the time the object was created.
Created *metav1.Time `json:"created,omitempty"`
// SyncStatus shows the health of syncing the object state to Datadog.
SyncStatus DatadogSyncStatus `json:"syncStatus,omitempty"`
// CurrentHash tracks the hash of the current DatadogGenericResourceSpec to know
// if the JsonSpec has changed and needs an update.
CurrentHash string `json:"currentHash,omitempty"`
// LastForceSyncTime is the last time the API object was last force synced with the custom resource
LastForceSyncTime *metav1.Time `json:"lastForceSyncTime,omitempty"`
}

type DatadogSyncStatus string

const (
// DatadogSyncStatusOK means syncing is OK.
DatadogSyncStatusOK DatadogSyncStatus = "OK"
// DatadogSyncStatusValidateError means there is an object validation error.
DatadogSyncStatusValidateError DatadogSyncStatus = "error validating object"
// DatadogSyncStatusUpdateError means there is an object update error.
DatadogSyncStatusUpdateError DatadogSyncStatus = "error updating object"
// DatadogSyncStatusCreateError means there is an error getting the object.
DatadogSyncStatusCreateError DatadogSyncStatus = "error creating object"
// DatadogSyncStatusGetError means there is an error getting the object.
DatadogSyncStatusGetError DatadogSyncStatus = "error getting object"
)

// DatadogGenericResource is the Schema for the DatadogGenericResources API
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:path=datadoggenericresources,scope=Namespaced,shortName=ddgr
// +kubebuilder:printcolumn:name="id",type="string",JSONPath=".status.id"
// +kubebuilder:printcolumn:name="sync status",type="string",JSONPath=".status.syncStatus"
// +kubebuilder:printcolumn:name="age",type="date",JSONPath=".metadata.creationTimestamp"
// +k8s:openapi-gen=true
// +genclient
type DatadogGenericResource struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec DatadogGenericResourceSpec `json:"spec,omitempty"`
Status DatadogGenericResourceStatus `json:"status,omitempty"`
}

// DatadogGenericResourceList contains a list of DatadogGenericResource
// +kubebuilder:object:root=true
type DatadogGenericResourceList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []DatadogGenericResource `json:"items"`
}

func init() {
SchemeBuilder.Register(&DatadogGenericResource{}, &DatadogGenericResourceList{})
}
33 changes: 33 additions & 0 deletions api/datadoghq/v1alpha1/datadoggenericresource_validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.

package v1alpha1

import (
"fmt"

utilserrors "k8s.io/apimachinery/pkg/util/errors"
)

var allowedCustomResourcesEnumMap = map[SupportedResourcesType]string{
Notebook: "",
SyntheticsAPITest: "",
SyntheticsBrowserTest: "",
// mockSubresource is used to mock the subresource in tests
"mock_resource": "",
}

func IsValidDatadogGenericResource(spec *DatadogGenericResourceSpec) error {
var errs []error
if _, ok := allowedCustomResourcesEnumMap[spec.Type]; !ok {
errs = append(errs, fmt.Errorf("spec.Type must be a supported resource type"))
}

if spec.JsonSpec == "" {
errs = append(errs, fmt.Errorf("spec.JsonSpec must be defined"))
}

return utilserrors.NewAggregate(errs)
}
57 changes: 57 additions & 0 deletions api/datadoghq/v1alpha1/datadoggenericresource_validation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.

package v1alpha1

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_IsValidDatadogGenericResource(t *testing.T) {
tests := []struct {
name string
spec *DatadogGenericResourceSpec
wantErr string
}{
{
name: "supported resource type and non empty json spec",
spec: &DatadogGenericResourceSpec{
Type: SyntheticsBrowserTest,
// N.B. This is a valid JSON string but not valid for the API (not a model payload).
// This is just for testing purposes.
JsonSpec: "{\"foo\": \"bar\"}",
},
wantErr: "",
},
{
name: "unsupported resource type",
spec: &DatadogGenericResourceSpec{
Type: "foo",
JsonSpec: "{\"foo\": \"bar\"}",
},
wantErr: "spec.Type must be a supported resource type",
},
{
name: "empty json spec",
spec: &DatadogGenericResourceSpec{
Type: Notebook,
JsonSpec: "",
},
wantErr: "spec.JsonSpec must be defined",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := IsValidDatadogGenericResource(test.spec)
if test.wantErr != "" {
assert.EqualError(t, err, test.wantErr)
} else {
assert.NoError(t, err)
}
})
}
}
104 changes: 104 additions & 0 deletions api/datadoghq/v1alpha1/zz_generated.deepcopy.go

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

Loading

0 comments on commit 36017c4

Please sign in to comment.