Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PC-9367 API for creating / editing datasource to support release channel field #81

Merged
merged 13 commits into from
Jul 27, 2023
1 change: 1 addition & 0 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type AgentStatus struct {
type AgentSpec struct {
Description string `json:"description,omitempty"`
SourceOf []string `json:"sourceOf" example:"Metrics,Services"`
ReleaseChannel string `json:"releaseChannel,omitempty" example:"beta,stable"`
QueryDelay *QueryDelayDuration `json:"queryDelay"`
AmazonPrometheus *AmazonPrometheusAgentConfig `json:"amazonPrometheus,omitempty"`
AppDynamics *AppDynamicsAgentConfig `json:"appDynamics,omitempty"`
Expand Down
1 change: 1 addition & 0 deletions direct.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type DirectStatus struct {
type DirectSpec struct {
Description string `json:"description,omitempty" example:"Datadog description"` //nolint:lll
SourceOf []string `json:"sourceOf" example:"Metrics,Services"`
ReleaseChannel string `json:"releaseChannel,omitempty" example:"beta,stable"`
LogCollectionEnabled *bool `json:"logCollectionEnabled,omitempty"`
HistoricalDataRetrieval *HistoricalDataRetrieval `json:"historicalDataRetrieval"`
QueryDelay *QueryDelayDuration `json:"queryDelay"`
Expand Down
3 changes: 2 additions & 1 deletion manifest/v1alpha/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@ type QueryDelay struct {
type AgentSpec struct {
Description string `json:"description,omitempty" validate:"description" example:"Prometheus description"` //nolint:lll
SourceOf []string `json:"sourceOf" example:"Metrics,Services"`
ReleaseChannel string `json:"releaseChannel,omitempty" example:"beta,stable"`
ReleaseChannel ReleaseChannel `json:"releaseChannel,omitempty" example:"beta,stable"`
Prometheus *PrometheusAgentConfig `json:"prometheus,omitempty"`
Datadog *DatadogAgentConfig `json:"datadog,omitempty"`
NewRelic *NewRelicAgentConfig `json:"newRelic,omitempty"`
Expand Down Expand Up @@ -1122,6 +1122,7 @@ type PublicDirectWithSLOs struct {
type DirectSpec struct {
Description string `json:"description,omitempty" validate:"description" example:"Datadog description"` //nolint:lll
SourceOf []string `json:"sourceOf" example:"Metrics,Services"`
ReleaseChannel ReleaseChannel `json:"releaseChannel,omitempty" example:"beta,stable"`
Datadog *DatadogDirectConfig `json:"datadog,omitempty"`
LogCollectionEnabled *bool `json:"logCollectionEnabled,omitempty"`
NewRelic *NewRelicDirectConfig `json:"newRelic,omitempty"`
Expand Down
31 changes: 31 additions & 0 deletions manifest/v1alpha/release_channel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package v1alpha

import (
"fmt"
)

//go:generate ../../bin/go-enum --nocase --names --lower --values

// ReleaseChannel /* ENUM(stable = 1, beta, alpha)*/
type ReleaseChannel int

// MarshalText implements the text marshaller method.
func (r ReleaseChannel) MarshalText() ([]byte, error) {
return []byte(r.String()), nil
}

// UnmarshalText implements the text unmarshaller method.
func (r *ReleaseChannel) UnmarshalText(text []byte) error {
if len(text) == 0 {
*r = 0
return nil
}
tmp, err := ParseReleaseChannel(string(text))
if err != nil {
// We're only allowing a subset of valid release channels to be set by the user, inform only on them.
return fmt.Errorf("%s is not a valid ReleaseChannel, try [%s, %s]",
string(text), ReleaseChannelStable, ReleaseChannelBeta)
}
*r = tmp
return nil
}
89 changes: 89 additions & 0 deletions manifest/v1alpha/release_channel_enum.go

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

16 changes: 16 additions & 0 deletions manifest/v1alpha/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1506,6 +1506,10 @@ func agentSpecStructLevelValidation(sl v.StructLevel) {
}
agentQueryDelayValidation(sa, sl)
sourceOfValidation(sa.SourceOf, sl)

if !isValidReleaseChannel(sa.ReleaseChannel) {
sl.ReportError(sa, "ReleaseChannel", "ReleaseChannel", "unknownReleaseChannel", "")
}
}

func agentQueryDelayValidation(sa AgentSpec, sl v.StructLevel) {
Expand Down Expand Up @@ -1874,6 +1878,10 @@ func directSpecStructLevelValidation(sl v.StructLevel) {
directTypeValidation(sa, sl)
directQueryDelayValidation(sa, sl)
sourceOfValidation(sa.SourceOf, sl)

if !isValidReleaseChannel(sa.ReleaseChannel) {
sl.ReportError(sa, "ReleaseChannel", "ReleaseChannel", "unknownReleaseChannel", "")
}
}

func directTypeValidation(sa DirectSpec, sl v.StructLevel) {
Expand Down Expand Up @@ -1993,6 +2001,14 @@ func sourceOfItemsValidation(sourceOf []string, sl v.StructLevel) bool {
return true
}

func isValidReleaseChannel(releaseChannel ReleaseChannel) bool {
if releaseChannel == 0 {
return true
}
// We do not allow ReleaseChannelAlpha to be set by the user.
return releaseChannel.IsValid() && releaseChannel != ReleaseChannelAlpha
}

// Check performs validation, it accepts all possible structs and perform checks based on tags for structs fields
func (val *Validate) Check(s interface{}) error {
return val.validate.Struct(s)
Expand Down
16 changes: 16 additions & 0 deletions manifest/v1alpha/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1002,3 +1002,19 @@ func TestAlertConditionOpSupport(t *testing.T) {
})
}
}

func TestIsReleaseChannelValid(t *testing.T) {
for name, test := range map[string]struct {
ReleaseChannel ReleaseChannel
IsValid bool
}{
"unset release channel, valid": {IsValid: true},
"beta channel, valid": {ReleaseChannel: ReleaseChannelBeta, IsValid: true},
"stable channel, valid": {ReleaseChannel: ReleaseChannelStable, IsValid: true},
"alpha channel, invalid": {ReleaseChannel: ReleaseChannelAlpha},
} {
t.Run(name, func(t *testing.T) {
assert.Equal(t, test.IsValid, isValidReleaseChannel(test.ReleaseChannel))
})
}
}
Loading