Skip to content

Commit

Permalink
Merge branch 'main' into swang392/refactor-api
Browse files Browse the repository at this point in the history
  • Loading branch information
swang392 authored Jan 13, 2025
2 parents 63e5a7f + 3d36de7 commit e9b69f5
Show file tree
Hide file tree
Showing 51 changed files with 2,832 additions and 330 deletions.
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,22 @@ WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
COPY go.work go.work
COPY go.work.sum go.work.sum

COPY api/go.mod api/go.mod
COPY api/go.sum api/go.sum

COPY test/e2e/go.mod test/e2e/go.mod
COPY test/e2e/go.sum test/e2e/go.sum
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
RUN go mod download
WORKDIR /workspace/api
RUN go mod download
WORKDIR /workspace/test/e2e
RUN go mod download
WORKDIR /workspace

# Copy the go source
COPY cmd/main.go cmd/main.go
Expand Down
4 changes: 4 additions & 0 deletions LICENSE-3rdparty.csv
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ core,github.com/DataDog/datadog-agent/pkg/util/uuid,Apache-2.0
core,github.com/DataDog/datadog-api-client-go/v2,Apache-2.0
core,github.com/DataDog/datadog-go/v5/statsd,MIT
core,github.com/DataDog/datadog-operator,Apache-2.0
core,github.com/DataDog/datadog-operator/api/datadoghq/common,Unknown
core,github.com/DataDog/datadog-operator/api/datadoghq/v1alpha1,Unknown
core,github.com/DataDog/datadog-operator/api/datadoghq/v2alpha1,Unknown
core,github.com/DataDog/datadog-operator/api/utils,Unknown
core,github.com/DataDog/extendeddaemonset/api/v1alpha1,Apache-2.0
core,github.com/DataDog/go-libddwaf/v3,Apache-2.0
core,github.com/DataDog/go-sqllexer,MIT
Expand Down
12 changes: 4 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,11 @@ endef
##@ Deploy

.PHONY: manager
manager: generate lint managergobuild ## Build manager binary
manager: sync generate lint managergobuild ## Build manager binary
go build -ldflags '${LDFLAGS}' -o bin/$(PLATFORM)/manager cmd/main.go
managergobuild: ## Builds only manager go binary
go build -ldflags '${LDFLAGS}' -o bin/$(PLATFORM)/manager cmd/main.go

##@ Deploy

manager: generate lint managergobuild ## Build manager binary

.PHONY: run
run: generate lint manifests ## Run against the configured Kubernetes cluster in ~/.kube/config
go run ./cmd/main.go
Expand Down Expand Up @@ -319,9 +315,9 @@ verify-licenses: bin/$(PLATFORM)/go-licenses ## Verify licenses
update-golang:
hack/update-golang.sh

.PHONY: tidy
tidy: ## Run go tidy
go mod tidy -v
.PHONY: sync
sync: ## Run go work sync
go work sync

kubectl-datadog: lint
go build -ldflags '${LDFLAGS}' -o bin/kubectl-datadog ./cmd/kubectl-datadog/main.go
Expand Down
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 e9b69f5

Please sign in to comment.