Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Evsyukov Denis <denis.evsyukov@flant.com>
  • Loading branch information
juev committed Sep 12, 2024
1 parent 8c562d5 commit 6d5f926
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 228 deletions.
8 changes: 3 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,18 @@ func main() {
logger.InitLogger()

dirs := parseFlags()
if dirs == nil {
return
}
logger.InfoF("Dirs: %v", dirs)

cfg := config.NewDefault()
err := config.NewLoader(cfg).Load()
logger.CheckErr(err)

mng := manager.NewManager(dirs)
// for i := range mng.Modules {

Check failure on line 22 in main.go

View workflow job for this annotation

GitHub Actions / golangci-lint

commentedOutCode: may want to remove commented-out code (gocritic)
// logger.Infof("module[%d]: %s", i, mng.Modules[i])
// logger.InfoF("module[%d]: %s", i, mng.Modules[i])
// }

result := mng.Run()
fmt.Printf("%s\n", result)
fmt.Printf("%v\n", result.ConvertToError())
logger.CheckErr(result.ConvertToError())
}
12 changes: 6 additions & 6 deletions pkg/config/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (l *Loader) setupConfigFileSearch() {

configSearchPaths := l.getConfigSearchPaths()

logger.Infof("Config search paths: %s", configSearchPaths)
logger.InfoF("Config search paths: %s", configSearchPaths)

for _, p := range configSearchPaths {
l.viper.AddConfigPath(p)
Expand All @@ -102,7 +102,7 @@ func (l *Loader) getConfigSearchPaths() []string {

absPath, err := filepath.Abs(firstArg)
if err != nil {
logger.Warnf("Can't make abs path for %q: %s", firstArg, err)
logger.WarnF("Can't make abs path for %q: %s", firstArg, err)
absPath = filepath.Clean(firstArg)
}

Expand Down Expand Up @@ -130,7 +130,7 @@ func (l *Loader) getConfigSearchPaths() []string {

// find home directory for global config
if home, err := homedir.Dir(); err != nil {
logger.Warnf("Can't get user's home directory: %v", err)
logger.WarnF("Can't get user's home directory: %v", err)
} else if !slices.Contains(searchPaths, home) {
searchPaths = append(searchPaths, home)
}
Expand Down Expand Up @@ -175,15 +175,15 @@ func (l *Loader) setConfigDir() error {

if usedConfigFile == os.Stdin.Name() {
usedConfigFile = ""
logger.Infof("Reading config file stdin")
logger.InfoF("Reading config file stdin")
} else {
var err error
usedConfigFile, err = fsutils.ShortestRelPath(usedConfigFile, "")
if err != nil {
logger.Warnf("Can't pretty print config file path: %v", err)
logger.WarnF("Can't pretty print config file path: %v", err)
}

logger.Infof("Used config file %s", usedConfigFile)
logger.InfoF("Used config file %s", usedConfigFile)
}

usedConfigDir, err := filepath.Abs(filepath.Dir(usedConfigFile))
Expand Down
9 changes: 6 additions & 3 deletions pkg/linters/openapi/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ func RunOpenAPIValidator(fileC chan fileValidation) chan fileValidation {
continue
}

runFileParser(strings.TrimPrefix(vfile.filePath, vfile.rootPath), yamlStruct, parseResultC)
//runFileParser(strings.TrimPrefix(vfile.filePath, vfile.rootPath), yamlStruct, parseResultC)

Check failure on line 90 in pkg/linters/openapi/library.go

View workflow job for this annotation

GitHub Actions / golangci-lint

commentFormatting: put a space between `//` and comment text (gocritic)
runFileParser(vfile.filePath, yamlStruct, parseResultC)

var result *multierror.Error

Expand Down Expand Up @@ -196,7 +197,7 @@ func runFileParser(fileName string, data map[any]any, resultC chan error) {
keyValidators: map[string]validator{
"enum": validators.NewEnumValidator(),
"highAvailability": validators.NewHAValidator(),
"https": validators.NewKeyNameValidator(),
"https": validators.NewHAValidator(),
},
resultC: resultC,
}
Expand Down Expand Up @@ -255,9 +256,11 @@ func (fp fileParser) parseValue(upperKey string, v any) {
}
case reflect.Slice:
fp.parseSlice(upperKey, v.([]any))
default:

}

Check failure on line 261 in pkg/linters/openapi/library.go

View workflow job for this annotation

GitHub Actions / golangci-lint

unnecessary trailing newline (whitespace)
}

type validator interface {
Run(fileName, absoulteKey string, value any) error
Run(fileName, absoluteKey string, value any) error
}
197 changes: 2 additions & 195 deletions pkg/linters/openapi/validators/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,185 +10,7 @@ import (
)

var (
// openapi key excludes by file
fileExcludes = map[string][]string{
// all files
"*": {"apiVersions[*].openAPISpec.properties.apiVersion"},
// exclude zone - ru-center-1, ru-center-2, ru-center-3
"candi/cloud-providers/yandex/openapi/cluster_configuration.yaml": {
"apiVersions[0].openAPISpec.properties.nodeGroups.items.properties.zones.items",
"apiVersions[0].openAPISpec.properties.masterNodeGroup.properties.zones.items",
"apiVersions[0].openAPISpec.properties.zones.items",
"apiVersions[0].openAPISpec.properties.masterNodeGroup.properties.instanceClass.properties.diskType",
"apiVersions[0].openAPISpec.properties.nodeGroups.items.properties.instanceClass.properties.diskType",
},
// disk types - gp2.,..
"candi/cloud-providers/aws/openapi/cluster_configuration.yaml": {
"apiVersions[0].openAPISpec.properties.masterNodeGroup.properties.instanceClass.properties.diskType",
"apiVersions[0].openAPISpec.properties.nodeGroups.items.properties.instanceClass.properties.diskType",
"apiVersions[0].openAPISpec.properties.withNAT.properties.bastionInstance.properties.instanceClass.properties.diskType",
},
// disk types: pd-standard, pd-ssd, ...
"candi/cloud-providers/gcp/openapi/instance_class.yaml": {
"spec.versions[*].schema.openAPIV3Schema.properties.spec.properties.diskType",
},
// disk types: network-ssd, network-hdd
"candi/cloud-providers/yandex/openapi/instance_class.yaml": {
"spec.versions[*].schema.openAPIV3Schema.properties.spec.properties.diskType",
// v1alpha1 : SOFTWARE_ACCELERATED - migrated in v1
"spec.versions[0].schema.openAPIV3Schema.properties.spec.properties.networkType",
},
"candi/openapi/cluster_configuration.yaml": {
// vSphere
"apiVersions[0].openAPISpec.properties.cloud.properties.provider",
},
"global-hooks/openapi/values.yaml": {
// from openapispec
"properties.clusterConfiguration.properties.apiVersion",
"properties.clusterConfiguration.properties.cloud.properties.provider",
// http and https
"properties.modulesImages.properties.registry.properties.scheme",
},
"modules/010-user-authn-crd/crds/dex-provider.yaml": {
// v1alpha1 migrated to v1
"spec.versions[0].schema.openAPIV3Schema.properties.spec.properties.github.properties.teamNameField",
},
"modules/010-prometheus-crd/crds/grafanaadditionaldatasources.yaml": {
// v1alpha1 migrated to v1
"spec.versions[0].schema.openAPIV3Schema.properties.spec.properties.access",
},
"modules/015-admission-policy-engine/crds/operation-policy.yaml": {
// probes are inherited from Kubernetes
"spec.versions[0].schema.openAPIV3Schema.properties.spec.properties.policies.properties.requiredProbes.items",
// requests and limits are cpu and memory, they are taken from kubernetes
"spec.versions[0].schema.openAPIV3Schema.properties.spec.properties.policies.properties.requiredResources.properties.requests.items",
"spec.versions[0].schema.openAPIV3Schema.properties.spec.properties.policies.properties.requiredResources.properties.limits.items",
},
"modules/015-admission-policy-engine/crds/security-policy.yaml": {
// volumes are inherited from kubernetes
"spec.versions[0].schema.openAPIV3Schema.properties.spec.properties.policies.properties.allowedVolumes.items",
// capabilities names are hardcoded, it's not ours
"spec.versions[0].schema.openAPIV3Schema.properties.spec.properties.policies.properties.allowedCapabilities.items",
"spec.versions[0].schema.openAPIV3Schema.properties.spec.properties.policies.properties.requiredDropCapabilities.items",
},
"modules/015-admission-policy-engine/openapi/values.yaml": {
// enforcement actions are discovered from label values and should be propagated further into the helm chart as is
"properties.internal.properties.podSecurityStandards.properties.enforcementActions.items",
},
"modules/030-cloud-provider-azure/openapi/config-values.yaml": {
// ignore Azure disk types
"properties.storageClass.properties.provision.items.properties.type",
"properties.storageClass.properties.provision.items.oneOf[*].properties.type",
},
"modules/030-cloud-provider-aws/openapi/config-values.yaml": {
// ignore AWS disk types
"properties.storageClass.properties.provision.items.properties.type",
"properties.storageClass.properties.provision.items.oneOf[*].properties.type",
},
"modules/030-cloud-provider-openstack/openapi/values.yaml": {
// ignore internal values
"properties.internal.properties.discoveryData.properties.apiVersion",
},
// for local tests run
"ee/modules/030-cloud-provider-openstack/openapi/values.yaml": {
// ignore internal values
"properties.internal.properties.discoveryData.properties.apiVersion",
},
"modules/030-cloud-provider-aws/openapi/values.yaml": {
// ignore AWS disk types
"properties.internal.properties.storageClasses.items.oneOf[*].properties.type",
},
"modules/030-cloud-provider-vsphere/openapi/config-values.yaml": {
// ignore temporary flag that is already used (will be deleted after all CSIs are migrated)
"properties.storageClass.properties.compatibilityFlag",
},
"ee/modules/030-cloud-provider-vsphere/openapi/config-values.yaml": {
// ignore temporary flag that is already used (will be deleted after all CSIs are migrated)
"properties.storageClass.properties.compatibilityFlag",
},
"modules/030-cloud-provider-vsphere/openapi/values.yaml": {
// ignore internal values
"properties.internal.properties.providerDiscoveryData.properties.apiVersion",
"properties.internal.properties.providerClusterConfiguration.properties.apiVersion",
},
"ee/modules/030-cloud-provider-vsphere/openapi/values.yaml": {
// ignore internal values
"properties.internal.properties.providerDiscoveryData.properties.apiVersion",
"properties.internal.properties.providerClusterConfiguration.properties.apiVersion",
},
"modules/030-cloud-provider-vcd/openapi/values.yaml": {
// ignore internal values
"properties.internal.properties.discoveryData.properties.apiVersion",
"properties.internal.properties.providerDiscoveryData.properties.apiVersion",
"properties.internal.properties.providerClusterConfiguration.properties.apiVersion",
},
"modules/030-cloud-provider-zvirt/openapi/values.yaml": {
// ignore internal values
"properties.internal.properties.providerClusterConfiguration.properties.apiVersion",
"properties.internal.properties.providerDiscoveryData.properties.apiVersion",
},
"ee/modules/030-cloud-provider-vcd/openapi/values.yaml": {
// ignore internal values
"properties.internal.properties.providerDiscoveryData.properties.apiVersion",
"properties.internal.properties.providerClusterConfiguration.properties.apiVersion",
},
"modules/030-cloud-provider-yandex/openapi/values.yaml": {
// ignore internal values
"properties.internal.properties.providerDiscoveryData.properties.apiVersion",
"properties.internal.properties.providerClusterConfiguration.properties.apiVersion",
"properties.internal.properties.providerClusterConfiguration.properties.zones.items",
"properties.internal.properties.providerClusterConfiguration.properties.nodeGroups.items.properties.zones.items",
"properties.internal.properties.providerClusterConfiguration.properties.masterNodeGroup.properties.zones.items",
},
"modules/035-cni-flannel/openapi/values.yaml": {
// ignore internal values
"properties.internal.properties.podNetworkMode",
},
"modules/040-node-manager/openapi/config-values.yaml": {
// ignore internal values
"properties.allowedBundles.items",
},
"ee/modules/040-node-manager/openapi/config-values.yaml": {
// ignore internal values
"properties.allowedBundles.items",
},
"modules/042-kube-dns/openapi/values.yaml": {
// ignore internal values
"properties.internal.properties.specificNodeType",
},
"modules/300-prometheus/openapi/values.yaml": {
// grafana constant in internal values
"properties.internal.properties.grafana.properties.alertsChannelsConfig.properties.notifiers.items.properties.type",
},
"modules/402-ingress-nginx/crds/ingress-nginx.yaml": {
// GeoIP base constants: GeoIP2-ISP, GeoIP2-ASN, ...
"spec.versions[*].schema.openAPIV3Schema.properties.spec.properties.geoIP2.properties.maxmindEditionIDs.items",
},
"modules/031-ceph-csi/crds/cephcsi.yaml": {
// ignore file system names: ext4, xfs, etc.
"properties.internal.properties.crs.items.properties.spec.properties.rbd.properties.storageClasses.items.properties.defaultFSType",
"spec.versions[*].schema.openAPIV3Schema.properties.spec.properties.rbd.properties.storageClasses.items.properties.defaultFSType",
},
"modules/031-ceph-csi/openapi/values.yaml": {
// ignore file system names: ext4, xfs, etc.
"properties.internal.properties.crs.items.properties.spec.properties.rbd.properties.storageClasses.items.properties.defaultFSType",
"spec.versions[*].schema.openAPIV3Schema.properties.spec.properties.rbd.properties.storageClasses.items.properties.defaultFSType",
},
"modules/380-metallb/openapi/config-values.yaml": {
// ignore enum values
"properties.addressPools.items.properties.protocol",
},
"ee/modules/380-metallb/openapi/config-values.yaml": {
// ignore enum values
"properties.addressPools.items.properties.protocol",
},
"candi/cloud-providers/azure/openapi/cluster_configuration.yaml": {
// ignore enum values
"apiVersions[*].openAPISpec.properties.serviceEndpoints.items",
},
}

arrayPathRegex = regexp.MustCompile(`\[\d+\]`)
arrayPathRegex = regexp.MustCompile(`[\d+]`)
)

type EnumValidator struct {
Expand All @@ -197,27 +19,12 @@ type EnumValidator struct {
}

func NewEnumValidator() EnumValidator {
keyExcludes := make(map[string]struct{})

for _, exc := range fileExcludes["*"] {
keyExcludes[exc+".enum"] = struct{}{}
}

return EnumValidator{
key: "enum",
excludes: keyExcludes,
key: "enum",
}
}

func (en EnumValidator) Run(fileName, absoluteKey string, value any) error {

Check failure on line 27 in pkg/linters/openapi/validators/enum.go

View workflow job for this annotation

GitHub Actions / golangci-lint

unused-parameter: parameter 'fileName' seems to be unused, consider removing or renaming it as _ (revive)
for _, exc := range fileExcludes[fileName] {
en.excludes[exc+".enum"] = struct{}{}
}
if _, ok := en.excludes[absoluteKey]; ok {
// excluding key, dont check it
return nil
}

// check for slice path with wildcard
index := arrayPathRegex.FindString(absoluteKey)
if index != "" {
Expand Down
23 changes: 18 additions & 5 deletions pkg/linters/openapi/validators/ha_and_https.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package validators
import (
"fmt"
"reflect"

"github.com/deckhouse/d8-lint/pkg/logger"
)

var (
Expand All @@ -20,13 +22,24 @@ func NewHAValidator() HAValidator {
}

func (HAValidator) Run(file, absoluteKey string, value any) error {
values, ok := value.(map[any]any)
if !ok {
fmt.Printf("Possible Bug? Have to be a map. Type: %s, Value: %s, File: %s, Key: %s\n", reflect.TypeOf(value), value, file, absoluteKey)
return nil
//values, ok := value.(map[string]any)

Check failure on line 25 in pkg/linters/openapi/validators/ha_and_https.go

View workflow job for this annotation

GitHub Actions / golangci-lint

commentFormatting: put a space between `//` and comment text (gocritic)
//if !ok {
// logger.ErrorF("Possible Bug? Have to be a map. Type: %s, Value: %s, File: %s, Key: %s", reflect.TypeOf(value), value, file, absoluteKey)
// return nil
//}

m := make(map[any]any)
rv := reflect.ValueOf(value)
if rv.Kind() != reflect.Map {
logger.ErrorF("Possible Bug? Have to be a map. Type: %s, Value: %s, File: %s", reflect.TypeOf(value), value, file)
return fmt.Errorf("not map")
}
for _, key := range rv.MapKeys() {
value := rv.MapIndex(key)
m[key.Interface()] = value.Interface()
}

for key := range values {
for key := range m {
if key == "default" {
if absoluteKeysExcludes[file] == absoluteKey {
continue
Expand Down
Loading

0 comments on commit 6d5f926

Please sign in to comment.