-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow gRPC cluster validation configuration (#650)
* Allow gRPC cluster validation configuration Signed-off-by: Yuri Nikolic <durica.nikolic@grafana.com> * Updating CHANGELOG.md Signed-off-by: Yuri Nikolic <durica.nikolic@grafana.com> * Fix failing tests Signed-off-by: Yuri Nikolic <durica.nikolic@grafana.com> * Fixing review findings Signed-off-by: Yuri Nikolic <durica.nikolic@grafana.com> --------- Signed-off-by: Yuri Nikolic <durica.nikolic@grafana.com>
- Loading branch information
1 parent
14a4665
commit f61fc86
Showing
4 changed files
with
126 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package clusterutil | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
type ClusterValidationConfig struct { | ||
Label string | ||
GRPC ClusterValidationProtocolConfig | ||
} | ||
|
||
type ClusterValidationProtocolConfig struct { | ||
Enabled bool | ||
SoftValidation bool | ||
} | ||
|
||
func (cfg *ClusterValidationConfig) Validate() error { | ||
return cfg.GRPC.Validate("grpc", cfg.Label) | ||
} | ||
|
||
func (cfg *ClusterValidationConfig) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) { | ||
clusterValidationPrefix := prefix + ".cluster-validation" | ||
f.StringVar(&cfg.Label, clusterValidationPrefix+".label", "", "Optionally define server's cluster validation label.") | ||
cfg.GRPC.RegisterFlagsWithPrefix(clusterValidationPrefix+".grpc", f) | ||
} | ||
|
||
func (cfg *ClusterValidationProtocolConfig) Validate(prefix string, label string) error { | ||
if label == "" { | ||
if cfg.Enabled || cfg.SoftValidation { | ||
return fmt.Errorf("%s: validation cannot be enabled if cluster validation label is not configured", prefix) | ||
} | ||
return nil | ||
} | ||
|
||
if !cfg.Enabled && cfg.SoftValidation { | ||
return fmt.Errorf("%s: soft validation can be enabled only if cluster validation is enabled", prefix) | ||
} | ||
return nil | ||
} | ||
|
||
func (cfg *ClusterValidationProtocolConfig) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) { | ||
softValidationFlag := prefix + ".soft-validation" | ||
enabledFlag := prefix + ".enabled" | ||
f.BoolVar(&cfg.SoftValidation, softValidationFlag, false, fmt.Sprintf("When enabled, soft cluster label validation will be executed. Can be enabled only together with %s", enabledFlag)) | ||
f.BoolVar(&cfg.Enabled, enabledFlag, false, "When enabled, cluster label validation will be executed.") | ||
} |
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,62 @@ | ||
package clusterutil | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestClusterValidationProtocolConfigValidate(t *testing.T) { | ||
testCases := map[string]struct { | ||
label string | ||
enabled bool | ||
softValidation bool | ||
expectedErr error | ||
}{ | ||
"soft validation cannot be done if cluster validation label is not set": { | ||
softValidation: true, | ||
expectedErr: fmt.Errorf("testProtocol: validation cannot be enabled if cluster validation label is not configured"), | ||
}, | ||
"cluster validation cannot be done if cluster validation label is not set": { | ||
enabled: true, | ||
expectedErr: fmt.Errorf("testProtocol: validation cannot be enabled if cluster validation label is not configured"), | ||
}, | ||
"cluster validation and soft validation can be disabled if cluster validation label is not set": { | ||
label: "", | ||
enabled: false, | ||
softValidation: false, | ||
}, | ||
"cluster validation and soft validation can be disabled if cluster validation label is set": { | ||
label: "my-cluster", | ||
enabled: false, | ||
softValidation: false, | ||
}, | ||
"soft validation cannot be enabled if cluster validation is disabled": { | ||
label: "my-cluster", | ||
enabled: false, | ||
softValidation: true, | ||
expectedErr: fmt.Errorf("testProtocol: soft validation can be enabled only if cluster validation is enabled"), | ||
}, | ||
"soft validation can be disabled if cluster validation is enabled": { | ||
label: "my-cluster", | ||
enabled: true, | ||
softValidation: false, | ||
}, | ||
"cluster validation and soft validation can be enabled at the same time": { | ||
label: "my-cluster", | ||
enabled: true, | ||
softValidation: true, | ||
}, | ||
} | ||
for testName, testCase := range testCases { | ||
t.Run(testName, func(t *testing.T) { | ||
testProtocolCfg := ClusterValidationProtocolConfig{ | ||
Enabled: testCase.enabled, | ||
SoftValidation: testCase.softValidation, | ||
} | ||
err := testProtocolCfg.Validate("testProtocol", testCase.label) | ||
require.Equal(t, testCase.expectedErr, err) | ||
}) | ||
} | ||
} |
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