-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial Discovery properties provider and config incorporation
- Loading branch information
1 parent
c04d35e
commit 9b79500
Showing
20 changed files
with
995 additions
and
57 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,57 @@ | ||
// Copyright Splunk, Inc. | ||
// | ||
// 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 properties | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/alecthomas/participle/v2" | ||
"github.com/alecthomas/participle/v2/lexer" | ||
) | ||
|
||
// SPLUNK_DISCOVERY_RECEIVERS_receiver_x2d_type_x2f_receiver_x2d_name_CONFIG_field_x3a3a_subfield=VAL | ||
// SPLUNK_DISCOVERY_EXTENSIONS_observer_x2d_type_x2f_observer_x2d_name_CONFIG_field_x3a3a_subfield=VAL | ||
|
||
var envVarLex = lexer.MustSimple([]lexer.SimpleRule{ | ||
{Name: "Underscore", Pattern: `_`}, | ||
{Name: "String", Pattern: `[^_]+`}, | ||
}) | ||
|
||
var envVarParser = participle.MustBuild[EnvVarProperty]( | ||
participle.Lexer(envVarLex), | ||
participle.UseLookahead(participle.MaxLookahead), | ||
) | ||
|
||
type EnvVarProperty struct { | ||
ComponentType string `parser:"'SPLUNK' Underscore 'DISCOVERY' Underscore @('RECEIVERS' | 'EXTENSIONS') Underscore"` | ||
Component EnvVarComponentID `parser:"@@"` | ||
Key string `parser:"Underscore 'CONFIG' Underscore @(String|Underscore)+"` | ||
Val string | ||
} | ||
|
||
type EnvVarComponentID struct { | ||
Type string `parser:"@~(Underscore (?= 'CONFIG'))+"` | ||
// _x2f_ -> '/' | ||
Name string `parser:"(Underscore 'x2f' Underscore @(~(?= Underscore (?= 'CONFIG'))+|''))?"` | ||
} | ||
|
||
func NewEnvVarProperty(property, val string) (*EnvVarProperty, error) { | ||
p, err := envVarParser.ParseString("SPLUNK_DISCOVERY", property) | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid property env var (parsing error): %w", err) | ||
} | ||
p.Val = val | ||
return p, nil | ||
} |
88 changes: 88 additions & 0 deletions
88
internal/confmapprovider/discovery/properties/env_var_test.go
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,88 @@ | ||
// Copyright Splunk, Inc. | ||
// | ||
// 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 properties | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestValidEnvVarProperties(t *testing.T) { | ||
for _, tt := range []struct { | ||
expected *Property | ||
envVar string | ||
}{ | ||
{envVar: "SPLUNK_DISCOVERY_RECEIVERS_receiver_x2d_type_x2f__CONFIG_one", | ||
expected: &Property{ | ||
stringMap: map[string]any{ | ||
"receivers": map[string]any{ | ||
"receiver-type": map[string]any{ | ||
"config": map[string]any{ | ||
"one": "val"}, | ||
}, | ||
}, | ||
}, | ||
ComponentType: "receivers", | ||
Component: ComponentID{Type: "receiver-type"}, | ||
Key: "one", | ||
Val: "val", | ||
}, | ||
}, | ||
{envVar: "SPLUNK_DISCOVERY_EXTENSIONS_extension_x2e_type_x2f_extension____name_CONFIG_one_x3a__x3a_two", | ||
expected: &Property{ | ||
stringMap: map[string]any{ | ||
"extensions": map[string]any{ | ||
"extension.type/extension____name": map[string]any{ | ||
"one": map[string]any{ | ||
"two": "val", | ||
}, | ||
}, | ||
}, | ||
}, | ||
ComponentType: "extensions", | ||
Component: ComponentID{Type: "extension.type", Name: "extension____name"}, | ||
Key: "one::two", | ||
Val: "val", | ||
}, | ||
}, | ||
} { | ||
t.Run(tt.envVar, func(t *testing.T) { | ||
p, ok, err := NewPropertyFromEnvVar(tt.envVar, "val") | ||
require.True(t, ok) | ||
require.NoError(t, err) | ||
require.NotNil(t, p) | ||
require.Equal(t, tt.expected, p) | ||
}) | ||
} | ||
} | ||
|
||
func TestInvalidEnvVarProperties(t *testing.T) { | ||
for _, tt := range []struct { | ||
envVar, expectedError string | ||
}{ | ||
{envVar: "SPLUNK_DISCOVERY_NOTVALIDCOMPONENT_TYPE_CONFIG_ONE", expectedError: "invalid env var property (parsing error): invalid property env var (parsing error): SPLUNK_DISCOVERY:1:18: unexpected token \"NOTVALIDCOMPONENT\" (expected (\"RECEIVERS\" | \"EXTENSIONS\") <underscore> EnvVarComponentID <underscore> \"CONFIG\" <underscore> (<string> | <underscore>)+)"}, | ||
{envVar: "SPLUNK_DISCOVERY_RECEIVERS_TYPE_NOTCONFIG_ONE", expectedError: "invalid env var property (parsing error): invalid property env var (parsing error): SPLUNK_DISCOVERY:1:46: unexpected token \"<EOF>\" (expected <underscore> \"CONFIG\" <underscore> (<string> | <underscore>)+)"}, | ||
{envVar: "SPLUNK_DISCOVERY_EXTENSIONS_TYPE_x2f_NAME_CONFIG_", expectedError: "invalid env var property (parsing error): invalid property env var (parsing error): SPLUNK_DISCOVERY:1:50: sub-expression (<string> | <underscore>)+ must match at least once"}, | ||
} { | ||
t.Run(tt.envVar, func(t *testing.T) { | ||
p, ok, err := NewPropertyFromEnvVar(tt.envVar, "val") | ||
require.True(t, ok) | ||
require.Error(t, err) | ||
require.EqualError(t, err, tt.expectedError) | ||
require.Nil(t, p) | ||
}) | ||
} | ||
} |
Oops, something went wrong.