-
Notifications
You must be signed in to change notification settings - Fork 440
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
Restart collector pod when config is updated #215
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0889624
197 - Restart collector pod when config is updated
bhiravabhatla bebdc15
197 - Add license header to annotations.go file
bhiravabhatla a493076
197 - Change test package name to collector
bhiravabhatla 512cc69
Change eventRecorder name to be consistent & fix annotations issue
bhiravabhatla 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package collector | ||
|
||
import ( | ||
"crypto/sha256" | ||
"fmt" | ||
|
||
"github.com/open-telemetry/opentelemetry-operator/api/v1alpha1" | ||
) | ||
|
||
// Annotations return the annotations for OpenTelemetryCollector pod. | ||
func Annotations(instance v1alpha1.OpenTelemetryCollector) map[string]string { | ||
// new map every time, so that we don't touch the instance's annotations | ||
annotations := map[string]string{} | ||
|
||
// set default prometheus annotations | ||
annotations["prometheus.io/scrape"] = "true" | ||
annotations["prometheus.io/port"] = "8888" | ||
annotations["prometheus.io/path"] = "/metrics" | ||
|
||
// allow override of prometheus annotations | ||
if nil != instance.Annotations { | ||
for k, v := range instance.Annotations { | ||
annotations[k] = v | ||
} | ||
} | ||
// make sure sha256 for configMap is always calculated | ||
annotations["opentelemetry-operator-config/sha256"] = getConfigMapSHA(instance.Spec.Config) | ||
|
||
return annotations | ||
} | ||
|
||
func getConfigMapSHA(config string) string { | ||
h := sha256.Sum256([]byte(config)) | ||
return fmt.Sprintf("%x", h) | ||
} |
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,89 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package collector | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"github.com/open-telemetry/opentelemetry-operator/api/v1alpha1" | ||
) | ||
|
||
func TestDefaultAnnotations(t *testing.T) { | ||
// prepare | ||
otelcol := v1alpha1.OpenTelemetryCollector{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "my-instance", | ||
Namespace: "my-ns", | ||
}, | ||
Spec: v1alpha1.OpenTelemetryCollectorSpec{ | ||
Config: "test", | ||
}, | ||
} | ||
|
||
// test | ||
annotations := Annotations(otelcol) | ||
|
||
//verify | ||
assert.Equal(t, "true", annotations["prometheus.io/scrape"]) | ||
assert.Equal(t, "8888", annotations["prometheus.io/port"]) | ||
assert.Equal(t, "/metrics", annotations["prometheus.io/path"]) | ||
assert.Equal(t, "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", annotations["opentelemetry-operator-config/sha256"]) | ||
} | ||
|
||
func TestUserAnnotations(t *testing.T) { | ||
// prepare | ||
otelcol := v1alpha1.OpenTelemetryCollector{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "my-instance", | ||
Namespace: "my-ns", | ||
Annotations: map[string]string{"prometheus.io/scrape": "false", | ||
"prometheus.io/port": "1234", | ||
"prometheus.io/path": "/test", | ||
"opentelemetry-operator-config/sha256": "shouldBeOverwritten", | ||
}, | ||
}, | ||
Spec: v1alpha1.OpenTelemetryCollectorSpec{ | ||
Config: "test", | ||
}, | ||
} | ||
|
||
// test | ||
annotations := Annotations(otelcol) | ||
|
||
//verify | ||
assert.Equal(t, "false", annotations["prometheus.io/scrape"]) | ||
assert.Equal(t, "1234", annotations["prometheus.io/port"]) | ||
assert.Equal(t, "/test", annotations["prometheus.io/path"]) | ||
assert.Equal(t, "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", annotations["opentelemetry-operator-config/sha256"]) | ||
} | ||
|
||
func TestAnnotationsPropagateDown(t *testing.T) { | ||
// prepare | ||
otelcol := v1alpha1.OpenTelemetryCollector{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Annotations: map[string]string{"myapp": "mycomponent"}, | ||
}, | ||
} | ||
|
||
// test | ||
annotations := Annotations(otelcol) | ||
|
||
// verify | ||
assert.Len(t, annotations, 5) | ||
assert.Equal(t, "mycomponent", annotations["myapp"]) | ||
} |
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
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.
Could you also add a test for the case where the annotation exists in the CR? If a user has specified
scrape
as false, we should honor that. I think the code is currently doing the opposite.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.
Yes. My bad, fixing it and adding a test for the same