-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[controllers/datadogagent] Fix watch of ClusterRoles and ClusterRoleB…
…indings (#369) * controllers/datadogagent: add PartOfLabelValue to handle part-of labels * controllers/datadogagent: watch ClusterRoles and ClusterRoleBindings
- Loading branch information
1 parent
e06da64
commit 434db7c
Showing
8 changed files
with
137 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package datadogagent | ||
|
||
import ( | ||
"strings" | ||
|
||
datadoghqv1alpha1 "github.com/DataDog/datadog-operator/api/v1alpha1" | ||
"k8s.io/apimachinery/pkg/types" | ||
) | ||
|
||
const ( | ||
partOfSplitChar string = "-" | ||
partOfEscapedSplitChar string = "--" | ||
) | ||
|
||
// PartOfLabelValue is helpful to work with the "app.kubernetes.io/part-of" | ||
// label. We use that label to track the of an object when we can't use owner | ||
// references (cannot be used across namespaces). | ||
// In order to identify an owner, we encode its namespace and name in the value | ||
// of the label separated with a "-". Because names and namespaces can contain | ||
// "-" too, we escape them. There are not any characters that are allowed in | ||
// labels but not in names and namespaces so escaping is needed. | ||
type PartOfLabelValue struct { | ||
Value string | ||
} | ||
|
||
// NewPartOfLabelValue creates an instance of PartOfLabelValue from a | ||
// DatadogAgent. | ||
func NewPartOfLabelValue(dda *datadoghqv1alpha1.DatadogAgent) *PartOfLabelValue { | ||
value := strings.ReplaceAll(dda.Namespace, partOfSplitChar, partOfEscapedSplitChar) + | ||
partOfSplitChar + | ||
strings.ReplaceAll(dda.Name, partOfSplitChar, partOfEscapedSplitChar) | ||
|
||
return &PartOfLabelValue{Value: value} | ||
} | ||
|
||
// NamespacedName returns the NamespaceName that corresponds to the value of a | ||
// part-of label. | ||
func (partOfLabelValue *PartOfLabelValue) NamespacedName() types.NamespacedName { | ||
l := len(partOfLabelValue.Value) | ||
for i, c := range partOfLabelValue.Value { | ||
if string(c) == partOfSplitChar { | ||
// Tt's split index if it's just one "-" | ||
isSplitIndex := (i != 0 && string(partOfLabelValue.Value[i-1]) != partOfSplitChar) && | ||
(i != l-1 && string(partOfLabelValue.Value[i+1]) != partOfSplitChar) | ||
|
||
if isSplitIndex { | ||
return types.NamespacedName{ | ||
// The ReplaceAll calls undoes the escaping done in the constructor | ||
Namespace: strings.ReplaceAll(partOfLabelValue.Value[:i], partOfEscapedSplitChar, partOfSplitChar), | ||
Name: strings.ReplaceAll(partOfLabelValue.Value[i+1:], partOfEscapedSplitChar, partOfSplitChar), | ||
} | ||
} | ||
} | ||
} | ||
return types.NamespacedName{} | ||
} | ||
|
||
func (partOfLabelValue *PartOfLabelValue) String() string { | ||
return partOfLabelValue.Value | ||
} |
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,46 @@ | ||
package datadogagent | ||
|
||
import ( | ||
datadoghqv1alpha1 "github.com/DataDog/datadog-operator/api/v1alpha1" | ||
"github.com/stretchr/testify/assert" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"testing" | ||
) | ||
|
||
func Test_NamespacedName(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
agentName string | ||
agentNamespace string | ||
expectedNamespacedName types.NamespacedName | ||
}{ | ||
{ | ||
name: "without the split char", | ||
agentNamespace: "foo", | ||
agentName: "bar", | ||
expectedNamespacedName: types.NamespacedName{Namespace: "foo", Name: "bar"}, | ||
}, | ||
{ | ||
name: "with the split char", | ||
agentNamespace: "f-o-o", | ||
agentName: "bar", | ||
expectedNamespacedName: types.NamespacedName{Namespace: "f-o-o", Name: "bar"}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
dda := datadoghqv1alpha1.DatadogAgent{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: tt.agentName, | ||
Namespace: tt.agentNamespace, | ||
}, | ||
} | ||
|
||
value := NewPartOfLabelValue(&dda) | ||
|
||
assert.Equal(t, tt.expectedNamespacedName, value.NamespacedName()) | ||
}) | ||
} | ||
} |
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