-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Mateusz Urbanek <[email protected]>
- Loading branch information
Showing
20 changed files
with
635 additions
and
25 deletions.
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
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,44 @@ | ||
/* | ||
Copyright 2024 anza-labs contributors. | ||
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. | ||
*/ | ||
|
||
// Additional copyrights: | ||
// Copyright The OpenTelemetry Authors | ||
|
||
package manifests | ||
|
||
import ( | ||
"reflect" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
type Builder[Params any] func(params Params) ([]client.Object, error) | ||
|
||
type ManifestFactory[T client.Object, Params any] func(params Params) (T, error) | ||
type K8sManifestFactory[Params any] ManifestFactory[client.Object, Params] | ||
|
||
func Factory[T client.Object, Params any](f ManifestFactory[T, Params]) K8sManifestFactory[Params] { | ||
return func(params Params) (client.Object, error) { | ||
return f(params) | ||
} | ||
} | ||
|
||
// ObjectIsNotNil ensures that we only create an object IFF it isn't nil, | ||
// and it's concrete type isn't nil either. This works around the Go type system | ||
// by using reflection to verify its concrete type isn't nil. | ||
func ObjectIsNotNil(obj client.Object) bool { | ||
return obj != nil && !reflect.ValueOf(obj).IsNil() | ||
} |
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,38 @@ | ||
/* | ||
Copyright 2024 anza-labs contributors. | ||
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. | ||
*/ | ||
|
||
// Additional copyrights: | ||
// Copyright The OpenTelemetry Authors | ||
|
||
package manifestutils | ||
|
||
import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
// Annotations return the annotations for the resources. | ||
func Annotations(instance metav1.ObjectMeta, filterAnnotations []string) (map[string]string, error) { | ||
// new map every time, so that we don't touch the instance's annotations | ||
annotations := map[string]string{} | ||
|
||
if nil != instance.Annotations { | ||
for k, v := range instance.Annotations { | ||
if !IsFilteredSet(k, filterAnnotations) { | ||
annotations[k] = v | ||
} | ||
} | ||
} | ||
|
||
return annotations, 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,60 @@ | ||
/* | ||
Copyright 2024 anza-labs contributors. | ||
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 manifestutils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestAnnotationsPropagateDown(t *testing.T) { | ||
// prepare | ||
meta := metav1.ObjectMeta{ | ||
Annotations: map[string]string{"myapp": "mycomponent"}, | ||
} | ||
|
||
// test | ||
annotations, err := Annotations(meta, []string{}) | ||
require.NoError(t, err) | ||
|
||
// verify | ||
assert.Len(t, annotations, 1) | ||
assert.Equal(t, "mycomponent", annotations["myapp"]) | ||
} | ||
|
||
func TestAnnotationsFilter(t *testing.T) { | ||
meta := metav1.ObjectMeta{ | ||
Annotations: map[string]string{ | ||
"test.bar.io": "foo", | ||
"test.io/port": "1234", | ||
"test.io/path": "/test", | ||
}, | ||
} | ||
|
||
// This requires the filter to be in regex match form and not the other simpler wildcard one. | ||
annotations, err := Annotations(meta, []string{".*\\.bar\\.io"}) | ||
|
||
// verify | ||
require.NoError(t, err) | ||
assert.Len(t, annotations, 2) | ||
assert.NotContains(t, annotations, "test.bar.io") | ||
assert.Equal(t, "1234", annotations["test.io/port"]) | ||
} |
Oops, something went wrong.