Commit 069da4cb authored by liangyuzhou's avatar liangyuzhou
Browse files

fix: add missed collector validation of monitor

parent cbf5d4ad
Showing with 23 additions and 1 deletion
+23 -1
......@@ -19,7 +19,10 @@
package collector
import (
"fmt"
apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation/field"
"tkestack.io/tke/api/monitor"
)
......@@ -28,6 +31,11 @@ import (
// subdomain.
var ValidateName = apimachineryvalidation.ValidateNamespaceName
var types = sets.NewString(
string(monitor.CollectorImportedPrometheus),
string(monitor.CollectorManaged),
)
// ValidateCollector tests if required fields in the cluster are set.
func ValidateCollector(collector *monitor.Collector) field.ErrorList {
allErrs := apimachineryvalidation.ValidateObjectMeta(&collector.ObjectMeta, false, ValidateName, field.NewPath("metadata"))
......@@ -36,6 +44,20 @@ func ValidateCollector(collector *monitor.Collector) field.ErrorList {
allErrs = append(allErrs, field.Required(field.NewPath("spec", "clusterName"), "must specify a cluster name"))
}
if collector.Spec.Type == "" {
allErrs = append(allErrs, field.Required(field.NewPath("spec", "type"), fmt.Sprintf("available type are %v", types)))
} else {
if !types.Has(string(collector.Spec.Type)) {
allErrs = append(allErrs, field.NotSupported(field.NewPath("spec", "type"), collector.Spec.Type, types.List()))
}
if collector.Spec.Type == monitor.CollectorManaged {
if collector.Spec.Version == "" {
allErrs = append(allErrs, field.Required(field.NewPath("spec", "version"), "must specify collector version"))
}
}
}
return allErrs
}
......
......@@ -62,7 +62,7 @@ func ValidateCluster(clusterProviders *sync.Map, obj *platform.Cluster, platform
}
if obj.Spec.Type == "" {
allErrs = append(allErrs, field.Required(field.NewPath("spec", "type"), fmt.Sprintf("availble type are %v", types)))
allErrs = append(allErrs, field.Required(field.NewPath("spec", "type"), fmt.Sprintf("available type are %v", types)))
} else {
if !types.Has(string(obj.Spec.Type)) {
allErrs = append(allErrs, field.NotSupported(field.NewPath("spec", "type"), obj.Spec.Type, types.List()))
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment