forked from elastic/elastic-agent-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
272 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you 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 cfgwarn | ||
|
||
import ( | ||
"fmt" | ||
|
||
"go.uber.org/zap" | ||
|
||
"github.com/elastic/elastic-agent-libs/logp" | ||
) | ||
|
||
const selector = "cfgwarn" | ||
|
||
// Beta logs the usage of an beta feature. | ||
func Beta(format string, v ...interface{}) { | ||
logp.NewLogger(selector, zap.AddCallerSkip(1)).Warnf("BETA: "+format, v...) | ||
} | ||
|
||
// Deprecate logs a deprecation message. | ||
// The version string contains the version when the future will be removed. | ||
// If version is empty, the message will not mention the removal of the feature. | ||
func Deprecate(version string, format string, v ...interface{}) { | ||
var postfix string | ||
if version != "" { | ||
postfix = fmt.Sprintf(" Will be removed in version: %s", version) | ||
} | ||
logp.NewLogger(selector, zap.AddCallerSkip(1)).Warnf("DEPRECATED: "+format+postfix, v...) | ||
} | ||
|
||
// Experimental logs the usage of an experimental feature. | ||
func Experimental(format string, v ...interface{}) { | ||
logp.NewLogger(selector, zap.AddCallerSkip(1)).Warnf("EXPERIMENTAL: "+format, v...) | ||
} |
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,77 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you 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 cfgwarn | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/joeshaw/multierror" | ||
|
||
"github.com/elastic/elastic-agent-libs/config" | ||
) | ||
|
||
func checkRemovedSettings(cfg *config.C, settings ...string) error { | ||
var errs multierror.Errors | ||
for _, setting := range settings { | ||
if err := checkRemovedSetting(cfg, setting); err != nil { | ||
errs = append(errs, err) | ||
} | ||
} | ||
|
||
return errs.Err() | ||
} | ||
|
||
func checkRemovedSetting(cfg *config.C, setting string) error { | ||
segments := strings.Split(setting, ".") | ||
|
||
L := len(segments) | ||
name := segments[L-1] | ||
path := segments[:L-1] | ||
|
||
current := cfg | ||
|
||
// we are looking for any key that match the name. | ||
for _, p := range path { | ||
current, _ = current.Child(p, -1) | ||
if current == nil { | ||
break | ||
} | ||
} | ||
|
||
// full path to setting not available -> setting not found | ||
if current == nil { | ||
return nil | ||
} | ||
|
||
if !current.HasField(name) { | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("setting '%v' has been removed", current.PathOf(name)) | ||
} | ||
|
||
// CheckRemoved6xSettings prints a warning if the obsolete setting is used. | ||
func CheckRemoved6xSettings(cfg *config.C, settings ...string) error { | ||
return checkRemovedSettings(cfg, settings...) | ||
} | ||
|
||
// CheckRemoved6xSetting prints a warning if the obsolete setting is used. | ||
func CheckRemoved6xSetting(cfg *config.C, setting string) error { | ||
return checkRemovedSetting(cfg, setting) | ||
} |
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,142 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you 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 cfgwarn | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/joeshaw/multierror" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/elastic/elastic-agent-libs/config" | ||
) | ||
|
||
func TestRemovedSetting(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
cfg *config.C | ||
lookup string | ||
expected error | ||
}{ | ||
{ | ||
name: "no obsolete setting", | ||
lookup: "notfound", | ||
cfg: config.MustNewConfigFrom(map[string]interface{}{ | ||
"hello.world": "ok", | ||
}), | ||
expected: nil, | ||
}, | ||
{ | ||
name: "obsolete setting found", | ||
lookup: "hello", | ||
cfg: config.MustNewConfigFrom(map[string]interface{}{ | ||
"hello.world": "ok", | ||
}), | ||
expected: errors.New("setting 'hello' has been removed"), | ||
}, | ||
} | ||
|
||
functions := []struct { | ||
name string | ||
fn func(*config.C, string) error | ||
}{ | ||
{name: "checkRemovedSetting", fn: checkRemovedSetting}, | ||
{name: "checkRemoved6xSetting", fn: CheckRemoved6xSetting}, | ||
} | ||
|
||
for _, function := range functions { | ||
t.Run(function.name, func(t *testing.T) { | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
err := function.fn(test.cfg, test.lookup) | ||
assert.Equal(t, test.expected, err) | ||
}) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestRemovedSettings(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
cfg *config.C | ||
lookup []string | ||
expected error | ||
}{ | ||
{ | ||
name: "no obsolete setting", | ||
lookup: []string{"notfound"}, | ||
cfg: config.MustNewConfigFrom(map[string]interface{}{ | ||
"hello.world": "ok", | ||
}), | ||
expected: nil, | ||
}, | ||
{ | ||
name: "obsolete setting found", | ||
lookup: []string{"hello"}, | ||
cfg: config.MustNewConfigFrom(map[string]interface{}{ | ||
"hello.world": "ok", | ||
}), | ||
expected: multierror.Errors{errors.New("setting 'hello' has been removed")}.Err(), | ||
}, | ||
{ | ||
name: "multiple obsolete settings", | ||
lookup: []string{"hello", "bad"}, | ||
cfg: config.MustNewConfigFrom(map[string]interface{}{ | ||
"hello.world": "ok", | ||
"bad": "true", | ||
}), | ||
expected: multierror.Errors{ | ||
errors.New("setting 'hello' has been removed"), | ||
errors.New("setting 'bad' has been removed"), | ||
}.Err(), | ||
}, | ||
{ | ||
name: "multiple obsolete settings not on first level", | ||
lookup: []string{"filebeat.config.prospectors", "filebeat.prospectors"}, | ||
cfg: config.MustNewConfigFrom(map[string]interface{}{ | ||
"filebeat.prospectors": "ok", | ||
"filebeat.config.prospectors": map[string]interface{}{"ok": "ok1"}, | ||
}), | ||
expected: multierror.Errors{ | ||
errors.New("setting 'filebeat.config.prospectors' has been removed"), | ||
errors.New("setting 'filebeat.prospectors' has been removed"), | ||
}.Err(), | ||
}, | ||
} | ||
|
||
functions := []struct { | ||
name string | ||
fn func(*config.C, ...string) error | ||
}{ | ||
{name: "checkRemovedSetting", fn: checkRemovedSettings}, | ||
{name: "checkRemoved6xSetting", fn: CheckRemoved6xSettings}, | ||
} | ||
|
||
for _, function := range functions { | ||
t.Run(function.name, func(t *testing.T) { | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
err := checkRemovedSettings(test.cfg, test.lookup...) | ||
assert.Equal(t, test.expected, err) | ||
}) | ||
} | ||
}) | ||
} | ||
} |