-
Notifications
You must be signed in to change notification settings - Fork 344
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
New DeploymentStrategy type for JaegerSpec.Strategy #704
Merged
+178
−91
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4dcf62a
Add DeploymentStrategy type and apply to JaegerSpec.Strategy
volmedo 5e43380
Add constants for DeploymentStrategy
volmedo 5843126
Adapt code to the new type
volmedo cafa6ce
Complete comments
volmedo 0a29840
Remove strategy type constants in pkg/strategy and use the newly defi…
volmedo a8850d6
Change package aliases to match convention
volmedo ae31e2b
Add unmarshaling code and tests
volmedo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package v1 | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
) | ||
|
||
// DeploymentStrategy represents the possible values for deployment strategies | ||
// +k8s:openapi-gen=true | ||
type DeploymentStrategy string | ||
|
||
const ( | ||
// DeploymentStrategyDeprecatedAllInOne represents the (deprecated) 'all-in-one' deployment strategy | ||
// +k8s:openapi-gen=true | ||
DeploymentStrategyDeprecatedAllInOne DeploymentStrategy = "all-in-one" | ||
|
||
// DeploymentStrategyAllInOne represents the 'allInOne' deployment strategy (default) | ||
// +k8s:openapi-gen=true | ||
DeploymentStrategyAllInOne DeploymentStrategy = "allinone" | ||
|
||
// DeploymentStrategyStreaming represents the 'streaming' deployment strategy | ||
// +k8s:openapi-gen=true | ||
DeploymentStrategyStreaming DeploymentStrategy = "streaming" | ||
|
||
// DeploymentStrategyProduction represents the 'production' deployment strategy | ||
// +k8s:openapi-gen=true | ||
DeploymentStrategyProduction DeploymentStrategy = "production" | ||
) | ||
|
||
// UnmarshalText implements encoding.TextUnmarshaler to ensure that JSON values in the | ||
// strategy field of JSON jaeger specs are interpreted in a case-insensitive manner | ||
func (ds *DeploymentStrategy) UnmarshalText(text []byte) error { | ||
if ds == nil { | ||
return errors.New("DeploymentStrategy: UnmarshalText on nil pointer") | ||
} | ||
|
||
switch strings.ToLower(string(text)) { | ||
default: | ||
*ds = DeploymentStrategyAllInOne | ||
case string(DeploymentStrategyDeprecatedAllInOne): | ||
*ds = DeploymentStrategyDeprecatedAllInOne | ||
case string(DeploymentStrategyStreaming): | ||
*ds = DeploymentStrategyStreaming | ||
case string(DeploymentStrategyProduction): | ||
*ds = DeploymentStrategyProduction | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package v1 | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestUnmarshalJSON(t *testing.T) { | ||
tcs := map[string]struct { | ||
json string | ||
expected DeploymentStrategy | ||
}{ | ||
"allInOne": {json: `"allInOne"`, expected: DeploymentStrategyAllInOne}, | ||
"streaming": {json: `"streaming"`, expected: DeploymentStrategyStreaming}, | ||
"production": {json: `"production"`, expected: DeploymentStrategyProduction}, | ||
"all-in-one": {json: `"all-in-one"`, expected: DeploymentStrategyDeprecatedAllInOne}, | ||
"ALLinONE": {json: `"ALLinONE"`, expected: DeploymentStrategyAllInOne}, | ||
"StReAmInG": {json: `"StReAmInG"`, expected: DeploymentStrategyStreaming}, | ||
"Production": {json: `"Production"`, expected: DeploymentStrategyProduction}, | ||
"All-IN-One": {json: `"All-IN-One"`, expected: DeploymentStrategyDeprecatedAllInOne}, | ||
"random value": {json: `"random value"`, expected: DeploymentStrategyAllInOne}, | ||
"empty string": {json: `""`, expected: DeploymentStrategyAllInOne}, | ||
} | ||
|
||
for name, tc := range tcs { | ||
t.Run(name, func(t *testing.T) { | ||
ds := DeploymentStrategy("") | ||
err := json.Unmarshal([]byte(tc.json), &ds) | ||
assert.NoError(t, err) | ||
assert.Equal(t, tc.expected, ds) | ||
}) | ||
} | ||
} | ||
|
||
func TestMarshalJSON(t *testing.T) { | ||
tcs := map[string]struct { | ||
strategy DeploymentStrategy | ||
expected string | ||
}{ | ||
"allinone": {strategy: DeploymentStrategyAllInOne, expected: `"allinone"`}, | ||
"streaming": {strategy: DeploymentStrategyStreaming, expected: `"streaming"`}, | ||
"production": {strategy: DeploymentStrategyProduction, expected: `"production"`}, | ||
"all-in-one": {strategy: DeploymentStrategyDeprecatedAllInOne, expected: `"all-in-one"`}, | ||
} | ||
|
||
for name, tc := range tcs { | ||
t.Run(name, func(t *testing.T) { | ||
data, err := json.Marshal(tc.strategy) | ||
assert.NoError(t, err) | ||
assert.Equal(t, tc.expected, string(data)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ import ( | |
"fmt" | ||
"sort" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/spf13/viper" | ||
appsv1 "k8s.io/api/apps/v1" | ||
|
@@ -76,9 +75,9 @@ func (c *Collector) Get() *appsv1.Deployment { | |
} | ||
|
||
storageType := c.jaeger.Spec.Storage.Type | ||
// If strategy is "streaming", then change storage type | ||
// If strategy is DeploymentStrategyStreaming, then change storage type | ||
// to Kafka, and the storage options will be used in the Ingester instead | ||
if strings.EqualFold(c.jaeger.Spec.Strategy, "streaming") { | ||
if c.jaeger.Spec.Strategy == v1.DeploymentStrategyStreaming { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What would happen if we have a CR with a strategy set to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was assuming that the value was being normalized somewhere else. Sorry, my bad. |
||
storageType = "kafka" | ||
} | ||
options := allArgs(c.jaeger.Spec.Collector.Options, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is interesting. I'm probably missing something obvious, but how would this be triggered? Wouldn't a nil object fail with a nil reference before reaching this code? Can you come up with a test case for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not obvious. It is possible for the function to be called on a nil receiver when interfaces are involved (as is the case here).
I was digging into the docs and I had a look at how json.RawMessage.UnmarshalJSON is implemented. The check for nil felt strange, so I did a bit of research and came up with this https://tour.golang.org/methods/12
Wonderful Go! :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, I guess this kind of check should be implemented in other types implementing interfaces for additional safety.
Options
come to my mind, implementing bothjson.Marshaler
andjson.Unmarshaler
interfaces, but I'm sure there are several others.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL! I'm merging this one. Would you mind creating an issue to do the same nil check in the other relevant types?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do!